blob: 7df4c3f2acc4277158736c27a7f9b8506203972a [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
489 // double.
490 MVT::ValueType VT = CFP->getValueType(0);
491 bool isDouble = VT == MVT::f64;
Dale Johannesen7f724e92007-09-16 16:51:49 +0000492 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen98d3a082007-09-14 22:26:36 +0000493 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000494 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000495 if (VT!=MVT::f64 && VT!=MVT::f32)
496 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000497 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
498 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000499 }
500
Dale Johannesend246b2c2007-08-30 00:23:21 +0000501 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000502 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000503 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000504 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
505 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
506 VT = MVT::f32;
507 Extend = true;
508 }
509
510 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
511 if (Extend) {
512 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Dan Gohman16d4bc32008-02-07 18:41:25 +0000513 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman2d489b52008-02-06 22:27:42 +0000514 0, MVT::f32);
Evan Cheng47833a12006-12-12 21:32:44 +0000515 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +0000516 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +0000517 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng47833a12006-12-12 21:32:44 +0000518 }
519}
520
Chris Lattner462505f2006-02-13 09:18:02 +0000521
Evan Cheng003feb02007-01-04 21:56:39 +0000522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000527 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000532
Evan Cheng003feb02007-01-04 21:56:39 +0000533 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000534 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000572 if (MVT::isFloatingPoint(ST->getMemoryVT())) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000587 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000588 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000589 // Get the half-size VT
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000590 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000606 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000607 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
608 ST->getSrcValue(), SVOffset + IncrementSize,
609 NewStoredVT, ST->isVolatile(), Alignment);
610
611 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
612}
613
614/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
615static
616SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
617 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000618 int SVOffset = LD->getSrcValueOffset();
619 SDOperand Chain = LD->getChain();
620 SDOperand Ptr = LD->getBasePtr();
621 MVT::ValueType VT = LD->getValueType(0);
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000622 MVT::ValueType LoadedVT = LD->getMemoryVT();
Chris Lattner09c03932007-11-19 21:38:03 +0000623 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
Chris Lattner09c03932007-11-19 21:38:03 +0000627 if (LoadedVT == MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000628 intVT = MVT::i64;
Chris Lattner09c03932007-11-19 21:38:03 +0000629 else if (LoadedVT == MVT::f32)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000630 intVT = MVT::i32;
631 else
632 assert(0 && "Unaligned load of unsupported floating point type");
633
634 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
635 SVOffset, LD->isVolatile(),
636 LD->getAlignment());
637 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
638 if (LoadedVT != VT)
639 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
640
641 SDOperand Ops[] = { Result, Chain };
642 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
643 Ops, 2);
644 }
Chris Lattner09c03932007-11-19 21:38:03 +0000645 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
646 "Unaligned load of unsupported type.");
647
648 // Compute the new VT that is half the size of the old one. We either have an
649 // integer MVT or we have a vector MVT.
650 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
651 MVT::ValueType NewLoadedVT;
652 if (!MVT::isVector(LoadedVT)) {
653 NewLoadedVT = MVT::getIntegerType(NumBits/2);
654 } else {
655 // FIXME: This is not right for <1 x anything> it is also not right for
656 // non-power-of-two vectors.
657 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
658 MVT::getVectorNumElements(LoadedVT)/2);
659 }
660 NumBits >>= 1;
661
662 unsigned Alignment = LD->getAlignment();
663 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000664 ISD::LoadExtType HiExtType = LD->getExtensionType();
665
666 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
667 if (HiExtType == ISD::NON_EXTLOAD)
668 HiExtType = ISD::ZEXTLOAD;
669
670 // Load the value in two parts
671 SDOperand Lo, Hi;
672 if (TLI.isLittleEndian()) {
673 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
674 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
675 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
676 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
677 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
678 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000679 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000680 } else {
681 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
682 NewLoadedVT,LD->isVolatile(), Alignment);
683 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
684 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
685 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
686 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000687 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000688 }
689
690 // aggregate the two parts
691 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
692 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
693 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
694
695 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
696 Hi.getValue(1));
697
698 SDOperand Ops[] = { Result, TF };
699 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
700}
Evan Cheng003feb02007-01-04 21:56:39 +0000701
Dan Gohman2a7de412007-10-11 23:57:53 +0000702/// UnrollVectorOp - We know that the given vector has a legal type, however
703/// the operation it performs is not legal and is an operation that we have
704/// no way of lowering. "Unroll" the vector, splitting out the scalars and
705/// operating on each element individually.
706SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
707 MVT::ValueType VT = Op.getValueType();
708 assert(isTypeLegal(VT) &&
709 "Caller should expand or promote operands that are not legal!");
710 assert(Op.Val->getNumValues() == 1 &&
711 "Can't unroll a vector with multiple results!");
712 unsigned NE = MVT::getVectorNumElements(VT);
713 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
714
715 SmallVector<SDOperand, 8> Scalars;
716 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
717 for (unsigned i = 0; i != NE; ++i) {
718 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
719 SDOperand Operand = Op.getOperand(j);
720 MVT::ValueType OperandVT = Operand.getValueType();
721 if (MVT::isVector(OperandVT)) {
722 // A vector operand; extract a single element.
723 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
724 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
725 OperandEltVT,
726 Operand,
727 DAG.getConstant(i, MVT::i32));
728 } else {
729 // A scalar operand; just use it as is.
730 Operands[j] = Operand;
731 }
732 }
733 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
734 &Operands[0], Operands.size()));
735 }
736
737 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
738}
739
Duncan Sands53c954f2008-01-10 10:28:30 +0000740/// GetFPLibCall - Return the right libcall for the given floating point type.
741static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
742 RTLIB::Libcall Call_F32,
743 RTLIB::Libcall Call_F64,
744 RTLIB::Libcall Call_F80,
745 RTLIB::Libcall Call_PPCF128) {
746 return
747 VT == MVT::f32 ? Call_F32 :
748 VT == MVT::f64 ? Call_F64 :
749 VT == MVT::f80 ? Call_F80 :
750 VT == MVT::ppcf128 ? Call_PPCF128 :
751 RTLIB::UNKNOWN_LIBCALL;
752}
753
Dan Gohmanff727882007-07-13 20:14:11 +0000754/// LegalizeOp - We know that the specified value has a legal type, and
755/// that its operands are legal. Now ensure that the operation itself
756/// is legal, recursively ensuring that the operands' operations remain
757/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000758SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000759 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
760 return Op;
761
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000762 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000763 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000764 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000765
Chris Lattnerdc750592005-01-07 07:47:09 +0000766 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000767 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000768 if (Node->getNumValues() > 1) {
769 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000770 if (getTypeAction(Node->getValueType(i)) != Legal) {
771 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000772 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000773 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000774 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000775 }
776 }
777
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000778 // Note that LegalizeOp may be reentered even from single-use nodes, which
779 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000780 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000781 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000782
Nate Begemane5b86d72005-08-10 20:51:12 +0000783 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000784 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000785 bool isCustom = false;
786
Chris Lattnerdc750592005-01-07 07:47:09 +0000787 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000788 case ISD::FrameIndex:
789 case ISD::EntryToken:
790 case ISD::Register:
791 case ISD::BasicBlock:
792 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000793 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000794 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000795 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000796 case ISD::TargetConstantPool:
797 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000798 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000799 case ISD::TargetExternalSymbol:
800 case ISD::VALUETYPE:
801 case ISD::SRCVALUE:
Dan Gohman2d489b52008-02-06 22:27:42 +0000802 case ISD::MEMOPERAND:
Chris Lattnereb637512006-01-28 08:31:04 +0000803 case ISD::STRING:
804 case ISD::CONDCODE:
805 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000806 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000807 "This must be legal!");
808 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000809 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000810 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
811 // If this is a target node, legalize it by legalizing the operands then
812 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000813 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000816
Chris Lattner97af9d52006-08-08 01:09:31 +0000817 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000818
819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
820 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
821 return Result.getValue(Op.ResNo);
822 }
823 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000824#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000825 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000826#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000827 assert(0 && "Do not know how to legalize this operator!");
828 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000829 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000830 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000831 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000832 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000833 case ISD::ConstantPool:
834 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000835 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
836 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000837 case TargetLowering::Custom:
838 Tmp1 = TLI.LowerOperation(Op, DAG);
839 if (Tmp1.Val) Result = Tmp1;
840 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000841 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000842 break;
843 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000844 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000845 case ISD::FRAMEADDR:
846 case ISD::RETURNADDR:
847 // The only option for these nodes is to custom lower them. If the target
848 // does not custom lower them, then return zero.
849 Tmp1 = TLI.LowerOperation(Op, DAG);
850 if (Tmp1.Val)
851 Result = Tmp1;
852 else
853 Result = DAG.getConstant(0, TLI.getPointerTy());
854 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000855 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000856 MVT::ValueType VT = Node->getValueType(0);
857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Custom:
860 Result = TLI.LowerOperation(Op, DAG);
861 if (Result.Val) break;
862 // Fall Thru
863 case TargetLowering::Legal:
864 Result = DAG.getConstant(0, VT);
865 break;
866 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000867 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000868 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000869 case ISD::EXCEPTIONADDR: {
870 Tmp1 = LegalizeOp(Node->getOperand(0));
871 MVT::ValueType VT = Node->getValueType(0);
872 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
873 default: assert(0 && "This action is not supported yet!");
874 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000875 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000876 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000877 }
878 break;
879 case TargetLowering::Custom:
880 Result = TLI.LowerOperation(Op, DAG);
881 if (Result.Val) break;
882 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000883 case TargetLowering::Legal: {
884 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
885 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000886 Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000887 break;
888 }
889 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000890 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000891 if (Result.Val->getNumValues() == 1) break;
892
893 assert(Result.Val->getNumValues() == 2 &&
894 "Cannot return more than two values!");
895
896 // Since we produced two values, make sure to remember that we
897 // legalized both of them.
898 Tmp1 = LegalizeOp(Result);
899 Tmp2 = LegalizeOp(Result.getValue(1));
900 AddLegalizedOperand(Op.getValue(0), Tmp1);
901 AddLegalizedOperand(Op.getValue(1), Tmp2);
902 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +0000903 case ISD::EHSELECTION: {
904 Tmp1 = LegalizeOp(Node->getOperand(0));
905 Tmp2 = LegalizeOp(Node->getOperand(1));
906 MVT::ValueType VT = Node->getValueType(0);
907 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
908 default: assert(0 && "This action is not supported yet!");
909 case TargetLowering::Expand: {
910 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000911 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +0000912 }
913 break;
914 case TargetLowering::Custom:
915 Result = TLI.LowerOperation(Op, DAG);
916 if (Result.Val) break;
917 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000918 case TargetLowering::Legal: {
919 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
920 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000921 Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +0000922 break;
923 }
924 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000925 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000926 if (Result.Val->getNumValues() == 1) break;
927
928 assert(Result.Val->getNumValues() == 2 &&
929 "Cannot return more than two values!");
930
931 // Since we produced two values, make sure to remember that we
932 // legalized both of them.
933 Tmp1 = LegalizeOp(Result);
934 Tmp2 = LegalizeOp(Result.getValue(1));
935 AddLegalizedOperand(Op.getValue(0), Tmp1);
936 AddLegalizedOperand(Op.getValue(1), Tmp2);
937 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000938 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000939 MVT::ValueType VT = Node->getValueType(0);
940 // The only "good" option for this node is to custom lower it.
941 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
942 default: assert(0 && "This action is not supported at all!");
943 case TargetLowering::Custom:
944 Result = TLI.LowerOperation(Op, DAG);
945 if (Result.Val) break;
946 // Fall Thru
947 case TargetLowering::Legal:
948 // Target does not know, how to lower this, lower to noop
949 Result = LegalizeOp(Node->getOperand(0));
950 break;
951 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000952 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000953 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000954 case ISD::AssertSext:
955 case ISD::AssertZext:
956 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000958 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000959 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000960 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000961 Result = Node->getOperand(Op.ResNo);
962 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000963 case ISD::CopyFromReg:
964 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000965 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000966 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000967 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000968 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000969 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000970 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000971 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
973 } else {
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
975 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000976 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
977 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000978 // Since CopyFromReg produces two values, make sure to remember that we
979 // legalized both of them.
980 AddLegalizedOperand(Op.getValue(0), Result);
981 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
982 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000983 case ISD::UNDEF: {
984 MVT::ValueType VT = Op.getValueType();
985 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000986 default: assert(0 && "This action is not supported yet!");
987 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000988 if (MVT::isInteger(VT))
989 Result = DAG.getConstant(0, VT);
990 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000991 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
992 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000993 else
994 assert(0 && "Unknown value type!");
995 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000996 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000997 break;
998 }
999 break;
1000 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001001
Chris Lattnere55d1712006-03-28 00:40:33 +00001002 case ISD::INTRINSIC_W_CHAIN:
1003 case ISD::INTRINSIC_WO_CHAIN:
1004 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001005 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001006 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1007 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001008 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001009
1010 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001011 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001012 TargetLowering::Custom) {
1013 Tmp3 = TLI.LowerOperation(Result, DAG);
1014 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001015 }
1016
1017 if (Result.Val->getNumValues() == 1) break;
1018
1019 // Must have return value and chain result.
1020 assert(Result.Val->getNumValues() == 2 &&
1021 "Cannot return more than two values!");
1022
1023 // Since loads produce two values, make sure to remember that we
1024 // legalized both of them.
1025 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1026 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1027 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +00001028 }
Chris Lattner435b4022005-11-29 06:21:05 +00001029
1030 case ISD::LOCATION:
1031 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1033
1034 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1035 case TargetLowering::Promote:
1036 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001037 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +00001038 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001039 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +00001040 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001041
Jim Laskeyc56315c2007-01-26 21:22:28 +00001042 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001043 const std::string &FName =
1044 cast<StringSDNode>(Node->getOperand(3))->getValue();
1045 const std::string &DirName =
1046 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001047 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001048
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001049 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +00001050 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +00001051 SDOperand LineOp = Node->getOperand(1);
1052 SDOperand ColOp = Node->getOperand(2);
1053
1054 if (useDEBUG_LOC) {
1055 Ops.push_back(LineOp); // line #
1056 Ops.push_back(ColOp); // col #
1057 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001058 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001059 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001060 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1061 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng263070e2008-02-01 02:05:57 +00001062 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001063 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001064 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1065 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001066 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001067 } else {
1068 Result = Tmp1; // chain
1069 }
Chris Lattner435b4022005-11-29 06:21:05 +00001070 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001071 }
Chris Lattner435b4022005-11-29 06:21:05 +00001072 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001073 if (Tmp1 != Node->getOperand(0) ||
1074 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001075 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001076 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001077 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1078 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1079 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1080 } else {
1081 // Otherwise promote them.
1082 Ops.push_back(PromoteOp(Node->getOperand(1)));
1083 Ops.push_back(PromoteOp(Node->getOperand(2)));
1084 }
Chris Lattner435b4022005-11-29 06:21:05 +00001085 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1086 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001087 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001088 }
1089 break;
1090 }
1091 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001092
1093 case ISD::DECLARE:
1094 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1095 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1096 default: assert(0 && "This action is not supported yet!");
1097 case TargetLowering::Legal:
1098 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1099 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1100 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1102 break;
1103 }
1104 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001105
1106 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001107 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001108 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001109 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001110 case TargetLowering::Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1113 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1114 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001116 break;
1117 }
1118 break;
1119
Jim Laskeyf9e54452007-01-26 14:34:52 +00001120 case ISD::LABEL:
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001121 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskeyf9e54452007-01-26 14:34:52 +00001122 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001123 default: assert(0 && "This action is not supported yet!");
1124 case TargetLowering::Legal:
1125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1126 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001127 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskey7c462762005-12-16 22:45:29 +00001129 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001130 case TargetLowering::Expand:
1131 Result = LegalizeOp(Node->getOperand(0));
1132 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001133 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001134 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001135
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001136 case ISD::MEMBARRIER: {
1137 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001138 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1139 default: assert(0 && "This action is not supported yet!");
1140 case TargetLowering::Legal: {
1141 SDOperand Ops[6];
1142 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands96658d02008-02-27 08:53:44 +00001143 for (int x = 1; x < 6; ++x) {
1144 Ops[x] = Node->getOperand(x);
1145 if (!isTypeLegal(Ops[x].getValueType()))
1146 Ops[x] = PromoteOp(Ops[x]);
1147 }
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001148 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1149 break;
1150 }
1151 case TargetLowering::Expand:
1152 //There is no libgcc call for this op
1153 Result = Node->getOperand(0); // Noop
1154 break;
1155 }
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001156 break;
1157 }
1158
Andrew Lenharth95528942008-02-21 06:45:13 +00001159 case ISD::ATOMIC_LCS:
1160 case ISD::ATOMIC_LAS:
1161 case ISD::ATOMIC_SWAP: {
1162 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1163 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1164 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
1165 "Invalid MemBarrier node!");
1166 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
1167 MVT::ValueType VT = Node->getValueType(0);
1168 switch (TLI.getOperationAction(ISD::ATOMIC_LCS, VT)) {
1169 default: assert(0 && "This action is not supported yet!");
1170 case TargetLowering::Legal: {
1171 SDOperand Ops[4];
1172 for (int x = 0; x < num; ++x)
1173 Ops[x] = LegalizeOp(Node->getOperand(x));
1174 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1175 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1176 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1177 return Result.getValue(Op.ResNo);
1178 break;
1179 }
1180 }
1181 break;
1182 }
1183
Scott Michel9d09c5c2007-08-08 23:23:31 +00001184 case ISD::Constant: {
1185 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1186 unsigned opAction =
1187 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1188
Chris Lattnerdc750592005-01-07 07:47:09 +00001189 // We know we don't need to expand constants here, constants only have one
1190 // value and we check that it is fine above.
1191
Scott Michel9d09c5c2007-08-08 23:23:31 +00001192 if (opAction == TargetLowering::Custom) {
1193 Tmp1 = TLI.LowerOperation(Result, DAG);
1194 if (Tmp1.Val)
1195 Result = Tmp1;
1196 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001197 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001198 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001199 case ISD::ConstantFP: {
1200 // Spill FP immediates to the constant pool if the target cannot directly
1201 // codegen them. Targets often have some immediate values that can be
1202 // efficiently generated into an FP register without a load. We explicitly
1203 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001204 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1205
Chris Lattner758b0ac2006-01-29 06:26:56 +00001206 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1207 default: assert(0 && "This action is not supported yet!");
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001208 case TargetLowering::Legal:
1209 break;
Chris Lattner758b0ac2006-01-29 06:26:56 +00001210 case TargetLowering::Custom:
1211 Tmp3 = TLI.LowerOperation(Result, DAG);
1212 if (Tmp3.Val) {
1213 Result = Tmp3;
1214 break;
1215 }
1216 // FALLTHROUGH
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001217 case TargetLowering::Expand: {
1218 // Check to see if this FP immediate is already legal.
1219 bool isLegal = false;
1220 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1221 E = TLI.legal_fpimm_end(); I != E; ++I) {
1222 if (CFP->isExactlyValue(*I)) {
1223 isLegal = true;
1224 break;
1225 }
1226 }
1227 // If this is a legal constant, turn it into a TargetConstantFP node.
1228 if (isLegal)
1229 break;
Evan Cheng3766fc602006-12-12 22:19:28 +00001230 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001231 }
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001232 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001233 break;
1234 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001235 case ISD::TokenFactor:
1236 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001237 Tmp1 = LegalizeOp(Node->getOperand(0));
1238 Tmp2 = LegalizeOp(Node->getOperand(1));
1239 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1240 } else if (Node->getNumOperands() == 3) {
1241 Tmp1 = LegalizeOp(Node->getOperand(0));
1242 Tmp2 = LegalizeOp(Node->getOperand(1));
1243 Tmp3 = LegalizeOp(Node->getOperand(2));
1244 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001245 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001246 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001247 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001248 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1249 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001250 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001251 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001252 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001253
1254 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001255 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001256 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001257 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1258 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001259
1260 // The number of incoming and outgoing values should match; unless the final
1261 // outgoing value is a flag.
1262 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1263 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1264 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1265 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001266 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001267
Chris Lattneraaa23d92006-05-16 22:53:20 +00001268 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001269 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001270 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendlingf359fed2007-11-13 00:44:25 +00001271 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1272 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001273 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001274 if (Op.ResNo == i)
1275 Tmp2 = Tmp1;
1276 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1277 }
1278 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001279 case ISD::EXTRACT_SUBREG: {
1280 Tmp1 = LegalizeOp(Node->getOperand(0));
1281 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1282 assert(idx && "Operand must be a constant");
1283 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1284 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1285 }
1286 break;
1287 case ISD::INSERT_SUBREG: {
1288 Tmp1 = LegalizeOp(Node->getOperand(0));
1289 Tmp2 = LegalizeOp(Node->getOperand(1));
1290 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1291 assert(idx && "Operand must be a constant");
1292 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1294 }
1295 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001296 case ISD::BUILD_VECTOR:
1297 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001298 default: assert(0 && "This action is not supported yet!");
1299 case TargetLowering::Custom:
1300 Tmp3 = TLI.LowerOperation(Result, DAG);
1301 if (Tmp3.Val) {
1302 Result = Tmp3;
1303 break;
1304 }
1305 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001306 case TargetLowering::Expand:
1307 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001308 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001309 }
Chris Lattner29b23012006-03-19 01:17:20 +00001310 break;
1311 case ISD::INSERT_VECTOR_ELT:
1312 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner29b23012006-03-19 01:17:20 +00001313 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman735ab3c2008-02-13 06:43:04 +00001314
1315 // The type of the value to insert may not be legal, even though the vector
1316 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1317 // here.
1318 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1319 default: assert(0 && "Cannot expand insert element operand");
1320 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1321 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1322 }
Chris Lattner29b23012006-03-19 01:17:20 +00001323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1324
1325 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1326 Node->getValueType(0))) {
1327 default: assert(0 && "This action is not supported yet!");
1328 case TargetLowering::Legal:
1329 break;
1330 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001331 Tmp4 = TLI.LowerOperation(Result, DAG);
1332 if (Tmp4.Val) {
1333 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001334 break;
1335 }
1336 // FALLTHROUGH
1337 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001338 // If the insert index is a constant, codegen this as a scalar_to_vector,
1339 // then a shuffle that inserts it into the right position in the vector.
1340 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman735ab3c2008-02-13 06:43:04 +00001341 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1342 // match the element type of the vector being created.
1343 if (Tmp2.getValueType() ==
1344 MVT::getVectorElementType(Op.getValueType())) {
1345 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1346 Tmp1.getValueType(), Tmp2);
1347
1348 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1349 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1350 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1351
1352 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1353 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1354 // elt 0 of the RHS.
1355 SmallVector<SDOperand, 8> ShufOps;
1356 for (unsigned i = 0; i != NumElts; ++i) {
1357 if (i != InsertPos->getValue())
1358 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1359 else
1360 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1361 }
1362 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1363 &ShufOps[0], ShufOps.size());
1364
1365 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1366 Tmp1, ScVec, ShufMask);
1367 Result = LegalizeOp(Result);
1368 break;
Chris Lattner326870b2006-04-17 19:21:01 +00001369 }
Chris Lattner326870b2006-04-17 19:21:01 +00001370 }
1371
Chris Lattner29b23012006-03-19 01:17:20 +00001372 // If the target doesn't support this, we have to spill the input vector
1373 // to a temporary stack slot, update the element, then reload it. This is
1374 // badness. We could also load the value into a vector register (either
1375 // with a "move to register" or "extload into register" instruction, then
1376 // permute it into place, if the idx is a constant and if the idx is
1377 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001378 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman735ab3c2008-02-13 06:43:04 +00001379 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng78e3d562006-04-08 01:46:37 +00001380 MVT::ValueType IdxVT = Tmp3.getValueType();
1381 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001382 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001383
Dan Gohman54d3b5a2008-02-11 18:58:42 +00001384 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman2d489b52008-02-06 22:27:42 +00001385 int SPFI = StackPtrFI->getIndex();
1386
Evan Cheng168e45b2006-03-31 01:27:51 +00001387 // Store the vector.
Dan Gohman2d489b52008-02-06 22:27:42 +00001388 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001389 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00001390 SPFI);
Evan Cheng168e45b2006-03-31 01:27:51 +00001391
1392 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001393 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1394 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001395 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001396 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1397 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1398 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001399 // Store the scalar value.
Nate Begeman735ab3c2008-02-13 06:43:04 +00001400 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1401 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001402 // Load the updated vector.
Dan Gohman2d489b52008-02-06 22:27:42 +00001403 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001404 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner29b23012006-03-19 01:17:20 +00001405 break;
1406 }
1407 }
1408 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001409 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001410 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1411 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1412 break;
1413 }
1414
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001415 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1416 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1417 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1418 Node->getValueType(0))) {
1419 default: assert(0 && "This action is not supported yet!");
1420 case TargetLowering::Legal:
1421 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001422 case TargetLowering::Custom:
1423 Tmp3 = TLI.LowerOperation(Result, DAG);
1424 if (Tmp3.Val) {
1425 Result = Tmp3;
1426 break;
1427 }
1428 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001429 case TargetLowering::Expand:
1430 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001431 break;
1432 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001433 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001434 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1436 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1438
1439 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001440 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1441 default: assert(0 && "Unknown operation action!");
1442 case TargetLowering::Legal:
1443 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1444 "vector shuffle should not be created if not legal!");
1445 break;
1446 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001447 Tmp3 = TLI.LowerOperation(Result, DAG);
1448 if (Tmp3.Val) {
1449 Result = Tmp3;
1450 break;
1451 }
1452 // FALLTHROUGH
1453 case TargetLowering::Expand: {
1454 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001455 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001456 MVT::ValueType PtrVT = TLI.getPointerTy();
1457 SDOperand Mask = Node->getOperand(2);
1458 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001459 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001460 for (unsigned i = 0; i != NumElems; ++i) {
1461 SDOperand Arg = Mask.getOperand(i);
1462 if (Arg.getOpcode() == ISD::UNDEF) {
1463 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1464 } else {
1465 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1466 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1467 if (Idx < NumElems)
1468 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1469 DAG.getConstant(Idx, PtrVT)));
1470 else
1471 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1472 DAG.getConstant(Idx - NumElems, PtrVT)));
1473 }
1474 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001475 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001476 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001477 }
Chris Lattner6be79822006-04-04 17:23:26 +00001478 case TargetLowering::Promote: {
1479 // Change base type to a different vector type.
1480 MVT::ValueType OVT = Node->getValueType(0);
1481 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1482
1483 // Cast the two input vectors.
1484 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1485 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1486
1487 // Convert the shuffle mask to the right # elements.
1488 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1489 assert(Tmp3.Val && "Shuffle not legal?");
1490 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1491 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1492 break;
1493 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001494 }
1495 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001496
1497 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001498 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001499 Tmp2 = LegalizeOp(Node->getOperand(1));
1500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001501 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001502 break;
1503
Dan Gohmana8665142007-06-25 16:23:39 +00001504 case ISD::EXTRACT_SUBVECTOR:
1505 Tmp1 = Node->getOperand(0);
1506 Tmp2 = LegalizeOp(Node->getOperand(1));
1507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1508 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001509 break;
1510
Chris Lattner462505f2006-02-13 09:18:02 +00001511 case ISD::CALLSEQ_START: {
1512 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1513
1514 // Recursively Legalize all of the inputs of the call end that do not lead
1515 // to this call start. This ensures that any libcalls that need be inserted
1516 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001517 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001518 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001519 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1520 NodesLeadingTo);
1521 }
Chris Lattner462505f2006-02-13 09:18:02 +00001522
1523 // Now that we legalized all of the inputs (which may have inserted
1524 // libcalls) create the new CALLSEQ_START node.
1525 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1526
1527 // Merge in the last call, to ensure that this call start after the last
1528 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001529 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001530 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1531 Tmp1 = LegalizeOp(Tmp1);
1532 }
Chris Lattner462505f2006-02-13 09:18:02 +00001533
1534 // Do not try to legalize the target-specific arguments (#1+).
1535 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001536 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001537 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001538 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001539 }
1540
1541 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001542 AddLegalizedOperand(Op.getValue(0), Result);
1543 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1544 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1545
Chris Lattner462505f2006-02-13 09:18:02 +00001546 // Now that the callseq_start and all of the non-call nodes above this call
1547 // sequence have been legalized, legalize the call itself. During this
1548 // process, no libcalls can/will be inserted, guaranteeing that no calls
1549 // can overlap.
1550 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1551 SDOperand InCallSEQ = LastCALLSEQ_END;
1552 // Note that we are selecting this call!
1553 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1554 IsLegalizingCall = true;
1555
1556 // Legalize the call, starting from the CALLSEQ_END.
1557 LegalizeOp(LastCALLSEQ_END);
1558 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1559 return Result;
1560 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001561 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001562 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1563 // will cause this node to be legalized as well as handling libcalls right.
1564 if (LastCALLSEQ_END.Val != Node) {
1565 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001566 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001567 assert(I != LegalizedNodes.end() &&
1568 "Legalizing the call start should have legalized this node!");
1569 return I->second;
1570 }
1571
1572 // Otherwise, the call start has been legalized and everything is going
1573 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001574 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001575 // Do not try to legalize the target-specific arguments (#1+), except for
1576 // an optional flag input.
1577 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1578 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001579 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001580 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001581 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001582 }
1583 } else {
1584 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1585 if (Tmp1 != Node->getOperand(0) ||
1586 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001587 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001588 Ops[0] = Tmp1;
1589 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001590 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001591 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001592 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001593 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001594 // This finishes up call legalization.
1595 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001596
1597 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1598 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1599 if (Node->getNumValues() == 2)
1600 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1601 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001602 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001603 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001604 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1605 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1606 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001608
Chris Lattnerd02b0542006-01-28 10:58:55 +00001609 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001610 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001611 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001612 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001613 case TargetLowering::Expand: {
1614 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1615 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1616 " not tell us which reg is the stack pointer!");
1617 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001618
1619 // Chain the dynamic stack allocation so that it doesn't modify the stack
1620 // pointer when other instructions are using the stack.
1621 Chain = DAG.getCALLSEQ_START(Chain,
1622 DAG.getConstant(0, TLI.getPointerTy()));
1623
Chris Lattner59b82f92006-01-15 08:54:32 +00001624 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001625 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1626 Chain = SP.getValue(1);
1627 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1628 unsigned StackAlign =
1629 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1630 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001631 SP = DAG.getNode(ISD::AND, VT, SP,
1632 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001633 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001634 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1635
1636 Tmp2 =
1637 DAG.getCALLSEQ_END(Chain,
1638 DAG.getConstant(0, TLI.getPointerTy()),
1639 DAG.getConstant(0, TLI.getPointerTy()),
1640 SDOperand());
1641
Chris Lattnerd02b0542006-01-28 10:58:55 +00001642 Tmp1 = LegalizeOp(Tmp1);
1643 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001644 break;
1645 }
1646 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001647 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1648 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001649 Tmp1 = LegalizeOp(Tmp3);
1650 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001651 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001652 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001653 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001654 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001655 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001656 // Since this op produce two values, make sure to remember that we
1657 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001658 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1659 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001660 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001661 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001662 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001663 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001664 bool Changed = false;
1665 // Legalize all of the operands of the inline asm, in case they are nodes
1666 // that need to be expanded or something. Note we skip the asm string and
1667 // all of the TargetConstant flags.
1668 SDOperand Op = LegalizeOp(Ops[0]);
1669 Changed = Op != Ops[0];
1670 Ops[0] = Op;
1671
1672 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1673 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1674 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1675 for (++i; NumVals; ++i, --NumVals) {
1676 SDOperand Op = LegalizeOp(Ops[i]);
1677 if (Op != Ops[i]) {
1678 Changed = true;
1679 Ops[i] = Op;
1680 }
1681 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001682 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001683
1684 if (HasInFlag) {
1685 Op = LegalizeOp(Ops.back());
1686 Changed |= Op != Ops.back();
1687 Ops.back() = Op;
1688 }
1689
1690 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001691 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001692
1693 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001694 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001695 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1696 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001697 }
Chris Lattner68a12142005-01-07 22:12:08 +00001698 case ISD::BR:
1699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001700 // Ensure that libcalls are emitted before a branch.
1701 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1702 Tmp1 = LegalizeOp(Tmp1);
1703 LastCALLSEQ_END = DAG.getEntryNode();
1704
Chris Lattnerd02b0542006-01-28 10:58:55 +00001705 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001706 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001707 case ISD::BRIND:
1708 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1709 // Ensure that libcalls are emitted before a branch.
1710 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1711 Tmp1 = LegalizeOp(Tmp1);
1712 LastCALLSEQ_END = DAG.getEntryNode();
1713
1714 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1715 default: assert(0 && "Indirect target must be legal type (pointer)!");
1716 case Legal:
1717 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1718 break;
1719 }
1720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1721 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001722 case ISD::BR_JT:
1723 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1724 // Ensure that libcalls are emitted before a branch.
1725 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1726 Tmp1 = LegalizeOp(Tmp1);
1727 LastCALLSEQ_END = DAG.getEntryNode();
1728
1729 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1731
1732 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1733 default: assert(0 && "This action is not supported yet!");
1734 case TargetLowering::Legal: break;
1735 case TargetLowering::Custom:
1736 Tmp1 = TLI.LowerOperation(Result, DAG);
1737 if (Tmp1.Val) Result = Tmp1;
1738 break;
1739 case TargetLowering::Expand: {
1740 SDOperand Chain = Result.getOperand(0);
1741 SDOperand Table = Result.getOperand(1);
1742 SDOperand Index = Result.getOperand(2);
1743
1744 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001745 MachineFunction &MF = DAG.getMachineFunction();
1746 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001747 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1748 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001749
1750 SDOperand LD;
1751 switch (EntrySize) {
1752 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001753 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001754 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001755 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001756 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskey70323a82006-12-14 19:17:33 +00001757 }
1758
Evan Cheng797d56f2007-11-09 01:32:10 +00001759 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001760 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001761 // For PIC, the sequence is:
1762 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001763 // RelocBase can be JumpTable, GOT or some sort of global base.
1764 if (PTy != MVT::i32)
1765 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1766 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1767 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001768 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001769 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001770 }
1771 }
1772 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001773 case ISD::BRCOND:
1774 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001775 // Ensure that libcalls are emitted before a return.
1776 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1777 Tmp1 = LegalizeOp(Tmp1);
1778 LastCALLSEQ_END = DAG.getEntryNode();
1779
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001780 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1781 case Expand: assert(0 && "It's impossible to expand bools");
1782 case Legal:
1783 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1784 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00001785 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001786 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001787
1788 // The top bits of the promoted condition are not necessarily zero, ensure
1789 // that the value is properly zero extended.
Dan Gohman1f372ed2008-02-25 21:11:39 +00001790 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00001791 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman1f372ed2008-02-25 21:11:39 +00001792 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerdb189382006-11-27 04:39:56 +00001793 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001794 break;
1795 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00001796 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001797
1798 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001799 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001800
1801 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1802 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001803 case TargetLowering::Legal: break;
1804 case TargetLowering::Custom:
1805 Tmp1 = TLI.LowerOperation(Result, DAG);
1806 if (Tmp1.Val) Result = Tmp1;
1807 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001808 case TargetLowering::Expand:
1809 // Expand brcond's setcc into its constituent parts and create a BR_CC
1810 // Node.
1811 if (Tmp2.getOpcode() == ISD::SETCC) {
1812 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1813 Tmp2.getOperand(0), Tmp2.getOperand(1),
1814 Node->getOperand(2));
1815 } else {
1816 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1817 DAG.getCondCode(ISD::SETNE), Tmp2,
1818 DAG.getConstant(0, Tmp2.getValueType()),
1819 Node->getOperand(2));
1820 }
1821 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001822 }
1823 break;
1824 case ISD::BR_CC:
1825 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001826 // Ensure that libcalls are emitted before a branch.
1827 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1828 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001829 Tmp2 = Node->getOperand(2); // LHS
1830 Tmp3 = Node->getOperand(3); // RHS
1831 Tmp4 = Node->getOperand(1); // CC
1832
1833 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001834 LastCALLSEQ_END = DAG.getEntryNode();
1835
Nate Begeman7e7f4392006-02-01 07:19:44 +00001836 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1837 // the LHS is a legal SETCC itself. In this case, we need to compare
1838 // the result against zero to select between true and false values.
1839 if (Tmp3.Val == 0) {
1840 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1841 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001842 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001843
1844 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1845 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001846
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001847 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1848 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001849 case TargetLowering::Legal: break;
1850 case TargetLowering::Custom:
1851 Tmp4 = TLI.LowerOperation(Result, DAG);
1852 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001853 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001854 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001855 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001856 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001857 LoadSDNode *LD = cast<LoadSDNode>(Node);
1858 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1859 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001860
Evan Chenge71fe34d2006-10-09 20:57:25 +00001861 ISD::LoadExtType ExtType = LD->getExtensionType();
1862 if (ExtType == ISD::NON_EXTLOAD) {
1863 MVT::ValueType VT = Node->getValueType(0);
1864 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1865 Tmp3 = Result.getValue(0);
1866 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001867
Evan Chenge71fe34d2006-10-09 20:57:25 +00001868 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1869 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001870 case TargetLowering::Legal:
1871 // If this is an unaligned load and the target doesn't support it,
1872 // expand it.
1873 if (!TLI.allowsUnalignedMemoryAccesses()) {
1874 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001875 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001876 if (LD->getAlignment() < ABIAlignment){
1877 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1878 TLI);
1879 Tmp3 = Result.getOperand(0);
1880 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001881 Tmp3 = LegalizeOp(Tmp3);
1882 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001883 }
1884 }
1885 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001886 case TargetLowering::Custom:
1887 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1888 if (Tmp1.Val) {
1889 Tmp3 = LegalizeOp(Tmp1);
1890 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001891 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001892 break;
1893 case TargetLowering::Promote: {
1894 // Only promote a load of vector type to another.
1895 assert(MVT::isVector(VT) && "Cannot promote this load!");
1896 // Change base type to a different vector type.
1897 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1898
1899 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001900 LD->getSrcValueOffset(),
1901 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001902 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1903 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001904 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001905 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001906 }
1907 // Since loads produce two values, make sure to remember that we
1908 // legalized both of them.
1909 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1910 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1911 return Op.ResNo ? Tmp4 : Tmp3;
1912 } else {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001913 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands95d46ef2008-01-23 20:39:46 +00001914 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1915 int SVOffset = LD->getSrcValueOffset();
1916 unsigned Alignment = LD->getAlignment();
1917 bool isVolatile = LD->isVolatile();
1918
1919 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1920 // Some targets pretend to have an i1 loading operation, and actually
1921 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1922 // bits are guaranteed to be zero; it helps the optimizers understand
1923 // that these bits are zero. It is also useful for EXTLOAD, since it
1924 // tells the optimizers that those bits are undefined. It would be
1925 // nice to have an effective generic way of getting these benefits...
1926 // Until such a way is found, don't insist on promoting i1 here.
1927 (SrcVT != MVT::i1 ||
1928 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1929 // Promote to a byte-sized load if not loading an integral number of
1930 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1931 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1932 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1933 SDOperand Ch;
1934
1935 // The extra bits are guaranteed to be zero, since we stored them that
1936 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1937
1938 ISD::LoadExtType NewExtType =
1939 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1940
1941 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1942 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1943 NVT, isVolatile, Alignment);
1944
1945 Ch = Result.getValue(1); // The chain.
1946
1947 if (ExtType == ISD::SEXTLOAD)
1948 // Having the top bits zero doesn't help when sign extending.
1949 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1950 Result, DAG.getValueType(SrcVT));
1951 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1952 // All the top bits are guaranteed to be zero - inform the optimizers.
1953 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1954 DAG.getValueType(SrcVT));
1955
1956 Tmp1 = LegalizeOp(Result);
1957 Tmp2 = LegalizeOp(Ch);
1958 } else if (SrcWidth & (SrcWidth - 1)) {
1959 // If not loading a power-of-2 number of bits, expand as two loads.
1960 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1961 "Unsupported extload!");
1962 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1963 assert(RoundWidth < SrcWidth);
1964 unsigned ExtraWidth = SrcWidth - RoundWidth;
1965 assert(ExtraWidth < RoundWidth);
1966 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1967 "Load size not an integral number of bytes!");
1968 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1969 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1970 SDOperand Lo, Hi, Ch;
1971 unsigned IncrementSize;
1972
1973 if (TLI.isLittleEndian()) {
1974 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1975 // Load the bottom RoundWidth bits.
1976 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1977 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1978 Alignment);
1979
1980 // Load the remaining ExtraWidth bits.
1981 IncrementSize = RoundWidth / 8;
1982 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1983 DAG.getIntPtrConstant(IncrementSize));
1984 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1985 LD->getSrcValue(), SVOffset + IncrementSize,
1986 ExtraVT, isVolatile,
1987 MinAlign(Alignment, IncrementSize));
1988
1989 // Build a factor node to remember that this load is independent of the
1990 // other one.
1991 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1992 Hi.getValue(1));
1993
1994 // Move the top bits to the right place.
1995 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1996 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1997
1998 // Join the hi and lo parts.
1999 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002000 } else {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002001 // Big endian - avoid unaligned loads.
2002 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2003 // Load the top RoundWidth bits.
2004 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2005 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2006 Alignment);
2007
2008 // Load the remaining ExtraWidth bits.
2009 IncrementSize = RoundWidth / 8;
2010 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2011 DAG.getIntPtrConstant(IncrementSize));
2012 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2013 LD->getSrcValue(), SVOffset + IncrementSize,
2014 ExtraVT, isVolatile,
2015 MinAlign(Alignment, IncrementSize));
2016
2017 // Build a factor node to remember that this load is independent of the
2018 // other one.
2019 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2020 Hi.getValue(1));
2021
2022 // Move the top bits to the right place.
2023 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2024 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2025
2026 // Join the hi and lo parts.
2027 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2028 }
2029
2030 Tmp1 = LegalizeOp(Result);
2031 Tmp2 = LegalizeOp(Ch);
2032 } else {
2033 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2034 default: assert(0 && "This action is not supported yet!");
2035 case TargetLowering::Custom:
2036 isCustom = true;
2037 // FALLTHROUGH
2038 case TargetLowering::Legal:
2039 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2040 Tmp1 = Result.getValue(0);
2041 Tmp2 = Result.getValue(1);
2042
2043 if (isCustom) {
2044 Tmp3 = TLI.LowerOperation(Result, DAG);
2045 if (Tmp3.Val) {
2046 Tmp1 = LegalizeOp(Tmp3);
2047 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2048 }
2049 } else {
2050 // If this is an unaligned load and the target doesn't support it,
2051 // expand it.
2052 if (!TLI.allowsUnalignedMemoryAccesses()) {
2053 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002054 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands95d46ef2008-01-23 20:39:46 +00002055 if (LD->getAlignment() < ABIAlignment){
2056 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2057 TLI);
2058 Tmp1 = Result.getOperand(0);
2059 Tmp2 = Result.getOperand(1);
2060 Tmp1 = LegalizeOp(Tmp1);
2061 Tmp2 = LegalizeOp(Tmp2);
2062 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002063 }
2064 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002065 break;
2066 case TargetLowering::Expand:
2067 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2068 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2069 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2070 LD->getSrcValueOffset(),
2071 LD->isVolatile(), LD->getAlignment());
2072 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2073 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2074 Tmp2 = LegalizeOp(Load.getValue(1));
2075 break;
2076 }
2077 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2078 // Turn the unsupported load into an EXTLOAD followed by an explicit
2079 // zero/sign extend inreg.
2080 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2081 Tmp1, Tmp2, LD->getSrcValue(),
2082 LD->getSrcValueOffset(), SrcVT,
2083 LD->isVolatile(), LD->getAlignment());
2084 SDOperand ValRes;
2085 if (ExtType == ISD::SEXTLOAD)
2086 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2087 Result, DAG.getValueType(SrcVT));
2088 else
2089 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2090 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2091 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002092 break;
2093 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002094 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002095
Evan Chenge71fe34d2006-10-09 20:57:25 +00002096 // Since loads produce two values, make sure to remember that we legalized
2097 // both of them.
2098 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2099 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2100 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002101 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002102 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002103 case ISD::EXTRACT_ELEMENT: {
2104 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2105 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002106 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00002107 case Legal:
2108 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2109 // 1 -> Hi
2110 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2111 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2112 TLI.getShiftAmountTy()));
2113 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2114 } else {
2115 // 0 -> Lo
2116 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2117 Node->getOperand(0));
2118 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002119 break;
2120 case Expand:
2121 // Get both the low and high parts.
2122 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2123 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2124 Result = Tmp2; // 1 -> Hi
2125 else
2126 Result = Tmp1; // 0 -> Lo
2127 break;
2128 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002129 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00002130 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002131
2132 case ISD::CopyToReg:
2133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00002134
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002135 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00002136 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00002137 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00002138 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002139 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002141 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002142 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002143 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002144 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002145 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2146 Tmp3);
2147 } else {
2148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002149 }
2150
2151 // Since this produces two values, make sure to remember that we legalized
2152 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002153 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00002154 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002155 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00002156 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002157 break;
2158
2159 case ISD::RET:
2160 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002161
2162 // Ensure that libcalls are emitted before a return.
2163 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2164 Tmp1 = LegalizeOp(Tmp1);
2165 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002166
Chris Lattnerdc750592005-01-07 07:47:09 +00002167 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00002168 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00002169 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00002170 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002171 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00002172 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00002173 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00002174 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002175 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00002176 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002177 SDOperand Lo, Hi;
2178 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00002179
2180 // Big endian systems want the hi reg first.
Duncan Sands7377f5f2008-02-11 10:37:04 +00002181 if (TLI.isBigEndian())
Chris Lattner13780ac2007-03-06 20:01:06 +00002182 std::swap(Lo, Hi);
2183
Evan Cheng3432ab92006-12-11 19:27:14 +00002184 if (Hi.Val)
2185 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2186 else
2187 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00002188 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002189 } else {
2190 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002191 int InIx = Tmp2.ResNo;
2192 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2193 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002194
Dan Gohmana8665142007-06-25 16:23:39 +00002195 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002196 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002197 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002198 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002199 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002200 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002202 } else if (NumElems == 1) {
2203 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002204 Tmp2 = ScalarizeVectorOp(Tmp2);
2205 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002206 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002207
2208 // FIXME: Returns of gcc generic vectors smaller than a legal type
2209 // should be returned in integer registers!
2210
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002211 // The scalarized value type may not be legal, e.g. it might require
2212 // promotion or expansion. Relegalize the return.
2213 Result = LegalizeOp(Result);
2214 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002215 // FIXME: Returns of gcc generic vectors larger than a legal vector
2216 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002217 SDOperand Lo, Hi;
2218 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002219 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002220 Result = LegalizeOp(Result);
2221 }
2222 }
Misha Brukman835702a2005-04-21 22:36:52 +00002223 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002224 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002225 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002226 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002227 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002228 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002229 }
2230 break;
2231 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002232 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002233 break;
2234 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00002235 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002236 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002237 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002238 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2239 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002240 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002241 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002242 break;
2243 case Expand: {
2244 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00002245 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002246 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002247 ExpandOp(Node->getOperand(i), Lo, Hi);
2248 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002249 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00002250 if (Hi.Val) {
2251 NewValues.push_back(Hi);
2252 NewValues.push_back(Node->getOperand(i+1));
2253 }
Misha Brukman835702a2005-04-21 22:36:52 +00002254 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002255 }
2256 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002257 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002258 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002259
2260 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002261 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002262 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002263 Result = DAG.getNode(ISD::RET, MVT::Other,
2264 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002265 break;
2266 }
2267 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002268
Chris Lattner4d1ea712006-01-29 21:02:23 +00002269 if (Result.getOpcode() == ISD::RET) {
2270 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2271 default: assert(0 && "This action is not supported yet!");
2272 case TargetLowering::Legal: break;
2273 case TargetLowering::Custom:
2274 Tmp1 = TLI.LowerOperation(Result, DAG);
2275 if (Tmp1.Val) Result = Tmp1;
2276 break;
2277 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002278 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002279 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002280 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002281 StoreSDNode *ST = cast<StoreSDNode>(Node);
2282 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2283 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002284 int SVOffset = ST->getSrcValueOffset();
2285 unsigned Alignment = ST->getAlignment();
2286 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002287
Evan Chengab51cf22006-10-13 21:14:26 +00002288 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002289 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2290 // FIXME: We shouldn't do this for TargetConstantFP's.
2291 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2292 // to phase ordering between legalized code and the dag combiner. This
2293 // probably means that we need to integrate dag combiner and legalizer
2294 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002295 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002296 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002297 if (CFP->getValueType(0) == MVT::f32 &&
2298 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen028084e2007-09-12 03:30:33 +00002299 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2300 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002301 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002302 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2303 SVOffset, isVolatile, Alignment);
2304 break;
2305 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002306 // If this target supports 64-bit registers, do a single 64-bit store.
2307 if (getTypeAction(MVT::i64) == Legal) {
2308 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2309 getZExtValue(), MVT::i64);
2310 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2311 SVOffset, isVolatile, Alignment);
2312 break;
2313 } else if (getTypeAction(MVT::i32) == Legal) {
2314 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2315 // stores. If the target supports neither 32- nor 64-bits, this
2316 // xform is certainly not worth it.
2317 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2318 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2319 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00002320 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002321
2322 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2323 SVOffset, isVolatile, Alignment);
2324 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002325 DAG.getIntPtrConstant(4));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002326 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002327 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002328
2329 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2330 break;
2331 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002332 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002333 }
2334
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002335 switch (getTypeAction(ST->getMemoryVT())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002336 case Legal: {
2337 Tmp3 = LegalizeOp(ST->getValue());
2338 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2339 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002340
Evan Chengab51cf22006-10-13 21:14:26 +00002341 MVT::ValueType VT = Tmp3.getValueType();
2342 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2343 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002344 case TargetLowering::Legal:
2345 // If this is an unaligned store and the target doesn't support it,
2346 // expand it.
2347 if (!TLI.allowsUnalignedMemoryAccesses()) {
2348 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002349 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002350 if (ST->getAlignment() < ABIAlignment)
2351 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2352 TLI);
2353 }
2354 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002355 case TargetLowering::Custom:
2356 Tmp1 = TLI.LowerOperation(Result, DAG);
2357 if (Tmp1.Val) Result = Tmp1;
2358 break;
2359 case TargetLowering::Promote:
2360 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2361 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2362 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2363 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002364 ST->getSrcValue(), SVOffset, isVolatile,
2365 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002366 break;
2367 }
2368 break;
2369 }
2370 case Promote:
2371 // Truncate the value and store the result.
2372 Tmp3 = PromoteOp(ST->getValue());
2373 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002374 SVOffset, ST->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00002375 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002376 break;
2377
2378 case Expand:
2379 unsigned IncrementSize = 0;
2380 SDOperand Lo, Hi;
2381
2382 // If this is a vector type, then we have to calculate the increment as
2383 // the product of the element size in bytes, and the number of elements
2384 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002385 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002386 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002387 int InIx = ST->getValue().ResNo;
Chris Lattner72733e52008-01-17 07:00:52 +00002388 MVT::ValueType InVT = InVal->getValueType(InIx);
2389 unsigned NumElems = MVT::getVectorNumElements(InVT);
2390 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Chengab51cf22006-10-13 21:14:26 +00002391
Dan Gohmana8665142007-06-25 16:23:39 +00002392 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002393 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002394 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002395 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002396 // Turn this into a normal store of the vector type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002397 Tmp3 = LegalizeOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002398 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002399 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002400 Result = LegalizeOp(Result);
2401 break;
2402 } else if (NumElems == 1) {
2403 // Turn this into a normal store of the scalar type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002404 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002405 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002406 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002407 // The scalarized value type may not be legal, e.g. it might require
2408 // promotion or expansion. Relegalize the scalar store.
2409 Result = LegalizeOp(Result);
2410 break;
2411 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002412 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00002413 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2414 MVT::getSizeInBits(EVT)/8;
Evan Chengab51cf22006-10-13 21:14:26 +00002415 }
2416 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002417 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002418 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002419
Duncan Sands7377f5f2008-02-11 10:37:04 +00002420 if (TLI.isBigEndian())
Evan Chengab51cf22006-10-13 21:14:26 +00002421 std::swap(Lo, Hi);
2422 }
2423
2424 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002425 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002426
2427 if (Hi.Val == NULL) {
2428 // Must be int <-> float one-to-one expansion.
2429 Result = Lo;
2430 break;
2431 }
2432
Evan Chengab51cf22006-10-13 21:14:26 +00002433 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002434 DAG.getIntPtrConstant(IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00002435 assert(isTypeLegal(Tmp2.getValueType()) &&
2436 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002437 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002438 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002439 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002440 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002441 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2442 break;
2443 }
2444 } else {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002445 switch (getTypeAction(ST->getValue().getValueType())) {
2446 case Legal:
2447 Tmp3 = LegalizeOp(ST->getValue());
2448 break;
2449 case Promote:
2450 // We can promote the value, the truncstore will still take care of it.
2451 Tmp3 = PromoteOp(ST->getValue());
2452 break;
2453 case Expand:
2454 // Just store the low part. This may become a non-trunc store, so make
2455 // sure to use getTruncStore, not UpdateNodeOperands below.
2456 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2457 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2458 SVOffset, MVT::i8, isVolatile, Alignment);
2459 }
Evan Chengab51cf22006-10-13 21:14:26 +00002460
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002461 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands88de26c2008-01-22 07:17:34 +00002462 unsigned StWidth = MVT::getSizeInBits(StVT);
2463
2464 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2465 // Promote to a byte-sized store with upper bits zero if not
2466 // storing an integral number of bytes. For example, promote
2467 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2468 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2469 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2470 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2471 SVOffset, NVT, isVolatile, Alignment);
2472 } else if (StWidth & (StWidth - 1)) {
2473 // If not storing a power-of-2 number of bits, expand as two stores.
2474 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2475 "Unsupported truncstore!");
2476 unsigned RoundWidth = 1 << Log2_32(StWidth);
2477 assert(RoundWidth < StWidth);
2478 unsigned ExtraWidth = StWidth - RoundWidth;
2479 assert(ExtraWidth < RoundWidth);
2480 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2481 "Store size not an integral number of bytes!");
2482 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2483 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2484 SDOperand Lo, Hi;
2485 unsigned IncrementSize;
2486
2487 if (TLI.isLittleEndian()) {
2488 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2489 // Store the bottom RoundWidth bits.
2490 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2491 SVOffset, RoundVT,
2492 isVolatile, Alignment);
2493
2494 // Store the remaining ExtraWidth bits.
2495 IncrementSize = RoundWidth / 8;
2496 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2497 DAG.getIntPtrConstant(IncrementSize));
2498 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2499 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2500 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2501 SVOffset + IncrementSize, ExtraVT, isVolatile,
2502 MinAlign(Alignment, IncrementSize));
2503 } else {
2504 // Big endian - avoid unaligned stores.
2505 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2506 // Store the top RoundWidth bits.
2507 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2508 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2509 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2510 RoundVT, isVolatile, Alignment);
2511
2512 // Store the remaining ExtraWidth bits.
2513 IncrementSize = RoundWidth / 8;
2514 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2515 DAG.getIntPtrConstant(IncrementSize));
2516 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2517 SVOffset + IncrementSize, ExtraVT, isVolatile,
2518 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002519 }
Duncan Sands88de26c2008-01-22 07:17:34 +00002520
2521 // The order of the stores doesn't matter.
2522 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2523 } else {
2524 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2525 Tmp2 != ST->getBasePtr())
2526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2527 ST->getOffset());
2528
2529 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2530 default: assert(0 && "This action is not supported yet!");
2531 case TargetLowering::Legal:
2532 // If this is an unaligned store and the target doesn't support it,
2533 // expand it.
2534 if (!TLI.allowsUnalignedMemoryAccesses()) {
2535 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002536 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands88de26c2008-01-22 07:17:34 +00002537 if (ST->getAlignment() < ABIAlignment)
2538 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2539 TLI);
2540 }
2541 break;
2542 case TargetLowering::Custom:
2543 Result = TLI.LowerOperation(Result, DAG);
2544 break;
2545 case Expand:
2546 // TRUNCSTORE:i16 i32 -> STORE i16
2547 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2548 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2549 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2550 isVolatile, Alignment);
2551 break;
2552 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00002553 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002554 }
2555 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002556 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002557 case ISD::PCMARKER:
2558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002559 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002560 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002561 case ISD::STACKSAVE:
2562 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002563 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2564 Tmp1 = Result.getValue(0);
2565 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002566
Chris Lattnerb3266452006-01-13 02:50:02 +00002567 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2568 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002569 case TargetLowering::Legal: break;
2570 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002571 Tmp3 = TLI.LowerOperation(Result, DAG);
2572 if (Tmp3.Val) {
2573 Tmp1 = LegalizeOp(Tmp3);
2574 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002575 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002576 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002577 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002578 // Expand to CopyFromReg if the target set
2579 // StackPointerRegisterToSaveRestore.
2580 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002581 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002582 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002583 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002584 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002585 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2586 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002587 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002588 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002589 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002590
2591 // Since stacksave produce two values, make sure to remember that we
2592 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002593 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2594 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2595 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002596
Chris Lattnerb3266452006-01-13 02:50:02 +00002597 case ISD::STACKRESTORE:
2598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2599 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002601
2602 switch (TLI.getOperationAction(ISD::STACKRESTORE, 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:
2606 Tmp1 = TLI.LowerOperation(Result, DAG);
2607 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002608 break;
2609 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002610 // Expand to CopyToReg if the target set
2611 // StackPointerRegisterToSaveRestore.
2612 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2613 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2614 } else {
2615 Result = Tmp1;
2616 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002617 break;
2618 }
2619 break;
2620
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002621 case ISD::READCYCLECOUNTER:
2622 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002623 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002624 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2625 Node->getValueType(0))) {
2626 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002627 case TargetLowering::Legal:
2628 Tmp1 = Result.getValue(0);
2629 Tmp2 = Result.getValue(1);
2630 break;
Evan Cheng69739932006-11-29 08:26:18 +00002631 case TargetLowering::Custom:
2632 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002633 Tmp1 = LegalizeOp(Result.getValue(0));
2634 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002635 break;
2636 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002637
2638 // Since rdcc produce two values, make sure to remember that we legalized
2639 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002640 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2641 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002642 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002643
Chris Lattner39c67442005-01-14 22:08:15 +00002644 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002645 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2646 case Expand: assert(0 && "It's impossible to expand bools");
2647 case Legal:
2648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2649 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002650 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002651 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002652 // Make sure the condition is either zero or one.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002653 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002654 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002655 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattner3abb6362006-11-28 01:03:30 +00002656 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002657 break;
2658 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002659 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002660 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002661 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002662
Chris Lattnerd02b0542006-01-28 10:58:55 +00002663 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002664
Nate Begeman987121a2005-08-23 04:29:48 +00002665 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002666 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002667 case TargetLowering::Legal: break;
2668 case TargetLowering::Custom: {
2669 Tmp1 = TLI.LowerOperation(Result, DAG);
2670 if (Tmp1.Val) Result = Tmp1;
2671 break;
2672 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002673 case TargetLowering::Expand:
2674 if (Tmp1.getOpcode() == ISD::SETCC) {
2675 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2676 Tmp2, Tmp3,
2677 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2678 } else {
2679 Result = DAG.getSelectCC(Tmp1,
2680 DAG.getConstant(0, Tmp1.getValueType()),
2681 Tmp2, Tmp3, ISD::SETNE);
2682 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002683 break;
2684 case TargetLowering::Promote: {
2685 MVT::ValueType NVT =
2686 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2687 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002688 if (MVT::isVector(Tmp2.getValueType())) {
2689 ExtOp = ISD::BIT_CONVERT;
2690 TruncOp = ISD::BIT_CONVERT;
2691 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002692 ExtOp = ISD::ANY_EXTEND;
2693 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002694 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002695 ExtOp = ISD::FP_EXTEND;
2696 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002697 }
2698 // Promote each of the values to the new type.
2699 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2700 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2701 // Perform the larger operation, then round down.
2702 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner72733e52008-01-17 07:00:52 +00002703 if (TruncOp != ISD::FP_ROUND)
2704 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2705 else
2706 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2707 DAG.getIntPtrConstant(0));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002708 break;
2709 }
2710 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002711 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002712 case ISD::SELECT_CC: {
2713 Tmp1 = Node->getOperand(0); // LHS
2714 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002715 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2716 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002717 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002718
Nate Begeman7e7f4392006-02-01 07:19:44 +00002719 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2720
2721 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2722 // the LHS is a legal SETCC itself. In this case, we need to compare
2723 // the result against zero to select between true and false values.
2724 if (Tmp2.Val == 0) {
2725 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2726 CC = DAG.getCondCode(ISD::SETNE);
2727 }
2728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2729
2730 // Everything is legal, see if we should expand this op or something.
2731 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2732 default: assert(0 && "This action is not supported yet!");
2733 case TargetLowering::Legal: break;
2734 case TargetLowering::Custom:
2735 Tmp1 = TLI.LowerOperation(Result, DAG);
2736 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002737 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002738 }
2739 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002740 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002741 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002742 Tmp1 = Node->getOperand(0);
2743 Tmp2 = Node->getOperand(1);
2744 Tmp3 = Node->getOperand(2);
2745 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2746
2747 // If we had to Expand the SetCC operands into a SELECT node, then it may
2748 // not always be possible to return a true LHS & RHS. In this case, just
2749 // return the value we legalized, returned in the LHS
2750 if (Tmp2.Val == 0) {
2751 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002752 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002753 }
Nate Begeman987121a2005-08-23 04:29:48 +00002754
Chris Lattnerf263a232006-01-30 22:43:50 +00002755 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002756 default: assert(0 && "Cannot handle this action for SETCC yet!");
2757 case TargetLowering::Custom:
2758 isCustom = true;
2759 // FALLTHROUGH.
2760 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002761 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002762 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002763 Tmp4 = TLI.LowerOperation(Result, DAG);
2764 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002765 }
Nate Begeman987121a2005-08-23 04:29:48 +00002766 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002767 case TargetLowering::Promote: {
2768 // First step, figure out the appropriate operation to use.
2769 // Allow SETCC to not be supported for all legal data types
2770 // Mostly this targets FP
2771 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002772 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002773
2774 // Scan for the appropriate larger type to use.
2775 while (1) {
2776 NewInTy = (MVT::ValueType)(NewInTy+1);
2777
2778 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2779 "Fell off of the edge of the integer world");
2780 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2781 "Fell off of the edge of the floating point world");
2782
2783 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002784 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002785 break;
2786 }
2787 if (MVT::isInteger(NewInTy))
2788 assert(0 && "Cannot promote Legal Integer SETCC yet");
2789 else {
2790 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2791 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2792 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002793 Tmp1 = LegalizeOp(Tmp1);
2794 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002796 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002797 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002798 }
Nate Begeman987121a2005-08-23 04:29:48 +00002799 case TargetLowering::Expand:
2800 // Expand a setcc node into a select_cc of the same condition, lhs, and
2801 // rhs that selects between const 1 (true) and const 0 (false).
2802 MVT::ValueType VT = Node->getValueType(0);
2803 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2804 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002805 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002806 break;
2807 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002808 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002809 case ISD::MEMSET:
2810 case ISD::MEMCPY:
2811 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002812 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002813 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2814
2815 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2816 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2817 case Expand: assert(0 && "Cannot expand a byte!");
2818 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002819 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002820 break;
2821 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002822 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002823 break;
2824 }
2825 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002826 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002827 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002828
2829 SDOperand Tmp4;
2830 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002831 case Expand: {
2832 // Length is too big, just take the lo-part of the length.
2833 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002834 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002835 break;
2836 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002837 case Legal:
2838 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002839 break;
2840 case Promote:
2841 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002842 break;
2843 }
2844
2845 SDOperand Tmp5;
2846 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2847 case Expand: assert(0 && "Cannot expand this yet!");
2848 case Legal:
2849 Tmp5 = LegalizeOp(Node->getOperand(4));
2850 break;
2851 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002852 Tmp5 = PromoteOp(Node->getOperand(4));
2853 break;
2854 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002855
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002856 SDOperand Tmp6;
2857 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2858 case Expand: assert(0 && "Cannot expand this yet!");
2859 case Legal:
2860 Tmp6 = LegalizeOp(Node->getOperand(5));
2861 break;
2862 case Promote:
2863 Tmp6 = PromoteOp(Node->getOperand(5));
2864 break;
2865 }
2866
Chris Lattner3c0dd462005-01-16 07:29:19 +00002867 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2868 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002869 case TargetLowering::Custom:
2870 isCustom = true;
2871 // FALLTHROUGH
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002872 case TargetLowering::Legal: {
2873 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2874 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002875 if (isCustom) {
2876 Tmp1 = TLI.LowerOperation(Result, DAG);
2877 if (Tmp1.Val) Result = Tmp1;
2878 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002879 break;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002880 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002881 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002882 // Otherwise, the target does not support this operation. Lower the
2883 // operation to an explicit libcall as appropriate.
2884 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002885 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002886 TargetLowering::ArgListTy Args;
2887 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002888
Reid Spencer6dced922005-01-12 14:53:45 +00002889 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002890 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002891 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002892 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002893 // Extend the (previously legalized) ubyte argument to be an int value
2894 // for the call.
2895 if (Tmp3.getValueType() > MVT::i32)
2896 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2897 else
2898 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002899 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002900 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002901 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002902 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002903
2904 FnName = "memset";
2905 } else if (Node->getOpcode() == ISD::MEMCPY ||
2906 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002907 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002908 Entry.Node = Tmp2; Args.push_back(Entry);
2909 Entry.Node = Tmp3; Args.push_back(Entry);
2910 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002911 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2912 } else {
2913 assert(0 && "Unknown op!");
2914 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002915
Chris Lattner85d70c62005-01-11 05:57:22 +00002916 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00002917 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2918 false, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002919 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002920 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002921 break;
2922 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002923 }
2924 break;
2925 }
Chris Lattner5385db52005-05-09 20:23:03 +00002926
Chris Lattner4157c412005-04-02 04:00:59 +00002927 case ISD::SHL_PARTS:
2928 case ISD::SRA_PARTS:
2929 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002930 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002931 bool Changed = false;
2932 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2933 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2934 Changed |= Ops.back() != Node->getOperand(i);
2935 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002936 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002937 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002938
Evan Cheng870e4f82006-01-09 18:31:59 +00002939 switch (TLI.getOperationAction(Node->getOpcode(),
2940 Node->getValueType(0))) {
2941 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002942 case TargetLowering::Legal: break;
2943 case TargetLowering::Custom:
2944 Tmp1 = TLI.LowerOperation(Result, DAG);
2945 if (Tmp1.Val) {
2946 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002947 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002948 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002949 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2950 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002951 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002952 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002953 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002954 return RetVal;
2955 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002956 break;
2957 }
2958
Chris Lattner13fe99c2005-04-02 05:00:07 +00002959 // Since these produce multiple values, make sure to remember that we
2960 // legalized all of them.
2961 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2962 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2963 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002964 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002965
2966 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002967 case ISD::ADD:
2968 case ISD::SUB:
2969 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002970 case ISD::MULHS:
2971 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002972 case ISD::UDIV:
2973 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002974 case ISD::AND:
2975 case ISD::OR:
2976 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002977 case ISD::SHL:
2978 case ISD::SRL:
2979 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002980 case ISD::FADD:
2981 case ISD::FSUB:
2982 case ISD::FMUL:
2983 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002984 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002985 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002986 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2987 case Expand: assert(0 && "Not possible");
2988 case Legal:
2989 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2990 break;
2991 case Promote:
2992 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2993 break;
2994 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002995
2996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002997
Andrew Lenharth72594262005-12-24 23:42:32 +00002998 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002999 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003000 case TargetLowering::Legal: break;
3001 case TargetLowering::Custom:
3002 Tmp1 = TLI.LowerOperation(Result, DAG);
3003 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00003004 break;
Chris Lattner87f08092006-04-02 03:57:31 +00003005 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00003006 MVT::ValueType VT = Op.getValueType();
3007
3008 // See if multiply or divide can be lowered using two-result operations.
3009 SDVTList VTs = DAG.getVTList(VT, VT);
3010 if (Node->getOpcode() == ISD::MUL) {
3011 // We just need the low half of the multiply; try both the signed
3012 // and unsigned forms. If the target supports both SMUL_LOHI and
3013 // UMUL_LOHI, form a preference by checking which forms of plain
3014 // MULH it supports.
3015 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3016 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3017 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3018 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3019 unsigned OpToUse = 0;
3020 if (HasSMUL_LOHI && !HasMULHS) {
3021 OpToUse = ISD::SMUL_LOHI;
3022 } else if (HasUMUL_LOHI && !HasMULHU) {
3023 OpToUse = ISD::UMUL_LOHI;
3024 } else if (HasSMUL_LOHI) {
3025 OpToUse = ISD::SMUL_LOHI;
3026 } else if (HasUMUL_LOHI) {
3027 OpToUse = ISD::UMUL_LOHI;
3028 }
3029 if (OpToUse) {
3030 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3031 break;
3032 }
3033 }
3034 if (Node->getOpcode() == ISD::MULHS &&
3035 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3036 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3037 break;
3038 }
3039 if (Node->getOpcode() == ISD::MULHU &&
3040 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3041 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3042 break;
3043 }
3044 if (Node->getOpcode() == ISD::SDIV &&
3045 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3046 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3047 break;
3048 }
3049 if (Node->getOpcode() == ISD::UDIV &&
3050 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3051 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3052 break;
3053 }
3054
Dan Gohman2a7de412007-10-11 23:57:53 +00003055 // Check to see if we have a libcall for this operator.
3056 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3057 bool isSigned = false;
3058 switch (Node->getOpcode()) {
3059 case ISD::UDIV:
3060 case ISD::SDIV:
3061 if (VT == MVT::i32) {
3062 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00003063 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00003064 isSigned = Node->getOpcode() == ISD::SDIV;
3065 }
3066 break;
3067 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00003068 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3069 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00003070 break;
3071 default: break;
3072 }
3073 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3074 SDOperand Dummy;
3075 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003076 break;
3077 }
3078
Chris Lattner87f08092006-04-02 03:57:31 +00003079 assert(MVT::isVector(Node->getValueType(0)) &&
3080 "Cannot expand this binary operator!");
3081 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00003082 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00003083 break;
3084 }
Evan Cheng119266e2006-04-12 21:20:24 +00003085 case TargetLowering::Promote: {
3086 switch (Node->getOpcode()) {
3087 default: assert(0 && "Do not know how to promote this BinOp!");
3088 case ISD::AND:
3089 case ISD::OR:
3090 case ISD::XOR: {
3091 MVT::ValueType OVT = Node->getValueType(0);
3092 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3093 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3094 // Bit convert each of the values to the new type.
3095 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3096 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3097 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3098 // Bit convert the result back the original type.
3099 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3100 break;
3101 }
3102 }
3103 }
Andrew Lenharth72594262005-12-24 23:42:32 +00003104 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003105 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003106
Dan Gohman12334ac2007-10-05 14:17:22 +00003107 case ISD::SMUL_LOHI:
3108 case ISD::UMUL_LOHI:
3109 case ISD::SDIVREM:
3110 case ISD::UDIVREM:
3111 // These nodes will only be produced by target-specific lowering, so
3112 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00003113 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00003114 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00003115
3116 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3117 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3118 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00003119 break;
3120
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003121 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3122 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3123 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3124 case Expand: assert(0 && "Not possible");
3125 case Legal:
3126 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3127 break;
3128 case Promote:
3129 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3130 break;
3131 }
3132
3133 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3134
3135 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3136 default: assert(0 && "Operation not supported");
3137 case TargetLowering::Custom:
3138 Tmp1 = TLI.LowerOperation(Result, DAG);
3139 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00003140 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003141 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00003142 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00003143 // If this target supports fabs/fneg natively and select is cheap,
3144 // do this efficiently.
3145 if (!TLI.isSelectExpensive() &&
3146 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3147 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00003148 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00003149 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00003150 // Get the sign bit of the RHS.
3151 MVT::ValueType IVT =
3152 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3153 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3154 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3155 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3156 // Get the absolute value of the result.
3157 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3158 // Select between the nabs and abs value based on the sign bit of
3159 // the input.
3160 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3161 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3162 AbsVal),
3163 AbsVal);
3164 Result = LegalizeOp(Result);
3165 break;
3166 }
3167
3168 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00003169 MVT::ValueType NVT =
3170 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3171 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3172 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3173 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003174 break;
3175 }
Evan Cheng003feb02007-01-04 21:56:39 +00003176 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003177 break;
3178
Nate Begeman5965bd12006-02-17 05:43:56 +00003179 case ISD::ADDC:
3180 case ISD::SUBC:
3181 Tmp1 = LegalizeOp(Node->getOperand(0));
3182 Tmp2 = LegalizeOp(Node->getOperand(1));
3183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3184 // Since this produces two values, make sure to remember that we legalized
3185 // both of them.
3186 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3187 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3188 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00003189
Nate Begeman5965bd12006-02-17 05:43:56 +00003190 case ISD::ADDE:
3191 case ISD::SUBE:
3192 Tmp1 = LegalizeOp(Node->getOperand(0));
3193 Tmp2 = LegalizeOp(Node->getOperand(1));
3194 Tmp3 = LegalizeOp(Node->getOperand(2));
3195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3196 // Since this produces two values, make sure to remember that we legalized
3197 // both of them.
3198 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3199 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3200 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00003201
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003202 case ISD::BUILD_PAIR: {
3203 MVT::ValueType PairTy = Node->getValueType(0);
3204 // TODO: handle the case where the Lo and Hi operands are not of legal type
3205 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3206 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3207 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003208 case TargetLowering::Promote:
3209 case TargetLowering::Custom:
3210 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003211 case TargetLowering::Legal:
3212 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3213 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3214 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003215 case TargetLowering::Expand:
3216 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3217 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3218 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3219 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3220 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003221 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003222 break;
3223 }
3224 break;
3225 }
3226
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003227 case ISD::UREM:
3228 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003229 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003230 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3231 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003232
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003233 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003234 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3235 case TargetLowering::Custom:
3236 isCustom = true;
3237 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003238 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003239 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003240 if (isCustom) {
3241 Tmp1 = TLI.LowerOperation(Result, DAG);
3242 if (Tmp1.Val) Result = Tmp1;
3243 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003244 break;
Dan Gohmana1603612007-10-08 18:33:35 +00003245 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00003246 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00003247 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00003248 MVT::ValueType VT = Node->getValueType(0);
3249
3250 // See if remainder can be lowered using two-result operations.
3251 SDVTList VTs = DAG.getVTList(VT, VT);
3252 if (Node->getOpcode() == ISD::SREM &&
3253 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3254 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3255 break;
3256 }
3257 if (Node->getOpcode() == ISD::UREM &&
3258 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3259 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3260 break;
3261 }
3262
3263 if (MVT::isInteger(VT)) {
3264 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003265 TargetLowering::Legal) {
3266 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00003267 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003268 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3269 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman08143e32007-11-05 23:35:22 +00003270 } else if (MVT::isVector(VT)) {
3271 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003272 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00003273 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003274 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00003275 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3276 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003277 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003278 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003279 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003280 } else {
3281 assert(MVT::isFloatingPoint(VT) &&
3282 "remainder op must have integer or floating-point type");
Dan Gohman08143e32007-11-05 23:35:22 +00003283 if (MVT::isVector(VT)) {
3284 Result = LegalizeOp(UnrollVectorOp(Op));
3285 } else {
3286 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003287 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3288 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman08143e32007-11-05 23:35:22 +00003289 SDOperand Dummy;
3290 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3291 false/*sign irrelevant*/, Dummy);
3292 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003293 }
3294 break;
3295 }
Dan Gohmana1603612007-10-08 18:33:35 +00003296 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003297 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003298 case ISD::VAARG: {
3299 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3300 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3301
Chris Lattner364b89a2006-01-28 07:42:08 +00003302 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003303 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3304 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003305 case TargetLowering::Custom:
3306 isCustom = true;
3307 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003308 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003309 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3310 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003311 Tmp1 = Result.getValue(1);
3312
3313 if (isCustom) {
3314 Tmp2 = TLI.LowerOperation(Result, DAG);
3315 if (Tmp2.Val) {
3316 Result = LegalizeOp(Tmp2);
3317 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3318 }
3319 }
Nate Begemane74795c2006-01-25 18:21:52 +00003320 break;
3321 case TargetLowering::Expand: {
Dan Gohman2d489b52008-02-06 22:27:42 +00003322 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3323 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003324 // Increment the pointer, VAList, to the next vaarg
3325 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3326 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3327 TLI.getPointerTy()));
3328 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00003329 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003330 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003331 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003332 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003333 Result = LegalizeOp(Result);
3334 break;
3335 }
3336 }
3337 // Since VAARG produces two values, make sure to remember that we
3338 // legalized both of them.
3339 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003340 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3341 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003342 }
3343
3344 case ISD::VACOPY:
3345 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3347 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3348
3349 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3350 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003351 case TargetLowering::Custom:
3352 isCustom = true;
3353 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003354 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003355 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3356 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003357 if (isCustom) {
3358 Tmp1 = TLI.LowerOperation(Result, DAG);
3359 if (Tmp1.Val) Result = Tmp1;
3360 }
Nate Begemane74795c2006-01-25 18:21:52 +00003361 break;
3362 case TargetLowering::Expand:
3363 // This defaults to loading a pointer from the input and storing it to the
3364 // output, returning the chain.
Dan Gohman2d489b52008-02-06 22:27:42 +00003365 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3366 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3367 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3368 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003369 break;
3370 }
3371 break;
3372
3373 case ISD::VAEND:
3374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3375 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3376
3377 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3378 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003379 case TargetLowering::Custom:
3380 isCustom = true;
3381 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003382 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003383 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003384 if (isCustom) {
3385 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3386 if (Tmp1.Val) Result = Tmp1;
3387 }
Nate Begemane74795c2006-01-25 18:21:52 +00003388 break;
3389 case TargetLowering::Expand:
3390 Result = Tmp1; // Default to a no-op, return the chain
3391 break;
3392 }
3393 break;
3394
3395 case ISD::VASTART:
3396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3397 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3398
Chris Lattnerd02b0542006-01-28 10:58:55 +00003399 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3400
Nate Begemane74795c2006-01-25 18:21:52 +00003401 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3402 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003403 case TargetLowering::Legal: break;
3404 case TargetLowering::Custom:
3405 Tmp1 = TLI.LowerOperation(Result, DAG);
3406 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003407 break;
3408 }
3409 break;
3410
Nate Begeman1b8121b2006-01-11 21:21:00 +00003411 case ISD::ROTL:
3412 case ISD::ROTR:
3413 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3414 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003415 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003416 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3417 default:
3418 assert(0 && "ROTL/ROTR legalize operation not supported");
3419 break;
3420 case TargetLowering::Legal:
3421 break;
3422 case TargetLowering::Custom:
3423 Tmp1 = TLI.LowerOperation(Result, DAG);
3424 if (Tmp1.Val) Result = Tmp1;
3425 break;
3426 case TargetLowering::Promote:
3427 assert(0 && "Do not know how to promote ROTL/ROTR");
3428 break;
3429 case TargetLowering::Expand:
3430 assert(0 && "Do not know how to expand ROTL/ROTR");
3431 break;
3432 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003433 break;
3434
Nate Begeman2fba8a32006-01-14 03:14:10 +00003435 case ISD::BSWAP:
3436 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3437 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003438 case TargetLowering::Custom:
3439 assert(0 && "Cannot custom legalize this yet!");
3440 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003441 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003442 break;
3443 case TargetLowering::Promote: {
3444 MVT::ValueType OVT = Tmp1.getValueType();
3445 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003446 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003447
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003448 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3449 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3450 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3451 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3452 break;
3453 }
3454 case TargetLowering::Expand:
3455 Result = ExpandBSWAP(Tmp1);
3456 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003457 }
3458 break;
3459
Andrew Lenharth5e177822005-05-03 17:19:30 +00003460 case ISD::CTPOP:
3461 case ISD::CTTZ:
3462 case ISD::CTLZ:
3463 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3464 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003465 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003466 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003467 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003468 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003469 TargetLowering::Custom) {
3470 Tmp1 = TLI.LowerOperation(Result, DAG);
3471 if (Tmp1.Val) {
3472 Result = Tmp1;
3473 }
Scott Michel34e2d222007-07-30 21:00:31 +00003474 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003475 break;
3476 case TargetLowering::Promote: {
3477 MVT::ValueType OVT = Tmp1.getValueType();
3478 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003479
3480 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003481 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3482 // Perform the larger operation, then subtract if needed.
3483 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003484 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003485 case ISD::CTPOP:
3486 Result = Tmp1;
3487 break;
3488 case ISD::CTTZ:
3489 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00003490 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003491 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003492 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003493 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003494 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003495 break;
3496 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003497 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003498 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003499 DAG.getConstant(MVT::getSizeInBits(NVT) -
3500 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003501 break;
3502 }
3503 break;
3504 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003505 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003506 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003507 break;
3508 }
3509 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003510
Chris Lattner13fe99c2005-04-02 05:00:07 +00003511 // Unary operators
3512 case ISD::FABS:
3513 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003514 case ISD::FSQRT:
3515 case ISD::FSIN:
3516 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003517 Tmp1 = LegalizeOp(Node->getOperand(0));
3518 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003519 case TargetLowering::Promote:
3520 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003521 isCustom = true;
3522 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003523 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003524 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003525 if (isCustom) {
3526 Tmp1 = TLI.LowerOperation(Result, DAG);
3527 if (Tmp1.Val) Result = Tmp1;
3528 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003529 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003530 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003531 switch (Node->getOpcode()) {
3532 default: assert(0 && "Unreachable!");
3533 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003534 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3535 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003536 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003537 break;
Chris Lattner80026402005-04-30 04:43:14 +00003538 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003539 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3540 MVT::ValueType VT = Node->getValueType(0);
3541 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003542 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003543 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3544 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003545 break;
3546 }
3547 case ISD::FSQRT:
3548 case ISD::FSIN:
3549 case ISD::FCOS: {
3550 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003551
3552 // Expand unsupported unary vector operators by unrolling them.
3553 if (MVT::isVector(VT)) {
3554 Result = LegalizeOp(UnrollVectorOp(Op));
3555 break;
3556 }
3557
Evan Cheng31cbddf2007-01-12 02:11:51 +00003558 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003559 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003560 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003561 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3562 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003563 break;
3564 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003565 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3566 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003567 break;
3568 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003569 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3570 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003571 break;
Chris Lattner80026402005-04-30 04:43:14 +00003572 default: assert(0 && "Unreachable!");
3573 }
Nate Begeman77558da2005-08-04 21:43:28 +00003574 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003575 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3576 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003577 break;
3578 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003579 }
3580 break;
3581 }
3582 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003583 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003584 MVT::ValueType VT = Node->getValueType(0);
3585
3586 // Expand unsupported unary vector operators by unrolling them.
3587 if (MVT::isVector(VT)) {
3588 Result = LegalizeOp(UnrollVectorOp(Op));
3589 break;
3590 }
3591
3592 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003593 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3594 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003595 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003596 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3597 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003598 break;
3599 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003600 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003601 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003602 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3603 Node->getValueType(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003604 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3605 // The input has to be a vector type, we have to either scalarize it, pack
3606 // it, or convert it based on whether the input vector type is legal.
3607 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003608 int InIx = Node->getOperand(0).ResNo;
3609 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3610 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmana8665142007-06-25 16:23:39 +00003611
3612 // Figure out if there is a simple type corresponding to this Vector
3613 // type. If so, convert to the vector type.
3614 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3615 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003616 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003617 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3618 LegalizeOp(Node->getOperand(0)));
3619 break;
3620 } else if (NumElems == 1) {
3621 // Turn this into a bit convert of the scalar input.
3622 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3623 ScalarizeVectorOp(Node->getOperand(0)));
3624 break;
3625 } else {
3626 // FIXME: UNIMP! Store then reload
3627 assert(0 && "Cast from unsupported vector type not implemented yet!");
3628 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003629 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003630 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3631 Node->getOperand(0).getValueType())) {
3632 default: assert(0 && "Unknown operation action!");
3633 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003634 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3635 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003636 break;
3637 case TargetLowering::Legal:
3638 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003639 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003640 break;
3641 }
3642 }
3643 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003644
Chris Lattner13fe99c2005-04-02 05:00:07 +00003645 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003646 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003647 case ISD::UINT_TO_FP: {
3648 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003649 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3650 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003651 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003652 Node->getOperand(0).getValueType())) {
3653 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003654 case TargetLowering::Custom:
3655 isCustom = true;
3656 // FALLTHROUGH
3657 case TargetLowering::Legal:
3658 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003659 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003660 if (isCustom) {
3661 Tmp1 = TLI.LowerOperation(Result, DAG);
3662 if (Tmp1.Val) Result = Tmp1;
3663 }
3664 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003665 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003666 Result = ExpandLegalINT_TO_FP(isSigned,
3667 LegalizeOp(Node->getOperand(0)),
3668 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003669 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003670 case TargetLowering::Promote:
3671 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3672 Node->getValueType(0),
3673 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003674 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003675 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003676 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003677 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003678 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3679 Node->getValueType(0), Node->getOperand(0));
3680 break;
3681 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003682 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003683 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003684 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3685 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003686 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003687 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3688 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003689 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003690 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3691 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003692 break;
3693 }
3694 break;
3695 }
3696 case ISD::TRUNCATE:
3697 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3698 case Legal:
3699 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003700 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003701 break;
3702 case Expand:
3703 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3704
3705 // Since the result is legal, we should just be able to truncate the low
3706 // part of the source.
3707 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3708 break;
3709 case Promote:
3710 Result = PromoteOp(Node->getOperand(0));
3711 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3712 break;
3713 }
3714 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003715
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003716 case ISD::FP_TO_SINT:
3717 case ISD::FP_TO_UINT:
3718 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3719 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003720 Tmp1 = LegalizeOp(Node->getOperand(0));
3721
Chris Lattner44fe26f2005-07-29 00:11:56 +00003722 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3723 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003724 case TargetLowering::Custom:
3725 isCustom = true;
3726 // FALLTHROUGH
3727 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003728 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003729 if (isCustom) {
3730 Tmp1 = TLI.LowerOperation(Result, DAG);
3731 if (Tmp1.Val) Result = Tmp1;
3732 }
3733 break;
3734 case TargetLowering::Promote:
3735 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3736 Node->getOpcode() == ISD::FP_TO_SINT);
3737 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003738 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003739 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3740 SDOperand True, False;
3741 MVT::ValueType VT = Node->getOperand(0).getValueType();
3742 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenb59d25f2007-09-19 17:53:26 +00003743 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003744 const uint64_t zero[] = {0, 0};
3745 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3746 uint64_t x = 1ULL << ShiftAmt;
Neil Booth5f009732007-10-07 11:45:55 +00003747 (void)apf.convertFromZeroExtendedInteger
3748 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003749 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begeman36853ee2005-08-14 01:20:53 +00003750 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3751 Node->getOperand(0), Tmp2, ISD::SETLT);
3752 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3753 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003754 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003755 Tmp2));
3756 False = DAG.getNode(ISD::XOR, NVT, False,
3757 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003758 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3759 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003760 } else {
3761 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3762 }
3763 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003764 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003765 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003766 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003767 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003768 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003769 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003770 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner72733e52008-01-17 07:00:52 +00003771 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3772 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3773 Node->getOperand(0), DAG.getValueType(MVT::f64));
3774 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3775 DAG.getIntPtrConstant(1));
3776 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3777 } else {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003778 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3779 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3780 Tmp2 = DAG.getConstantFP(apf, OVT);
3781 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3782 // FIXME: generated code sucks.
3783 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3784 DAG.getNode(ISD::ADD, MVT::i32,
3785 DAG.getNode(ISD::FP_TO_SINT, VT,
3786 DAG.getNode(ISD::FSUB, OVT,
3787 Node->getOperand(0), Tmp2)),
3788 DAG.getConstant(0x80000000, MVT::i32)),
3789 DAG.getNode(ISD::FP_TO_SINT, VT,
3790 Node->getOperand(0)),
3791 DAG.getCondCode(ISD::SETGE));
3792 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003793 break;
3794 }
Dale Johannesen6472eb62007-10-11 23:32:15 +00003795 // Convert f32 / f64 to i32 / i64.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003796 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003797 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003798 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003799 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003800 LC = (VT == MVT::i32)
3801 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003802 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003803 LC = (VT == MVT::i32)
3804 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003805 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003806 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003807 LC = RTLIB::FPTOSINT_F80_I64;
3808 }
3809 else if (OVT == MVT::ppcf128) {
3810 assert(VT == MVT::i64);
3811 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003812 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003813 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003814 }
3815 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003816 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003817 LC = (VT == MVT::i32)
3818 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003819 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003820 LC = (VT == MVT::i32)
3821 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003822 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003823 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003824 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3825 }
3826 else if (OVT == MVT::ppcf128) {
3827 assert(VT == MVT::i64);
3828 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003829 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003830 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003831 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003832 default: assert(0 && "Unreachable!");
3833 }
3834 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003835 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3836 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003837 break;
3838 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003839 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003840 Tmp1 = PromoteOp(Node->getOperand(0));
3841 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3842 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003843 break;
3844 }
3845 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003846
Chris Lattner91d86242008-01-16 06:57:07 +00003847 case ISD::FP_EXTEND: {
Chris Lattner72733e52008-01-17 07:00:52 +00003848 MVT::ValueType DstVT = Op.getValueType();
3849 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3850 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3851 // The only other way we can lower this is to turn it into a STORE,
3852 // LOAD pair, targetting a temporary location (a stack slot).
3853 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3854 break;
Chris Lattner91d86242008-01-16 06:57:07 +00003855 }
3856 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3857 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3858 case Legal:
3859 Tmp1 = LegalizeOp(Node->getOperand(0));
3860 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3861 break;
3862 case Promote:
3863 Tmp1 = PromoteOp(Node->getOperand(0));
3864 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3865 break;
3866 }
3867 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003868 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003869 case ISD::FP_ROUND: {
Chris Lattner72733e52008-01-17 07:00:52 +00003870 MVT::ValueType DstVT = Op.getValueType();
3871 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3872 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3873 if (SrcVT == MVT::ppcf128) {
Dale Johannesen949e5a22008-01-20 01:18:38 +00003874 SDOperand Lo;
3875 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner72733e52008-01-17 07:00:52 +00003876 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen949e5a22008-01-20 01:18:38 +00003877 if (DstVT!=MVT::f64)
3878 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner72733e52008-01-17 07:00:52 +00003879 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003880 }
Chris Lattner72733e52008-01-17 07:00:52 +00003881 // The only other way we can lower this is to turn it into a STORE,
3882 // LOAD pair, targetting a temporary location (a stack slot).
3883 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3884 break;
Dale Johannesena2b3c172007-07-03 00:53:03 +00003885 }
Chris Lattner91d86242008-01-16 06:57:07 +00003886 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3887 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3888 case Legal:
3889 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003890 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003891 break;
3892 case Promote:
3893 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003894 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3895 Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003896 break;
3897 }
3898 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003899 }
Chris Lattner7753f172005-09-02 00:18:10 +00003900 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003901 case ISD::ZERO_EXTEND:
3902 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003903 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003904 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003905 case Legal:
3906 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michela3cefea2008-02-15 23:05:48 +00003907 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3908 TargetLowering::Custom) {
3909 Tmp2 = TLI.LowerOperation(Result, DAG);
3910 if (Tmp2.Val) {
3911 Tmp1 = Tmp2;
3912 }
3913 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003914 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003915 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003916 case Promote:
3917 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003918 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003919 Tmp1 = PromoteOp(Node->getOperand(0));
3920 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003921 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003922 case ISD::ZERO_EXTEND:
3923 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003924 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003925 Result = DAG.getZeroExtendInReg(Result,
3926 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003927 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003928 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003929 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003930 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003931 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003932 Result,
3933 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003934 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003935 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003936 }
3937 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003938 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003939 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003940 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003941 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003942
3943 // If this operation is not supported, convert it to a shl/shr or load/store
3944 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003945 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3946 default: assert(0 && "This action not supported for this op yet!");
3947 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003949 break;
3950 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003951 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003952 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003953 // NOTE: we could fall back on load/store here too for targets without
3954 // SAR. However, it is doubtful that any exist.
3955 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3956 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003957 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003958 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3959 Node->getOperand(0), ShiftCst);
3960 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3961 Result, ShiftCst);
3962 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003963 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003964 // EXTLOAD pair, targetting a temporary location (a stack slot).
3965
3966 // NOTE: there is a choice here between constantly creating new stack
3967 // slots and always reusing the same one. We currently always create
3968 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003969 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3970 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00003971 } else {
3972 assert(0 && "Unknown op");
3973 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003974 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003975 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003976 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003977 }
Duncan Sands644f9172007-07-27 12:58:54 +00003978 case ISD::TRAMPOLINE: {
3979 SDOperand Ops[6];
3980 for (unsigned i = 0; i != 6; ++i)
3981 Ops[i] = LegalizeOp(Node->getOperand(i));
3982 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3983 // The only option for this node is to custom lower it.
3984 Result = TLI.LowerOperation(Result, DAG);
3985 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003986
3987 // Since trampoline produces two values, make sure to remember that we
3988 // legalized both of them.
3989 Tmp1 = LegalizeOp(Result.getValue(1));
3990 Result = LegalizeOp(Result);
3991 AddLegalizedOperand(SDOperand(Node, 0), Result);
3992 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3993 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003994 }
Dan Gohman9ba4d762008-01-31 00:41:03 +00003995 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00003996 MVT::ValueType VT = Node->getValueType(0);
3997 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3998 default: assert(0 && "This action not supported for this op yet!");
3999 case TargetLowering::Custom:
4000 Result = TLI.LowerOperation(Op, DAG);
4001 if (Result.Val) break;
4002 // Fall Thru
4003 case TargetLowering::Legal:
4004 // If this operation is not supported, lower it to constant 1
4005 Result = DAG.getConstant(1, VT);
4006 break;
4007 }
4008 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004009 case ISD::TRAP: {
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004010 MVT::ValueType VT = Node->getValueType(0);
4011 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4012 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00004013 case TargetLowering::Legal:
4014 Tmp1 = LegalizeOp(Node->getOperand(0));
4015 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4016 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004017 case TargetLowering::Custom:
4018 Result = TLI.LowerOperation(Op, DAG);
4019 if (Result.Val) break;
4020 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00004021 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004022 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00004023 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004024 TargetLowering::ArgListTy Args;
4025 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00004026 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4027 false, false, false, CallingConv::C, false,
Chris Lattnerec224882008-01-15 22:09:33 +00004028 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4029 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004030 Result = CallResult.second;
4031 break;
4032 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004033 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004034 }
Chris Lattner99222f72005-01-15 07:15:18 +00004035 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004036
Chris Lattner101ea662006-04-08 04:13:17 +00004037 assert(Result.getValueType() == Op.getValueType() &&
4038 "Bad legalization!");
4039
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004040 // Make sure that the generated code is itself legal.
4041 if (Result != Op)
4042 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004043
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004044 // Note that LegalizeOp may be reentered even from single-use nodes, which
4045 // means that we always must cache transformed nodes.
4046 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004047 return Result;
4048}
4049
Chris Lattner4d978642005-01-15 22:16:26 +00004050/// PromoteOp - Given an operation that produces a value in an invalid type,
4051/// promote it to compute the value into a larger type. The produced value will
4052/// have the correct bits for the low portion of the register, but no guarantee
4053/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004054SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4055 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004056 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004057 assert(getTypeAction(VT) == Promote &&
4058 "Caller should expand or legalize operands that are not promotable!");
4059 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4060 "Cannot promote to smaller type!");
4061
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004062 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004063 SDOperand Result;
4064 SDNode *Node = Op.Val;
4065
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004066 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00004067 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004068
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004069 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00004070 case ISD::CopyFromReg:
4071 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004072 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004073#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004074 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004075#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004076 assert(0 && "Do not know how to promote this operator!");
4077 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004078 case ISD::UNDEF:
4079 Result = DAG.getNode(ISD::UNDEF, NVT);
4080 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004081 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00004082 if (VT != MVT::i1)
4083 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4084 else
4085 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004086 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4087 break;
4088 case ISD::ConstantFP:
4089 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4090 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4091 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00004092
Chris Lattner2cb338d2005-01-18 02:59:52 +00004093 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004094 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00004095 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4096 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00004097 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00004098
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004099 case ISD::TRUNCATE:
4100 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4101 case Legal:
4102 Result = LegalizeOp(Node->getOperand(0));
4103 assert(Result.getValueType() >= NVT &&
4104 "This truncation doesn't make sense!");
4105 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4106 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4107 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00004108 case Promote:
4109 // The truncation is not required, because we don't guarantee anything
4110 // about high bits anyway.
4111 Result = PromoteOp(Node->getOperand(0));
4112 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004113 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00004114 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4115 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00004116 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004117 }
4118 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004119 case ISD::SIGN_EXTEND:
4120 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00004121 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00004122 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4123 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4124 case Legal:
4125 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004126 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004127 break;
4128 case Promote:
4129 // Promote the reg if it's smaller.
4130 Result = PromoteOp(Node->getOperand(0));
4131 // The high bits are not guaranteed to be anything. Insert an extend.
4132 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00004133 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004134 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00004135 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00004136 Result = DAG.getZeroExtendInReg(Result,
4137 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00004138 break;
4139 }
4140 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004141 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00004142 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4143 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00004144 Result = PromoteOp(Result);
4145 break;
4146
Chris Lattner4d978642005-01-15 22:16:26 +00004147 case ISD::FP_EXTEND:
4148 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4149 case ISD::FP_ROUND:
4150 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4151 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4152 case Promote: assert(0 && "Unreachable with 2 FP types!");
4153 case Legal:
Chris Lattner72733e52008-01-17 07:00:52 +00004154 if (Node->getConstantOperandVal(1) == 0) {
4155 // Input is legal? Do an FP_ROUND_INREG.
4156 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4157 DAG.getValueType(VT));
4158 } else {
4159 // Just remove the truncate, it isn't affecting the value.
4160 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4161 Node->getOperand(1));
4162 }
Chris Lattner4d978642005-01-15 22:16:26 +00004163 break;
4164 }
4165 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004166 case ISD::SINT_TO_FP:
4167 case ISD::UINT_TO_FP:
4168 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4169 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00004170 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004171 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004172 break;
4173
4174 case Promote:
4175 Result = PromoteOp(Node->getOperand(0));
4176 if (Node->getOpcode() == ISD::SINT_TO_FP)
4177 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004178 Result,
4179 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00004180 else
Chris Lattner0e852af2005-04-13 02:38:47 +00004181 Result = DAG.getZeroExtendInReg(Result,
4182 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00004183 // No extra round required here.
4184 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00004185 break;
4186 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00004187 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4188 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00004189 // Round if we cannot tolerate excess precision.
4190 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004191 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4192 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00004193 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004194 }
Chris Lattner4d978642005-01-15 22:16:26 +00004195 break;
4196
Chris Lattner268d4572005-12-09 17:32:47 +00004197 case ISD::SIGN_EXTEND_INREG:
4198 Result = PromoteOp(Node->getOperand(0));
4199 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4200 Node->getOperand(1));
4201 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004202 case ISD::FP_TO_SINT:
4203 case ISD::FP_TO_UINT:
4204 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4205 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00004206 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004207 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00004208 break;
4209 case Promote:
4210 // The input result is prerounded, so we don't have to do anything
4211 // special.
4212 Tmp1 = PromoteOp(Node->getOperand(0));
4213 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004214 }
Nate Begeman36853ee2005-08-14 01:20:53 +00004215 // If we're promoting a UINT to a larger size, check to see if the new node
4216 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4217 // we can use that instead. This allows us to generate better code for
4218 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4219 // legal, such as PowerPC.
4220 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004221 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00004222 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4223 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00004224 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4225 } else {
4226 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4227 }
Chris Lattner4d978642005-01-15 22:16:26 +00004228 break;
4229
Chris Lattner13fe99c2005-04-02 05:00:07 +00004230 case ISD::FABS:
4231 case ISD::FNEG:
4232 Tmp1 = PromoteOp(Node->getOperand(0));
4233 assert(Tmp1.getValueType() == NVT);
4234 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4235 // NOTE: we do not have to do any extra rounding here for
4236 // NoExcessFPPrecision, because we know the input will have the appropriate
4237 // precision, and these operations don't modify precision at all.
4238 break;
4239
Chris Lattner9d6fa982005-04-28 21:44:33 +00004240 case ISD::FSQRT:
4241 case ISD::FSIN:
4242 case ISD::FCOS:
4243 Tmp1 = PromoteOp(Node->getOperand(0));
4244 assert(Tmp1.getValueType() == NVT);
4245 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004246 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004247 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4248 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00004249 break;
4250
Chris Lattnerca401aa2007-03-03 23:43:21 +00004251 case ISD::FPOWI: {
4252 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4253 // directly as well, which may be better.
4254 Tmp1 = PromoteOp(Node->getOperand(0));
4255 assert(Tmp1.getValueType() == NVT);
4256 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4257 if (NoExcessFPPrecision)
4258 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4259 DAG.getValueType(VT));
4260 break;
4261 }
4262
Andrew Lenharth95528942008-02-21 06:45:13 +00004263 case ISD::ATOMIC_LCS: {
4264 Tmp2 = PromoteOp(Node->getOperand(2));
4265 Tmp3 = PromoteOp(Node->getOperand(3));
4266 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4267 Node->getOperand(1), Tmp2, Tmp3,
4268 cast<AtomicSDNode>(Node)->getVT());
4269 // Remember that we legalized the chain.
4270 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4271 break;
4272 }
4273 case ISD::ATOMIC_LAS:
4274 case ISD::ATOMIC_SWAP: {
4275 Tmp2 = PromoteOp(Node->getOperand(2));
4276 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4277 Node->getOperand(1), Tmp2,
4278 cast<AtomicSDNode>(Node)->getVT());
4279 // Remember that we legalized the chain.
4280 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4281 break;
4282 }
4283
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004284 case ISD::AND:
4285 case ISD::OR:
4286 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004287 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00004288 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004289 case ISD::MUL:
4290 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00004291 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004292 // that too is okay if they are integer operations.
4293 Tmp1 = PromoteOp(Node->getOperand(0));
4294 Tmp2 = PromoteOp(Node->getOperand(1));
4295 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4296 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00004297 break;
4298 case ISD::FADD:
4299 case ISD::FSUB:
4300 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004301 Tmp1 = PromoteOp(Node->getOperand(0));
4302 Tmp2 = PromoteOp(Node->getOperand(1));
4303 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4304 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4305
4306 // Floating point operations will give excess precision that we may not be
4307 // able to tolerate. If we DO allow excess precision, just leave it,
4308 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004309 // FIXME: Why would we need to round FP ops more than integer ones?
4310 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004311 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004312 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4313 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004314 break;
4315
Chris Lattner4d978642005-01-15 22:16:26 +00004316 case ISD::SDIV:
4317 case ISD::SREM:
4318 // These operators require that their input be sign extended.
4319 Tmp1 = PromoteOp(Node->getOperand(0));
4320 Tmp2 = PromoteOp(Node->getOperand(1));
4321 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004322 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4323 DAG.getValueType(VT));
4324 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4325 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004326 }
4327 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4328
4329 // Perform FP_ROUND: this is probably overly pessimistic.
4330 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004331 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4332 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004333 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004334 case ISD::FDIV:
4335 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004336 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004337 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004338 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004339 case Expand: assert(0 && "not implemented");
4340 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4341 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004342 }
4343 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004344 case Expand: assert(0 && "not implemented");
4345 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4346 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004347 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004348 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4349
4350 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004351 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004352 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4353 DAG.getValueType(VT));
4354 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004355
4356 case ISD::UDIV:
4357 case ISD::UREM:
4358 // These operators require that their input be zero extended.
4359 Tmp1 = PromoteOp(Node->getOperand(0));
4360 Tmp2 = PromoteOp(Node->getOperand(1));
4361 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004362 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4363 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004364 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4365 break;
4366
4367 case ISD::SHL:
4368 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004369 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004370 break;
4371 case ISD::SRA:
4372 // The input value must be properly sign extended.
4373 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004374 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4375 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004376 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004377 break;
4378 case ISD::SRL:
4379 // The input value must be properly zero extended.
4380 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004381 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004382 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004383 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004384
4385 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004386 Tmp1 = Node->getOperand(0); // Get the chain.
4387 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004388 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4389 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4390 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4391 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +00004392 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4393 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004394 // Increment the pointer, VAList, to the next vaarg
4395 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4396 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4397 TLI.getPointerTy()));
4398 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00004399 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004400 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004401 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004402 }
4403 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004404 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004405 break;
4406
Evan Chenge71fe34d2006-10-09 20:57:25 +00004407 case ISD::LOAD: {
4408 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004409 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4410 ? ISD::EXTLOAD : LD->getExtensionType();
4411 Result = DAG.getExtLoad(ExtType, NVT,
4412 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004413 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00004414 LD->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00004415 LD->isVolatile(),
4416 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004417 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004418 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004419 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004420 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004421 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004422 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4423 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004424 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004425 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004426 case ISD::SELECT_CC:
4427 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4428 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4429 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004430 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004431 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004432 case ISD::BSWAP:
4433 Tmp1 = Node->getOperand(0);
4434 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4435 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4436 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004437 DAG.getConstant(MVT::getSizeInBits(NVT) -
4438 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004439 TLI.getShiftAmountTy()));
4440 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004441 case ISD::CTPOP:
4442 case ISD::CTTZ:
4443 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004444 // Zero extend the argument
4445 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004446 // Perform the larger operation, then subtract if needed.
4447 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004448 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004449 case ISD::CTPOP:
4450 Result = Tmp1;
4451 break;
4452 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004453 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00004454 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004455 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4456 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004457 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004458 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004459 break;
4460 case ISD::CTLZ:
4461 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004462 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004463 DAG.getConstant(MVT::getSizeInBits(NVT) -
4464 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004465 break;
4466 }
4467 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004468 case ISD::EXTRACT_SUBVECTOR:
4469 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004470 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004471 case ISD::EXTRACT_VECTOR_ELT:
4472 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4473 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004474 }
4475
4476 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004477
4478 // Make sure the result is itself legal.
4479 Result = LegalizeOp(Result);
4480
4481 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004482 AddPromotedOperand(Op, Result);
4483 return Result;
4484}
Chris Lattnerdc750592005-01-07 07:47:09 +00004485
Dan Gohmana8665142007-06-25 16:23:39 +00004486/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4487/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4488/// based on the vector type. The return type of this matches the element type
4489/// of the vector, which may not be legal for the target.
4490SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004491 // We know that operand #0 is the Vec vector. If the index is a constant
4492 // or if the invec is a supported hardware type, we can use it. Otherwise,
4493 // lower to a store then an indexed load.
4494 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004495 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004496
Dan Gohman60028182007-09-24 15:54:53 +00004497 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004498 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004499
Dan Gohmana8665142007-06-25 16:23:39 +00004500 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4501 default: assert(0 && "This action is not supported yet!");
4502 case TargetLowering::Custom: {
4503 Vec = LegalizeOp(Vec);
4504 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4505 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4506 if (Tmp3.Val)
4507 return Tmp3;
4508 break;
4509 }
4510 case TargetLowering::Legal:
4511 if (isTypeLegal(TVT)) {
4512 Vec = LegalizeOp(Vec);
4513 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004514 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004515 }
4516 break;
4517 case TargetLowering::Expand:
4518 break;
4519 }
4520
4521 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004522 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004523 Op = ScalarizeVectorOp(Vec);
4524 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begemanef337672008-01-29 02:24:00 +00004525 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmana8665142007-06-25 16:23:39 +00004526 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004527 SDOperand Lo, Hi;
4528 SplitVectorOp(Vec, Lo, Hi);
Nate Begemanef337672008-01-29 02:24:00 +00004529 if (CIdx->getValue() < NumLoElts) {
Chris Lattner6f423252006-03-31 17:55:51 +00004530 Vec = Lo;
4531 } else {
4532 Vec = Hi;
Nate Begemanef337672008-01-29 02:24:00 +00004533 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmana8665142007-06-25 16:23:39 +00004534 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004535 }
Dan Gohmana8665142007-06-25 16:23:39 +00004536
Chris Lattner6f423252006-03-31 17:55:51 +00004537 // It's now an extract from the appropriate high or low part. Recurse.
4538 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004539 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004540 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004541 // Store the value to a temporary stack slot, then LOAD the scalar
4542 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004543 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004544 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4545
4546 // Add the offset to the index.
4547 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4548 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4549 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004550
4551 if (MVT::getSizeInBits(Idx.getValueType()) >
4552 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004553 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004554 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004555 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004556
Dan Gohmana8665142007-06-25 16:23:39 +00004557 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4558
4559 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004560 }
Dan Gohmana8665142007-06-25 16:23:39 +00004561 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004562}
4563
Dan Gohmana8665142007-06-25 16:23:39 +00004564/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004565/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004566SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004567 // We know that operand #0 is the Vec vector. For now we assume the index
4568 // is a constant and that the extracted result is a supported hardware type.
4569 SDOperand Vec = Op.getOperand(0);
4570 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4571
Dan Gohmana8665142007-06-25 16:23:39 +00004572 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004573
4574 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4575 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004576 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004577 }
4578
4579 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4580 SDOperand Lo, Hi;
4581 SplitVectorOp(Vec, Lo, Hi);
4582 if (CIdx->getValue() < NumElems/2) {
4583 Vec = Lo;
4584 } else {
4585 Vec = Hi;
4586 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4587 }
4588
4589 // It's now an extract from the appropriate high or low part. Recurse.
4590 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004591 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004592}
4593
Nate Begeman7e7f4392006-02-01 07:19:44 +00004594/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4595/// with condition CC on the current target. This usually involves legalizing
4596/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4597/// there may be no choice but to create a new SetCC node to represent the
4598/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4599/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4600void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4601 SDOperand &RHS,
4602 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004603 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004604
4605 switch (getTypeAction(LHS.getValueType())) {
4606 case Legal:
4607 Tmp1 = LegalizeOp(LHS); // LHS
4608 Tmp2 = LegalizeOp(RHS); // RHS
4609 break;
4610 case Promote:
4611 Tmp1 = PromoteOp(LHS); // LHS
4612 Tmp2 = PromoteOp(RHS); // RHS
4613
4614 // If this is an FP compare, the operands have already been extended.
4615 if (MVT::isInteger(LHS.getValueType())) {
4616 MVT::ValueType VT = LHS.getValueType();
4617 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4618
4619 // Otherwise, we have to insert explicit sign or zero extends. Note
4620 // that we could insert sign extends for ALL conditions, but zero extend
4621 // is cheaper on many machines (an AND instead of two shifts), so prefer
4622 // it.
4623 switch (cast<CondCodeSDNode>(CC)->get()) {
4624 default: assert(0 && "Unknown integer comparison!");
4625 case ISD::SETEQ:
4626 case ISD::SETNE:
4627 case ISD::SETUGE:
4628 case ISD::SETUGT:
4629 case ISD::SETULE:
4630 case ISD::SETULT:
4631 // ALL of these operations will work if we either sign or zero extend
4632 // the operands (including the unsigned comparisons!). Zero extend is
4633 // usually a simpler/cheaper operation, so prefer it.
4634 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4635 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4636 break;
4637 case ISD::SETGE:
4638 case ISD::SETGT:
4639 case ISD::SETLT:
4640 case ISD::SETLE:
4641 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4642 DAG.getValueType(VT));
4643 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4644 DAG.getValueType(VT));
4645 break;
4646 }
4647 }
4648 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004649 case Expand: {
4650 MVT::ValueType VT = LHS.getValueType();
4651 if (VT == MVT::f32 || VT == MVT::f64) {
4652 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004653 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004654 switch (cast<CondCodeSDNode>(CC)->get()) {
4655 case ISD::SETEQ:
4656 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004657 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004658 break;
4659 case ISD::SETNE:
4660 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004661 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004662 break;
4663 case ISD::SETGE:
4664 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004665 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004666 break;
4667 case ISD::SETLT:
4668 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004669 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004670 break;
4671 case ISD::SETLE:
4672 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004673 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004674 break;
4675 case ISD::SETGT:
4676 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004677 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004678 break;
4679 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004680 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4681 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004682 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004683 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004684 break;
4685 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004686 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004687 switch (cast<CondCodeSDNode>(CC)->get()) {
4688 case ISD::SETONE:
4689 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004690 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004691 // Fallthrough
4692 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004693 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004694 break;
4695 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004696 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004697 break;
4698 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004699 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004700 break;
4701 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004702 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004703 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004704 case ISD::SETUEQ:
4705 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004706 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004707 default: assert(0 && "Unsupported FP setcc!");
4708 }
4709 }
4710
4711 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004712 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004713 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004714 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004715 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004716 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004717 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004718 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004719 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004720 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004721 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004722 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004723 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004724 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4725 Tmp2 = SDOperand();
4726 }
4727 LHS = Tmp1;
4728 RHS = Tmp2;
4729 return;
4730 }
4731
Nate Begeman7e7f4392006-02-01 07:19:44 +00004732 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4733 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004734 ExpandOp(RHS, RHSLo, RHSHi);
4735 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4736
4737 if (VT==MVT::ppcf128) {
4738 // FIXME: This generated code sucks. We want to generate
4739 // FCMP crN, hi1, hi2
4740 // BNE crN, L:
4741 // FCMP crN, lo1, lo2
4742 // The following can be improved, but not that much.
4743 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4744 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4745 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4746 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4747 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4748 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4749 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4750 Tmp2 = SDOperand();
4751 break;
4752 }
4753
4754 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004755 case ISD::SETEQ:
4756 case ISD::SETNE:
4757 if (RHSLo == RHSHi)
4758 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4759 if (RHSCST->isAllOnesValue()) {
4760 // Comparison to -1.
4761 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4762 Tmp2 = RHSLo;
4763 break;
4764 }
4765
4766 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4767 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4768 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4769 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4770 break;
4771 default:
4772 // If this is a comparison of the sign bit, just look at the top part.
4773 // X > -1, x < 0
4774 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4775 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4776 CST->getValue() == 0) || // X < 0
4777 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4778 CST->isAllOnesValue())) { // X > -1
4779 Tmp1 = LHSHi;
4780 Tmp2 = RHSHi;
4781 break;
4782 }
4783
4784 // FIXME: This generated code sucks.
4785 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004786 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004787 default: assert(0 && "Unknown integer setcc!");
4788 case ISD::SETLT:
4789 case ISD::SETULT: LowCC = ISD::SETULT; break;
4790 case ISD::SETGT:
4791 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4792 case ISD::SETLE:
4793 case ISD::SETULE: LowCC = ISD::SETULE; break;
4794 case ISD::SETGE:
4795 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4796 }
4797
4798 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4799 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4800 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4801
4802 // NOTE: on targets without efficient SELECT of bools, we can always use
4803 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004804 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4805 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4806 false, DagCombineInfo);
4807 if (!Tmp1.Val)
4808 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4809 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4810 CCCode, false, DagCombineInfo);
4811 if (!Tmp2.Val)
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004812 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004813
4814 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4815 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4816 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4817 (Tmp2C && Tmp2C->getValue() == 0 &&
4818 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4819 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4820 (Tmp2C && Tmp2C->getValue() == 1 &&
4821 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4822 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4823 // low part is known false, returns high part.
4824 // For LE / GE, if high part is known false, ignore the low part.
4825 // For LT / GT, if high part is known true, ignore the low part.
4826 Tmp1 = Tmp2;
4827 Tmp2 = SDOperand();
4828 } else {
4829 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4830 ISD::SETEQ, false, DagCombineInfo);
4831 if (!Result.Val)
4832 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4833 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4834 Result, Tmp1, Tmp2));
4835 Tmp1 = Result;
4836 Tmp2 = SDOperand();
4837 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004838 }
4839 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004840 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004841 LHS = Tmp1;
4842 RHS = Tmp2;
4843}
4844
Chris Lattner87bc3e72008-01-16 07:45:30 +00004845/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4846/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4847/// a load from the stack slot to DestVT, extending it if needed.
4848/// The resultant code need not be legal.
4849SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4850 MVT::ValueType SlotVT,
4851 MVT::ValueType DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00004852 // Create the stack frame object.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004853 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4854
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004855 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004856 int SPFI = StackPtrFI->getIndex();
4857
Chris Lattner87bc3e72008-01-16 07:45:30 +00004858 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4859 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4860 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004861
Chris Lattner87bc3e72008-01-16 07:45:30 +00004862 // Emit a store to the stack slot. Use a truncstore if the input value is
4863 // later than DestVT.
4864 SDOperand Store;
4865 if (SrcSize > SlotSize)
Dan Gohman2d489b52008-02-06 22:27:42 +00004866 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004867 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004868 SPFI, SlotVT);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004869 else {
4870 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman2d489b52008-02-06 22:27:42 +00004871 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004872 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004873 SPFI, SlotVT);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004874 }
4875
Chris Lattner36e663d2005-12-23 00:16:34 +00004876 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004877 if (SlotSize == DestSize)
4878 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4879
4880 assert(SlotSize < DestSize && "Unknown extension!");
4881 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004882}
4883
Chris Lattner6be79822006-04-04 17:23:26 +00004884SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4885 // Create a vector sized/aligned stack slot, store the value to element #0,
4886 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004887 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00004888
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004889 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004890 int SPFI = StackPtrFI->getIndex();
4891
Evan Chengdf9ac472006-10-05 23:01:46 +00004892 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004893 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman2d489b52008-02-06 22:27:42 +00004894 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004895 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner6be79822006-04-04 17:23:26 +00004896}
4897
4898
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004899/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004900/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004901SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4902
4903 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004904 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004905 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004906 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004907 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004908 std::map<SDOperand, std::vector<unsigned> > Values;
4909 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004910 bool isConstant = true;
4911 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4912 SplatValue.getOpcode() != ISD::UNDEF)
4913 isConstant = false;
4914
Evan Cheng1d2e9952006-03-24 01:17:21 +00004915 for (unsigned i = 1; i < NumElems; ++i) {
4916 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004917 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004918 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004919 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004920 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004921 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004922
4923 // If this isn't a constant element or an undef, we can't use a constant
4924 // pool load.
4925 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4926 V.getOpcode() != ISD::UNDEF)
4927 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004928 }
4929
4930 if (isOnlyLowElement) {
4931 // If the low element is an undef too, then this whole things is an undef.
4932 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4933 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4934 // Otherwise, turn this into a scalar_to_vector node.
4935 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4936 Node->getOperand(0));
4937 }
4938
Chris Lattner77e271c2006-03-24 07:29:17 +00004939 // If all elements are constants, create a load from the constant pool.
4940 if (isConstant) {
4941 MVT::ValueType VT = Node->getValueType(0);
4942 const Type *OpNTy =
4943 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4944 std::vector<Constant*> CV;
4945 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4946 if (ConstantFPSDNode *V =
4947 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004948 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004949 } else if (ConstantSDNode *V =
4950 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004951 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004952 } else {
4953 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4954 CV.push_back(UndefValue::get(OpNTy));
4955 }
4956 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004957 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004958 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00004959 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004960 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004961 }
4962
Chris Lattner21e68c82006-03-20 01:52:29 +00004963 if (SplatValue.Val) { // Splat of one value?
4964 // Build the shuffle constant vector: <0, 0, 0, 0>
4965 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004966 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004967 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004968 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004969 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4970 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004971
4972 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004973 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004974 // Get the splatted value into the low element of a vector register.
4975 SDOperand LowValVec =
4976 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4977
4978 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4979 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4980 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4981 SplatMask);
4982 }
4983 }
4984
Evan Cheng1d2e9952006-03-24 01:17:21 +00004985 // If there are only two unique elements, we may be able to turn this into a
4986 // vector shuffle.
4987 if (Values.size() == 2) {
4988 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4989 MVT::ValueType MaskVT =
4990 MVT::getIntVectorWithNumElements(NumElems);
4991 std::vector<SDOperand> MaskVec(NumElems);
4992 unsigned i = 0;
4993 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4994 E = Values.end(); I != E; ++I) {
4995 for (std::vector<unsigned>::iterator II = I->second.begin(),
4996 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004997 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004998 i += NumElems;
4999 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005000 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5001 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00005002
5003 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005004 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5005 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005006 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00005007 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5008 E = Values.end(); I != E; ++I) {
5009 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5010 I->first);
5011 Ops.push_back(Op);
5012 }
5013 Ops.push_back(ShuffleMask);
5014
5015 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005016 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5017 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00005018 }
5019 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005020
5021 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5022 // aligned object on the stack, store each element into it, then load
5023 // the result as a vector.
5024 MVT::ValueType VT = Node->getValueType(0);
5025 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00005026 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005027
5028 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005029 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005030 unsigned TypeByteSize =
5031 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005032 // Store (in the right endianness) the elements to memory.
5033 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5034 // Ignore undef elements.
5035 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5036
Chris Lattner8fa445a2006-03-22 01:46:54 +00005037 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005038
5039 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5040 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5041
Evan Chengdf9ac472006-10-05 23:01:46 +00005042 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00005043 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005044 }
5045
5046 SDOperand StoreChain;
5047 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005048 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5049 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005050 else
5051 StoreChain = DAG.getEntryNode();
5052
5053 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005054 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005055}
5056
Chris Lattner4157c412005-04-02 04:00:59 +00005057void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5058 SDOperand Op, SDOperand Amt,
5059 SDOperand &Lo, SDOperand &Hi) {
5060 // Expand the subcomponents.
5061 SDOperand LHSL, LHSH;
5062 ExpandOp(Op, LHSL, LHSH);
5063
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005064 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00005065 MVT::ValueType VT = LHSL.getValueType();
5066 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00005067 Hi = Lo.getValue(1);
5068}
5069
5070
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005071/// ExpandShift - Try to find a clever way to expand this shift operation out to
5072/// smaller elements. If we can't find a way that is more efficient than a
5073/// libcall on this target, return false. Otherwise, return true with the
5074/// low-parts expanded into Lo and Hi.
5075bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5076 SDOperand &Lo, SDOperand &Hi) {
5077 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5078 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00005079
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005080 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00005081 SDOperand ShAmt = LegalizeOp(Amt);
5082 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman34fc7db2008-02-20 16:57:27 +00005083 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanb0674922005-04-06 21:13:14 +00005084 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5085 unsigned NVTBits = MVT::getSizeInBits(NVT);
5086
Chris Lattnerfbbe5702007-10-14 20:35:12 +00005087 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00005088 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5089 unsigned Cst = CN->getValue();
5090 // Expand the incoming operand to be shifted, so that we have its parts
5091 SDOperand InL, InH;
5092 ExpandOp(Op, InL, InH);
5093 switch(Opc) {
5094 case ISD::SHL:
5095 if (Cst > VTBits) {
5096 Lo = DAG.getConstant(0, NVT);
5097 Hi = DAG.getConstant(0, NVT);
5098 } else if (Cst > NVTBits) {
5099 Lo = DAG.getConstant(0, NVT);
5100 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005101 } else if (Cst == NVTBits) {
5102 Lo = DAG.getConstant(0, NVT);
5103 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00005104 } else {
5105 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5106 Hi = DAG.getNode(ISD::OR, NVT,
5107 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5108 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5109 }
5110 return true;
5111 case ISD::SRL:
5112 if (Cst > VTBits) {
5113 Lo = DAG.getConstant(0, NVT);
5114 Hi = DAG.getConstant(0, NVT);
5115 } else if (Cst > NVTBits) {
5116 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5117 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00005118 } else if (Cst == NVTBits) {
5119 Lo = InH;
5120 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00005121 } else {
5122 Lo = DAG.getNode(ISD::OR, NVT,
5123 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5124 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5125 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5126 }
5127 return true;
5128 case ISD::SRA:
5129 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005130 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005131 DAG.getConstant(NVTBits-1, ShTy));
5132 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005133 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005134 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00005135 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005136 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005137 } else if (Cst == NVTBits) {
5138 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00005139 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00005140 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00005141 } else {
5142 Lo = DAG.getNode(ISD::OR, NVT,
5143 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5144 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5145 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5146 }
5147 return true;
5148 }
5149 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00005150
5151 // Okay, the shift amount isn't constant. However, if we can tell that it is
5152 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005153 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5154 APInt KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00005155 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00005156
Dan Gohmanf3057a92008-02-22 01:12:31 +00005157 // If we know that if any of the high bits of the shift amount are one, then
5158 // we can do this as a couple of simple shifts.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005159 if (KnownOne.intersects(Mask)) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005160 // Mask out the high bit, which we know is set.
5161 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman34fc7db2008-02-20 16:57:27 +00005162 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner875ea0c2006-09-20 03:38:48 +00005163
5164 // Expand the incoming operand to be shifted, so that we have its parts
5165 SDOperand InL, InH;
5166 ExpandOp(Op, InL, InH);
5167 switch(Opc) {
5168 case ISD::SHL:
5169 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5170 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5171 return true;
5172 case ISD::SRL:
5173 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5174 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5175 return true;
5176 case ISD::SRA:
5177 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5178 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5179 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5180 return true;
5181 }
5182 }
5183
Dan Gohmanf3057a92008-02-22 01:12:31 +00005184 // If we know that the high bits of the shift amount are all zero, then we can
5185 // do this as a couple of simple shifts.
5186 if ((KnownZero & Mask) == Mask) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005187 // Compute 32-amt.
5188 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5189 DAG.getConstant(NVTBits, Amt.getValueType()),
5190 Amt);
5191
5192 // Expand the incoming operand to be shifted, so that we have its parts
5193 SDOperand InL, InH;
5194 ExpandOp(Op, InL, InH);
5195 switch(Opc) {
5196 case ISD::SHL:
5197 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5198 Hi = DAG.getNode(ISD::OR, NVT,
5199 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5200 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5201 return true;
5202 case ISD::SRL:
5203 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5204 Lo = DAG.getNode(ISD::OR, NVT,
5205 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5206 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5207 return true;
5208 case ISD::SRA:
5209 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5210 Lo = DAG.getNode(ISD::OR, NVT,
5211 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5212 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5213 return true;
5214 }
5215 }
5216
Nate Begemanb0674922005-04-06 21:13:14 +00005217 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005218}
Chris Lattneraac464e2005-01-21 06:05:23 +00005219
Chris Lattner4add7e32005-01-23 04:42:50 +00005220
Chris Lattneraac464e2005-01-21 06:05:23 +00005221// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5222// does not fit into a register, return the lo part and set the hi part to the
5223// by-reg argument. If it does fit into a single register, return the result
5224// and leave the Hi part unset.
5225SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00005226 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00005227 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5228 // The input chain to this libcall is the entry node of the function.
5229 // Legalizing the call will automatically add the previous call to the
5230 // dependence.
5231 SDOperand InChain = DAG.getEntryNode();
5232
Chris Lattneraac464e2005-01-21 06:05:23 +00005233 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00005234 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00005235 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5236 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5237 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00005238 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00005239 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005240 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00005241 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00005242 }
5243 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00005244
Chris Lattner06bbeb62005-05-11 19:02:11 +00005245 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00005246 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00005247 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005248 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5249 false, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00005250
Chris Lattner462505f2006-02-13 09:18:02 +00005251 // Legalize the call sequence, starting with the chain. This will advance
5252 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5253 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5254 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00005255 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005256 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005257 default: assert(0 && "Unknown thing");
5258 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005259 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00005260 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005261 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00005262 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00005263 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005264 }
Chris Lattner63022662005-09-02 20:26:58 +00005265 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005266}
5267
Chris Lattner4add7e32005-01-23 04:42:50 +00005268
Evan Chengbf535fc2007-04-27 07:33:31 +00005269/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5270///
Chris Lattneraac464e2005-01-21 06:05:23 +00005271SDOperand SelectionDAGLegalize::
5272ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005273 assert(getTypeAction(Source.getValueType()) == Expand &&
5274 "This is not an expansion!");
5275 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5276
Chris Lattner06bbeb62005-05-11 19:02:11 +00005277 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005278 assert(Source.getValueType() == MVT::i64 &&
5279 "This only works for 64-bit -> FP");
5280 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5281 // incoming integer is set. To handle this, we dynamically test to see if
5282 // it is set, and, if so, add a fudge factor.
5283 SDOperand Lo, Hi;
5284 ExpandOp(Source, Lo, Hi);
5285
Chris Lattner2a4f7312005-05-13 04:45:13 +00005286 // If this is unsigned, and not supported, first perform the conversion to
5287 // signed, then adjust the result if the sign bit is set.
5288 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5289 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5290
Chris Lattnerd47675e2005-08-09 20:20:18 +00005291 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5292 DAG.getConstant(0, Hi.getValueType()),
5293 ISD::SETLT);
Chris Lattner72733e52008-01-17 07:00:52 +00005294 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005295 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5296 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00005297 uint64_t FF = 0x5f800000ULL;
5298 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005299 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005300
Chris Lattnerc30405e2005-08-26 17:15:30 +00005301 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005302 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5303 SDOperand FudgeInReg;
5304 if (DestTy == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005305 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005306 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00005307 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00005308 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00005309 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00005310 CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005311 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005312 MVT::f32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00005313 else
5314 assert(0 && "Unexpected conversion");
5315
Evan Chengbf535fc2007-04-27 07:33:31 +00005316 MVT::ValueType SCVT = SignedConv.getValueType();
5317 if (SCVT != DestTy) {
5318 // Destination type needs to be expanded as well. The FADD now we are
5319 // constructing will be expanded into a libcall.
5320 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5321 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5322 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5323 SignedConv, SignedConv.getValue(1));
5324 }
5325 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5326 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005327 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005328 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005329
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005330 // Check to see if the target has a custom way to lower this. If so, use it.
5331 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5332 default: assert(0 && "This action not implemented for this operation!");
5333 case TargetLowering::Legal:
5334 case TargetLowering::Expand:
5335 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005336 case TargetLowering::Custom: {
5337 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5338 Source), DAG);
5339 if (NV.Val)
5340 return LegalizeOp(NV);
5341 break; // The target decided this was legal after all
5342 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005343 }
5344
Chris Lattner153587e2005-05-12 07:00:44 +00005345 // Expand the source, then glue it back together for the call. We must expand
5346 // the source in case it is shared (this pass of legalize must traverse it).
5347 SDOperand SrcLo, SrcHi;
5348 ExpandOp(Source, SrcLo, SrcHi);
5349 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5350
Evan Cheng31cbddf2007-01-12 02:11:51 +00005351 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005352 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005353 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005354 else {
5355 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00005356 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005357 }
Chris Lattner462505f2006-02-13 09:18:02 +00005358
Evan Chengbf535fc2007-04-27 07:33:31 +00005359 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00005360 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5361 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005362 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5363 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00005364}
Misha Brukman835702a2005-04-21 22:36:52 +00005365
Chris Lattner689bdcc2006-01-28 08:25:58 +00005366/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5367/// INT_TO_FP operation of the specified operand when the target requests that
5368/// we expand it. At this point, we know that the result and operand types are
5369/// legal for the target.
5370SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5371 SDOperand Op0,
5372 MVT::ValueType DestVT) {
5373 if (Op0.getValueType() == MVT::i32) {
5374 // simple 32-bit [signed|unsigned] integer to float/double expansion
5375
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005376 // Get the stack frame index of a 8 byte buffer.
5377 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5378
Chris Lattner689bdcc2006-01-28 08:25:58 +00005379 // word offset constant for Hi/Lo address computation
5380 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5381 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00005382 SDOperand Hi = StackSlot;
5383 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5384 if (TLI.isLittleEndian())
5385 std::swap(Hi, Lo);
5386
Chris Lattner689bdcc2006-01-28 08:25:58 +00005387 // if signed map to unsigned space
5388 SDOperand Op0Mapped;
5389 if (isSigned) {
5390 // constant used to invert sign bit (signed to unsigned mapping)
5391 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5392 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5393 } else {
5394 Op0Mapped = Op0;
5395 }
5396 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00005397 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005398 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005399 // initial hi portion of constructed double
5400 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5401 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00005402 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005403 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00005404 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005405 // FP constant to bias correct the final result
5406 SDOperand Bias = DAG.getConstantFP(isSigned ?
5407 BitsToDouble(0x4330000080000000ULL)
5408 : BitsToDouble(0x4330000000000000ULL),
5409 MVT::f64);
5410 // subtract the bias
5411 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5412 // final result
5413 SDOperand Result;
5414 // handle final rounding
5415 if (DestVT == MVT::f64) {
5416 // do nothing
5417 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00005418 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner72733e52008-01-17 07:00:52 +00005419 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5420 DAG.getIntPtrConstant(0));
Dale Johannesen7f724e92007-09-16 16:51:49 +00005421 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5422 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005423 }
5424 return Result;
5425 }
5426 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5427 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5428
5429 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5430 DAG.getConstant(0, Op0.getValueType()),
5431 ISD::SETLT);
Chris Lattner72733e52008-01-17 07:00:52 +00005432 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005433 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5434 SignSet, Four, Zero);
5435
5436 // If the sign bit of the integer is set, the large number will be treated
5437 // as a negative number. To counteract this, the dynamic code adds an
5438 // offset depending on the data type.
5439 uint64_t FF;
5440 switch (Op0.getValueType()) {
5441 default: assert(0 && "Unsupported integer type!");
5442 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5443 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5444 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5445 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5446 }
5447 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005448 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005449
5450 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5451 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5452 SDOperand FudgeInReg;
5453 if (DestVT == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005454 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005455 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005456 else {
Dan Gohman2d489b52008-02-06 22:27:42 +00005457 FudgeInReg =
5458 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5459 DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005460 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005461 MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005462 }
5463
5464 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5465}
5466
5467/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5468/// *INT_TO_FP operation of the specified operand when the target requests that
5469/// we promote it. At this point, we know that the result and operand types are
5470/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5471/// operation that takes a larger input.
5472SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5473 MVT::ValueType DestVT,
5474 bool isSigned) {
5475 // First step, figure out the appropriate *INT_TO_FP operation to use.
5476 MVT::ValueType NewInTy = LegalOp.getValueType();
5477
5478 unsigned OpToUse = 0;
5479
5480 // Scan for the appropriate larger type to use.
5481 while (1) {
5482 NewInTy = (MVT::ValueType)(NewInTy+1);
5483 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5484
5485 // If the target supports SINT_TO_FP of this type, use it.
5486 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5487 default: break;
5488 case TargetLowering::Legal:
5489 if (!TLI.isTypeLegal(NewInTy))
5490 break; // Can't use this datatype.
5491 // FALL THROUGH.
5492 case TargetLowering::Custom:
5493 OpToUse = ISD::SINT_TO_FP;
5494 break;
5495 }
5496 if (OpToUse) break;
5497 if (isSigned) continue;
5498
5499 // If the target supports UINT_TO_FP of this type, use it.
5500 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5501 default: break;
5502 case TargetLowering::Legal:
5503 if (!TLI.isTypeLegal(NewInTy))
5504 break; // Can't use this datatype.
5505 // FALL THROUGH.
5506 case TargetLowering::Custom:
5507 OpToUse = ISD::UINT_TO_FP;
5508 break;
5509 }
5510 if (OpToUse) break;
5511
5512 // Otherwise, try a larger type.
5513 }
5514
5515 // Okay, we found the operation and type to use. Zero extend our input to the
5516 // desired type then run the operation on it.
5517 return DAG.getNode(OpToUse, DestVT,
5518 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5519 NewInTy, LegalOp));
5520}
5521
5522/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5523/// FP_TO_*INT operation of the specified operand when the target requests that
5524/// we promote it. At this point, we know that the result and operand types are
5525/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5526/// operation that returns a larger result.
5527SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5528 MVT::ValueType DestVT,
5529 bool isSigned) {
5530 // First step, figure out the appropriate FP_TO*INT operation to use.
5531 MVT::ValueType NewOutTy = DestVT;
5532
5533 unsigned OpToUse = 0;
5534
5535 // Scan for the appropriate larger type to use.
5536 while (1) {
5537 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5538 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5539
5540 // If the target supports FP_TO_SINT returning this type, use it.
5541 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5542 default: break;
5543 case TargetLowering::Legal:
5544 if (!TLI.isTypeLegal(NewOutTy))
5545 break; // Can't use this datatype.
5546 // FALL THROUGH.
5547 case TargetLowering::Custom:
5548 OpToUse = ISD::FP_TO_SINT;
5549 break;
5550 }
5551 if (OpToUse) break;
5552
5553 // If the target supports FP_TO_UINT of this type, use it.
5554 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5555 default: break;
5556 case TargetLowering::Legal:
5557 if (!TLI.isTypeLegal(NewOutTy))
5558 break; // Can't use this datatype.
5559 // FALL THROUGH.
5560 case TargetLowering::Custom:
5561 OpToUse = ISD::FP_TO_UINT;
5562 break;
5563 }
5564 if (OpToUse) break;
5565
5566 // Otherwise, try a larger type.
5567 }
5568
Chris Lattnerf81d5882007-11-24 07:07:01 +00005569
5570 // Okay, we found the operation and type to use.
5571 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5572
5573 // If the operation produces an invalid type, it must be custom lowered. Use
5574 // the target lowering hooks to expand it. Just keep the low part of the
5575 // expanded operation, we know that we're truncating anyway.
5576 if (getTypeAction(NewOutTy) == Expand) {
5577 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5578 assert(Operation.Val && "Didn't return anything");
5579 }
5580
5581 // Truncate the result of the extended FP_TO_*INT operation to the desired
5582 // size.
5583 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005584}
5585
5586/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5587///
5588SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5589 MVT::ValueType VT = Op.getValueType();
5590 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5591 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5592 switch (VT) {
5593 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5594 case MVT::i16:
5595 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5596 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5597 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5598 case MVT::i32:
5599 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5600 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5601 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5602 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5603 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5604 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5605 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5606 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5607 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5608 case MVT::i64:
5609 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5610 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5611 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5612 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5613 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5614 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5615 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5616 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5617 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5618 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5619 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5620 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5621 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5622 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5623 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5624 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5625 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5626 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5627 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5628 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5629 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5630 }
5631}
5632
5633/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5634///
5635SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5636 switch (Opc) {
5637 default: assert(0 && "Cannot expand this yet!");
5638 case ISD::CTPOP: {
5639 static const uint64_t mask[6] = {
5640 0x5555555555555555ULL, 0x3333333333333333ULL,
5641 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5642 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5643 };
5644 MVT::ValueType VT = Op.getValueType();
5645 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005646 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005647 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5648 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5649 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5650 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5651 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5652 DAG.getNode(ISD::AND, VT,
5653 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5654 }
5655 return Op;
5656 }
5657 case ISD::CTLZ: {
5658 // for now, we do this:
5659 // x = x | (x >> 1);
5660 // x = x | (x >> 2);
5661 // ...
5662 // x = x | (x >>16);
5663 // x = x | (x >>32); // for 64-bit input
5664 // return popcount(~x);
5665 //
5666 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5667 MVT::ValueType VT = Op.getValueType();
5668 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005669 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005670 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5671 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5672 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5673 }
5674 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5675 return DAG.getNode(ISD::CTPOP, VT, Op);
5676 }
5677 case ISD::CTTZ: {
5678 // for now, we use: { return popcount(~x & (x - 1)); }
5679 // unless the target has ctlz but not ctpop, in which case we use:
5680 // { return 32 - nlz(~x & (x-1)); }
5681 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5682 MVT::ValueType VT = Op.getValueType();
5683 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5684 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5685 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5686 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5687 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5688 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5689 TLI.isOperationLegal(ISD::CTLZ, VT))
5690 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005691 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005692 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5693 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5694 }
5695 }
5696}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005697
Chris Lattnerdc750592005-01-07 07:47:09 +00005698/// ExpandOp - Expand the specified SDOperand into its two component pieces
5699/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5700/// LegalizeNodes map is filled in for any results that are not expanded, the
5701/// ExpandedNodes map is filled in for any results that are expanded, and the
5702/// Lo/Hi values are returned.
5703void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5704 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005705 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005706 SDNode *Node = Op.Val;
5707 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005708 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005709 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005710 "Cannot expand to FP value or to larger int value!");
5711
Chris Lattner1a570f12005-09-02 20:32:45 +00005712 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005713 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005714 = ExpandedNodes.find(Op);
5715 if (I != ExpandedNodes.end()) {
5716 Lo = I->second.first;
5717 Hi = I->second.second;
5718 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005719 }
5720
Chris Lattnerdc750592005-01-07 07:47:09 +00005721 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005722 case ISD::CopyFromReg:
5723 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005724 case ISD::FP_ROUND_INREG:
5725 if (VT == MVT::ppcf128 &&
5726 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5727 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005728 SDOperand SrcLo, SrcHi, Src;
5729 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5730 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5731 SDOperand Result = TLI.LowerOperation(
5732 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005733 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5734 Lo = Result.Val->getOperand(0);
5735 Hi = Result.Val->getOperand(1);
5736 break;
5737 }
5738 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005739 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005740#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005741 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005742#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005743 assert(0 && "Do not know how to expand this operator!");
5744 abort();
Dan Gohman66272a52008-02-27 01:52:30 +00005745 case ISD::EXTRACT_ELEMENT:
5746 ExpandOp(Node->getOperand(0), Lo, Hi);
5747 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5748 return ExpandOp(Hi, Lo, Hi);
Dan Gohmane5e32ec2008-02-27 19:44:57 +00005749 return ExpandOp(Lo, Lo, Hi);
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005750 case ISD::EXTRACT_VECTOR_ELT:
5751 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5752 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5753 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5754 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005755 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005756 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005757 Lo = DAG.getNode(ISD::UNDEF, NVT);
5758 Hi = DAG.getNode(ISD::UNDEF, NVT);
5759 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005760 case ISD::Constant: {
5761 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5762 Lo = DAG.getConstant(Cst, NVT);
5763 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5764 break;
5765 }
Evan Cheng47833a12006-12-12 21:32:44 +00005766 case ISD::ConstantFP: {
5767 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005768 if (CFP->getValueType(0) == MVT::ppcf128) {
5769 APInt api = CFP->getValueAPF().convertToAPInt();
5770 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5771 MVT::f64);
5772 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5773 MVT::f64);
5774 break;
5775 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005776 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005777 if (getTypeAction(Lo.getValueType()) == Expand)
5778 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005779 break;
5780 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005781 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005782 // Return the operands.
5783 Lo = Node->getOperand(0);
5784 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005785 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00005786
5787 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00005788 if (Node->getNumValues() == 1) {
5789 ExpandOp(Op.getOperand(0), Lo, Hi);
5790 break;
5791 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005792 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5793 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5794 Op.getValue(1).getValueType() == MVT::Other &&
5795 "unhandled MERGE_VALUES");
5796 ExpandOp(Op.getOperand(0), Lo, Hi);
5797 // Remember that we legalized the chain.
5798 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5799 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005800
5801 case ISD::SIGN_EXTEND_INREG:
5802 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005803 // sext_inreg the low part if needed.
5804 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5805
5806 // The high part gets the sign extension from the lo-part. This handles
5807 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005808 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5809 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5810 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005811 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005812
Nate Begeman2fba8a32006-01-14 03:14:10 +00005813 case ISD::BSWAP: {
5814 ExpandOp(Node->getOperand(0), Lo, Hi);
5815 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5816 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5817 Lo = TempLo;
5818 break;
5819 }
5820
Chris Lattner55e9cde2005-05-11 04:51:16 +00005821 case ISD::CTPOP:
5822 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005823 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5824 DAG.getNode(ISD::CTPOP, NVT, Lo),
5825 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005826 Hi = DAG.getConstant(0, NVT);
5827 break;
5828
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005829 case ISD::CTLZ: {
5830 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005831 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005832 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5833 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005834 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5835 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005836 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5837 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5838
5839 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5840 Hi = DAG.getConstant(0, NVT);
5841 break;
5842 }
5843
5844 case ISD::CTTZ: {
5845 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005846 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005847 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5848 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005849 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5850 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005851 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5852 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5853
5854 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5855 Hi = DAG.getConstant(0, NVT);
5856 break;
5857 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005858
Nate Begemane74795c2006-01-25 18:21:52 +00005859 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005860 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5861 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005862 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5863 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5864
5865 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005866 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005867 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005868 if (TLI.isBigEndian())
Nate Begemane74795c2006-01-25 18:21:52 +00005869 std::swap(Lo, Hi);
5870 break;
5871 }
5872
Chris Lattnerdc750592005-01-07 07:47:09 +00005873 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005874 LoadSDNode *LD = cast<LoadSDNode>(Node);
5875 SDOperand Ch = LD->getChain(); // Legalize the chain.
5876 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5877 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005878 int SVOffset = LD->getSrcValueOffset();
5879 unsigned Alignment = LD->getAlignment();
5880 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005881
Evan Chenge71fe34d2006-10-09 20:57:25 +00005882 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005883 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5884 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005885 if (VT == MVT::f32 || VT == MVT::f64) {
5886 // f32->i32 or f64->i64 one to one expansion.
5887 // Remember that we legalized the chain.
5888 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005889 // Recursively expand the new load.
5890 if (getTypeAction(NVT) == Expand)
5891 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005892 break;
5893 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005894
Evan Chenge71fe34d2006-10-09 20:57:25 +00005895 // Increment the pointer to the other half.
5896 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5897 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00005898 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005899 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005900 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005901 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5902 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005903
Evan Chenge71fe34d2006-10-09 20:57:25 +00005904 // Build a factor node to remember that this load is independent of the
5905 // other one.
5906 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5907 Hi.getValue(1));
5908
5909 // Remember that we legalized the chain.
5910 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005911 if (TLI.isBigEndian())
Evan Chenge71fe34d2006-10-09 20:57:25 +00005912 std::swap(Lo, Hi);
5913 } else {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005914 MVT::ValueType EVT = LD->getMemoryVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005915
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005916 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5917 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005918 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5919 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005920 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005921 // Remember that we legalized the chain.
5922 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5923 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5924 break;
5925 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005926
5927 if (EVT == NVT)
5928 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005929 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005930 else
5931 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005932 SVOffset, EVT, isVolatile,
5933 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005934
5935 // Remember that we legalized the chain.
5936 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5937
5938 if (ExtType == ISD::SEXTLOAD) {
5939 // The high part is obtained by SRA'ing all but one of the bits of the
5940 // lo part.
5941 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5942 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5943 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5944 } else if (ExtType == ISD::ZEXTLOAD) {
5945 // The high part is just a zero.
5946 Hi = DAG.getConstant(0, NVT);
5947 } else /* if (ExtType == ISD::EXTLOAD) */ {
5948 // The high part is undefined.
5949 Hi = DAG.getNode(ISD::UNDEF, NVT);
5950 }
5951 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005952 break;
5953 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005954 case ISD::AND:
5955 case ISD::OR:
5956 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5957 SDOperand LL, LH, RL, RH;
5958 ExpandOp(Node->getOperand(0), LL, LH);
5959 ExpandOp(Node->getOperand(1), RL, RH);
5960 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5961 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5962 break;
5963 }
5964 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005965 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005966 ExpandOp(Node->getOperand(1), LL, LH);
5967 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005968 if (getTypeAction(NVT) == Expand)
5969 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005970 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005971 if (VT != MVT::f32)
5972 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005973 break;
5974 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005975 case ISD::SELECT_CC: {
5976 SDOperand TL, TH, FL, FH;
5977 ExpandOp(Node->getOperand(2), TL, TH);
5978 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005979 if (getTypeAction(NVT) == Expand)
5980 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005981 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5982 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005983 if (VT != MVT::f32)
5984 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5985 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005986 break;
5987 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005988 case ISD::ANY_EXTEND:
5989 // The low part is any extension of the input (which degenerates to a copy).
5990 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5991 // The high part is undefined.
5992 Hi = DAG.getNode(ISD::UNDEF, NVT);
5993 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005994 case ISD::SIGN_EXTEND: {
5995 // The low part is just a sign extension of the input (which degenerates to
5996 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005997 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005998
Chris Lattnerdc750592005-01-07 07:47:09 +00005999 // The high part is obtained by SRA'ing all but one of the bits of the lo
6000 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00006001 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006002 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6003 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00006004 break;
6005 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006006 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00006007 // The low part is just a zero extension of the input (which degenerates to
6008 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006009 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006010
Chris Lattnerdc750592005-01-07 07:47:09 +00006011 // The high part is just a zero.
6012 Hi = DAG.getConstant(0, NVT);
6013 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00006014
Chris Lattner59b27fa372007-02-13 23:55:16 +00006015 case ISD::TRUNCATE: {
6016 // The input value must be larger than this value. Expand *it*.
6017 SDOperand NewLo;
6018 ExpandOp(Node->getOperand(0), NewLo, Hi);
6019
6020 // The low part is now either the right size, or it is closer. If not the
6021 // right size, make an illegal truncate so we recursively expand it.
6022 if (NewLo.getValueType() != Node->getValueType(0))
6023 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6024 ExpandOp(NewLo, Lo, Hi);
6025 break;
6026 }
6027
Chris Lattner36e663d2005-12-23 00:16:34 +00006028 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006029 SDOperand Tmp;
6030 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6031 // If the target wants to, allow it to lower this itself.
6032 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6033 case Expand: assert(0 && "cannot expand FP!");
6034 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6035 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6036 }
6037 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6038 }
6039
Evan Cheng4eee7242006-12-09 02:42:38 +00006040 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00006041 if (VT == MVT::f32 || VT == MVT::f64) {
6042 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00006043 if (getTypeAction(NVT) == Expand)
6044 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00006045 break;
6046 }
6047
6048 // If source operand will be expanded to the same type as VT, i.e.
6049 // i64 <- f64, i32 <- f32, expand the source operand instead.
6050 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6051 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6052 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006053 break;
6054 }
6055
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006056 // Turn this into a load/store pair by default.
6057 if (Tmp.Val == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00006058 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006059
Chris Lattner36e663d2005-12-23 00:16:34 +00006060 ExpandOp(Tmp, Lo, Hi);
6061 break;
6062 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006063
Chris Lattnerf81d5882007-11-24 07:07:01 +00006064 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00006065 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6066 TargetLowering::Custom &&
6067 "Must custom expand ReadCycleCounter");
Chris Lattnerf81d5882007-11-24 07:07:01 +00006068 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6069 assert(Tmp.Val && "Node must be custom expanded!");
6070 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner44c28c22005-11-20 22:56:56 +00006071 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00006072 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006073 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006074 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006075
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006076 // These operators cannot be expanded directly, emit them as calls to
6077 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006078 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006079 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00006080 SDOperand Op;
6081 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6082 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006083 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6084 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00006085 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006086
Chris Lattner941d84a2005-07-30 01:40:57 +00006087 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6088
Chris Lattnerfe68d752005-07-29 00:33:32 +00006089 // Now that the custom expander is done, expand the result, which is still
6090 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006091 if (Op.Val) {
6092 ExpandOp(Op, Lo, Hi);
6093 break;
6094 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006095 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006096
Dale Johannesenc0154c02007-10-05 20:04:43 +00006097 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006098 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006099 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006100 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006101 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006102 else if (Node->getOperand(0).getValueType() == MVT::f80)
6103 LC = RTLIB::FPTOSINT_F80_I64;
6104 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6105 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006106 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6107 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006108 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006109 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006110
Evan Cheng31cbddf2007-01-12 02:11:51 +00006111 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006112 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006113 SDOperand Op;
6114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6115 case Expand: assert(0 && "cannot expand FP!");
6116 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6117 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6118 }
6119
6120 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6121
6122 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006123 if (Op.Val) {
6124 ExpandOp(Op, Lo, Hi);
6125 break;
6126 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006127 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006128
Evan Chengfd11ef42007-10-05 01:09:32 +00006129 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006130 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006131 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00006132 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006133 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006134 else if (Node->getOperand(0).getValueType() == MVT::f80)
6135 LC = RTLIB::FPTOUINT_F80_I64;
6136 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6137 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006138 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6139 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006140 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006141 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006142
Evan Cheng870e4f82006-01-09 18:31:59 +00006143 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006144 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006145 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006146 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006147 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006148 Op = TLI.LowerOperation(Op, DAG);
6149 if (Op.Val) {
6150 // Now that the custom expander is done, expand the result, which is
6151 // still VT.
6152 ExpandOp(Op, Lo, Hi);
6153 break;
6154 }
6155 }
6156
Chris Lattner72b503b2006-09-13 03:50:39 +00006157 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6158 // this X << 1 as X+X.
6159 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6160 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6161 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6162 SDOperand LoOps[2], HiOps[3];
6163 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6164 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6165 LoOps[1] = LoOps[0];
6166 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6167
6168 HiOps[1] = HiOps[0];
6169 HiOps[2] = Lo.getValue(1);
6170 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6171 break;
6172 }
6173 }
6174
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006175 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006176 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006177 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006178
6179 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006180 TargetLowering::LegalizeAction Action =
6181 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6182 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6183 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006184 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006185 break;
6186 }
6187
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006188 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006189 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6190 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006191 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006192 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006193
Evan Cheng870e4f82006-01-09 18:31:59 +00006194 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006195 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006196 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006197 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006198 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006199 Op = TLI.LowerOperation(Op, DAG);
6200 if (Op.Val) {
6201 // Now that the custom expander is done, expand the result, which is
6202 // still VT.
6203 ExpandOp(Op, Lo, Hi);
6204 break;
6205 }
6206 }
6207
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006208 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006209 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006210 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006211
6212 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006213 TargetLowering::LegalizeAction Action =
6214 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6215 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6216 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006217 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006218 break;
6219 }
6220
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006221 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006222 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6223 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006224 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006225 }
6226
6227 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006228 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006229 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006230 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006231 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006232 Op = TLI.LowerOperation(Op, DAG);
6233 if (Op.Val) {
6234 // Now that the custom expander is done, expand the result, which is
6235 // still VT.
6236 ExpandOp(Op, Lo, Hi);
6237 break;
6238 }
6239 }
6240
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006241 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006242 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006243 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006244
6245 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006246 TargetLowering::LegalizeAction Action =
6247 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6248 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6249 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006250 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006251 break;
6252 }
6253
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006254 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006255 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6256 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006257 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006258 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006259
Misha Brukman835702a2005-04-21 22:36:52 +00006260 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006261 case ISD::SUB: {
6262 // If the target wants to custom expand this, let them.
6263 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6264 TargetLowering::Custom) {
6265 Op = TLI.LowerOperation(Op, DAG);
6266 if (Op.Val) {
6267 ExpandOp(Op, Lo, Hi);
6268 break;
6269 }
6270 }
6271
6272 // Expand the subcomponents.
6273 SDOperand LHSL, LHSH, RHSL, RHSH;
6274 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6275 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00006276 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6277 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006278 LoOps[0] = LHSL;
6279 LoOps[1] = RHSL;
6280 HiOps[0] = LHSH;
6281 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00006282 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00006283 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006284 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006285 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006286 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00006287 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006288 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006289 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006290 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00006291 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006292 }
Chris Lattner2135bc02007-05-17 18:15:41 +00006293
6294 case ISD::ADDC:
6295 case ISD::SUBC: {
6296 // Expand the subcomponents.
6297 SDOperand LHSL, LHSH, RHSL, RHSH;
6298 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6299 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6300 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6301 SDOperand LoOps[2] = { LHSL, RHSL };
6302 SDOperand HiOps[3] = { LHSH, RHSH };
6303
6304 if (Node->getOpcode() == ISD::ADDC) {
6305 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6306 HiOps[2] = Lo.getValue(1);
6307 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6308 } else {
6309 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6310 HiOps[2] = Lo.getValue(1);
6311 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6312 }
6313 // Remember that we legalized the flag.
6314 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6315 break;
6316 }
6317 case ISD::ADDE:
6318 case ISD::SUBE: {
6319 // Expand the subcomponents.
6320 SDOperand LHSL, LHSH, RHSL, RHSH;
6321 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6322 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6323 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6324 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6325 SDOperand HiOps[3] = { LHSH, RHSH };
6326
6327 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6328 HiOps[2] = Lo.getValue(1);
6329 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6330
6331 // Remember that we legalized the flag.
6332 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6333 break;
6334 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006335 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006336 // If the target wants to custom expand this, let them.
6337 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00006338 SDOperand New = TLI.LowerOperation(Op, DAG);
6339 if (New.Val) {
6340 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006341 break;
6342 }
6343 }
6344
Evan Chenge93762d2006-09-01 18:17:58 +00006345 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6346 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00006347 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6348 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6349 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00006350 SDOperand LL, LH, RL, RH;
6351 ExpandOp(Node->getOperand(0), LL, LH);
6352 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman1f372ed2008-02-25 21:11:39 +00006353 unsigned OuterBitSize = Op.getValueSizeInBits();
6354 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohmana1603612007-10-08 18:33:35 +00006355 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6356 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman1f372ed2008-02-25 21:11:39 +00006357 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6358 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6359 DAG.MaskedValueIsZero(Op.getOperand(1),
6360 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohmana1603612007-10-08 18:33:35 +00006361 // The inputs are both zero-extended.
6362 if (HasUMUL_LOHI) {
6363 // We can emit a umul_lohi.
6364 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6365 Hi = SDOperand(Lo.Val, 1);
6366 break;
6367 }
6368 if (HasMULHU) {
6369 // We can emit a mulhu+mul.
6370 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6371 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6372 break;
6373 }
Dan Gohmana1603612007-10-08 18:33:35 +00006374 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00006375 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohmana1603612007-10-08 18:33:35 +00006376 // The input values are both sign-extended.
6377 if (HasSMUL_LOHI) {
6378 // We can emit a smul_lohi.
6379 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6380 Hi = SDOperand(Lo.Val, 1);
6381 break;
6382 }
6383 if (HasMULHS) {
6384 // We can emit a mulhs+mul.
6385 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6386 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6387 break;
6388 }
6389 }
6390 if (HasUMUL_LOHI) {
6391 // Lo,Hi = umul LHS, RHS.
6392 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6393 DAG.getVTList(NVT, NVT), LL, RL);
6394 Lo = UMulLOHI;
6395 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00006396 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6397 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6398 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6399 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00006400 break;
Nate Begeman43144a22005-08-30 02:44:00 +00006401 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00006402 if (HasMULHU) {
6403 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6404 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6405 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6406 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6407 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6408 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6409 break;
6410 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006411 }
Evan Chenge93762d2006-09-01 18:17:58 +00006412
Dan Gohmana1603612007-10-08 18:33:35 +00006413 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006414 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6415 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00006416 break;
6417 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006418 case ISD::SDIV:
6419 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6420 break;
6421 case ISD::UDIV:
6422 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6423 break;
6424 case ISD::SREM:
6425 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6426 break;
6427 case ISD::UREM:
6428 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6429 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00006430
Evan Cheng4eee7242006-12-09 02:42:38 +00006431 case ISD::FADD:
Duncan Sands53c954f2008-01-10 10:28:30 +00006432 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6433 RTLIB::ADD_F64,
6434 RTLIB::ADD_F80,
6435 RTLIB::ADD_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006436 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006437 break;
6438 case ISD::FSUB:
Duncan Sands53c954f2008-01-10 10:28:30 +00006439 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6440 RTLIB::SUB_F64,
6441 RTLIB::SUB_F80,
6442 RTLIB::SUB_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006443 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006444 break;
6445 case ISD::FMUL:
Duncan Sands53c954f2008-01-10 10:28:30 +00006446 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6447 RTLIB::MUL_F64,
6448 RTLIB::MUL_F80,
6449 RTLIB::MUL_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006450 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006451 break;
6452 case ISD::FDIV:
Duncan Sands53c954f2008-01-10 10:28:30 +00006453 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6454 RTLIB::DIV_F64,
6455 RTLIB::DIV_F80,
6456 RTLIB::DIV_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006457 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006458 break;
6459 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006460 if (VT == MVT::ppcf128) {
6461 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6462 Node->getOperand(0).getValueType()==MVT::f64);
6463 const uint64_t zero = 0;
6464 if (Node->getOperand(0).getValueType()==MVT::f32)
6465 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6466 else
6467 Hi = Node->getOperand(0);
6468 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6469 break;
6470 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006471 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006472 break;
6473 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00006474 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006475 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006476 case ISD::FPOWI:
Duncan Sands53c954f2008-01-10 10:28:30 +00006477 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6478 RTLIB::POWI_F64,
6479 RTLIB::POWI_F80,
6480 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006481 Node, false, Hi);
6482 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006483 case ISD::FSQRT:
6484 case ISD::FSIN:
6485 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006486 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006487 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006488 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00006489 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6490 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006491 break;
6492 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00006493 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6494 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006495 break;
6496 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00006497 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6498 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006499 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006500 default: assert(0 && "Unreachable!");
6501 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006502 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006503 break;
6504 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006505 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006506 if (VT == MVT::ppcf128) {
6507 SDOperand Tmp;
6508 ExpandOp(Node->getOperand(0), Lo, Tmp);
6509 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6510 // lo = hi==fabs(hi) ? lo : -lo;
6511 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6512 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6513 DAG.getCondCode(ISD::SETEQ));
6514 break;
6515 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006516 SDOperand Mask = (VT == MVT::f64)
6517 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6518 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6519 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6520 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6521 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6522 if (getTypeAction(NVT) == Expand)
6523 ExpandOp(Lo, Lo, Hi);
6524 break;
6525 }
6526 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006527 if (VT == MVT::ppcf128) {
6528 ExpandOp(Node->getOperand(0), Lo, Hi);
6529 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6530 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6531 break;
6532 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006533 SDOperand Mask = (VT == MVT::f64)
6534 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6535 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6536 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6537 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6538 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6539 if (getTypeAction(NVT) == Expand)
6540 ExpandOp(Lo, Lo, Hi);
6541 break;
6542 }
Evan Cheng003feb02007-01-04 21:56:39 +00006543 case ISD::FCOPYSIGN: {
6544 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6545 if (getTypeAction(NVT) == Expand)
6546 ExpandOp(Lo, Lo, Hi);
6547 break;
6548 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006549 case ISD::SINT_TO_FP:
6550 case ISD::UINT_TO_FP: {
6551 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6552 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006553 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohman432e4a62008-02-25 21:39:34 +00006554 static const uint64_t zero = 0;
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006555 if (isSigned) {
6556 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6557 Node->getOperand(0)));
6558 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6559 } else {
Dan Gohman432e4a62008-02-25 21:39:34 +00006560 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006561 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6562 Node->getOperand(0)));
6563 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6564 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006565 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006566 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6567 DAG.getConstant(0, MVT::i32),
6568 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6569 DAG.getConstantFP(
6570 APFloat(APInt(128, 2, TwoE32)),
6571 MVT::ppcf128)),
6572 Hi,
6573 DAG.getCondCode(ISD::SETLT)),
6574 Lo, Hi);
6575 }
6576 break;
6577 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006578 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6579 // si64->ppcf128 done by libcall, below
Dan Gohman432e4a62008-02-25 21:39:34 +00006580 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006581 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6582 Lo, Hi);
6583 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6584 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6585 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6586 DAG.getConstant(0, MVT::i64),
6587 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6588 DAG.getConstantFP(
6589 APFloat(APInt(128, 2, TwoE64)),
6590 MVT::ppcf128)),
6591 Hi,
6592 DAG.getCondCode(ISD::SETLT)),
6593 Lo, Hi);
6594 break;
6595 }
Evan Cheng75439b32007-09-27 07:35:39 +00006596 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006597 if (Node->getOperand(0).getValueType() == MVT::i64) {
6598 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006599 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006600 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006601 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006602 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00006603 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00006604 LC = RTLIB::SINTTOFP_I64_F80;
6605 }
6606 else if (VT == MVT::ppcf128) {
6607 assert(isSigned);
6608 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006609 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006610 } else {
6611 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006612 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006613 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00006614 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006615 }
6616
6617 // Promote the operand if needed.
6618 if (getTypeAction(SrcVT) == Promote) {
6619 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6620 Tmp = isSigned
6621 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6622 DAG.getValueType(SrcVT))
6623 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6624 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6625 }
Evan Chengbf535fc2007-04-27 07:33:31 +00006626
6627 const char *LibCall = TLI.getLibcallName(LC);
6628 if (LibCall)
6629 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6630 else {
6631 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6632 Node->getOperand(0));
6633 if (getTypeAction(Lo.getValueType()) == Expand)
6634 ExpandOp(Lo, Lo, Hi);
6635 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006636 break;
6637 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006638 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006639
Chris Lattnerac12f682005-12-21 18:02:52 +00006640 // Make sure the resultant values have been legalized themselves, unless this
6641 // is a type that requires multi-step expansion.
6642 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6643 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006644 if (Hi.Val)
6645 // Don't legalize the high part if it is expanded to a single node.
6646 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006647 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006648
6649 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006650 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006651 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006652}
6653
Dan Gohmana8665142007-06-25 16:23:39 +00006654/// SplitVectorOp - Given an operand of vector type, break it down into
6655/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006656void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6657 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006658 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006659 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006660 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006661 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00006662
Dan Gohman60028182007-09-24 15:54:53 +00006663 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begemanbd117f02007-11-15 21:15:26 +00006664
6665 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6666 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6667
6668 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6669 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6670
Chris Lattner32206f52006-03-18 01:44:44 +00006671 // See if we already split it.
6672 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6673 = SplitNodes.find(Op);
6674 if (I != SplitNodes.end()) {
6675 Lo = I->second.first;
6676 Hi = I->second.second;
6677 return;
6678 }
6679
6680 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006681 default:
6682#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006683 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006684#endif
6685 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00006686 case ISD::UNDEF:
6687 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6688 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6689 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006690 case ISD::BUILD_PAIR:
6691 Lo = Node->getOperand(0);
6692 Hi = Node->getOperand(1);
6693 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006694 case ISD::INSERT_VECTOR_ELT: {
6695 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6696 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6697 SDOperand ScalarOp = Node->getOperand(1);
Nate Begemanbd117f02007-11-15 21:15:26 +00006698 if (Index < NewNumElts_Lo)
6699 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmana90183e2007-09-28 23:53:40 +00006700 DAG.getConstant(Index, TLI.getPointerTy()));
6701 else
Nate Begemanbd117f02007-11-15 21:15:26 +00006702 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6703 DAG.getConstant(Index - NewNumElts_Lo,
6704 TLI.getPointerTy()));
Dan Gohmana90183e2007-09-28 23:53:40 +00006705 break;
6706 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006707 case ISD::VECTOR_SHUFFLE: {
6708 // Build the low part.
6709 SDOperand Mask = Node->getOperand(2);
6710 SmallVector<SDOperand, 8> Ops;
6711 MVT::ValueType PtrVT = TLI.getPointerTy();
6712
6713 // Insert all of the elements from the input that are needed. We use
6714 // buildvector of extractelement here because the input vectors will have
6715 // to be legalized, so this makes the code simpler.
6716 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6717 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6718 SDOperand InVec = Node->getOperand(0);
6719 if (Idx >= NumElements) {
6720 InVec = Node->getOperand(1);
6721 Idx -= NumElements;
6722 }
6723 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6724 DAG.getConstant(Idx, PtrVT)));
6725 }
6726 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6727 Ops.clear();
6728
6729 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6730 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6731 SDOperand InVec = Node->getOperand(0);
6732 if (Idx >= NumElements) {
6733 InVec = Node->getOperand(1);
6734 Idx -= NumElements;
6735 }
6736 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6737 DAG.getConstant(Idx, PtrVT)));
6738 }
6739 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6740 break;
6741 }
Dan Gohmana8665142007-06-25 16:23:39 +00006742 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006743 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00006744 Node->op_begin()+NewNumElts_Lo);
6745 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006746
Nate Begemanbd117f02007-11-15 21:15:26 +00006747 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00006748 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006749 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006750 break;
6751 }
Dan Gohmana8665142007-06-25 16:23:39 +00006752 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00006753 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00006754 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6755 if (NewNumSubvectors == 1) {
6756 Lo = Node->getOperand(0);
6757 Hi = Node->getOperand(1);
6758 } else {
6759 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6760 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00006761 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006762
Dan Gohmana8665142007-06-25 16:23:39 +00006763 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6764 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006765 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00006766 }
Dan Gohman26455c42007-06-13 15:12:02 +00006767 break;
6768 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006769 case ISD::SELECT: {
6770 SDOperand Cond = Node->getOperand(0);
6771
6772 SDOperand LL, LH, RL, RH;
6773 SplitVectorOp(Node->getOperand(1), LL, LH);
6774 SplitVectorOp(Node->getOperand(2), RL, RH);
6775
6776 if (MVT::isVector(Cond.getValueType())) {
6777 // Handle a vector merge.
6778 SDOperand CL, CH;
6779 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00006780 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6781 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006782 } else {
6783 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00006784 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6785 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006786 }
6787 break;
6788 }
Dan Gohmana8665142007-06-25 16:23:39 +00006789 case ISD::ADD:
6790 case ISD::SUB:
6791 case ISD::MUL:
6792 case ISD::FADD:
6793 case ISD::FSUB:
6794 case ISD::FMUL:
6795 case ISD::SDIV:
6796 case ISD::UDIV:
6797 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006798 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006799 case ISD::AND:
6800 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00006801 case ISD::XOR:
6802 case ISD::UREM:
6803 case ISD::SREM:
6804 case ISD::FREM: {
Chris Lattner32206f52006-03-18 01:44:44 +00006805 SDOperand LL, LH, RL, RH;
6806 SplitVectorOp(Node->getOperand(0), LL, LH);
6807 SplitVectorOp(Node->getOperand(1), RL, RH);
6808
Nate Begemanbd117f02007-11-15 21:15:26 +00006809 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6810 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006811 break;
6812 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006813 case ISD::FPOWI: {
6814 SDOperand L, H;
6815 SplitVectorOp(Node->getOperand(0), L, H);
6816
Nate Begemanbd117f02007-11-15 21:15:26 +00006817 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6818 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00006819 break;
6820 }
6821 case ISD::CTTZ:
6822 case ISD::CTLZ:
6823 case ISD::CTPOP:
6824 case ISD::FNEG:
6825 case ISD::FABS:
6826 case ISD::FSQRT:
6827 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00006828 case ISD::FCOS:
6829 case ISD::FP_TO_SINT:
6830 case ISD::FP_TO_UINT:
6831 case ISD::SINT_TO_FP:
6832 case ISD::UINT_TO_FP: {
Dan Gohman2a7de412007-10-11 23:57:53 +00006833 SDOperand L, H;
6834 SplitVectorOp(Node->getOperand(0), L, H);
6835
Nate Begemanbd117f02007-11-15 21:15:26 +00006836 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6837 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00006838 break;
6839 }
Dan Gohmana8665142007-06-25 16:23:39 +00006840 case ISD::LOAD: {
6841 LoadSDNode *LD = cast<LoadSDNode>(Node);
6842 SDOperand Ch = LD->getChain();
6843 SDOperand Ptr = LD->getBasePtr();
6844 const Value *SV = LD->getSrcValue();
6845 int SVOffset = LD->getSrcValueOffset();
6846 unsigned Alignment = LD->getAlignment();
6847 bool isVolatile = LD->isVolatile();
6848
Nate Begemanbd117f02007-11-15 21:15:26 +00006849 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6850 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006851 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00006852 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006853 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006854 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begemanbd117f02007-11-15 21:15:26 +00006855 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006856
6857 // Build a factor node to remember that this load is independent of the
6858 // other one.
6859 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6860 Hi.getValue(1));
6861
6862 // Remember that we legalized the chain.
6863 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006864 break;
6865 }
Dan Gohmana8665142007-06-25 16:23:39 +00006866 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006867 // We know the result is a vector. The input may be either a vector or a
6868 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006869 SDOperand InOp = Node->getOperand(0);
6870 if (!MVT::isVector(InOp.getValueType()) ||
6871 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6872 // The input is a scalar or single-element vector.
6873 // Lower to a store/load so that it can be split.
6874 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006875 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman54d3b5a2008-02-11 18:58:42 +00006876 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006877
Evan Chengdf9ac472006-10-05 23:01:46 +00006878 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006879 InOp, Ptr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006880 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006881 FI->getIndex());
6882 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006883 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006884 FI->getIndex());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006885 }
Dan Gohman0de76942007-06-29 00:09:08 +00006886 // Split the vector and convert each of the pieces now.
6887 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006888 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6889 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00006890 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006891 }
Chris Lattner32206f52006-03-18 01:44:44 +00006892 }
6893
6894 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006895 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006896 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006897 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006898}
6899
6900
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006901/// ScalarizeVectorOp - Given an operand of single-element vector type
6902/// (e.g. v1f32), convert it into the equivalent operation that returns a
6903/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006904SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6905 assert(MVT::isVector(Op.getValueType()) &&
6906 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006907 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006908 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6909 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006910
Dan Gohmana8665142007-06-25 16:23:39 +00006911 // See if we already scalarized it.
6912 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6913 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006914
6915 SDOperand Result;
6916 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006917 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006918#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006919 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006920#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006921 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6922 case ISD::ADD:
6923 case ISD::FADD:
6924 case ISD::SUB:
6925 case ISD::FSUB:
6926 case ISD::MUL:
6927 case ISD::FMUL:
6928 case ISD::SDIV:
6929 case ISD::UDIV:
6930 case ISD::FDIV:
6931 case ISD::SREM:
6932 case ISD::UREM:
6933 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006934 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006935 case ISD::AND:
6936 case ISD::OR:
6937 case ISD::XOR:
6938 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006939 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006940 ScalarizeVectorOp(Node->getOperand(0)),
6941 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006942 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006943 case ISD::FNEG:
6944 case ISD::FABS:
6945 case ISD::FSQRT:
6946 case ISD::FSIN:
6947 case ISD::FCOS:
6948 Result = DAG.getNode(Node->getOpcode(),
6949 NewVT,
6950 ScalarizeVectorOp(Node->getOperand(0)));
6951 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006952 case ISD::FPOWI:
6953 Result = DAG.getNode(Node->getOpcode(),
6954 NewVT,
6955 ScalarizeVectorOp(Node->getOperand(0)),
6956 Node->getOperand(1));
6957 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006958 case ISD::LOAD: {
6959 LoadSDNode *LD = cast<LoadSDNode>(Node);
6960 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6961 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006962
Dan Gohmana8665142007-06-25 16:23:39 +00006963 const Value *SV = LD->getSrcValue();
6964 int SVOffset = LD->getSrcValueOffset();
6965 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6966 LD->isVolatile(), LD->getAlignment());
6967
Chris Lattner32206f52006-03-18 01:44:44 +00006968 // Remember that we legalized the chain.
6969 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6970 break;
6971 }
Dan Gohmana8665142007-06-25 16:23:39 +00006972 case ISD::BUILD_VECTOR:
6973 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006974 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006975 case ISD::INSERT_VECTOR_ELT:
6976 // Returning the inserted scalar element.
6977 Result = Node->getOperand(1);
6978 break;
6979 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006980 assert(Node->getOperand(0).getValueType() == NewVT &&
6981 "Concat of non-legal vectors not yet supported!");
6982 Result = Node->getOperand(0);
6983 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006984 case ISD::VECTOR_SHUFFLE: {
6985 // Figure out if the scalar is the LHS or RHS and return it.
6986 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6987 if (cast<ConstantSDNode>(EltNum)->getValue())
6988 Result = ScalarizeVectorOp(Node->getOperand(1));
6989 else
6990 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006991 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006992 }
6993 case ISD::EXTRACT_SUBVECTOR:
6994 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006995 assert(Result.getValueType() == NewVT);
6996 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006997 case ISD::BIT_CONVERT:
6998 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006999 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007000 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00007001 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00007002 ScalarizeVectorOp(Op.getOperand(1)),
7003 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00007004 break;
Chris Lattner32206f52006-03-18 01:44:44 +00007005 }
7006
7007 if (TLI.isTypeLegal(NewVT))
7008 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00007009 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7010 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00007011 return Result;
7012}
7013
Chris Lattnerdc750592005-01-07 07:47:09 +00007014
7015// SelectionDAG::Legalize - This is the entry point for the file.
7016//
Chris Lattner4add7e32005-01-23 04:42:50 +00007017void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00007018 if (ViewLegalizeDAGs) viewGraph();
7019
Chris Lattnerdc750592005-01-07 07:47:09 +00007020 /// run - This is the main entry point to this class.
7021 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00007022 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00007023}
7024