blob: 5f260ece552a0c42c4b6504e1884a646762e803f [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"
Dan Gohman544ab2c2008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000026#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000027#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000035#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000036using namespace llvm;
37
Chris Lattneref598052006-04-02 03:07:27 +000038#ifndef NDEBUG
39static cl::opt<bool>
40ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
41 cl::desc("Pop up a window to show dags before legalize"));
42#else
43static const bool ViewLegalizeDAGs = 0;
44#endif
45
Chris Lattnerdc750592005-01-07 07:47:09 +000046//===----------------------------------------------------------------------===//
47/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
48/// hacks on it until the target machine can handle it. This involves
49/// eliminating value sizes the machine cannot handle (promoting small sizes to
50/// large sizes or splitting up large values into small values) as well as
51/// eliminating operations the machine cannot handle.
52///
53/// This code also does a small amount of optimization and recognition of idioms
54/// as part of its processing. For example, if a target does not support a
55/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
56/// will attempt merge setcc and brc instructions into brcc's.
57///
58namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000059class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000060 TargetLowering &TLI;
61 SelectionDAG &DAG;
62
Chris Lattner462505f2006-02-13 09:18:02 +000063 // Libcall insertion helpers.
64
65 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
66 /// legalized. We use this to ensure that calls are properly serialized
67 /// against each other, including inserted libcalls.
68 SDOperand LastCALLSEQ_END;
69
70 /// IsLegalizingCall - This member is used *only* for purposes of providing
71 /// helpful assertions that a libcall isn't created while another call is
72 /// being legalized (which could lead to non-serialized call sequences).
73 bool IsLegalizingCall;
74
Chris Lattnerdc750592005-01-07 07:47:09 +000075 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000076 Legal, // The target natively supports this operation.
77 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000078 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000079 };
Chris Lattner462505f2006-02-13 09:18:02 +000080
Chris Lattnerdc750592005-01-07 07:47:09 +000081 /// ValueTypeActions - This is a bitvector that contains two bits for each
82 /// value type, where the two bits correspond to the LegalizeAction enum.
83 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000084 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086 /// LegalizedNodes - For nodes that are of legal width, and that have more
87 /// than one use, this map indicates what regularized operand to use. This
88 /// allows us to avoid legalizing the same thing more than once.
Roman Levensteina3ee1a32008-04-16 16:15:27 +000089 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000090
Chris Lattner1f2c9d82005-01-15 05:21:40 +000091 /// PromotedNodes - For nodes that are below legal width, and that have more
92 /// than one use, this map indicates what promoted value to use. This allows
93 /// us to avoid promoting the same thing more than once.
Roman Levensteina3ee1a32008-04-16 16:15:27 +000094 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000095
Chris Lattner32206f52006-03-18 01:44:44 +000096 /// ExpandedNodes - For nodes that need to be expanded this map indicates
97 /// which which operands are the expanded version of the input. This allows
98 /// us to avoid expanding the same node more than once.
Roman Levensteina3ee1a32008-04-16 16:15:27 +000099 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +0000100
Chris Lattner32206f52006-03-18 01:44:44 +0000101 /// SplitNodes - For vector nodes that need to be split, this map indicates
102 /// which which operands are the split version of the input. This allows us
103 /// to avoid splitting the same node more than once.
104 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
105
Dan Gohmana8665142007-06-25 16:23:39 +0000106 /// ScalarizedNodes - For nodes that need to be converted from vector types to
107 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000108 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000109 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000110
Chris Lattnerea4ca942005-01-07 22:28:47 +0000111 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000112 LegalizedNodes.insert(std::make_pair(From, To));
113 // If someone requests legalization of the new node, return itself.
114 if (From != To)
115 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000116 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000117 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000118 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000119 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000120 // If someone requests legalization of the new node, return itself.
121 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000123
Chris Lattnerdc750592005-01-07 07:47:09 +0000124public:
125
Chris Lattner4add7e32005-01-23 04:42:50 +0000126 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000127
Chris Lattnerdc750592005-01-07 07:47:09 +0000128 /// getTypeAction - Return how we should legalize values of this type, either
129 /// it is already legal or we need to expand it into multiple registers of
130 /// smaller integer type, or we need to promote it to a larger type.
131 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000132 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 }
134
135 /// isTypeLegal - Return true if this type is legal on this target.
136 ///
137 bool isTypeLegal(MVT::ValueType VT) const {
138 return getTypeAction(VT) == Legal;
139 }
140
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 void LegalizeDAG();
142
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000143private:
Dan Gohmana8665142007-06-25 16:23:39 +0000144 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000145 /// appropriate for its type.
146 void HandleOp(SDOperand Op);
147
148 /// LegalizeOp - We know that the specified value has a legal type.
149 /// Recursively ensure that the operands have legal types, then return the
150 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000151 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000152
Dan Gohman2a7de412007-10-11 23:57:53 +0000153 /// UnrollVectorOp - We know that the given vector has a legal type, however
154 /// the operation it performs is not legal and is an operation that we have
155 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
156 /// operating on each element individually.
157 SDOperand UnrollVectorOp(SDOperand O);
158
Chris Lattner32206f52006-03-18 01:44:44 +0000159 /// PromoteOp - Given an operation that produces a value in an invalid type,
160 /// promote it to compute the value into a larger type. The produced value
161 /// will have the correct bits for the low portion of the register, but no
162 /// guarantee is made about the top bits: it may be zero, sign-extended, or
163 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000164 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000165
Chris Lattner32206f52006-03-18 01:44:44 +0000166 /// ExpandOp - Expand the specified SDOperand into its two component pieces
167 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
168 /// the LegalizeNodes map is filled in for any results that are not expanded,
169 /// the ExpandedNodes map is filled in for any results that are expanded, and
170 /// the Lo/Hi values are returned. This applies to integer types and Vector
171 /// types.
172 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
173
Dan Gohmana8665142007-06-25 16:23:39 +0000174 /// SplitVectorOp - Given an operand of vector type, break it down into
175 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000176 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
177
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000178 /// ScalarizeVectorOp - Given an operand of single-element vector type
179 /// (e.g. v1f32), convert it into the equivalent operation that returns a
180 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000181 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000182
Chris Lattner6be79822006-04-04 17:23:26 +0000183 /// isShuffleLegal - Return true if a vector shuffle is legal with the
184 /// specified mask and type. Targets can specify exactly which masks they
185 /// support and the code generator is tasked with not creating illegal masks.
186 ///
187 /// Note that this will also return true for shuffles that are promoted to a
188 /// different type.
189 ///
190 /// If this is a legal shuffle, this method returns the (possibly promoted)
191 /// build_vector Mask. If it's not a legal shuffle, it returns null.
192 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
193
Chris Lattner4488f0c2006-07-26 23:55:56 +0000194 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000195 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000196
Nate Begeman7e7f4392006-02-01 07:19:44 +0000197 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
198
Duncan Sands844d55a2008-04-12 17:14:18 +0000199 SDOperand ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000200 SDOperand &Hi);
201 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
202 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000203
Chris Lattner87bc3e72008-01-16 07:45:30 +0000204 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
205 MVT::ValueType DestVT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000206 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000207 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000208 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
209 SDOperand LegalOp,
210 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000211 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000213 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
214 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000215
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000216 SDOperand ExpandBSWAP(SDOperand Op);
217 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000218 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000220 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
221 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000222
Dan Gohmana8665142007-06-25 16:23:39 +0000223 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000224 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattnerdc750592005-01-07 07:47:09 +0000225};
226}
227
Chris Lattner6be79822006-04-04 17:23:26 +0000228/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
229/// specified mask and type. Targets can specify exactly which masks they
230/// support and the code generator is tasked with not creating illegal masks.
231///
232/// Note that this will also return true for shuffles that are promoted to a
233/// different type.
234SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
235 SDOperand Mask) const {
236 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
237 default: return 0;
238 case TargetLowering::Legal:
239 case TargetLowering::Custom:
240 break;
241 case TargetLowering::Promote: {
242 // If this is promoted to a different type, convert the shuffle mask and
243 // ask if it is legal in the promoted type!
244 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
245
246 // If we changed # elements, change the shuffle mask.
247 unsigned NumEltsGrowth =
248 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
249 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
250 if (NumEltsGrowth > 1) {
251 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000252 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000253 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
254 SDOperand InOp = Mask.getOperand(i);
255 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
256 if (InOp.getOpcode() == ISD::UNDEF)
257 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
258 else {
259 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
260 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
261 }
262 }
263 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000264 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000265 }
266 VT = NVT;
267 break;
268 }
269 }
270 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
271}
272
Chris Lattner4add7e32005-01-23 04:42:50 +0000273SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
274 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
275 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000276 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000277 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000278}
279
Chris Lattnere31adc82007-06-18 21:28:10 +0000280/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
281/// contains all of a nodes operands before it contains the node.
282static void ComputeTopDownOrdering(SelectionDAG &DAG,
283 SmallVector<SDNode*, 64> &Order) {
284
285 DenseMap<SDNode*, unsigned> Visited;
286 std::vector<SDNode*> Worklist;
287 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000288
Chris Lattnere31adc82007-06-18 21:28:10 +0000289 // Compute ordering from all of the leaves in the graphs, those (like the
290 // entry node) that have no operands.
291 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
292 E = DAG.allnodes_end(); I != E; ++I) {
293 if (I->getNumOperands() == 0) {
294 Visited[I] = 0 - 1U;
295 Worklist.push_back(I);
296 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000297 }
298
Chris Lattnere31adc82007-06-18 21:28:10 +0000299 while (!Worklist.empty()) {
300 SDNode *N = Worklist.back();
301 Worklist.pop_back();
302
303 if (++Visited[N] != N->getNumOperands())
304 continue; // Haven't visited all operands yet
305
306 Order.push_back(N);
307
308 // Now that we have N in, add anything that uses it if all of their operands
309 // are now done.
310 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
311 UI != E; ++UI)
Roman Levenstein51f532f2008-04-07 10:06:32 +0000312 Worklist.push_back(UI->getUser());
Chris Lattnere31adc82007-06-18 21:28:10 +0000313 }
314
315 assert(Order.size() == Visited.size() &&
316 Order.size() ==
317 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
318 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000319}
320
Chris Lattner44fe26f2005-07-29 00:11:56 +0000321
Chris Lattnerdc750592005-01-07 07:47:09 +0000322void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000323 LastCALLSEQ_END = DAG.getEntryNode();
324 IsLegalizingCall = false;
325
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000326 // The legalize process is inherently a bottom-up recursive process (users
327 // legalize their uses before themselves). Given infinite stack space, we
328 // could just start legalizing on the root and traverse the whole graph. In
329 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000330 // blocks. To avoid this problem, compute an ordering of the nodes where each
331 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000332 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000333 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000334
Chris Lattner32206f52006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000346 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000347 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000350 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000351}
352
Chris Lattner462505f2006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
Roman Levenstein51f532f2008-04-07 10:06:32 +0000385 SDNode *User = UI->getUser();
Chris Lattner462505f2006-02-13 09:18:02 +0000386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000408///
409/// Keep track of the nodes we fine that actually do lead to Dest in
410/// NodesLeadingTo. This avoids retraversing them exponential number of times.
411///
412bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000413 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000414 if (N == Dest) return true; // N certainly leads to Dest :)
415
Chris Lattner4488f0c2006-07-26 23:55:56 +0000416 // If we've already processed this node and it does lead to Dest, there is no
417 // need to reprocess it.
418 if (NodesLeadingTo.count(N)) return true;
419
Chris Lattner462505f2006-02-13 09:18:02 +0000420 // If the first result of this node has been already legalized, then it cannot
421 // reach N.
422 switch (getTypeAction(N->getValueType(0))) {
423 case Legal:
424 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
425 break;
426 case Promote:
427 if (PromotedNodes.count(SDOperand(N, 0))) return false;
428 break;
429 case Expand:
430 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 }
433
434 // Okay, this node has not already been legalized. Check and legalize all
435 // operands. If none lead to Dest, then we can legalize this node.
436 bool OperandsLeadToDest = false;
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000439 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000440
Chris Lattner4488f0c2006-07-26 23:55:56 +0000441 if (OperandsLeadToDest) {
442 NodesLeadingTo.insert(N);
443 return true;
444 }
Chris Lattner462505f2006-02-13 09:18:02 +0000445
446 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000447 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000448 return false;
449}
450
Dan Gohmana8665142007-06-25 16:23:39 +0000451/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000452/// appropriate for its type.
453void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000454 MVT::ValueType VT = Op.getValueType();
455 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000456 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000457 case Legal: (void)LegalizeOp(Op); break;
458 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000459 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000460 if (!MVT::isVector(VT)) {
461 // If this is an illegal scalar, expand it into its two component
462 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000463 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000464 if (Op.getOpcode() == ISD::TargetConstant)
465 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000466 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000467 } else if (MVT::getVectorNumElements(VT) == 1) {
468 // If this is an illegal single element vector, convert it to a
469 // scalar operation.
470 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000471 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000472 // Otherwise, this is an illegal multiple element vector.
473 // Split it in half and legalize both parts.
474 SDOperand X, Y;
475 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000476 }
477 break;
478 }
479}
Chris Lattner462505f2006-02-13 09:18:02 +0000480
Evan Cheng22cf8992006-12-13 20:57:08 +0000481/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
482/// a load from the constant pool.
483static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000484 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000485 bool Extend = false;
486
487 // If a FP immediate is precise when represented as a float and if the
488 // target can do an extending load from float to double, we put it into
489 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000490 // double. This shrinks FP constants and canonicalizes them for targets where
491 // an FP extending load is the same cost as a normal load (such as on the x87
492 // fp stack or PPC FP unit).
Evan Cheng47833a12006-12-12 21:32:44 +0000493 MVT::ValueType VT = CFP->getValueType(0);
Chris Lattner3b187622008-04-20 00:41:09 +0000494 ConstantFP *LLVMC = ConstantFP::get(CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000495 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dan Gohman10f7d852008-03-11 00:11:06 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Cheng38caf772008-03-04 08:05:30 +0000499 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000500 }
501
Evan Cheng38caf772008-03-04 08:05:30 +0000502 MVT::ValueType OrigVT = VT;
503 MVT::ValueType SVT = VT;
504 while (SVT != MVT::f32) {
505 SVT = (unsigned)SVT - 1;
506 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
507 // Only do this if the target has a native EXTLOAD instruction from
508 // smaller type.
Evan Cheng0a62cb42008-03-05 01:30:59 +0000509 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattner3dc38992008-03-05 06:46:58 +0000510 TLI.ShouldShrinkFPConstant(OrigVT)) {
Evan Cheng38caf772008-03-04 08:05:30 +0000511 const Type *SType = MVT::getTypeForValueType(SVT);
512 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
513 VT = SVT;
514 Extend = true;
515 }
Evan Cheng47833a12006-12-12 21:32:44 +0000516 }
517
518 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng38caf772008-03-04 08:05:30 +0000519 if (Extend)
520 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman16d4bc32008-02-07 18:41:25 +0000521 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Cheng38caf772008-03-04 08:05:30 +0000522 0, VT);
523 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
524 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng47833a12006-12-12 21:32:44 +0000525}
526
Chris Lattner462505f2006-02-13 09:18:02 +0000527
Evan Cheng003feb02007-01-04 21:56:39 +0000528/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
529/// operations.
530static
531SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
532 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000533 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000534 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000535 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
536 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000537 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000538
Evan Cheng003feb02007-01-04 21:56:39 +0000539 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000540 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000541 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
542 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000543 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000544 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000545 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000546 // Shift right or sign-extend it if the two operands have different types.
547 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
548 if (SizeDiff > 0) {
549 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
550 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
551 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
552 } else if (SizeDiff < 0)
553 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000554
555 // Clear the sign bit of first operand.
556 SDOperand Mask2 = (VT == MVT::f64)
557 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
558 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
559 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000560 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000561 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
562
563 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000564 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
565 return Result;
566}
567
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000568/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
569static
570SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
571 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000572 SDOperand Chain = ST->getChain();
573 SDOperand Ptr = ST->getBasePtr();
574 SDOperand Val = ST->getValue();
575 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000576 int Alignment = ST->getAlignment();
577 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbf76a082008-02-27 22:36:00 +0000578 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
579 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000580 // Expand to a bitconvert of the value to the integer type of the
581 // same size, then a (misaligned) int store.
582 MVT::ValueType intVT;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000583 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000584 intVT = MVT::i128;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000585 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000586 intVT = MVT::i64;
587 else if (VT==MVT::f32)
588 intVT = MVT::i32;
589 else
Dale Johannesenc4c3de22008-02-28 18:36:51 +0000590 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000591
592 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
593 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
594 SVOffset, ST->isVolatile(), Alignment);
595 }
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000596 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesenbf76a082008-02-27 22:36:00 +0000597 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000598 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000599 // Get the half-size VT
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000600 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000601 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000602 int IncrementSize = NumBits / 8;
603
604 // Divide the stored value in two parts.
605 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
606 SDOperand Lo = Val;
607 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
608
609 // Store the two parts
610 SDOperand Store1, Store2;
611 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
612 ST->getSrcValue(), SVOffset, NewStoredVT,
613 ST->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000616 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000617 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
618 ST->getSrcValue(), SVOffset + IncrementSize,
619 NewStoredVT, ST->isVolatile(), Alignment);
620
621 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
622}
623
624/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
625static
626SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
627 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000628 int SVOffset = LD->getSrcValueOffset();
629 SDOperand Chain = LD->getChain();
630 SDOperand Ptr = LD->getBasePtr();
631 MVT::ValueType VT = LD->getValueType(0);
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000632 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesenbf76a082008-02-27 22:36:00 +0000633 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000634 // Expand to a (misaligned) integer load of the same size,
Dale Johannesenbf76a082008-02-27 22:36:00 +0000635 // then bitconvert to floating point or vector.
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000636 MVT::ValueType intVT;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000637 if (MVT::is128BitVector(LoadedVT) ||
638 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000639 intVT = MVT::i128;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000640 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000641 intVT = MVT::i64;
Chris Lattner09c03932007-11-19 21:38:03 +0000642 else if (LoadedVT == MVT::f32)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000643 intVT = MVT::i32;
644 else
Dale Johannesenbf76a082008-02-27 22:36:00 +0000645 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000646
647 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
648 SVOffset, LD->isVolatile(),
649 LD->getAlignment());
650 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesenbf76a082008-02-27 22:36:00 +0000651 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000652 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
653
654 SDOperand Ops[] = { Result, Chain };
655 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
656 Ops, 2);
657 }
Dale Johannesenbf76a082008-02-27 22:36:00 +0000658 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattner09c03932007-11-19 21:38:03 +0000659 "Unaligned load of unsupported type.");
660
Dale Johannesenbf76a082008-02-27 22:36:00 +0000661 // Compute the new VT that is half the size of the old one. This is an
662 // integer MVT.
Chris Lattner09c03932007-11-19 21:38:03 +0000663 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
664 MVT::ValueType NewLoadedVT;
Dale Johannesenbf76a082008-02-27 22:36:00 +0000665 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattner09c03932007-11-19 21:38:03 +0000666 NumBits >>= 1;
667
668 unsigned Alignment = LD->getAlignment();
669 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000670 ISD::LoadExtType HiExtType = LD->getExtensionType();
671
672 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
673 if (HiExtType == ISD::NON_EXTLOAD)
674 HiExtType = ISD::ZEXTLOAD;
675
676 // Load the value in two parts
677 SDOperand Lo, Hi;
678 if (TLI.isLittleEndian()) {
679 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
680 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
684 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000685 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000686 } else {
687 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
688 NewLoadedVT,LD->isVolatile(), Alignment);
689 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
690 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
691 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
692 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000693 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000694 }
695
696 // aggregate the two parts
697 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
698 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
699 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
700
701 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
702 Hi.getValue(1));
703
704 SDOperand Ops[] = { Result, TF };
705 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
706}
Evan Cheng003feb02007-01-04 21:56:39 +0000707
Dan Gohman2a7de412007-10-11 23:57:53 +0000708/// UnrollVectorOp - We know that the given vector has a legal type, however
709/// the operation it performs is not legal and is an operation that we have
710/// no way of lowering. "Unroll" the vector, splitting out the scalars and
711/// operating on each element individually.
712SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
713 MVT::ValueType VT = Op.getValueType();
714 assert(isTypeLegal(VT) &&
715 "Caller should expand or promote operands that are not legal!");
716 assert(Op.Val->getNumValues() == 1 &&
717 "Can't unroll a vector with multiple results!");
718 unsigned NE = MVT::getVectorNumElements(VT);
719 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
720
721 SmallVector<SDOperand, 8> Scalars;
722 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
723 for (unsigned i = 0; i != NE; ++i) {
724 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
725 SDOperand Operand = Op.getOperand(j);
726 MVT::ValueType OperandVT = Operand.getValueType();
727 if (MVT::isVector(OperandVT)) {
728 // A vector operand; extract a single element.
729 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
730 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
731 OperandEltVT,
732 Operand,
733 DAG.getConstant(i, MVT::i32));
734 } else {
735 // A scalar operand; just use it as is.
736 Operands[j] = Operand;
737 }
738 }
739 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
740 &Operands[0], Operands.size()));
741 }
742
743 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
744}
745
Duncan Sands53c954f2008-01-10 10:28:30 +0000746/// GetFPLibCall - Return the right libcall for the given floating point type.
747static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
748 RTLIB::Libcall Call_F32,
749 RTLIB::Libcall Call_F64,
750 RTLIB::Libcall Call_F80,
751 RTLIB::Libcall Call_PPCF128) {
752 return
753 VT == MVT::f32 ? Call_F32 :
754 VT == MVT::f64 ? Call_F64 :
755 VT == MVT::f80 ? Call_F80 :
756 VT == MVT::ppcf128 ? Call_PPCF128 :
757 RTLIB::UNKNOWN_LIBCALL;
758}
759
Dan Gohmanff727882007-07-13 20:14:11 +0000760/// LegalizeOp - We know that the specified value has a legal type, and
761/// that its operands are legal. Now ensure that the operation itself
762/// is legal, recursively ensuring that the operands' operations remain
763/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000764SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
766 return Op;
767
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000768 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000769 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000770 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000771
Chris Lattnerdc750592005-01-07 07:47:09 +0000772 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000773 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000774 if (Node->getNumValues() > 1) {
775 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000776 if (getTypeAction(Node->getValueType(i)) != Legal) {
777 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000778 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000779 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000780 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000781 }
782 }
783
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000784 // Note that LegalizeOp may be reentered even from single-use nodes, which
785 // means that we always must cache transformed nodes.
Roman Levensteina3ee1a32008-04-16 16:15:27 +0000786 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000787 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000788
Nate Begemane5b86d72005-08-10 20:51:12 +0000789 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000790 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000791 bool isCustom = false;
792
Chris Lattnerdc750592005-01-07 07:47:09 +0000793 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000794 case ISD::FrameIndex:
795 case ISD::EntryToken:
796 case ISD::Register:
797 case ISD::BasicBlock:
798 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000799 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000800 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000801 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000802 case ISD::TargetConstantPool:
803 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000804 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000805 case ISD::TargetExternalSymbol:
806 case ISD::VALUETYPE:
807 case ISD::SRCVALUE:
Dan Gohman2d489b52008-02-06 22:27:42 +0000808 case ISD::MEMOPERAND:
Chris Lattnereb637512006-01-28 08:31:04 +0000809 case ISD::STRING:
810 case ISD::CONDCODE:
Duncan Sandsd97eea32008-03-21 09:14:45 +0000811 case ISD::ARG_FLAGS:
Chris Lattnereb637512006-01-28 08:31:04 +0000812 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000813 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000814 "This must be legal!");
815 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000816 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000817 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
818 // If this is a target node, legalize it by legalizing the operands then
819 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000820 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000821 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000822 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000823
Chris Lattner97af9d52006-08-08 01:09:31 +0000824 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000825
826 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
827 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
828 return Result.getValue(Op.ResNo);
829 }
830 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000831#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000832 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000833#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000834 assert(0 && "Do not know how to legalize this operator!");
835 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000836 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000837 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000838 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000839 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000840 case ISD::ConstantPool:
841 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000842 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
843 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000844 case TargetLowering::Custom:
845 Tmp1 = TLI.LowerOperation(Op, DAG);
846 if (Tmp1.Val) Result = Tmp1;
847 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000848 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000849 break;
850 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000851 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000852 case ISD::FRAMEADDR:
853 case ISD::RETURNADDR:
854 // The only option for these nodes is to custom lower them. If the target
855 // does not custom lower them, then return zero.
856 Tmp1 = TLI.LowerOperation(Op, DAG);
857 if (Tmp1.Val)
858 Result = Tmp1;
859 else
860 Result = DAG.getConstant(0, TLI.getPointerTy());
861 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000862 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000863 MVT::ValueType VT = Node->getValueType(0);
864 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
865 default: assert(0 && "This action is not supported yet!");
866 case TargetLowering::Custom:
867 Result = TLI.LowerOperation(Op, DAG);
868 if (Result.Val) break;
869 // Fall Thru
870 case TargetLowering::Legal:
871 Result = DAG.getConstant(0, VT);
872 break;
873 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000874 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000875 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000876 case ISD::EXCEPTIONADDR: {
877 Tmp1 = LegalizeOp(Node->getOperand(0));
878 MVT::ValueType VT = Node->getValueType(0);
879 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
880 default: assert(0 && "This action is not supported yet!");
881 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000882 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000883 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000884 }
885 break;
886 case TargetLowering::Custom:
887 Result = TLI.LowerOperation(Op, DAG);
888 if (Result.Val) break;
889 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000890 case TargetLowering::Legal: {
891 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
892 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000893 Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000894 break;
895 }
896 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000897 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000898 if (Result.Val->getNumValues() == 1) break;
899
900 assert(Result.Val->getNumValues() == 2 &&
901 "Cannot return more than two values!");
902
903 // Since we produced two values, make sure to remember that we
904 // legalized both of them.
905 Tmp1 = LegalizeOp(Result);
906 Tmp2 = LegalizeOp(Result.getValue(1));
907 AddLegalizedOperand(Op.getValue(0), Tmp1);
908 AddLegalizedOperand(Op.getValue(1), Tmp2);
909 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +0000910 case ISD::EHSELECTION: {
911 Tmp1 = LegalizeOp(Node->getOperand(0));
912 Tmp2 = LegalizeOp(Node->getOperand(1));
913 MVT::ValueType VT = Node->getValueType(0);
914 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
915 default: assert(0 && "This action is not supported yet!");
916 case TargetLowering::Expand: {
917 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000918 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +0000919 }
920 break;
921 case TargetLowering::Custom:
922 Result = TLI.LowerOperation(Op, DAG);
923 if (Result.Val) break;
924 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000925 case TargetLowering::Legal: {
926 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
927 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000928 Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +0000929 break;
930 }
931 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000932 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000933 if (Result.Val->getNumValues() == 1) break;
934
935 assert(Result.Val->getNumValues() == 2 &&
936 "Cannot return more than two values!");
937
938 // Since we produced two values, make sure to remember that we
939 // legalized both of them.
940 Tmp1 = LegalizeOp(Result);
941 Tmp2 = LegalizeOp(Result.getValue(1));
942 AddLegalizedOperand(Op.getValue(0), Tmp1);
943 AddLegalizedOperand(Op.getValue(1), Tmp2);
944 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000945 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000946 MVT::ValueType VT = Node->getValueType(0);
947 // The only "good" option for this node is to custom lower it.
948 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
949 default: assert(0 && "This action is not supported at all!");
950 case TargetLowering::Custom:
951 Result = TLI.LowerOperation(Op, DAG);
952 if (Result.Val) break;
953 // Fall Thru
954 case TargetLowering::Legal:
955 // Target does not know, how to lower this, lower to noop
956 Result = LegalizeOp(Node->getOperand(0));
957 break;
958 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000959 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000960 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000961 case ISD::AssertSext:
962 case ISD::AssertZext:
963 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000964 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000965 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000966 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000967 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000968 Result = Node->getOperand(Op.ResNo);
969 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000970 case ISD::CopyFromReg:
971 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000972 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000973 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000975 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000976 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000977 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000978 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
980 } else {
981 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
982 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000983 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
984 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000985 // Since CopyFromReg produces two values, make sure to remember that we
986 // legalized both of them.
987 AddLegalizedOperand(Op.getValue(0), Result);
988 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
989 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000990 case ISD::UNDEF: {
991 MVT::ValueType VT = Op.getValueType();
992 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000993 default: assert(0 && "This action is not supported yet!");
994 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000995 if (MVT::isInteger(VT))
996 Result = DAG.getConstant(0, VT);
997 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000998 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
999 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00001000 else
1001 assert(0 && "Unknown value type!");
1002 break;
Nate Begeman69d39432005-04-02 00:41:14 +00001003 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +00001004 break;
1005 }
1006 break;
1007 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001008
Chris Lattnere55d1712006-03-28 00:40:33 +00001009 case ISD::INTRINSIC_W_CHAIN:
1010 case ISD::INTRINSIC_WO_CHAIN:
1011 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001012 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001013 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1014 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001015 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001016
1017 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001018 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001019 TargetLowering::Custom) {
1020 Tmp3 = TLI.LowerOperation(Result, DAG);
1021 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001022 }
1023
1024 if (Result.Val->getNumValues() == 1) break;
1025
1026 // Must have return value and chain result.
1027 assert(Result.Val->getNumValues() == 2 &&
1028 "Cannot return more than two values!");
1029
1030 // Since loads produce two values, make sure to remember that we
1031 // legalized both of them.
1032 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1033 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1034 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +00001035 }
Chris Lattner435b4022005-11-29 06:21:05 +00001036
1037 case ISD::LOCATION:
1038 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1040
1041 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1042 case TargetLowering::Promote:
1043 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001044 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +00001045 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001046 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +00001047 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001048
Jim Laskeyc56315c2007-01-26 21:22:28 +00001049 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001050 const std::string &FName =
1051 cast<StringSDNode>(Node->getOperand(3))->getValue();
1052 const std::string &DirName =
1053 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001054 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001055
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001056 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +00001057 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +00001058 SDOperand LineOp = Node->getOperand(1);
1059 SDOperand ColOp = Node->getOperand(2);
1060
1061 if (useDEBUG_LOC) {
1062 Ops.push_back(LineOp); // line #
1063 Ops.push_back(ColOp); // col #
1064 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001065 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001066 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001067 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1068 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng263070e2008-02-01 02:05:57 +00001069 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001070 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001071 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1072 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001073 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001074 } else {
1075 Result = Tmp1; // chain
1076 }
Chris Lattner435b4022005-11-29 06:21:05 +00001077 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001078 }
Chris Lattner435b4022005-11-29 06:21:05 +00001079 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001080 if (Tmp1 != Node->getOperand(0) ||
1081 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001082 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001083 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001084 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1085 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1086 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1087 } else {
1088 // Otherwise promote them.
1089 Ops.push_back(PromoteOp(Node->getOperand(1)));
1090 Ops.push_back(PromoteOp(Node->getOperand(2)));
1091 }
Chris Lattner435b4022005-11-29 06:21:05 +00001092 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1093 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001094 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001095 }
1096 break;
1097 }
1098 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001099
1100 case ISD::DECLARE:
1101 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1102 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1103 default: assert(0 && "This action is not supported yet!");
1104 case TargetLowering::Legal:
1105 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1106 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1107 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1108 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1109 break;
Chris Lattner9824ffe2008-02-28 05:53:40 +00001110 case TargetLowering::Expand:
1111 Result = LegalizeOp(Node->getOperand(0));
1112 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001113 }
1114 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001115
1116 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001117 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001118 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001119 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001120 case TargetLowering::Legal:
1121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1123 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1124 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001126 break;
1127 }
1128 break;
1129
Jim Laskeyf9e54452007-01-26 14:34:52 +00001130 case ISD::LABEL:
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001131 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskeyf9e54452007-01-26 14:34:52 +00001132 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Legal:
1135 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1136 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001137 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1138 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskey7c462762005-12-16 22:45:29 +00001139 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001140 case TargetLowering::Expand:
1141 Result = LegalizeOp(Node->getOperand(0));
1142 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001143 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001144 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001145
Evan Cheng95cf6612008-03-08 00:58:38 +00001146 case ISD::PREFETCH:
1147 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1148 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1149 default: assert(0 && "This action is not supported yet!");
1150 case TargetLowering::Legal:
1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1152 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1153 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1154 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1155 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1156 break;
1157 case TargetLowering::Expand:
1158 // It's a noop.
1159 Result = LegalizeOp(Node->getOperand(0));
1160 break;
1161 }
1162 break;
1163
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001164 case ISD::MEMBARRIER: {
1165 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001166 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1167 default: assert(0 && "This action is not supported yet!");
1168 case TargetLowering::Legal: {
1169 SDOperand Ops[6];
1170 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands96658d02008-02-27 08:53:44 +00001171 for (int x = 1; x < 6; ++x) {
1172 Ops[x] = Node->getOperand(x);
1173 if (!isTypeLegal(Ops[x].getValueType()))
1174 Ops[x] = PromoteOp(Ops[x]);
1175 }
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1177 break;
1178 }
1179 case TargetLowering::Expand:
1180 //There is no libgcc call for this op
1181 Result = Node->getOperand(0); // Noop
1182 break;
1183 }
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001184 break;
1185 }
1186
Andrew Lenharth95528942008-02-21 06:45:13 +00001187 case ISD::ATOMIC_LCS:
1188 case ISD::ATOMIC_LAS:
1189 case ISD::ATOMIC_SWAP: {
1190 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1191 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1192 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharthd032c332008-03-01 21:52:34 +00001193 "Invalid Atomic node!");
Andrew Lenharth95528942008-02-21 06:45:13 +00001194 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharthd032c332008-03-01 21:52:34 +00001195 SDOperand Ops[4];
1196 for (int x = 0; x < num; ++x)
1197 Ops[x] = LegalizeOp(Node->getOperand(x));
1198 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1199
1200 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth95528942008-02-21 06:45:13 +00001201 default: assert(0 && "This action is not supported yet!");
Andrew Lenharthd032c332008-03-01 21:52:34 +00001202 case TargetLowering::Custom:
1203 Result = TLI.LowerOperation(Result, DAG);
1204 break;
1205 case TargetLowering::Legal:
Andrew Lenharth95528942008-02-21 06:45:13 +00001206 break;
1207 }
Andrew Lenharthd032c332008-03-01 21:52:34 +00001208 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1209 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1210 return Result.getValue(Op.ResNo);
Andrew Lenharth95528942008-02-21 06:45:13 +00001211 }
1212
Scott Michel9d09c5c2007-08-08 23:23:31 +00001213 case ISD::Constant: {
1214 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1215 unsigned opAction =
1216 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1217
Chris Lattnerdc750592005-01-07 07:47:09 +00001218 // We know we don't need to expand constants here, constants only have one
1219 // value and we check that it is fine above.
1220
Scott Michel9d09c5c2007-08-08 23:23:31 +00001221 if (opAction == TargetLowering::Custom) {
1222 Tmp1 = TLI.LowerOperation(Result, DAG);
1223 if (Tmp1.Val)
1224 Result = Tmp1;
1225 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001227 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001228 case ISD::ConstantFP: {
1229 // Spill FP immediates to the constant pool if the target cannot directly
1230 // codegen them. Targets often have some immediate values that can be
1231 // efficiently generated into an FP register without a load. We explicitly
1232 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001233 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1234
Chris Lattner758b0ac2006-01-29 06:26:56 +00001235 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1236 default: assert(0 && "This action is not supported yet!");
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001237 case TargetLowering::Legal:
1238 break;
Chris Lattner758b0ac2006-01-29 06:26:56 +00001239 case TargetLowering::Custom:
1240 Tmp3 = TLI.LowerOperation(Result, DAG);
1241 if (Tmp3.Val) {
1242 Result = Tmp3;
1243 break;
1244 }
1245 // FALLTHROUGH
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001246 case TargetLowering::Expand: {
1247 // Check to see if this FP immediate is already legal.
1248 bool isLegal = false;
1249 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1250 E = TLI.legal_fpimm_end(); I != E; ++I) {
1251 if (CFP->isExactlyValue(*I)) {
1252 isLegal = true;
1253 break;
1254 }
1255 }
1256 // If this is a legal constant, turn it into a TargetConstantFP node.
1257 if (isLegal)
1258 break;
Evan Cheng3766fc602006-12-12 22:19:28 +00001259 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001260 }
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001261 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001262 break;
1263 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001264 case ISD::TokenFactor:
1265 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001266 Tmp1 = LegalizeOp(Node->getOperand(0));
1267 Tmp2 = LegalizeOp(Node->getOperand(1));
1268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1269 } else if (Node->getNumOperands() == 3) {
1270 Tmp1 = LegalizeOp(Node->getOperand(0));
1271 Tmp2 = LegalizeOp(Node->getOperand(1));
1272 Tmp3 = LegalizeOp(Node->getOperand(2));
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001274 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001275 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001276 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001277 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1278 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001279 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001280 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001281 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001282
1283 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001284 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001285 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001286 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1287 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001288 // A call within a calling sequence must be legalized to something
1289 // other than the normal CALLSEQ_END. Violating this gets Legalize
1290 // into an infinite loop.
1291 assert ((!IsLegalizingCall ||
1292 Node->getOpcode() != ISD::CALL ||
1293 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1294 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001295
1296 // The number of incoming and outgoing values should match; unless the final
1297 // outgoing value is a flag.
1298 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1299 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1300 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1301 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001302 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001303
Chris Lattneraaa23d92006-05-16 22:53:20 +00001304 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001305 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001306 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendlingf359fed2007-11-13 00:44:25 +00001307 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1308 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001309 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001310 if (Op.ResNo == i)
1311 Tmp2 = Tmp1;
1312 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1313 }
1314 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001315 case ISD::EXTRACT_SUBREG: {
1316 Tmp1 = LegalizeOp(Node->getOperand(0));
1317 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1318 assert(idx && "Operand must be a constant");
1319 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1321 }
1322 break;
1323 case ISD::INSERT_SUBREG: {
1324 Tmp1 = LegalizeOp(Node->getOperand(0));
1325 Tmp2 = LegalizeOp(Node->getOperand(1));
1326 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1327 assert(idx && "Operand must be a constant");
1328 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1330 }
1331 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001332 case ISD::BUILD_VECTOR:
1333 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001334 default: assert(0 && "This action is not supported yet!");
1335 case TargetLowering::Custom:
1336 Tmp3 = TLI.LowerOperation(Result, DAG);
1337 if (Tmp3.Val) {
1338 Result = Tmp3;
1339 break;
1340 }
1341 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001342 case TargetLowering::Expand:
1343 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001344 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001345 }
Chris Lattner29b23012006-03-19 01:17:20 +00001346 break;
1347 case ISD::INSERT_VECTOR_ELT:
1348 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner29b23012006-03-19 01:17:20 +00001349 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman735ab3c2008-02-13 06:43:04 +00001350
1351 // The type of the value to insert may not be legal, even though the vector
1352 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1353 // here.
1354 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1355 default: assert(0 && "Cannot expand insert element operand");
1356 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1357 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1358 }
Chris Lattner29b23012006-03-19 01:17:20 +00001359 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1360
1361 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1362 Node->getValueType(0))) {
1363 default: assert(0 && "This action is not supported yet!");
1364 case TargetLowering::Legal:
1365 break;
1366 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001367 Tmp4 = TLI.LowerOperation(Result, DAG);
1368 if (Tmp4.Val) {
1369 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001370 break;
1371 }
1372 // FALLTHROUGH
1373 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001374 // If the insert index is a constant, codegen this as a scalar_to_vector,
1375 // then a shuffle that inserts it into the right position in the vector.
1376 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman735ab3c2008-02-13 06:43:04 +00001377 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1378 // match the element type of the vector being created.
1379 if (Tmp2.getValueType() ==
1380 MVT::getVectorElementType(Op.getValueType())) {
1381 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1382 Tmp1.getValueType(), Tmp2);
1383
1384 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1385 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1386 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1387
1388 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1389 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1390 // elt 0 of the RHS.
1391 SmallVector<SDOperand, 8> ShufOps;
1392 for (unsigned i = 0; i != NumElts; ++i) {
1393 if (i != InsertPos->getValue())
1394 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1395 else
1396 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1397 }
1398 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1399 &ShufOps[0], ShufOps.size());
1400
1401 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1402 Tmp1, ScVec, ShufMask);
1403 Result = LegalizeOp(Result);
1404 break;
Chris Lattner326870b2006-04-17 19:21:01 +00001405 }
Chris Lattner326870b2006-04-17 19:21:01 +00001406 }
1407
Chris Lattner29b23012006-03-19 01:17:20 +00001408 // If the target doesn't support this, we have to spill the input vector
1409 // to a temporary stack slot, update the element, then reload it. This is
1410 // badness. We could also load the value into a vector register (either
1411 // with a "move to register" or "extload into register" instruction, then
1412 // permute it into place, if the idx is a constant and if the idx is
1413 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001414 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman735ab3c2008-02-13 06:43:04 +00001415 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng78e3d562006-04-08 01:46:37 +00001416 MVT::ValueType IdxVT = Tmp3.getValueType();
1417 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001418 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001419
Dan Gohman54d3b5a2008-02-11 18:58:42 +00001420 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman2d489b52008-02-06 22:27:42 +00001421 int SPFI = StackPtrFI->getIndex();
1422
Evan Cheng168e45b2006-03-31 01:27:51 +00001423 // Store the vector.
Dan Gohman2d489b52008-02-06 22:27:42 +00001424 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001425 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00001426 SPFI);
Evan Cheng168e45b2006-03-31 01:27:51 +00001427
1428 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001429 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1430 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001431 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001432 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1433 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1434 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001435 // Store the scalar value.
Nate Begeman735ab3c2008-02-13 06:43:04 +00001436 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1437 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001438 // Load the updated vector.
Dan Gohman2d489b52008-02-06 22:27:42 +00001439 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001440 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner29b23012006-03-19 01:17:20 +00001441 break;
1442 }
1443 }
1444 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001445 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001446 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1447 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1448 break;
1449 }
1450
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001451 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1452 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1453 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1454 Node->getValueType(0))) {
1455 default: assert(0 && "This action is not supported yet!");
1456 case TargetLowering::Legal:
1457 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001458 case TargetLowering::Custom:
1459 Tmp3 = TLI.LowerOperation(Result, DAG);
1460 if (Tmp3.Val) {
1461 Result = Tmp3;
1462 break;
1463 }
1464 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001465 case TargetLowering::Expand:
1466 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001467 break;
1468 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001469 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001470 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1472 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1474
1475 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001476 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1477 default: assert(0 && "Unknown operation action!");
1478 case TargetLowering::Legal:
1479 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1480 "vector shuffle should not be created if not legal!");
1481 break;
1482 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001483 Tmp3 = TLI.LowerOperation(Result, DAG);
1484 if (Tmp3.Val) {
1485 Result = Tmp3;
1486 break;
1487 }
1488 // FALLTHROUGH
1489 case TargetLowering::Expand: {
1490 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001491 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001492 MVT::ValueType PtrVT = TLI.getPointerTy();
1493 SDOperand Mask = Node->getOperand(2);
1494 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001495 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001496 for (unsigned i = 0; i != NumElems; ++i) {
1497 SDOperand Arg = Mask.getOperand(i);
1498 if (Arg.getOpcode() == ISD::UNDEF) {
1499 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1500 } else {
1501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1502 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1503 if (Idx < NumElems)
1504 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1505 DAG.getConstant(Idx, PtrVT)));
1506 else
1507 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1508 DAG.getConstant(Idx - NumElems, PtrVT)));
1509 }
1510 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001511 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001512 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001513 }
Chris Lattner6be79822006-04-04 17:23:26 +00001514 case TargetLowering::Promote: {
1515 // Change base type to a different vector type.
1516 MVT::ValueType OVT = Node->getValueType(0);
1517 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1518
1519 // Cast the two input vectors.
1520 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1521 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1522
1523 // Convert the shuffle mask to the right # elements.
1524 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1525 assert(Tmp3.Val && "Shuffle not legal?");
1526 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1527 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1528 break;
1529 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001530 }
1531 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001532
1533 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001534 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001535 Tmp2 = LegalizeOp(Node->getOperand(1));
1536 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001537 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001538 break;
1539
Dan Gohmana8665142007-06-25 16:23:39 +00001540 case ISD::EXTRACT_SUBVECTOR:
1541 Tmp1 = Node->getOperand(0);
1542 Tmp2 = LegalizeOp(Node->getOperand(1));
1543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1544 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001545 break;
1546
Chris Lattner462505f2006-02-13 09:18:02 +00001547 case ISD::CALLSEQ_START: {
1548 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1549
1550 // Recursively Legalize all of the inputs of the call end that do not lead
1551 // to this call start. This ensures that any libcalls that need be inserted
1552 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001553 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001554 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001555 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1556 NodesLeadingTo);
1557 }
Chris Lattner462505f2006-02-13 09:18:02 +00001558
1559 // Now that we legalized all of the inputs (which may have inserted
1560 // libcalls) create the new CALLSEQ_START node.
1561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1562
1563 // Merge in the last call, to ensure that this call start after the last
1564 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001565 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001566 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1567 Tmp1 = LegalizeOp(Tmp1);
1568 }
Chris Lattner462505f2006-02-13 09:18:02 +00001569
1570 // Do not try to legalize the target-specific arguments (#1+).
1571 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001572 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001573 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001574 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001575 }
1576
1577 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001578 AddLegalizedOperand(Op.getValue(0), Result);
1579 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1580 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1581
Chris Lattner462505f2006-02-13 09:18:02 +00001582 // Now that the callseq_start and all of the non-call nodes above this call
1583 // sequence have been legalized, legalize the call itself. During this
1584 // process, no libcalls can/will be inserted, guaranteeing that no calls
1585 // can overlap.
1586 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1587 SDOperand InCallSEQ = LastCALLSEQ_END;
1588 // Note that we are selecting this call!
1589 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1590 IsLegalizingCall = true;
1591
1592 // Legalize the call, starting from the CALLSEQ_END.
1593 LegalizeOp(LastCALLSEQ_END);
1594 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1595 return Result;
1596 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001597 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001598 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1599 // will cause this node to be legalized as well as handling libcalls right.
1600 if (LastCALLSEQ_END.Val != Node) {
1601 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Roman Levensteina3ee1a32008-04-16 16:15:27 +00001602 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001603 assert(I != LegalizedNodes.end() &&
1604 "Legalizing the call start should have legalized this node!");
1605 return I->second;
1606 }
1607
1608 // Otherwise, the call start has been legalized and everything is going
1609 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001610 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001611 // Do not try to legalize the target-specific arguments (#1+), except for
1612 // an optional flag input.
1613 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1614 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001615 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001616 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001617 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001618 }
1619 } else {
1620 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1621 if (Tmp1 != Node->getOperand(0) ||
1622 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001623 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001624 Ops[0] = Tmp1;
1625 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001626 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001627 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001628 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001629 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001630 // This finishes up call legalization.
1631 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001632
1633 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1634 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1635 if (Node->getNumValues() == 2)
1636 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1637 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001638 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001639 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1642 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001644
Chris Lattnerd02b0542006-01-28 10:58:55 +00001645 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001646 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001647 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001648 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001649 case TargetLowering::Expand: {
1650 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1651 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1652 " not tell us which reg is the stack pointer!");
1653 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001654
1655 // Chain the dynamic stack allocation so that it doesn't modify the stack
1656 // pointer when other instructions are using the stack.
1657 Chain = DAG.getCALLSEQ_START(Chain,
1658 DAG.getConstant(0, TLI.getPointerTy()));
1659
Chris Lattner59b82f92006-01-15 08:54:32 +00001660 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001661 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1662 Chain = SP.getValue(1);
1663 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1664 unsigned StackAlign =
1665 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1666 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001667 SP = DAG.getNode(ISD::AND, VT, SP,
1668 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001669 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001670 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1671
1672 Tmp2 =
1673 DAG.getCALLSEQ_END(Chain,
1674 DAG.getConstant(0, TLI.getPointerTy()),
1675 DAG.getConstant(0, TLI.getPointerTy()),
1676 SDOperand());
1677
Chris Lattnerd02b0542006-01-28 10:58:55 +00001678 Tmp1 = LegalizeOp(Tmp1);
1679 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001680 break;
1681 }
1682 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001683 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1684 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001685 Tmp1 = LegalizeOp(Tmp3);
1686 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001687 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001688 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001689 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001690 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001691 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001692 // Since this op produce two values, make sure to remember that we
1693 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001694 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1695 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001696 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001697 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001698 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001699 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001700 bool Changed = false;
1701 // Legalize all of the operands of the inline asm, in case they are nodes
1702 // that need to be expanded or something. Note we skip the asm string and
1703 // all of the TargetConstant flags.
1704 SDOperand Op = LegalizeOp(Ops[0]);
1705 Changed = Op != Ops[0];
1706 Ops[0] = Op;
1707
1708 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1709 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1710 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1711 for (++i; NumVals; ++i, --NumVals) {
1712 SDOperand Op = LegalizeOp(Ops[i]);
1713 if (Op != Ops[i]) {
1714 Changed = true;
1715 Ops[i] = Op;
1716 }
1717 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001718 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001719
1720 if (HasInFlag) {
1721 Op = LegalizeOp(Ops.back());
1722 Changed |= Op != Ops.back();
1723 Ops.back() = Op;
1724 }
1725
1726 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001727 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001728
1729 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001730 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001731 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1732 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001733 }
Chris Lattner68a12142005-01-07 22:12:08 +00001734 case ISD::BR:
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001736 // Ensure that libcalls are emitted before a branch.
1737 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1738 Tmp1 = LegalizeOp(Tmp1);
1739 LastCALLSEQ_END = DAG.getEntryNode();
1740
Chris Lattnerd02b0542006-01-28 10:58:55 +00001741 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001742 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001743 case ISD::BRIND:
1744 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1745 // Ensure that libcalls are emitted before a branch.
1746 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1747 Tmp1 = LegalizeOp(Tmp1);
1748 LastCALLSEQ_END = DAG.getEntryNode();
1749
1750 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1751 default: assert(0 && "Indirect target must be legal type (pointer)!");
1752 case Legal:
1753 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1754 break;
1755 }
1756 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1757 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001758 case ISD::BR_JT:
1759 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1760 // Ensure that libcalls are emitted before a branch.
1761 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1762 Tmp1 = LegalizeOp(Tmp1);
1763 LastCALLSEQ_END = DAG.getEntryNode();
1764
1765 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1766 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1767
1768 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1769 default: assert(0 && "This action is not supported yet!");
1770 case TargetLowering::Legal: break;
1771 case TargetLowering::Custom:
1772 Tmp1 = TLI.LowerOperation(Result, DAG);
1773 if (Tmp1.Val) Result = Tmp1;
1774 break;
1775 case TargetLowering::Expand: {
1776 SDOperand Chain = Result.getOperand(0);
1777 SDOperand Table = Result.getOperand(1);
1778 SDOperand Index = Result.getOperand(2);
1779
1780 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001781 MachineFunction &MF = DAG.getMachineFunction();
1782 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001783 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1784 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001785
1786 SDOperand LD;
1787 switch (EntrySize) {
1788 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001789 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001790 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001791 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001792 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskey70323a82006-12-14 19:17:33 +00001793 }
1794
Evan Cheng797d56f2007-11-09 01:32:10 +00001795 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001796 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001797 // For PIC, the sequence is:
1798 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001799 // RelocBase can be JumpTable, GOT or some sort of global base.
1800 if (PTy != MVT::i32)
1801 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1802 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1803 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001804 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001805 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001806 }
1807 }
1808 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001809 case ISD::BRCOND:
1810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001811 // Ensure that libcalls are emitted before a return.
1812 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1813 Tmp1 = LegalizeOp(Tmp1);
1814 LastCALLSEQ_END = DAG.getEntryNode();
1815
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001816 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1817 case Expand: assert(0 && "It's impossible to expand bools");
1818 case Legal:
1819 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1820 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00001821 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001822 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001823
1824 // The top bits of the promoted condition are not necessarily zero, ensure
1825 // that the value is properly zero extended.
Dan Gohman1f372ed2008-02-25 21:11:39 +00001826 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00001827 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman1f372ed2008-02-25 21:11:39 +00001828 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerdb189382006-11-27 04:39:56 +00001829 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001830 break;
1831 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00001832 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001833
1834 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001835 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001836
1837 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1838 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001839 case TargetLowering::Legal: break;
1840 case TargetLowering::Custom:
1841 Tmp1 = TLI.LowerOperation(Result, DAG);
1842 if (Tmp1.Val) Result = Tmp1;
1843 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001844 case TargetLowering::Expand:
1845 // Expand brcond's setcc into its constituent parts and create a BR_CC
1846 // Node.
1847 if (Tmp2.getOpcode() == ISD::SETCC) {
1848 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1849 Tmp2.getOperand(0), Tmp2.getOperand(1),
1850 Node->getOperand(2));
1851 } else {
1852 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1853 DAG.getCondCode(ISD::SETNE), Tmp2,
1854 DAG.getConstant(0, Tmp2.getValueType()),
1855 Node->getOperand(2));
1856 }
1857 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001858 }
1859 break;
1860 case ISD::BR_CC:
1861 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001862 // Ensure that libcalls are emitted before a branch.
1863 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1864 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001865 Tmp2 = Node->getOperand(2); // LHS
1866 Tmp3 = Node->getOperand(3); // RHS
1867 Tmp4 = Node->getOperand(1); // CC
1868
1869 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001870 LastCALLSEQ_END = DAG.getEntryNode();
1871
Nate Begeman7e7f4392006-02-01 07:19:44 +00001872 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1873 // the LHS is a legal SETCC itself. In this case, we need to compare
1874 // the result against zero to select between true and false values.
1875 if (Tmp3.Val == 0) {
1876 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1877 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001878 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001879
1880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1881 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001882
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001883 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1884 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001885 case TargetLowering::Legal: break;
1886 case TargetLowering::Custom:
1887 Tmp4 = TLI.LowerOperation(Result, DAG);
1888 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001889 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001890 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001891 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001892 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001893 LoadSDNode *LD = cast<LoadSDNode>(Node);
1894 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1895 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001896
Evan Chenge71fe34d2006-10-09 20:57:25 +00001897 ISD::LoadExtType ExtType = LD->getExtensionType();
1898 if (ExtType == ISD::NON_EXTLOAD) {
1899 MVT::ValueType VT = Node->getValueType(0);
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1901 Tmp3 = Result.getValue(0);
1902 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001903
Evan Chenge71fe34d2006-10-09 20:57:25 +00001904 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1905 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001906 case TargetLowering::Legal:
1907 // If this is an unaligned load and the target doesn't support it,
1908 // expand it.
1909 if (!TLI.allowsUnalignedMemoryAccesses()) {
1910 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001911 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001912 if (LD->getAlignment() < ABIAlignment){
1913 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1914 TLI);
1915 Tmp3 = Result.getOperand(0);
1916 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001917 Tmp3 = LegalizeOp(Tmp3);
1918 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001919 }
1920 }
1921 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001922 case TargetLowering::Custom:
1923 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1924 if (Tmp1.Val) {
1925 Tmp3 = LegalizeOp(Tmp1);
1926 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001927 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001928 break;
1929 case TargetLowering::Promote: {
1930 // Only promote a load of vector type to another.
1931 assert(MVT::isVector(VT) && "Cannot promote this load!");
1932 // Change base type to a different vector type.
1933 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1934
1935 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001936 LD->getSrcValueOffset(),
1937 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001938 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1939 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001940 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001941 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001942 }
1943 // Since loads produce two values, make sure to remember that we
1944 // legalized both of them.
1945 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1946 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1947 return Op.ResNo ? Tmp4 : Tmp3;
1948 } else {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001949 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands95d46ef2008-01-23 20:39:46 +00001950 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1951 int SVOffset = LD->getSrcValueOffset();
1952 unsigned Alignment = LD->getAlignment();
1953 bool isVolatile = LD->isVolatile();
1954
1955 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1956 // Some targets pretend to have an i1 loading operation, and actually
1957 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1958 // bits are guaranteed to be zero; it helps the optimizers understand
1959 // that these bits are zero. It is also useful for EXTLOAD, since it
1960 // tells the optimizers that those bits are undefined. It would be
1961 // nice to have an effective generic way of getting these benefits...
1962 // Until such a way is found, don't insist on promoting i1 here.
1963 (SrcVT != MVT::i1 ||
1964 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1965 // Promote to a byte-sized load if not loading an integral number of
1966 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1967 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1968 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1969 SDOperand Ch;
1970
1971 // The extra bits are guaranteed to be zero, since we stored them that
1972 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1973
1974 ISD::LoadExtType NewExtType =
1975 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1976
1977 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1978 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1979 NVT, isVolatile, Alignment);
1980
1981 Ch = Result.getValue(1); // The chain.
1982
1983 if (ExtType == ISD::SEXTLOAD)
1984 // Having the top bits zero doesn't help when sign extending.
1985 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1986 Result, DAG.getValueType(SrcVT));
1987 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1988 // All the top bits are guaranteed to be zero - inform the optimizers.
1989 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1990 DAG.getValueType(SrcVT));
1991
1992 Tmp1 = LegalizeOp(Result);
1993 Tmp2 = LegalizeOp(Ch);
1994 } else if (SrcWidth & (SrcWidth - 1)) {
1995 // If not loading a power-of-2 number of bits, expand as two loads.
1996 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1997 "Unsupported extload!");
1998 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1999 assert(RoundWidth < SrcWidth);
2000 unsigned ExtraWidth = SrcWidth - RoundWidth;
2001 assert(ExtraWidth < RoundWidth);
2002 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2003 "Load size not an integral number of bytes!");
2004 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2005 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2006 SDOperand Lo, Hi, Ch;
2007 unsigned IncrementSize;
2008
2009 if (TLI.isLittleEndian()) {
2010 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2011 // Load the bottom RoundWidth bits.
2012 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2013 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2014 Alignment);
2015
2016 // Load the remaining ExtraWidth bits.
2017 IncrementSize = RoundWidth / 8;
2018 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2019 DAG.getIntPtrConstant(IncrementSize));
2020 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2021 LD->getSrcValue(), SVOffset + IncrementSize,
2022 ExtraVT, isVolatile,
2023 MinAlign(Alignment, IncrementSize));
2024
2025 // Build a factor node to remember that this load is independent of the
2026 // other one.
2027 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2028 Hi.getValue(1));
2029
2030 // Move the top bits to the right place.
2031 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2032 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2033
2034 // Join the hi and lo parts.
2035 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002036 } else {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002037 // Big endian - avoid unaligned loads.
2038 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2039 // Load the top RoundWidth bits.
2040 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2041 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2042 Alignment);
2043
2044 // Load the remaining ExtraWidth bits.
2045 IncrementSize = RoundWidth / 8;
2046 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2047 DAG.getIntPtrConstant(IncrementSize));
2048 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2049 LD->getSrcValue(), SVOffset + IncrementSize,
2050 ExtraVT, isVolatile,
2051 MinAlign(Alignment, IncrementSize));
2052
2053 // Build a factor node to remember that this load is independent of the
2054 // other one.
2055 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2056 Hi.getValue(1));
2057
2058 // Move the top bits to the right place.
2059 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2060 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2061
2062 // Join the hi and lo parts.
2063 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2064 }
2065
2066 Tmp1 = LegalizeOp(Result);
2067 Tmp2 = LegalizeOp(Ch);
2068 } else {
2069 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2070 default: assert(0 && "This action is not supported yet!");
2071 case TargetLowering::Custom:
2072 isCustom = true;
2073 // FALLTHROUGH
2074 case TargetLowering::Legal:
2075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2076 Tmp1 = Result.getValue(0);
2077 Tmp2 = Result.getValue(1);
2078
2079 if (isCustom) {
2080 Tmp3 = TLI.LowerOperation(Result, DAG);
2081 if (Tmp3.Val) {
2082 Tmp1 = LegalizeOp(Tmp3);
2083 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2084 }
2085 } else {
2086 // If this is an unaligned load and the target doesn't support it,
2087 // expand it.
2088 if (!TLI.allowsUnalignedMemoryAccesses()) {
2089 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002090 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands95d46ef2008-01-23 20:39:46 +00002091 if (LD->getAlignment() < ABIAlignment){
2092 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2093 TLI);
2094 Tmp1 = Result.getOperand(0);
2095 Tmp2 = Result.getOperand(1);
2096 Tmp1 = LegalizeOp(Tmp1);
2097 Tmp2 = LegalizeOp(Tmp2);
2098 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002099 }
2100 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002101 break;
2102 case TargetLowering::Expand:
2103 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2104 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2105 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2106 LD->getSrcValueOffset(),
2107 LD->isVolatile(), LD->getAlignment());
2108 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2109 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2110 Tmp2 = LegalizeOp(Load.getValue(1));
2111 break;
2112 }
2113 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2114 // Turn the unsupported load into an EXTLOAD followed by an explicit
2115 // zero/sign extend inreg.
2116 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2117 Tmp1, Tmp2, LD->getSrcValue(),
2118 LD->getSrcValueOffset(), SrcVT,
2119 LD->isVolatile(), LD->getAlignment());
2120 SDOperand ValRes;
2121 if (ExtType == ISD::SEXTLOAD)
2122 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2123 Result, DAG.getValueType(SrcVT));
2124 else
2125 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2126 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2127 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002128 break;
2129 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002130 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002131
Evan Chenge71fe34d2006-10-09 20:57:25 +00002132 // Since loads produce two values, make sure to remember that we legalized
2133 // both of them.
2134 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2135 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2136 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002137 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002138 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002139 case ISD::EXTRACT_ELEMENT: {
2140 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2141 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002142 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00002143 case Legal:
2144 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2145 // 1 -> Hi
2146 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2147 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2148 TLI.getShiftAmountTy()));
2149 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2150 } else {
2151 // 0 -> Lo
2152 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2153 Node->getOperand(0));
2154 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002155 break;
2156 case Expand:
2157 // Get both the low and high parts.
2158 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2159 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2160 Result = Tmp2; // 1 -> Hi
2161 else
2162 Result = Tmp1; // 0 -> Lo
2163 break;
2164 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002165 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00002166 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002167
2168 case ISD::CopyToReg:
2169 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00002170
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002171 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00002172 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00002173 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00002174 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002175 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002177 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002178 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002179 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002180 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002181 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2182 Tmp3);
2183 } else {
2184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002185 }
2186
2187 // Since this produces two values, make sure to remember that we legalized
2188 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00002190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002191 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00002192 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002193 break;
2194
2195 case ISD::RET:
2196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002197
2198 // Ensure that libcalls are emitted before a return.
2199 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2200 Tmp1 = LegalizeOp(Tmp1);
2201 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002202
Chris Lattnerdc750592005-01-07 07:47:09 +00002203 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00002204 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00002205 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00002206 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002207 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00002208 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00002209 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00002210 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002211 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00002212 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002213 SDOperand Lo, Hi;
2214 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00002215
2216 // Big endian systems want the hi reg first.
Duncan Sands7377f5f2008-02-11 10:37:04 +00002217 if (TLI.isBigEndian())
Chris Lattner13780ac2007-03-06 20:01:06 +00002218 std::swap(Lo, Hi);
2219
Evan Cheng3432ab92006-12-11 19:27:14 +00002220 if (Hi.Val)
2221 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2222 else
2223 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00002224 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002225 } else {
2226 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002227 int InIx = Tmp2.ResNo;
2228 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2229 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002230
Dan Gohmana8665142007-06-25 16:23:39 +00002231 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002232 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002233 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002234 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002235 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002236 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002237 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002238 } else if (NumElems == 1) {
2239 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002240 Tmp2 = ScalarizeVectorOp(Tmp2);
2241 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002243
2244 // FIXME: Returns of gcc generic vectors smaller than a legal type
2245 // should be returned in integer registers!
2246
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002247 // The scalarized value type may not be legal, e.g. it might require
2248 // promotion or expansion. Relegalize the return.
2249 Result = LegalizeOp(Result);
2250 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002251 // FIXME: Returns of gcc generic vectors larger than a legal vector
2252 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002253 SDOperand Lo, Hi;
2254 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002255 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002256 Result = LegalizeOp(Result);
2257 }
2258 }
Misha Brukman835702a2005-04-21 22:36:52 +00002259 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002260 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002261 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002263 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002264 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002265 }
2266 break;
2267 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002268 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002269 break;
2270 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00002271 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002272 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002273 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002274 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2275 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002276 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002277 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002278 break;
2279 case Expand: {
2280 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00002281 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002282 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002283 ExpandOp(Node->getOperand(i), Lo, Hi);
2284 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002285 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00002286 if (Hi.Val) {
2287 NewValues.push_back(Hi);
2288 NewValues.push_back(Node->getOperand(i+1));
2289 }
Misha Brukman835702a2005-04-21 22:36:52 +00002290 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002291 }
2292 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002293 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002294 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002295
2296 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002297 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002298 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002299 Result = DAG.getNode(ISD::RET, MVT::Other,
2300 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002301 break;
2302 }
2303 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002304
Chris Lattner4d1ea712006-01-29 21:02:23 +00002305 if (Result.getOpcode() == ISD::RET) {
2306 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2307 default: assert(0 && "This action is not supported yet!");
2308 case TargetLowering::Legal: break;
2309 case TargetLowering::Custom:
2310 Tmp1 = TLI.LowerOperation(Result, DAG);
2311 if (Tmp1.Val) Result = Tmp1;
2312 break;
2313 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002314 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002315 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002316 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002317 StoreSDNode *ST = cast<StoreSDNode>(Node);
2318 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2319 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002320 int SVOffset = ST->getSrcValueOffset();
2321 unsigned Alignment = ST->getAlignment();
2322 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002323
Evan Chengab51cf22006-10-13 21:14:26 +00002324 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002325 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2326 // FIXME: We shouldn't do this for TargetConstantFP's.
2327 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2328 // to phase ordering between legalized code and the dag combiner. This
2329 // probably means that we need to integrate dag combiner and legalizer
2330 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002331 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002332 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002333 if (CFP->getValueType(0) == MVT::f32 &&
2334 getTypeAction(MVT::i32) == Legal) {
Dan Gohman10f7d852008-03-11 00:11:06 +00002335 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2336 convertToAPInt().zextOrTrunc(32),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002337 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002338 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2339 SVOffset, isVolatile, Alignment);
2340 break;
2341 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002342 // If this target supports 64-bit registers, do a single 64-bit store.
2343 if (getTypeAction(MVT::i64) == Legal) {
2344 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman10f7d852008-03-11 00:11:06 +00002345 zextOrTrunc(64), MVT::i64);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002346 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2347 SVOffset, isVolatile, Alignment);
2348 break;
2349 } else if (getTypeAction(MVT::i32) == Legal) {
2350 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2351 // stores. If the target supports neither 32- nor 64-bits, this
2352 // xform is certainly not worth it.
Dan Gohman10f7d852008-03-11 00:11:06 +00002353 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
2354 SDOperand Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2355 SDOperand Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00002356 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002357
2358 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2359 SVOffset, isVolatile, Alignment);
2360 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002361 DAG.getIntPtrConstant(4));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002362 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002363 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002364
2365 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2366 break;
2367 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002368 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002369 }
2370
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002371 switch (getTypeAction(ST->getMemoryVT())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002372 case Legal: {
2373 Tmp3 = LegalizeOp(ST->getValue());
2374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2375 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002376
Evan Chengab51cf22006-10-13 21:14:26 +00002377 MVT::ValueType VT = Tmp3.getValueType();
2378 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2379 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002380 case TargetLowering::Legal:
2381 // If this is an unaligned store and the target doesn't support it,
2382 // expand it.
2383 if (!TLI.allowsUnalignedMemoryAccesses()) {
2384 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002385 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002386 if (ST->getAlignment() < ABIAlignment)
2387 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2388 TLI);
2389 }
2390 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002391 case TargetLowering::Custom:
2392 Tmp1 = TLI.LowerOperation(Result, DAG);
2393 if (Tmp1.Val) Result = Tmp1;
2394 break;
2395 case TargetLowering::Promote:
2396 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2397 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2398 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2399 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002400 ST->getSrcValue(), SVOffset, isVolatile,
2401 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002402 break;
2403 }
2404 break;
2405 }
2406 case Promote:
2407 // Truncate the value and store the result.
2408 Tmp3 = PromoteOp(ST->getValue());
2409 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002410 SVOffset, ST->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00002411 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002412 break;
2413
2414 case Expand:
2415 unsigned IncrementSize = 0;
2416 SDOperand Lo, Hi;
2417
2418 // If this is a vector type, then we have to calculate the increment as
2419 // the product of the element size in bytes, and the number of elements
2420 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002421 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002422 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002423 int InIx = ST->getValue().ResNo;
Chris Lattner72733e52008-01-17 07:00:52 +00002424 MVT::ValueType InVT = InVal->getValueType(InIx);
2425 unsigned NumElems = MVT::getVectorNumElements(InVT);
2426 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Chengab51cf22006-10-13 21:14:26 +00002427
Dan Gohmana8665142007-06-25 16:23:39 +00002428 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002429 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002430 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002431 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002432 // Turn this into a normal store of the vector type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002433 Tmp3 = LegalizeOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002434 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002435 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002436 Result = LegalizeOp(Result);
2437 break;
2438 } else if (NumElems == 1) {
2439 // Turn this into a normal store of the scalar type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002440 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002441 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002442 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002443 // The scalarized value type may not be legal, e.g. it might require
2444 // promotion or expansion. Relegalize the scalar store.
2445 Result = LegalizeOp(Result);
2446 break;
2447 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002448 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00002449 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2450 MVT::getSizeInBits(EVT)/8;
Evan Chengab51cf22006-10-13 21:14:26 +00002451 }
2452 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002453 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002454 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002455
Duncan Sands7377f5f2008-02-11 10:37:04 +00002456 if (TLI.isBigEndian())
Evan Chengab51cf22006-10-13 21:14:26 +00002457 std::swap(Lo, Hi);
2458 }
2459
2460 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002461 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002462
2463 if (Hi.Val == NULL) {
2464 // Must be int <-> float one-to-one expansion.
2465 Result = Lo;
2466 break;
2467 }
2468
Evan Chengab51cf22006-10-13 21:14:26 +00002469 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002470 DAG.getIntPtrConstant(IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00002471 assert(isTypeLegal(Tmp2.getValueType()) &&
2472 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002473 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002474 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002475 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002476 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002477 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2478 break;
2479 }
2480 } else {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002481 switch (getTypeAction(ST->getValue().getValueType())) {
2482 case Legal:
2483 Tmp3 = LegalizeOp(ST->getValue());
2484 break;
2485 case Promote:
2486 // We can promote the value, the truncstore will still take care of it.
2487 Tmp3 = PromoteOp(ST->getValue());
2488 break;
2489 case Expand:
2490 // Just store the low part. This may become a non-trunc store, so make
2491 // sure to use getTruncStore, not UpdateNodeOperands below.
2492 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2493 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2494 SVOffset, MVT::i8, isVolatile, Alignment);
2495 }
Evan Chengab51cf22006-10-13 21:14:26 +00002496
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002497 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands88de26c2008-01-22 07:17:34 +00002498 unsigned StWidth = MVT::getSizeInBits(StVT);
2499
2500 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2501 // Promote to a byte-sized store with upper bits zero if not
2502 // storing an integral number of bytes. For example, promote
2503 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2504 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2505 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2506 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2507 SVOffset, NVT, isVolatile, Alignment);
2508 } else if (StWidth & (StWidth - 1)) {
2509 // If not storing a power-of-2 number of bits, expand as two stores.
2510 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2511 "Unsupported truncstore!");
2512 unsigned RoundWidth = 1 << Log2_32(StWidth);
2513 assert(RoundWidth < StWidth);
2514 unsigned ExtraWidth = StWidth - RoundWidth;
2515 assert(ExtraWidth < RoundWidth);
2516 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2517 "Store size not an integral number of bytes!");
2518 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2519 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2520 SDOperand Lo, Hi;
2521 unsigned IncrementSize;
2522
2523 if (TLI.isLittleEndian()) {
2524 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2525 // Store the bottom RoundWidth bits.
2526 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2527 SVOffset, RoundVT,
2528 isVolatile, Alignment);
2529
2530 // Store the remaining ExtraWidth bits.
2531 IncrementSize = RoundWidth / 8;
2532 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2533 DAG.getIntPtrConstant(IncrementSize));
2534 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2535 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2536 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2537 SVOffset + IncrementSize, ExtraVT, isVolatile,
2538 MinAlign(Alignment, IncrementSize));
2539 } else {
2540 // Big endian - avoid unaligned stores.
2541 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2542 // Store the top RoundWidth bits.
2543 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2544 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2545 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2546 RoundVT, isVolatile, Alignment);
2547
2548 // Store the remaining ExtraWidth bits.
2549 IncrementSize = RoundWidth / 8;
2550 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2551 DAG.getIntPtrConstant(IncrementSize));
2552 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2553 SVOffset + IncrementSize, ExtraVT, isVolatile,
2554 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002555 }
Duncan Sands88de26c2008-01-22 07:17:34 +00002556
2557 // The order of the stores doesn't matter.
2558 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2559 } else {
2560 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2561 Tmp2 != ST->getBasePtr())
2562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2563 ST->getOffset());
2564
2565 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2566 default: assert(0 && "This action is not supported yet!");
2567 case TargetLowering::Legal:
2568 // If this is an unaligned store and the target doesn't support it,
2569 // expand it.
2570 if (!TLI.allowsUnalignedMemoryAccesses()) {
2571 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002572 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands88de26c2008-01-22 07:17:34 +00002573 if (ST->getAlignment() < ABIAlignment)
2574 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2575 TLI);
2576 }
2577 break;
2578 case TargetLowering::Custom:
2579 Result = TLI.LowerOperation(Result, DAG);
2580 break;
2581 case Expand:
2582 // TRUNCSTORE:i16 i32 -> STORE i16
2583 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2584 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2585 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2586 isVolatile, Alignment);
2587 break;
2588 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00002589 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002590 }
2591 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002592 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002593 case ISD::PCMARKER:
2594 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002596 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002597 case ISD::STACKSAVE:
2598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002599 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2600 Tmp1 = Result.getValue(0);
2601 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002602
Chris Lattnerb3266452006-01-13 02:50:02 +00002603 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2604 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002605 case TargetLowering::Legal: break;
2606 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002607 Tmp3 = TLI.LowerOperation(Result, DAG);
2608 if (Tmp3.Val) {
2609 Tmp1 = LegalizeOp(Tmp3);
2610 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002611 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002612 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002613 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002614 // Expand to CopyFromReg if the target set
2615 // StackPointerRegisterToSaveRestore.
2616 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002617 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002618 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002619 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002620 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002621 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2622 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002623 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002624 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002625 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002626
2627 // Since stacksave produce two values, make sure to remember that we
2628 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002629 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2630 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2631 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002632
Chris Lattnerb3266452006-01-13 02:50:02 +00002633 case ISD::STACKRESTORE:
2634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002637
2638 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2639 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002640 case TargetLowering::Legal: break;
2641 case TargetLowering::Custom:
2642 Tmp1 = TLI.LowerOperation(Result, DAG);
2643 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002644 break;
2645 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002646 // Expand to CopyToReg if the target set
2647 // StackPointerRegisterToSaveRestore.
2648 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2649 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2650 } else {
2651 Result = Tmp1;
2652 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002653 break;
2654 }
2655 break;
2656
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002657 case ISD::READCYCLECOUNTER:
2658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002659 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002660 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2661 Node->getValueType(0))) {
2662 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002663 case TargetLowering::Legal:
2664 Tmp1 = Result.getValue(0);
2665 Tmp2 = Result.getValue(1);
2666 break;
Evan Cheng69739932006-11-29 08:26:18 +00002667 case TargetLowering::Custom:
2668 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002669 Tmp1 = LegalizeOp(Result.getValue(0));
2670 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002671 break;
2672 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002673
2674 // Since rdcc produce two values, make sure to remember that we legalized
2675 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002676 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2677 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002678 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002679
Chris Lattner39c67442005-01-14 22:08:15 +00002680 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002681 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2682 case Expand: assert(0 && "It's impossible to expand bools");
2683 case Legal:
2684 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2685 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002686 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002687 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002688 // Make sure the condition is either zero or one.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002689 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002690 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002691 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattner3abb6362006-11-28 01:03:30 +00002692 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002693 break;
2694 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002695 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002696 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002697 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002698
Chris Lattnerd02b0542006-01-28 10:58:55 +00002699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002700
Nate Begeman987121a2005-08-23 04:29:48 +00002701 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002702 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002703 case TargetLowering::Legal: break;
2704 case TargetLowering::Custom: {
2705 Tmp1 = TLI.LowerOperation(Result, DAG);
2706 if (Tmp1.Val) Result = Tmp1;
2707 break;
2708 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002709 case TargetLowering::Expand:
2710 if (Tmp1.getOpcode() == ISD::SETCC) {
2711 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2712 Tmp2, Tmp3,
2713 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2714 } else {
2715 Result = DAG.getSelectCC(Tmp1,
2716 DAG.getConstant(0, Tmp1.getValueType()),
2717 Tmp2, Tmp3, ISD::SETNE);
2718 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002719 break;
2720 case TargetLowering::Promote: {
2721 MVT::ValueType NVT =
2722 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2723 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002724 if (MVT::isVector(Tmp2.getValueType())) {
2725 ExtOp = ISD::BIT_CONVERT;
2726 TruncOp = ISD::BIT_CONVERT;
2727 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002728 ExtOp = ISD::ANY_EXTEND;
2729 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002730 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002731 ExtOp = ISD::FP_EXTEND;
2732 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002733 }
2734 // Promote each of the values to the new type.
2735 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2736 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2737 // Perform the larger operation, then round down.
2738 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner72733e52008-01-17 07:00:52 +00002739 if (TruncOp != ISD::FP_ROUND)
2740 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2741 else
2742 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2743 DAG.getIntPtrConstant(0));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002744 break;
2745 }
2746 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002747 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002748 case ISD::SELECT_CC: {
2749 Tmp1 = Node->getOperand(0); // LHS
2750 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002751 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2752 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002753 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002754
Nate Begeman7e7f4392006-02-01 07:19:44 +00002755 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2756
2757 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2758 // the LHS is a legal SETCC itself. In this case, we need to compare
2759 // the result against zero to select between true and false values.
2760 if (Tmp2.Val == 0) {
2761 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2762 CC = DAG.getCondCode(ISD::SETNE);
2763 }
2764 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2765
2766 // Everything is legal, see if we should expand this op or something.
2767 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2768 default: assert(0 && "This action is not supported yet!");
2769 case TargetLowering::Legal: break;
2770 case TargetLowering::Custom:
2771 Tmp1 = TLI.LowerOperation(Result, DAG);
2772 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002773 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002774 }
2775 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002776 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002777 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002778 Tmp1 = Node->getOperand(0);
2779 Tmp2 = Node->getOperand(1);
2780 Tmp3 = Node->getOperand(2);
2781 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2782
2783 // If we had to Expand the SetCC operands into a SELECT node, then it may
2784 // not always be possible to return a true LHS & RHS. In this case, just
2785 // return the value we legalized, returned in the LHS
2786 if (Tmp2.Val == 0) {
2787 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002788 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002789 }
Nate Begeman987121a2005-08-23 04:29:48 +00002790
Chris Lattnerf263a232006-01-30 22:43:50 +00002791 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002792 default: assert(0 && "Cannot handle this action for SETCC yet!");
2793 case TargetLowering::Custom:
2794 isCustom = true;
2795 // FALLTHROUGH.
2796 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002797 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002798 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002799 Tmp4 = TLI.LowerOperation(Result, DAG);
2800 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002801 }
Nate Begeman987121a2005-08-23 04:29:48 +00002802 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002803 case TargetLowering::Promote: {
2804 // First step, figure out the appropriate operation to use.
2805 // Allow SETCC to not be supported for all legal data types
2806 // Mostly this targets FP
2807 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002808 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002809
2810 // Scan for the appropriate larger type to use.
2811 while (1) {
2812 NewInTy = (MVT::ValueType)(NewInTy+1);
2813
2814 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2815 "Fell off of the edge of the integer world");
2816 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2817 "Fell off of the edge of the floating point world");
2818
2819 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002820 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002821 break;
2822 }
2823 if (MVT::isInteger(NewInTy))
2824 assert(0 && "Cannot promote Legal Integer SETCC yet");
2825 else {
2826 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2827 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2828 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002829 Tmp1 = LegalizeOp(Tmp1);
2830 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002832 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002833 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002834 }
Nate Begeman987121a2005-08-23 04:29:48 +00002835 case TargetLowering::Expand:
2836 // Expand a setcc node into a select_cc of the same condition, lhs, and
2837 // rhs that selects between const 1 (true) and const 0 (false).
2838 MVT::ValueType VT = Node->getValueType(0);
2839 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2840 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002841 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002842 break;
2843 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002844 break;
Chris Lattner5385db52005-05-09 20:23:03 +00002845
Chris Lattner4157c412005-04-02 04:00:59 +00002846 case ISD::SHL_PARTS:
2847 case ISD::SRA_PARTS:
2848 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002849 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002850 bool Changed = false;
2851 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2852 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2853 Changed |= Ops.back() != Node->getOperand(i);
2854 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002855 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002856 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002857
Evan Cheng870e4f82006-01-09 18:31:59 +00002858 switch (TLI.getOperationAction(Node->getOpcode(),
2859 Node->getValueType(0))) {
2860 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002861 case TargetLowering::Legal: break;
2862 case TargetLowering::Custom:
2863 Tmp1 = TLI.LowerOperation(Result, DAG);
2864 if (Tmp1.Val) {
2865 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002866 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002867 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002868 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2869 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002870 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002871 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002872 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002873 return RetVal;
2874 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002875 break;
2876 }
2877
Chris Lattner13fe99c2005-04-02 05:00:07 +00002878 // Since these produce multiple values, make sure to remember that we
2879 // legalized all of them.
2880 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2881 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2882 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002883 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002884
2885 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002886 case ISD::ADD:
2887 case ISD::SUB:
2888 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002889 case ISD::MULHS:
2890 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002891 case ISD::UDIV:
2892 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002893 case ISD::AND:
2894 case ISD::OR:
2895 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002896 case ISD::SHL:
2897 case ISD::SRL:
2898 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002899 case ISD::FADD:
2900 case ISD::FSUB:
2901 case ISD::FMUL:
2902 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002903 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002904 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002905 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2906 case Expand: assert(0 && "Not possible");
2907 case Legal:
2908 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2909 break;
2910 case Promote:
2911 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2912 break;
2913 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002914
2915 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002916
Andrew Lenharth72594262005-12-24 23:42:32 +00002917 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002918 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002919 case TargetLowering::Legal: break;
2920 case TargetLowering::Custom:
2921 Tmp1 = TLI.LowerOperation(Result, DAG);
2922 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002923 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002924 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00002925 MVT::ValueType VT = Op.getValueType();
2926
2927 // See if multiply or divide can be lowered using two-result operations.
2928 SDVTList VTs = DAG.getVTList(VT, VT);
2929 if (Node->getOpcode() == ISD::MUL) {
2930 // We just need the low half of the multiply; try both the signed
2931 // and unsigned forms. If the target supports both SMUL_LOHI and
2932 // UMUL_LOHI, form a preference by checking which forms of plain
2933 // MULH it supports.
2934 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2935 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2936 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2937 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2938 unsigned OpToUse = 0;
2939 if (HasSMUL_LOHI && !HasMULHS) {
2940 OpToUse = ISD::SMUL_LOHI;
2941 } else if (HasUMUL_LOHI && !HasMULHU) {
2942 OpToUse = ISD::UMUL_LOHI;
2943 } else if (HasSMUL_LOHI) {
2944 OpToUse = ISD::SMUL_LOHI;
2945 } else if (HasUMUL_LOHI) {
2946 OpToUse = ISD::UMUL_LOHI;
2947 }
2948 if (OpToUse) {
2949 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2950 break;
2951 }
2952 }
2953 if (Node->getOpcode() == ISD::MULHS &&
2954 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2955 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2956 break;
2957 }
2958 if (Node->getOpcode() == ISD::MULHU &&
2959 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2960 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2961 break;
2962 }
2963 if (Node->getOpcode() == ISD::SDIV &&
2964 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2965 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2966 break;
2967 }
2968 if (Node->getOpcode() == ISD::UDIV &&
2969 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2970 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2971 break;
2972 }
2973
Dan Gohman2a7de412007-10-11 23:57:53 +00002974 // Check to see if we have a libcall for this operator.
2975 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2976 bool isSigned = false;
2977 switch (Node->getOpcode()) {
2978 case ISD::UDIV:
2979 case ISD::SDIV:
2980 if (VT == MVT::i32) {
2981 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00002982 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00002983 isSigned = Node->getOpcode() == ISD::SDIV;
2984 }
2985 break;
2986 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00002987 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2988 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00002989 break;
2990 default: break;
2991 }
2992 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2993 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00002994 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002995 break;
2996 }
2997
Chris Lattner87f08092006-04-02 03:57:31 +00002998 assert(MVT::isVector(Node->getValueType(0)) &&
2999 "Cannot expand this binary operator!");
3000 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00003001 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00003002 break;
3003 }
Evan Cheng119266e2006-04-12 21:20:24 +00003004 case TargetLowering::Promote: {
3005 switch (Node->getOpcode()) {
3006 default: assert(0 && "Do not know how to promote this BinOp!");
3007 case ISD::AND:
3008 case ISD::OR:
3009 case ISD::XOR: {
3010 MVT::ValueType OVT = Node->getValueType(0);
3011 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3012 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3013 // Bit convert each of the values to the new type.
3014 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3015 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3016 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3017 // Bit convert the result back the original type.
3018 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3019 break;
3020 }
3021 }
3022 }
Andrew Lenharth72594262005-12-24 23:42:32 +00003023 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003024 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003025
Dan Gohman12334ac2007-10-05 14:17:22 +00003026 case ISD::SMUL_LOHI:
3027 case ISD::UMUL_LOHI:
3028 case ISD::SDIVREM:
3029 case ISD::UDIVREM:
3030 // These nodes will only be produced by target-specific lowering, so
3031 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00003032 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00003033 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00003034
3035 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3036 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3037 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00003038 break;
3039
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003040 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3041 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3042 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3043 case Expand: assert(0 && "Not possible");
3044 case Legal:
3045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3046 break;
3047 case Promote:
3048 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3049 break;
3050 }
3051
3052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3053
3054 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3055 default: assert(0 && "Operation not supported");
3056 case TargetLowering::Custom:
3057 Tmp1 = TLI.LowerOperation(Result, DAG);
3058 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00003059 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003060 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00003061 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00003062 // If this target supports fabs/fneg natively and select is cheap,
3063 // do this efficiently.
3064 if (!TLI.isSelectExpensive() &&
3065 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3066 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00003067 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00003068 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00003069 // Get the sign bit of the RHS.
3070 MVT::ValueType IVT =
3071 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3072 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00003073 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner994d8e62006-03-13 06:08:38 +00003074 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3075 // Get the absolute value of the result.
3076 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3077 // Select between the nabs and abs value based on the sign bit of
3078 // the input.
3079 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3080 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3081 AbsVal),
3082 AbsVal);
3083 Result = LegalizeOp(Result);
3084 break;
3085 }
3086
3087 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00003088 MVT::ValueType NVT =
3089 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3090 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3091 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3092 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003093 break;
3094 }
Evan Cheng003feb02007-01-04 21:56:39 +00003095 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003096 break;
3097
Nate Begeman5965bd12006-02-17 05:43:56 +00003098 case ISD::ADDC:
3099 case ISD::SUBC:
3100 Tmp1 = LegalizeOp(Node->getOperand(0));
3101 Tmp2 = LegalizeOp(Node->getOperand(1));
3102 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3103 // Since this produces two values, make sure to remember that we legalized
3104 // both of them.
3105 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3106 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3107 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00003108
Nate Begeman5965bd12006-02-17 05:43:56 +00003109 case ISD::ADDE:
3110 case ISD::SUBE:
3111 Tmp1 = LegalizeOp(Node->getOperand(0));
3112 Tmp2 = LegalizeOp(Node->getOperand(1));
3113 Tmp3 = LegalizeOp(Node->getOperand(2));
3114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3115 // Since this produces two values, make sure to remember that we legalized
3116 // both of them.
3117 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3118 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3119 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00003120
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003121 case ISD::BUILD_PAIR: {
3122 MVT::ValueType PairTy = Node->getValueType(0);
3123 // TODO: handle the case where the Lo and Hi operands are not of legal type
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3126 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003127 case TargetLowering::Promote:
3128 case TargetLowering::Custom:
3129 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003130 case TargetLowering::Legal:
3131 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3132 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3133 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003134 case TargetLowering::Expand:
3135 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3136 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3137 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3138 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3139 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003140 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003141 break;
3142 }
3143 break;
3144 }
3145
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003146 case ISD::UREM:
3147 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003148 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003149 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3150 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003151
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003152 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003153 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3154 case TargetLowering::Custom:
3155 isCustom = true;
3156 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003157 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003159 if (isCustom) {
3160 Tmp1 = TLI.LowerOperation(Result, DAG);
3161 if (Tmp1.Val) Result = Tmp1;
3162 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003163 break;
Dan Gohmana1603612007-10-08 18:33:35 +00003164 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00003165 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00003166 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00003167 MVT::ValueType VT = Node->getValueType(0);
3168
3169 // See if remainder can be lowered using two-result operations.
3170 SDVTList VTs = DAG.getVTList(VT, VT);
3171 if (Node->getOpcode() == ISD::SREM &&
3172 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3173 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3174 break;
3175 }
3176 if (Node->getOpcode() == ISD::UREM &&
3177 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3178 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3179 break;
3180 }
3181
3182 if (MVT::isInteger(VT)) {
3183 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003184 TargetLowering::Legal) {
3185 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00003186 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003187 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3188 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman08143e32007-11-05 23:35:22 +00003189 } else if (MVT::isVector(VT)) {
3190 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003191 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00003192 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003193 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00003194 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3195 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003196 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003197 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003198 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003199 } else {
3200 assert(MVT::isFloatingPoint(VT) &&
3201 "remainder op must have integer or floating-point type");
Dan Gohman08143e32007-11-05 23:35:22 +00003202 if (MVT::isVector(VT)) {
3203 Result = LegalizeOp(UnrollVectorOp(Op));
3204 } else {
3205 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003206 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3207 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman08143e32007-11-05 23:35:22 +00003208 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003209 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman08143e32007-11-05 23:35:22 +00003210 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003211 }
3212 break;
3213 }
Dan Gohmana1603612007-10-08 18:33:35 +00003214 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003215 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003216 case ISD::VAARG: {
3217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3218 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3219
Chris Lattner364b89a2006-01-28 07:42:08 +00003220 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003221 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3222 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003223 case TargetLowering::Custom:
3224 isCustom = true;
3225 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003226 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003227 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3228 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003229 Tmp1 = Result.getValue(1);
3230
3231 if (isCustom) {
3232 Tmp2 = TLI.LowerOperation(Result, DAG);
3233 if (Tmp2.Val) {
3234 Result = LegalizeOp(Tmp2);
3235 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3236 }
3237 }
Nate Begemane74795c2006-01-25 18:21:52 +00003238 break;
3239 case TargetLowering::Expand: {
Dan Gohman2d489b52008-02-06 22:27:42 +00003240 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3241 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003242 // Increment the pointer, VAList, to the next vaarg
3243 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3244 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3245 TLI.getPointerTy()));
3246 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00003247 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003248 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003249 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003250 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003251 Result = LegalizeOp(Result);
3252 break;
3253 }
3254 }
3255 // Since VAARG produces two values, make sure to remember that we
3256 // legalized both of them.
3257 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003258 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3259 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003260 }
3261
3262 case ISD::VACOPY:
3263 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3264 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3265 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3266
3267 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3268 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003269 case TargetLowering::Custom:
3270 isCustom = true;
3271 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003272 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3274 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003275 if (isCustom) {
3276 Tmp1 = TLI.LowerOperation(Result, DAG);
3277 if (Tmp1.Val) Result = Tmp1;
3278 }
Nate Begemane74795c2006-01-25 18:21:52 +00003279 break;
3280 case TargetLowering::Expand:
3281 // This defaults to loading a pointer from the input and storing it to the
3282 // output, returning the chain.
Dan Gohman2d489b52008-02-06 22:27:42 +00003283 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3284 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman9752a8f32008-04-17 02:09:26 +00003285 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3286 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003287 break;
3288 }
3289 break;
3290
3291 case ISD::VAEND:
3292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3294
3295 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3296 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003297 case TargetLowering::Custom:
3298 isCustom = true;
3299 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003300 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003302 if (isCustom) {
3303 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3304 if (Tmp1.Val) Result = Tmp1;
3305 }
Nate Begemane74795c2006-01-25 18:21:52 +00003306 break;
3307 case TargetLowering::Expand:
3308 Result = Tmp1; // Default to a no-op, return the chain
3309 break;
3310 }
3311 break;
3312
3313 case ISD::VASTART:
3314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3315 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3316
Chris Lattnerd02b0542006-01-28 10:58:55 +00003317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3318
Nate Begemane74795c2006-01-25 18:21:52 +00003319 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3320 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003321 case TargetLowering::Legal: break;
3322 case TargetLowering::Custom:
3323 Tmp1 = TLI.LowerOperation(Result, DAG);
3324 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003325 break;
3326 }
3327 break;
3328
Nate Begeman1b8121b2006-01-11 21:21:00 +00003329 case ISD::ROTL:
3330 case ISD::ROTR:
3331 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3332 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003334 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3335 default:
3336 assert(0 && "ROTL/ROTR legalize operation not supported");
3337 break;
3338 case TargetLowering::Legal:
3339 break;
3340 case TargetLowering::Custom:
3341 Tmp1 = TLI.LowerOperation(Result, DAG);
3342 if (Tmp1.Val) Result = Tmp1;
3343 break;
3344 case TargetLowering::Promote:
3345 assert(0 && "Do not know how to promote ROTL/ROTR");
3346 break;
3347 case TargetLowering::Expand:
3348 assert(0 && "Do not know how to expand ROTL/ROTR");
3349 break;
3350 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003351 break;
3352
Nate Begeman2fba8a32006-01-14 03:14:10 +00003353 case ISD::BSWAP:
3354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3355 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003356 case TargetLowering::Custom:
3357 assert(0 && "Cannot custom legalize this yet!");
3358 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003359 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003360 break;
3361 case TargetLowering::Promote: {
3362 MVT::ValueType OVT = Tmp1.getValueType();
3363 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003364 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003365
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003366 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3367 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3368 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3369 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3370 break;
3371 }
3372 case TargetLowering::Expand:
3373 Result = ExpandBSWAP(Tmp1);
3374 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003375 }
3376 break;
3377
Andrew Lenharth5e177822005-05-03 17:19:30 +00003378 case ISD::CTPOP:
3379 case ISD::CTTZ:
3380 case ISD::CTLZ:
3381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3382 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003383 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003384 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003385 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003386 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003387 TargetLowering::Custom) {
3388 Tmp1 = TLI.LowerOperation(Result, DAG);
3389 if (Tmp1.Val) {
3390 Result = Tmp1;
3391 }
Scott Michel34e2d222007-07-30 21:00:31 +00003392 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003393 break;
3394 case TargetLowering::Promote: {
3395 MVT::ValueType OVT = Tmp1.getValueType();
3396 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003397
3398 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003399 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3400 // Perform the larger operation, then subtract if needed.
3401 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003402 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003403 case ISD::CTPOP:
3404 Result = Tmp1;
3405 break;
3406 case ISD::CTTZ:
3407 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00003408 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003409 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003410 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003411 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003412 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003413 break;
3414 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003415 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003416 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003417 DAG.getConstant(MVT::getSizeInBits(NVT) -
3418 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003419 break;
3420 }
3421 break;
3422 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003423 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003424 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003425 break;
3426 }
3427 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003428
Chris Lattner13fe99c2005-04-02 05:00:07 +00003429 // Unary operators
3430 case ISD::FABS:
3431 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003432 case ISD::FSQRT:
3433 case ISD::FSIN:
3434 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003435 Tmp1 = LegalizeOp(Node->getOperand(0));
3436 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003437 case TargetLowering::Promote:
3438 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003439 isCustom = true;
3440 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003441 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003442 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003443 if (isCustom) {
3444 Tmp1 = TLI.LowerOperation(Result, DAG);
3445 if (Tmp1.Val) Result = Tmp1;
3446 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003447 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003448 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003449 switch (Node->getOpcode()) {
3450 default: assert(0 && "Unreachable!");
3451 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003452 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3453 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003454 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003455 break;
Chris Lattner80026402005-04-30 04:43:14 +00003456 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003457 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3458 MVT::ValueType VT = Node->getValueType(0);
3459 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003460 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman63eb03f2008-03-14 00:53:31 +00003461 ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003462 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3463 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003464 break;
3465 }
3466 case ISD::FSQRT:
3467 case ISD::FSIN:
3468 case ISD::FCOS: {
3469 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003470
3471 // Expand unsupported unary vector operators by unrolling them.
3472 if (MVT::isVector(VT)) {
3473 Result = LegalizeOp(UnrollVectorOp(Op));
3474 break;
3475 }
3476
Evan Cheng31cbddf2007-01-12 02:11:51 +00003477 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003478 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003479 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003480 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3481 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003482 break;
3483 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003484 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3485 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003486 break;
3487 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003488 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3489 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003490 break;
Chris Lattner80026402005-04-30 04:43:14 +00003491 default: assert(0 && "Unreachable!");
3492 }
Nate Begeman77558da2005-08-04 21:43:28 +00003493 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003494 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003495 break;
3496 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003497 }
3498 break;
3499 }
3500 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003501 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003502 MVT::ValueType VT = Node->getValueType(0);
3503
3504 // Expand unsupported unary vector operators by unrolling them.
3505 if (MVT::isVector(VT)) {
3506 Result = LegalizeOp(UnrollVectorOp(Op));
3507 break;
3508 }
3509
3510 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003511 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3512 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003513 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003514 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003515 break;
3516 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003517 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003518 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003519 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3520 Node->getValueType(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003521 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3522 // The input has to be a vector type, we have to either scalarize it, pack
3523 // it, or convert it based on whether the input vector type is legal.
3524 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003525 int InIx = Node->getOperand(0).ResNo;
3526 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3527 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmana8665142007-06-25 16:23:39 +00003528
3529 // Figure out if there is a simple type corresponding to this Vector
3530 // type. If so, convert to the vector type.
3531 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3532 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003533 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003534 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3535 LegalizeOp(Node->getOperand(0)));
3536 break;
3537 } else if (NumElems == 1) {
3538 // Turn this into a bit convert of the scalar input.
3539 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3540 ScalarizeVectorOp(Node->getOperand(0)));
3541 break;
3542 } else {
3543 // FIXME: UNIMP! Store then reload
3544 assert(0 && "Cast from unsupported vector type not implemented yet!");
3545 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003546 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003547 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3548 Node->getOperand(0).getValueType())) {
3549 default: assert(0 && "Unknown operation action!");
3550 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003551 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3552 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003553 break;
3554 case TargetLowering::Legal:
3555 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003556 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003557 break;
3558 }
3559 }
3560 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003561
Chris Lattner13fe99c2005-04-02 05:00:07 +00003562 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003563 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003564 case ISD::UINT_TO_FP: {
3565 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3567 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003568 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003569 Node->getOperand(0).getValueType())) {
3570 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003571 case TargetLowering::Custom:
3572 isCustom = true;
3573 // FALLTHROUGH
3574 case TargetLowering::Legal:
3575 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003576 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003577 if (isCustom) {
3578 Tmp1 = TLI.LowerOperation(Result, DAG);
3579 if (Tmp1.Val) Result = Tmp1;
3580 }
3581 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003582 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003583 Result = ExpandLegalINT_TO_FP(isSigned,
3584 LegalizeOp(Node->getOperand(0)),
3585 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003586 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003587 case TargetLowering::Promote:
3588 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3589 Node->getValueType(0),
3590 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003591 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003592 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003593 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003594 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003595 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3596 Node->getValueType(0), Node->getOperand(0));
3597 break;
3598 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003599 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003600 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003601 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3602 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003603 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003604 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3605 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003606 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003607 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3608 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003609 break;
3610 }
3611 break;
3612 }
3613 case ISD::TRUNCATE:
3614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3615 case Legal:
3616 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003617 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003618 break;
3619 case Expand:
3620 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3621
3622 // Since the result is legal, we should just be able to truncate the low
3623 // part of the source.
3624 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3625 break;
3626 case Promote:
3627 Result = PromoteOp(Node->getOperand(0));
3628 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3629 break;
3630 }
3631 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003632
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003633 case ISD::FP_TO_SINT:
3634 case ISD::FP_TO_UINT:
3635 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3636 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003637 Tmp1 = LegalizeOp(Node->getOperand(0));
3638
Chris Lattner44fe26f2005-07-29 00:11:56 +00003639 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3640 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003641 case TargetLowering::Custom:
3642 isCustom = true;
3643 // FALLTHROUGH
3644 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003645 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003646 if (isCustom) {
3647 Tmp1 = TLI.LowerOperation(Result, DAG);
3648 if (Tmp1.Val) Result = Tmp1;
3649 }
3650 break;
3651 case TargetLowering::Promote:
3652 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3653 Node->getOpcode() == ISD::FP_TO_SINT);
3654 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003655 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003656 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3657 SDOperand True, False;
3658 MVT::ValueType VT = Node->getOperand(0).getValueType();
3659 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003660 const uint64_t zero[] = {0, 0};
3661 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohman837a6dc2008-02-29 01:44:25 +00003662 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3663 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003664 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003665 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begeman36853ee2005-08-14 01:20:53 +00003666 Node->getOperand(0), Tmp2, ISD::SETLT);
3667 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3668 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003669 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003670 Tmp2));
3671 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman837a6dc2008-02-29 01:44:25 +00003672 DAG.getConstant(x, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003673 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3674 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003675 } else {
3676 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3677 }
3678 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003679 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003680 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003681 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003682 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003683 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003684 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003685 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner72733e52008-01-17 07:00:52 +00003686 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3687 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3688 Node->getOperand(0), DAG.getValueType(MVT::f64));
3689 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3690 DAG.getIntPtrConstant(1));
3691 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3692 } else {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003693 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3694 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3695 Tmp2 = DAG.getConstantFP(apf, OVT);
3696 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3697 // FIXME: generated code sucks.
3698 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3699 DAG.getNode(ISD::ADD, MVT::i32,
3700 DAG.getNode(ISD::FP_TO_SINT, VT,
3701 DAG.getNode(ISD::FSUB, OVT,
3702 Node->getOperand(0), Tmp2)),
3703 DAG.getConstant(0x80000000, MVT::i32)),
3704 DAG.getNode(ISD::FP_TO_SINT, VT,
3705 Node->getOperand(0)),
3706 DAG.getCondCode(ISD::SETGE));
3707 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003708 break;
3709 }
Dan Gohmanf4300952008-03-10 23:03:31 +00003710 // Convert f32 / f64 to i32 / i64 / i128.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003711 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003712 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003713 case ISD::FP_TO_SINT: {
Dan Gohmanf4300952008-03-10 23:03:31 +00003714 if (VT == MVT::i32) {
3715 if (OVT == MVT::f32)
3716 LC = RTLIB::FPTOSINT_F32_I32;
3717 else if (OVT == MVT::f64)
3718 LC = RTLIB::FPTOSINT_F64_I32;
3719 else
3720 assert(0 && "Unexpected i32-to-fp conversion!");
3721 } else if (VT == MVT::i64) {
3722 if (OVT == MVT::f32)
3723 LC = RTLIB::FPTOSINT_F32_I64;
3724 else if (OVT == MVT::f64)
3725 LC = RTLIB::FPTOSINT_F64_I64;
3726 else if (OVT == MVT::f80)
3727 LC = RTLIB::FPTOSINT_F80_I64;
3728 else if (OVT == MVT::ppcf128)
3729 LC = RTLIB::FPTOSINT_PPCF128_I64;
3730 else
3731 assert(0 && "Unexpected i64-to-fp conversion!");
3732 } else if (VT == MVT::i128) {
3733 if (OVT == MVT::f32)
3734 LC = RTLIB::FPTOSINT_F32_I128;
3735 else if (OVT == MVT::f64)
3736 LC = RTLIB::FPTOSINT_F64_I128;
3737 else if (OVT == MVT::f80)
3738 LC = RTLIB::FPTOSINT_F80_I128;
3739 else if (OVT == MVT::ppcf128)
3740 LC = RTLIB::FPTOSINT_PPCF128_I128;
3741 else
3742 assert(0 && "Unexpected i128-to-fp conversion!");
3743 } else {
3744 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen7d67e542007-09-19 23:55:34 +00003745 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003746 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003747 }
3748 case ISD::FP_TO_UINT: {
Dan Gohmanf4300952008-03-10 23:03:31 +00003749 if (VT == MVT::i32) {
3750 if (OVT == MVT::f32)
3751 LC = RTLIB::FPTOUINT_F32_I32;
3752 else if (OVT == MVT::f64)
3753 LC = RTLIB::FPTOUINT_F64_I32;
3754 else if (OVT == MVT::f80)
3755 LC = RTLIB::FPTOUINT_F80_I32;
3756 else
3757 assert(0 && "Unexpected i32-to-fp conversion!");
3758 } else if (VT == MVT::i64) {
3759 if (OVT == MVT::f32)
3760 LC = RTLIB::FPTOUINT_F32_I64;
3761 else if (OVT == MVT::f64)
3762 LC = RTLIB::FPTOUINT_F64_I64;
3763 else if (OVT == MVT::f80)
3764 LC = RTLIB::FPTOUINT_F80_I64;
3765 else if (OVT == MVT::ppcf128)
3766 LC = RTLIB::FPTOUINT_PPCF128_I64;
3767 else
3768 assert(0 && "Unexpected i64-to-fp conversion!");
3769 } else if (VT == MVT::i128) {
3770 if (OVT == MVT::f32)
3771 LC = RTLIB::FPTOUINT_F32_I128;
3772 else if (OVT == MVT::f64)
3773 LC = RTLIB::FPTOUINT_F64_I128;
3774 else if (OVT == MVT::f80)
3775 LC = RTLIB::FPTOUINT_F80_I128;
3776 else if (OVT == MVT::ppcf128)
3777 LC = RTLIB::FPTOUINT_PPCF128_I128;
3778 else
3779 assert(0 && "Unexpected i128-to-fp conversion!");
3780 } else {
3781 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen7d67e542007-09-19 23:55:34 +00003782 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003783 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003784 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003785 default: assert(0 && "Unreachable!");
3786 }
3787 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003788 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003789 break;
3790 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003791 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003792 Tmp1 = PromoteOp(Node->getOperand(0));
3793 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3794 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003795 break;
3796 }
3797 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003798
Chris Lattner91d86242008-01-16 06:57:07 +00003799 case ISD::FP_EXTEND: {
Chris Lattner72733e52008-01-17 07:00:52 +00003800 MVT::ValueType DstVT = Op.getValueType();
3801 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3802 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3803 // The only other way we can lower this is to turn it into a STORE,
3804 // LOAD pair, targetting a temporary location (a stack slot).
3805 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3806 break;
Chris Lattner91d86242008-01-16 06:57:07 +00003807 }
3808 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3809 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3810 case Legal:
3811 Tmp1 = LegalizeOp(Node->getOperand(0));
3812 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3813 break;
3814 case Promote:
3815 Tmp1 = PromoteOp(Node->getOperand(0));
3816 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3817 break;
3818 }
3819 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003820 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003821 case ISD::FP_ROUND: {
Chris Lattner72733e52008-01-17 07:00:52 +00003822 MVT::ValueType DstVT = Op.getValueType();
3823 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3824 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3825 if (SrcVT == MVT::ppcf128) {
Dale Johannesen949e5a22008-01-20 01:18:38 +00003826 SDOperand Lo;
3827 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner72733e52008-01-17 07:00:52 +00003828 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen949e5a22008-01-20 01:18:38 +00003829 if (DstVT!=MVT::f64)
3830 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner72733e52008-01-17 07:00:52 +00003831 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003832 }
Chris Lattner72733e52008-01-17 07:00:52 +00003833 // The only other way we can lower this is to turn it into a STORE,
3834 // LOAD pair, targetting a temporary location (a stack slot).
3835 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3836 break;
Dale Johannesena2b3c172007-07-03 00:53:03 +00003837 }
Chris Lattner91d86242008-01-16 06:57:07 +00003838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3839 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3840 case Legal:
3841 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003843 break;
3844 case Promote:
3845 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003846 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3847 Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003848 break;
3849 }
3850 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003851 }
Chris Lattner7753f172005-09-02 00:18:10 +00003852 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003853 case ISD::ZERO_EXTEND:
3854 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003855 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003856 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003857 case Legal:
3858 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michela3cefea2008-02-15 23:05:48 +00003859 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3860 TargetLowering::Custom) {
3861 Tmp2 = TLI.LowerOperation(Result, DAG);
3862 if (Tmp2.Val) {
3863 Tmp1 = Tmp2;
3864 }
3865 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003866 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003867 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003868 case Promote:
3869 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003870 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003871 Tmp1 = PromoteOp(Node->getOperand(0));
3872 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003873 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003874 case ISD::ZERO_EXTEND:
3875 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003876 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003877 Result = DAG.getZeroExtendInReg(Result,
3878 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003879 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003880 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003881 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003882 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003883 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003884 Result,
3885 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003886 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003887 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003888 }
3889 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003890 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003891 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003892 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003893 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003894
3895 // If this operation is not supported, convert it to a shl/shr or load/store
3896 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003897 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3898 default: assert(0 && "This action not supported for this op yet!");
3899 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003901 break;
3902 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003903 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003904 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003905 // NOTE: we could fall back on load/store here too for targets without
3906 // SAR. However, it is doubtful that any exist.
3907 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3908 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003909 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003910 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3911 Node->getOperand(0), ShiftCst);
3912 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3913 Result, ShiftCst);
3914 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003915 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003916 // EXTLOAD pair, targetting a temporary location (a stack slot).
3917
3918 // NOTE: there is a choice here between constantly creating new stack
3919 // slots and always reusing the same one. We currently always create
3920 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003921 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3922 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00003923 } else {
3924 assert(0 && "Unknown op");
3925 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003926 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003927 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003928 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003929 }
Duncan Sands644f9172007-07-27 12:58:54 +00003930 case ISD::TRAMPOLINE: {
3931 SDOperand Ops[6];
3932 for (unsigned i = 0; i != 6; ++i)
3933 Ops[i] = LegalizeOp(Node->getOperand(i));
3934 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3935 // The only option for this node is to custom lower it.
3936 Result = TLI.LowerOperation(Result, DAG);
3937 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003938
3939 // Since trampoline produces two values, make sure to remember that we
3940 // legalized both of them.
3941 Tmp1 = LegalizeOp(Result.getValue(1));
3942 Result = LegalizeOp(Result);
3943 AddLegalizedOperand(SDOperand(Node, 0), Result);
3944 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3945 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003946 }
Dan Gohman9ba4d762008-01-31 00:41:03 +00003947 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00003948 MVT::ValueType VT = Node->getValueType(0);
3949 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3950 default: assert(0 && "This action not supported for this op yet!");
3951 case TargetLowering::Custom:
3952 Result = TLI.LowerOperation(Op, DAG);
3953 if (Result.Val) break;
3954 // Fall Thru
3955 case TargetLowering::Legal:
3956 // If this operation is not supported, lower it to constant 1
3957 Result = DAG.getConstant(1, VT);
3958 break;
3959 }
3960 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00003961 case ISD::TRAP: {
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003962 MVT::ValueType VT = Node->getValueType(0);
3963 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3964 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00003965 case TargetLowering::Legal:
3966 Tmp1 = LegalizeOp(Node->getOperand(0));
3967 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3968 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003969 case TargetLowering::Custom:
3970 Result = TLI.LowerOperation(Op, DAG);
3971 if (Result.Val) break;
3972 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00003973 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003974 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00003975 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003976 TargetLowering::ArgListTy Args;
3977 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00003978 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3979 false, false, false, CallingConv::C, false,
Chris Lattnerec224882008-01-15 22:09:33 +00003980 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3981 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003982 Result = CallResult.second;
3983 break;
3984 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00003985 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003986 }
Chris Lattner99222f72005-01-15 07:15:18 +00003987 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003988
Chris Lattner101ea662006-04-08 04:13:17 +00003989 assert(Result.getValueType() == Op.getValueType() &&
3990 "Bad legalization!");
3991
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003992 // Make sure that the generated code is itself legal.
3993 if (Result != Op)
3994 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003995
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003996 // Note that LegalizeOp may be reentered even from single-use nodes, which
3997 // means that we always must cache transformed nodes.
3998 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003999 return Result;
4000}
4001
Chris Lattner4d978642005-01-15 22:16:26 +00004002/// PromoteOp - Given an operation that produces a value in an invalid type,
4003/// promote it to compute the value into a larger type. The produced value will
4004/// have the correct bits for the low portion of the register, but no guarantee
4005/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004006SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4007 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004008 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004009 assert(getTypeAction(VT) == Promote &&
4010 "Caller should expand or legalize operands that are not promotable!");
4011 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4012 "Cannot promote to smaller type!");
4013
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004014 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004015 SDOperand Result;
4016 SDNode *Node = Op.Val;
4017
Roman Levensteina3ee1a32008-04-16 16:15:27 +00004018 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00004019 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004020
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004021 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00004022 case ISD::CopyFromReg:
4023 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004024 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004025#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004026 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004027#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004028 assert(0 && "Do not know how to promote this operator!");
4029 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004030 case ISD::UNDEF:
4031 Result = DAG.getNode(ISD::UNDEF, NVT);
4032 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004033 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00004034 if (VT != MVT::i1)
4035 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4036 else
4037 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004038 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4039 break;
4040 case ISD::ConstantFP:
4041 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4042 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4043 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00004044
Chris Lattner2cb338d2005-01-18 02:59:52 +00004045 case ISD::SETCC:
Scott Michela6729e82008-03-10 15:42:14 +00004046 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman63eb03f2008-03-14 00:53:31 +00004047 && "SetCC type is not legal??");
Scott Michela6729e82008-03-10 15:42:14 +00004048 Result = DAG.getNode(ISD::SETCC,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004049 TLI.getSetCCResultType(Node->getOperand(0)),
4050 Node->getOperand(0), Node->getOperand(1),
4051 Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00004052 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00004053
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004054 case ISD::TRUNCATE:
4055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4056 case Legal:
4057 Result = LegalizeOp(Node->getOperand(0));
4058 assert(Result.getValueType() >= NVT &&
4059 "This truncation doesn't make sense!");
4060 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4061 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4062 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00004063 case Promote:
4064 // The truncation is not required, because we don't guarantee anything
4065 // about high bits anyway.
4066 Result = PromoteOp(Node->getOperand(0));
4067 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004068 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00004069 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4070 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00004071 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004072 }
4073 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004074 case ISD::SIGN_EXTEND:
4075 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00004076 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00004077 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4078 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4079 case Legal:
4080 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004081 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004082 break;
4083 case Promote:
4084 // Promote the reg if it's smaller.
4085 Result = PromoteOp(Node->getOperand(0));
4086 // The high bits are not guaranteed to be anything. Insert an extend.
4087 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00004088 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004089 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00004090 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00004091 Result = DAG.getZeroExtendInReg(Result,
4092 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00004093 break;
4094 }
4095 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004096 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00004097 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4098 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00004099 Result = PromoteOp(Result);
4100 break;
4101
Chris Lattner4d978642005-01-15 22:16:26 +00004102 case ISD::FP_EXTEND:
4103 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4104 case ISD::FP_ROUND:
4105 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4106 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4107 case Promote: assert(0 && "Unreachable with 2 FP types!");
4108 case Legal:
Chris Lattner72733e52008-01-17 07:00:52 +00004109 if (Node->getConstantOperandVal(1) == 0) {
4110 // Input is legal? Do an FP_ROUND_INREG.
4111 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4112 DAG.getValueType(VT));
4113 } else {
4114 // Just remove the truncate, it isn't affecting the value.
4115 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4116 Node->getOperand(1));
4117 }
Chris Lattner4d978642005-01-15 22:16:26 +00004118 break;
4119 }
4120 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004121 case ISD::SINT_TO_FP:
4122 case ISD::UINT_TO_FP:
4123 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4124 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00004125 // No extra round required here.
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
4129 case Promote:
4130 Result = PromoteOp(Node->getOperand(0));
4131 if (Node->getOpcode() == ISD::SINT_TO_FP)
4132 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004133 Result,
4134 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00004135 else
Chris Lattner0e852af2005-04-13 02:38:47 +00004136 Result = DAG.getZeroExtendInReg(Result,
4137 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00004138 // No extra round required here.
4139 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00004140 break;
4141 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00004142 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4143 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00004144 // Round if we cannot tolerate excess precision.
4145 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004146 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4147 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00004148 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004149 }
Chris Lattner4d978642005-01-15 22:16:26 +00004150 break;
4151
Chris Lattner268d4572005-12-09 17:32:47 +00004152 case ISD::SIGN_EXTEND_INREG:
4153 Result = PromoteOp(Node->getOperand(0));
4154 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4155 Node->getOperand(1));
4156 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004157 case ISD::FP_TO_SINT:
4158 case ISD::FP_TO_UINT:
4159 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4160 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00004161 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004162 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00004163 break;
4164 case Promote:
4165 // The input result is prerounded, so we don't have to do anything
4166 // special.
4167 Tmp1 = PromoteOp(Node->getOperand(0));
4168 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004169 }
Nate Begeman36853ee2005-08-14 01:20:53 +00004170 // If we're promoting a UINT to a larger size, check to see if the new node
4171 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4172 // we can use that instead. This allows us to generate better code for
4173 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4174 // legal, such as PowerPC.
4175 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004176 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00004177 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4178 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00004179 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4180 } else {
4181 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4182 }
Chris Lattner4d978642005-01-15 22:16:26 +00004183 break;
4184
Chris Lattner13fe99c2005-04-02 05:00:07 +00004185 case ISD::FABS:
4186 case ISD::FNEG:
4187 Tmp1 = PromoteOp(Node->getOperand(0));
4188 assert(Tmp1.getValueType() == NVT);
4189 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4190 // NOTE: we do not have to do any extra rounding here for
4191 // NoExcessFPPrecision, because we know the input will have the appropriate
4192 // precision, and these operations don't modify precision at all.
4193 break;
4194
Chris Lattner9d6fa982005-04-28 21:44:33 +00004195 case ISD::FSQRT:
4196 case ISD::FSIN:
4197 case ISD::FCOS:
4198 Tmp1 = PromoteOp(Node->getOperand(0));
4199 assert(Tmp1.getValueType() == NVT);
4200 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004201 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004202 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4203 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00004204 break;
4205
Chris Lattnerca401aa2007-03-03 23:43:21 +00004206 case ISD::FPOWI: {
4207 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4208 // directly as well, which may be better.
4209 Tmp1 = PromoteOp(Node->getOperand(0));
4210 assert(Tmp1.getValueType() == NVT);
4211 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4212 if (NoExcessFPPrecision)
4213 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4214 DAG.getValueType(VT));
4215 break;
4216 }
4217
Andrew Lenharth95528942008-02-21 06:45:13 +00004218 case ISD::ATOMIC_LCS: {
4219 Tmp2 = PromoteOp(Node->getOperand(2));
4220 Tmp3 = PromoteOp(Node->getOperand(3));
4221 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4222 Node->getOperand(1), Tmp2, Tmp3,
4223 cast<AtomicSDNode>(Node)->getVT());
4224 // Remember that we legalized the chain.
4225 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4226 break;
4227 }
4228 case ISD::ATOMIC_LAS:
4229 case ISD::ATOMIC_SWAP: {
4230 Tmp2 = PromoteOp(Node->getOperand(2));
4231 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4232 Node->getOperand(1), Tmp2,
4233 cast<AtomicSDNode>(Node)->getVT());
4234 // Remember that we legalized the chain.
4235 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4236 break;
4237 }
4238
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004239 case ISD::AND:
4240 case ISD::OR:
4241 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004242 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00004243 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004244 case ISD::MUL:
4245 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00004246 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004247 // that too is okay if they are integer operations.
4248 Tmp1 = PromoteOp(Node->getOperand(0));
4249 Tmp2 = PromoteOp(Node->getOperand(1));
4250 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4251 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00004252 break;
4253 case ISD::FADD:
4254 case ISD::FSUB:
4255 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004256 Tmp1 = PromoteOp(Node->getOperand(0));
4257 Tmp2 = PromoteOp(Node->getOperand(1));
4258 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4259 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4260
4261 // Floating point operations will give excess precision that we may not be
4262 // able to tolerate. If we DO allow excess precision, just leave it,
4263 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004264 // FIXME: Why would we need to round FP ops more than integer ones?
4265 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004266 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004267 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4268 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004269 break;
4270
Chris Lattner4d978642005-01-15 22:16:26 +00004271 case ISD::SDIV:
4272 case ISD::SREM:
4273 // These operators require that their input be sign extended.
4274 Tmp1 = PromoteOp(Node->getOperand(0));
4275 Tmp2 = PromoteOp(Node->getOperand(1));
4276 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004277 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4278 DAG.getValueType(VT));
4279 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4280 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004281 }
4282 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4283
4284 // Perform FP_ROUND: this is probably overly pessimistic.
4285 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004286 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4287 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004288 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004289 case ISD::FDIV:
4290 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004291 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004292 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004293 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004294 case Expand: assert(0 && "not implemented");
4295 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4296 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004297 }
4298 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004299 case Expand: assert(0 && "not implemented");
4300 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4301 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004302 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004303 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4304
4305 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004306 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004307 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4308 DAG.getValueType(VT));
4309 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004310
4311 case ISD::UDIV:
4312 case ISD::UREM:
4313 // These operators require that their input be zero extended.
4314 Tmp1 = PromoteOp(Node->getOperand(0));
4315 Tmp2 = PromoteOp(Node->getOperand(1));
4316 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004317 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4318 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004319 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4320 break;
4321
4322 case ISD::SHL:
4323 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004324 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004325 break;
4326 case ISD::SRA:
4327 // The input value must be properly sign extended.
4328 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004329 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4330 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004331 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004332 break;
4333 case ISD::SRL:
4334 // The input value must be properly zero extended.
4335 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004336 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004337 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004338 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004339
4340 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004341 Tmp1 = Node->getOperand(0); // Get the chain.
4342 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004343 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4344 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4345 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4346 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +00004347 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4348 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004349 // Increment the pointer, VAList, to the next vaarg
4350 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4351 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4352 TLI.getPointerTy()));
4353 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00004354 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004355 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004356 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004357 }
4358 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004360 break;
4361
Evan Chenge71fe34d2006-10-09 20:57:25 +00004362 case ISD::LOAD: {
4363 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004364 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4365 ? ISD::EXTLOAD : LD->getExtensionType();
4366 Result = DAG.getExtLoad(ExtType, NVT,
4367 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004368 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00004369 LD->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00004370 LD->isVolatile(),
4371 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004372 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004373 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004374 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004375 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004376 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004377 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4378 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004379 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004380 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004381 case ISD::SELECT_CC:
4382 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4383 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4384 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004385 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004386 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004387 case ISD::BSWAP:
4388 Tmp1 = Node->getOperand(0);
4389 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4390 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4391 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004392 DAG.getConstant(MVT::getSizeInBits(NVT) -
4393 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004394 TLI.getShiftAmountTy()));
4395 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004396 case ISD::CTPOP:
4397 case ISD::CTTZ:
4398 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004399 // Zero extend the argument
4400 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004401 // Perform the larger operation, then subtract if needed.
4402 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004403 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004404 case ISD::CTPOP:
4405 Result = Tmp1;
4406 break;
4407 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004408 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00004409 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004410 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4411 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004412 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004413 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004414 break;
4415 case ISD::CTLZ:
4416 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004417 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004418 DAG.getConstant(MVT::getSizeInBits(NVT) -
4419 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004420 break;
4421 }
4422 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004423 case ISD::EXTRACT_SUBVECTOR:
4424 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004425 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004426 case ISD::EXTRACT_VECTOR_ELT:
4427 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4428 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004429 }
4430
4431 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004432
4433 // Make sure the result is itself legal.
4434 Result = LegalizeOp(Result);
4435
4436 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004437 AddPromotedOperand(Op, Result);
4438 return Result;
4439}
Chris Lattnerdc750592005-01-07 07:47:09 +00004440
Dan Gohmana8665142007-06-25 16:23:39 +00004441/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4442/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4443/// based on the vector type. The return type of this matches the element type
4444/// of the vector, which may not be legal for the target.
4445SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004446 // We know that operand #0 is the Vec vector. If the index is a constant
4447 // or if the invec is a supported hardware type, we can use it. Otherwise,
4448 // lower to a store then an indexed load.
4449 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004450 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004451
Dan Gohman60028182007-09-24 15:54:53 +00004452 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004453 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004454
Dan Gohmana8665142007-06-25 16:23:39 +00004455 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4456 default: assert(0 && "This action is not supported yet!");
4457 case TargetLowering::Custom: {
4458 Vec = LegalizeOp(Vec);
4459 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4460 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4461 if (Tmp3.Val)
4462 return Tmp3;
4463 break;
4464 }
4465 case TargetLowering::Legal:
4466 if (isTypeLegal(TVT)) {
4467 Vec = LegalizeOp(Vec);
4468 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004469 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004470 }
4471 break;
4472 case TargetLowering::Expand:
4473 break;
4474 }
4475
4476 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004477 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004478 Op = ScalarizeVectorOp(Vec);
4479 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begemanef337672008-01-29 02:24:00 +00004480 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmana8665142007-06-25 16:23:39 +00004481 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004482 SDOperand Lo, Hi;
4483 SplitVectorOp(Vec, Lo, Hi);
Nate Begemanef337672008-01-29 02:24:00 +00004484 if (CIdx->getValue() < NumLoElts) {
Chris Lattner6f423252006-03-31 17:55:51 +00004485 Vec = Lo;
4486 } else {
4487 Vec = Hi;
Nate Begemanef337672008-01-29 02:24:00 +00004488 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmana8665142007-06-25 16:23:39 +00004489 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004490 }
Dan Gohmana8665142007-06-25 16:23:39 +00004491
Chris Lattner6f423252006-03-31 17:55:51 +00004492 // It's now an extract from the appropriate high or low part. Recurse.
4493 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004494 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004495 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004496 // Store the value to a temporary stack slot, then LOAD the scalar
4497 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004498 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004499 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4500
4501 // Add the offset to the index.
4502 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4503 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4504 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004505
4506 if (MVT::getSizeInBits(Idx.getValueType()) >
4507 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004508 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004509 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004510 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004511
Dan Gohmana8665142007-06-25 16:23:39 +00004512 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4513
4514 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004515 }
Dan Gohmana8665142007-06-25 16:23:39 +00004516 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004517}
4518
Dan Gohmana8665142007-06-25 16:23:39 +00004519/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004520/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004521SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004522 // We know that operand #0 is the Vec vector. For now we assume the index
4523 // is a constant and that the extracted result is a supported hardware type.
4524 SDOperand Vec = Op.getOperand(0);
4525 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4526
Dan Gohmana8665142007-06-25 16:23:39 +00004527 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004528
4529 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4530 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004531 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004532 }
4533
4534 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4535 SDOperand Lo, Hi;
4536 SplitVectorOp(Vec, Lo, Hi);
4537 if (CIdx->getValue() < NumElems/2) {
4538 Vec = Lo;
4539 } else {
4540 Vec = Hi;
4541 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4542 }
4543
4544 // It's now an extract from the appropriate high or low part. Recurse.
4545 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004546 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004547}
4548
Nate Begeman7e7f4392006-02-01 07:19:44 +00004549/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4550/// with condition CC on the current target. This usually involves legalizing
4551/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4552/// there may be no choice but to create a new SetCC node to represent the
4553/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4554/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4555void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4556 SDOperand &RHS,
4557 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004558 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004559
4560 switch (getTypeAction(LHS.getValueType())) {
4561 case Legal:
4562 Tmp1 = LegalizeOp(LHS); // LHS
4563 Tmp2 = LegalizeOp(RHS); // RHS
4564 break;
4565 case Promote:
4566 Tmp1 = PromoteOp(LHS); // LHS
4567 Tmp2 = PromoteOp(RHS); // RHS
4568
4569 // If this is an FP compare, the operands have already been extended.
4570 if (MVT::isInteger(LHS.getValueType())) {
4571 MVT::ValueType VT = LHS.getValueType();
4572 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4573
4574 // Otherwise, we have to insert explicit sign or zero extends. Note
4575 // that we could insert sign extends for ALL conditions, but zero extend
4576 // is cheaper on many machines (an AND instead of two shifts), so prefer
4577 // it.
4578 switch (cast<CondCodeSDNode>(CC)->get()) {
4579 default: assert(0 && "Unknown integer comparison!");
4580 case ISD::SETEQ:
4581 case ISD::SETNE:
4582 case ISD::SETUGE:
4583 case ISD::SETUGT:
4584 case ISD::SETULE:
4585 case ISD::SETULT:
4586 // ALL of these operations will work if we either sign or zero extend
4587 // the operands (including the unsigned comparisons!). Zero extend is
4588 // usually a simpler/cheaper operation, so prefer it.
4589 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4590 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4591 break;
4592 case ISD::SETGE:
4593 case ISD::SETGT:
4594 case ISD::SETLT:
4595 case ISD::SETLE:
4596 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4597 DAG.getValueType(VT));
4598 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4599 DAG.getValueType(VT));
4600 break;
4601 }
4602 }
4603 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004604 case Expand: {
4605 MVT::ValueType VT = LHS.getValueType();
4606 if (VT == MVT::f32 || VT == MVT::f64) {
4607 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004608 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004609 switch (cast<CondCodeSDNode>(CC)->get()) {
4610 case ISD::SETEQ:
4611 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004612 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004613 break;
4614 case ISD::SETNE:
4615 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004616 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004617 break;
4618 case ISD::SETGE:
4619 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004620 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004621 break;
4622 case ISD::SETLT:
4623 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004624 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004625 break;
4626 case ISD::SETLE:
4627 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004628 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004629 break;
4630 case ISD::SETGT:
4631 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004632 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004633 break;
4634 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004635 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4636 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004637 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004638 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004639 break;
4640 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004641 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004642 switch (cast<CondCodeSDNode>(CC)->get()) {
4643 case ISD::SETONE:
4644 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004645 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004646 // Fallthrough
4647 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004648 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004649 break;
4650 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004651 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004652 break;
4653 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004654 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004655 break;
4656 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004657 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004658 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004659 case ISD::SETUEQ:
4660 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004661 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004662 default: assert(0 && "Unsupported FP setcc!");
4663 }
4664 }
4665
4666 SDOperand Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00004667 Tmp1 = ExpandLibCall(LC1,
Reid Spencere63b6512006-12-31 05:55:36 +00004668 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004669 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004670 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004671 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004672 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michela6729e82008-03-10 15:42:14 +00004673 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004674 CC);
Duncan Sands844d55a2008-04-12 17:14:18 +00004675 LHS = ExpandLibCall(LC2,
4676 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004677 false /*sign irrelevant*/, Dummy);
Scott Michela6729e82008-03-10 15:42:14 +00004678 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004679 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004680 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4681 Tmp2 = SDOperand();
4682 }
4683 LHS = Tmp1;
4684 RHS = Tmp2;
4685 return;
4686 }
4687
Nate Begeman7e7f4392006-02-01 07:19:44 +00004688 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4689 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004690 ExpandOp(RHS, RHSLo, RHSHi);
4691 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4692
4693 if (VT==MVT::ppcf128) {
4694 // FIXME: This generated code sucks. We want to generate
4695 // FCMP crN, hi1, hi2
4696 // BNE crN, L:
4697 // FCMP crN, lo1, lo2
4698 // The following can be improved, but not that much.
Scott Michela6729e82008-03-10 15:42:14 +00004699 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4700 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004701 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00004702 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4703 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004704 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4705 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4706 Tmp2 = SDOperand();
4707 break;
4708 }
4709
4710 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004711 case ISD::SETEQ:
4712 case ISD::SETNE:
4713 if (RHSLo == RHSHi)
4714 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4715 if (RHSCST->isAllOnesValue()) {
4716 // Comparison to -1.
4717 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4718 Tmp2 = RHSLo;
4719 break;
4720 }
4721
4722 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4723 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4724 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4725 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4726 break;
4727 default:
4728 // If this is a comparison of the sign bit, just look at the top part.
4729 // X > -1, x < 0
4730 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4731 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00004732 CST->isNullValue()) || // X < 0
Nate Begeman7e7f4392006-02-01 07:19:44 +00004733 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4734 CST->isAllOnesValue())) { // X > -1
4735 Tmp1 = LHSHi;
4736 Tmp2 = RHSHi;
4737 break;
4738 }
4739
4740 // FIXME: This generated code sucks.
4741 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004742 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004743 default: assert(0 && "Unknown integer setcc!");
4744 case ISD::SETLT:
4745 case ISD::SETULT: LowCC = ISD::SETULT; break;
4746 case ISD::SETGT:
4747 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4748 case ISD::SETLE:
4749 case ISD::SETULE: LowCC = ISD::SETULE; break;
4750 case ISD::SETGE:
4751 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4752 }
4753
4754 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4755 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4756 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4757
4758 // NOTE: on targets without efficient SELECT of bools, we can always use
4759 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004760 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michela6729e82008-03-10 15:42:14 +00004761 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004762 LowCC, false, DagCombineInfo);
Evan Cheng93049452007-02-08 22:16:19 +00004763 if (!Tmp1.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004764 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4765 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00004766 CCCode, false, DagCombineInfo);
4767 if (!Tmp2.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004768 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004769 RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004770
4771 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4772 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohmanb72127a2008-03-13 22:13:53 +00004773 if ((Tmp1C && Tmp1C->isNullValue()) ||
4774 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng93049452007-02-08 22:16:19 +00004775 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4776 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohmanb72127a2008-03-13 22:13:53 +00004777 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng93049452007-02-08 22:16:19 +00004778 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4779 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4780 // low part is known false, returns high part.
4781 // For LE / GE, if high part is known false, ignore the low part.
4782 // For LT / GT, if high part is known true, ignore the low part.
4783 Tmp1 = Tmp2;
4784 Tmp2 = SDOperand();
4785 } else {
Scott Michela6729e82008-03-10 15:42:14 +00004786 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00004787 ISD::SETEQ, false, DagCombineInfo);
4788 if (!Result.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004789 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004790 ISD::SETEQ);
Evan Cheng93049452007-02-08 22:16:19 +00004791 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4792 Result, Tmp1, Tmp2));
4793 Tmp1 = Result;
4794 Tmp2 = SDOperand();
4795 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004796 }
4797 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004798 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004799 LHS = Tmp1;
4800 RHS = Tmp2;
4801}
4802
Chris Lattner87bc3e72008-01-16 07:45:30 +00004803/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4804/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4805/// a load from the stack slot to DestVT, extending it if needed.
4806/// The resultant code need not be legal.
4807SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4808 MVT::ValueType SlotVT,
4809 MVT::ValueType DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00004810 // Create the stack frame object.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004811 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4812
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004813 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004814 int SPFI = StackPtrFI->getIndex();
4815
Chris Lattner87bc3e72008-01-16 07:45:30 +00004816 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4817 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4818 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004819
Chris Lattner87bc3e72008-01-16 07:45:30 +00004820 // Emit a store to the stack slot. Use a truncstore if the input value is
4821 // later than DestVT.
4822 SDOperand Store;
4823 if (SrcSize > SlotSize)
Dan Gohman2d489b52008-02-06 22:27:42 +00004824 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004825 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004826 SPFI, SlotVT);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004827 else {
4828 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman2d489b52008-02-06 22:27:42 +00004829 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004830 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004831 SPFI, SlotVT);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004832 }
4833
Chris Lattner36e663d2005-12-23 00:16:34 +00004834 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004835 if (SlotSize == DestSize)
4836 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4837
4838 assert(SlotSize < DestSize && "Unknown extension!");
4839 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004840}
4841
Chris Lattner6be79822006-04-04 17:23:26 +00004842SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4843 // Create a vector sized/aligned stack slot, store the value to element #0,
4844 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004845 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00004846
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004847 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004848 int SPFI = StackPtrFI->getIndex();
4849
Evan Chengdf9ac472006-10-05 23:01:46 +00004850 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004851 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman2d489b52008-02-06 22:27:42 +00004852 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004853 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner6be79822006-04-04 17:23:26 +00004854}
4855
4856
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004857/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004858/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004859SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4860
4861 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004862 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004863 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004864 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004865 SDOperand SplatValue = Node->getOperand(0);
Chris Lattner322c8262008-03-09 00:29:42 +00004866
4867 // FIXME: it would be far nicer to change this into map<SDOperand,uint64_t>
4868 // and use a bitmask instead of a list of elements.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004869 std::map<SDOperand, std::vector<unsigned> > Values;
4870 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004871 bool isConstant = true;
4872 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4873 SplatValue.getOpcode() != ISD::UNDEF)
4874 isConstant = false;
4875
Evan Cheng1d2e9952006-03-24 01:17:21 +00004876 for (unsigned i = 1; i < NumElems; ++i) {
4877 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004878 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004879 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004880 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004881 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004882 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004883
4884 // If this isn't a constant element or an undef, we can't use a constant
4885 // pool load.
4886 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4887 V.getOpcode() != ISD::UNDEF)
4888 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004889 }
4890
4891 if (isOnlyLowElement) {
4892 // If the low element is an undef too, then this whole things is an undef.
4893 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4894 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4895 // Otherwise, turn this into a scalar_to_vector node.
4896 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4897 Node->getOperand(0));
4898 }
4899
Chris Lattner77e271c2006-03-24 07:29:17 +00004900 // If all elements are constants, create a load from the constant pool.
4901 if (isConstant) {
4902 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004903 std::vector<Constant*> CV;
4904 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4905 if (ConstantFPSDNode *V =
4906 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Chris Lattner3b187622008-04-20 00:41:09 +00004907 CV.push_back(ConstantFP::get(V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004908 } else if (ConstantSDNode *V =
Chris Lattner3b187622008-04-20 00:41:09 +00004909 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4910 CV.push_back(ConstantInt::get(V->getAPIntValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004911 } else {
4912 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner3b187622008-04-20 00:41:09 +00004913 const Type *OpNTy =
4914 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
Chris Lattner77e271c2006-03-24 07:29:17 +00004915 CV.push_back(UndefValue::get(OpNTy));
4916 }
4917 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004918 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004919 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00004920 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004921 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004922 }
4923
Chris Lattner21e68c82006-03-20 01:52:29 +00004924 if (SplatValue.Val) { // Splat of one value?
4925 // Build the shuffle constant vector: <0, 0, 0, 0>
4926 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004927 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004928 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004929 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004930 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4931 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004932
4933 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004934 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004935 // Get the splatted value into the low element of a vector register.
4936 SDOperand LowValVec =
4937 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4938
4939 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4940 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4941 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4942 SplatMask);
4943 }
4944 }
4945
Evan Cheng1d2e9952006-03-24 01:17:21 +00004946 // If there are only two unique elements, we may be able to turn this into a
4947 // vector shuffle.
4948 if (Values.size() == 2) {
Chris Lattner322c8262008-03-09 00:29:42 +00004949 // Get the two values in deterministic order.
4950 SDOperand Val1 = Node->getOperand(1);
4951 SDOperand Val2;
4952 std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin();
4953 if (MI->first != Val1)
4954 Val2 = MI->first;
4955 else
4956 Val2 = (++MI)->first;
4957
4958 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4959 // vector shuffle has the undef vector on the RHS.
4960 if (Val1.getOpcode() == ISD::UNDEF)
4961 std::swap(Val1, Val2);
4962
Evan Cheng1d2e9952006-03-24 01:17:21 +00004963 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Chris Lattner322c8262008-03-09 00:29:42 +00004964 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4965 MVT::ValueType MaskEltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004966 std::vector<SDOperand> MaskVec(NumElems);
Chris Lattner322c8262008-03-09 00:29:42 +00004967
4968 // Set elements of the shuffle mask for Val1.
4969 std::vector<unsigned> &Val1Elts = Values[Val1];
4970 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4971 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4972
4973 // Set elements of the shuffle mask for Val2.
4974 std::vector<unsigned> &Val2Elts = Values[Val2];
4975 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4976 if (Val2.getOpcode() != ISD::UNDEF)
4977 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4978 else
4979 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4980
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004981 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4982 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004983
Chris Lattner322c8262008-03-09 00:29:42 +00004984 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004985 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4986 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattner322c8262008-03-09 00:29:42 +00004987 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
4988 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
4989 SDOperand Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng1d2e9952006-03-24 01:17:21 +00004990
4991 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattner322c8262008-03-09 00:29:42 +00004992 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004993 }
4994 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004995
4996 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4997 // aligned object on the stack, store each element into it, then load
4998 // the result as a vector.
4999 MVT::ValueType VT = Node->getValueType(0);
5000 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00005001 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005002
5003 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005004 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005005 unsigned TypeByteSize =
5006 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005007 // Store (in the right endianness) the elements to memory.
5008 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5009 // Ignore undef elements.
5010 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5011
Chris Lattner8fa445a2006-03-22 01:46:54 +00005012 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005013
5014 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5015 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5016
Evan Chengdf9ac472006-10-05 23:01:46 +00005017 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00005018 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005019 }
5020
5021 SDOperand StoreChain;
5022 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005023 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5024 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005025 else
5026 StoreChain = DAG.getEntryNode();
5027
5028 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005029 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005030}
5031
Chris Lattner4157c412005-04-02 04:00:59 +00005032void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5033 SDOperand Op, SDOperand Amt,
5034 SDOperand &Lo, SDOperand &Hi) {
5035 // Expand the subcomponents.
5036 SDOperand LHSL, LHSH;
5037 ExpandOp(Op, LHSL, LHSH);
5038
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005039 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00005040 MVT::ValueType VT = LHSL.getValueType();
5041 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00005042 Hi = Lo.getValue(1);
5043}
5044
5045
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005046/// ExpandShift - Try to find a clever way to expand this shift operation out to
5047/// smaller elements. If we can't find a way that is more efficient than a
5048/// libcall on this target, return false. Otherwise, return true with the
5049/// low-parts expanded into Lo and Hi.
5050bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5051 SDOperand &Lo, SDOperand &Hi) {
5052 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5053 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00005054
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005055 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00005056 SDOperand ShAmt = LegalizeOp(Amt);
5057 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman34fc7db2008-02-20 16:57:27 +00005058 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanb0674922005-04-06 21:13:14 +00005059 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5060 unsigned NVTBits = MVT::getSizeInBits(NVT);
5061
Chris Lattnerfbbe5702007-10-14 20:35:12 +00005062 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00005063 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5064 unsigned Cst = CN->getValue();
5065 // Expand the incoming operand to be shifted, so that we have its parts
5066 SDOperand InL, InH;
5067 ExpandOp(Op, InL, InH);
5068 switch(Opc) {
5069 case ISD::SHL:
5070 if (Cst > VTBits) {
5071 Lo = DAG.getConstant(0, NVT);
5072 Hi = DAG.getConstant(0, NVT);
5073 } else if (Cst > NVTBits) {
5074 Lo = DAG.getConstant(0, NVT);
5075 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005076 } else if (Cst == NVTBits) {
5077 Lo = DAG.getConstant(0, NVT);
5078 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00005079 } else {
5080 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5081 Hi = DAG.getNode(ISD::OR, NVT,
5082 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5083 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5084 }
5085 return true;
5086 case ISD::SRL:
5087 if (Cst > VTBits) {
5088 Lo = DAG.getConstant(0, NVT);
5089 Hi = DAG.getConstant(0, NVT);
5090 } else if (Cst > NVTBits) {
5091 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5092 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00005093 } else if (Cst == NVTBits) {
5094 Lo = InH;
5095 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00005096 } else {
5097 Lo = DAG.getNode(ISD::OR, NVT,
5098 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5099 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5100 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5101 }
5102 return true;
5103 case ISD::SRA:
5104 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005105 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005106 DAG.getConstant(NVTBits-1, ShTy));
5107 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005108 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005109 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00005110 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005111 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005112 } else if (Cst == NVTBits) {
5113 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00005114 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00005115 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00005116 } else {
5117 Lo = DAG.getNode(ISD::OR, NVT,
5118 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5119 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5120 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5121 }
5122 return true;
5123 }
5124 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00005125
5126 // Okay, the shift amount isn't constant. However, if we can tell that it is
5127 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005128 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5129 APInt KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00005130 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00005131
Dan Gohmanf3057a92008-02-22 01:12:31 +00005132 // If we know that if any of the high bits of the shift amount are one, then
5133 // we can do this as a couple of simple shifts.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005134 if (KnownOne.intersects(Mask)) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005135 // Mask out the high bit, which we know is set.
5136 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman34fc7db2008-02-20 16:57:27 +00005137 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner875ea0c2006-09-20 03:38:48 +00005138
5139 // Expand the incoming operand to be shifted, so that we have its parts
5140 SDOperand InL, InH;
5141 ExpandOp(Op, InL, InH);
5142 switch(Opc) {
5143 case ISD::SHL:
5144 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5145 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5146 return true;
5147 case ISD::SRL:
5148 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5149 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5150 return true;
5151 case ISD::SRA:
5152 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5153 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5154 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5155 return true;
5156 }
5157 }
5158
Dan Gohmanf3057a92008-02-22 01:12:31 +00005159 // If we know that the high bits of the shift amount are all zero, then we can
5160 // do this as a couple of simple shifts.
5161 if ((KnownZero & Mask) == Mask) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005162 // Compute 32-amt.
5163 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5164 DAG.getConstant(NVTBits, Amt.getValueType()),
5165 Amt);
5166
5167 // Expand the incoming operand to be shifted, so that we have its parts
5168 SDOperand InL, InH;
5169 ExpandOp(Op, InL, InH);
5170 switch(Opc) {
5171 case ISD::SHL:
5172 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5173 Hi = DAG.getNode(ISD::OR, NVT,
5174 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5175 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5176 return true;
5177 case ISD::SRL:
5178 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5179 Lo = DAG.getNode(ISD::OR, NVT,
5180 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5181 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5182 return true;
5183 case ISD::SRA:
5184 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5185 Lo = DAG.getNode(ISD::OR, NVT,
5186 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5187 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5188 return true;
5189 }
5190 }
5191
Nate Begemanb0674922005-04-06 21:13:14 +00005192 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005193}
Chris Lattneraac464e2005-01-21 06:05:23 +00005194
Chris Lattner4add7e32005-01-23 04:42:50 +00005195
Chris Lattneraac464e2005-01-21 06:05:23 +00005196// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5197// does not fit into a register, return the lo part and set the hi part to the
5198// by-reg argument. If it does fit into a single register, return the result
5199// and leave the Hi part unset.
Duncan Sands844d55a2008-04-12 17:14:18 +00005200SDOperand SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00005201 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00005202 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5203 // The input chain to this libcall is the entry node of the function.
5204 // Legalizing the call will automatically add the previous call to the
5205 // dependence.
5206 SDOperand InChain = DAG.getEntryNode();
5207
Chris Lattneraac464e2005-01-21 06:05:23 +00005208 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00005209 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00005210 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5211 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5212 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00005213 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00005214 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005215 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00005216 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00005217 }
Duncan Sands844d55a2008-04-12 17:14:18 +00005218 SDOperand Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
5219 TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00005220
Chris Lattner06bbeb62005-05-11 19:02:11 +00005221 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00005222 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00005223 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005224 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5225 false, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00005226
Chris Lattner462505f2006-02-13 09:18:02 +00005227 // Legalize the call sequence, starting with the chain. This will advance
5228 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5229 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5230 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00005231 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005232 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005233 default: assert(0 && "Unknown thing");
5234 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005235 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00005236 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005237 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00005238 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00005239 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005240 }
Chris Lattner63022662005-09-02 20:26:58 +00005241 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005242}
5243
Chris Lattner4add7e32005-01-23 04:42:50 +00005244
Evan Chengbf535fc2007-04-27 07:33:31 +00005245/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5246///
Chris Lattneraac464e2005-01-21 06:05:23 +00005247SDOperand SelectionDAGLegalize::
5248ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmand9d874b2008-03-05 01:08:17 +00005249 MVT::ValueType SourceVT = Source.getValueType();
Dan Gohmand6819da2008-03-11 01:59:03 +00005250 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattneraac464e2005-01-21 06:05:23 +00005251
Evan Cheng0bd72c52008-04-01 02:18:22 +00005252 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5253 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmanda7897c2008-03-05 02:07:31 +00005254 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005255 // incoming integer is set. To handle this, we dynamically test to see if
5256 // it is set, and, if so, add a fudge factor.
Dan Gohmand6819da2008-03-11 01:59:03 +00005257 SDOperand Hi;
5258 if (ExpandSource) {
5259 SDOperand Lo;
5260 ExpandOp(Source, Lo, Hi);
5261 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5262 } else {
5263 // The comparison for the sign bit will use the entire operand.
5264 Hi = Source;
5265 }
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005266
Chris Lattner2a4f7312005-05-13 04:45:13 +00005267 // If this is unsigned, and not supported, first perform the conversion to
5268 // signed, then adjust the result if the sign bit is set.
Dan Gohmand6819da2008-03-11 01:59:03 +00005269 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner2a4f7312005-05-13 04:45:13 +00005270
Scott Michela6729e82008-03-10 15:42:14 +00005271 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005272 DAG.getConstant(0, Hi.getValueType()),
5273 ISD::SETLT);
Chris Lattner72733e52008-01-17 07:00:52 +00005274 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005275 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5276 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00005277 uint64_t FF = 0x5f800000ULL;
5278 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmanda7897c2008-03-05 02:07:31 +00005279 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005280
Chris Lattnerc30405e2005-08-26 17:15:30 +00005281 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005282 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5283 SDOperand FudgeInReg;
5284 if (DestTy == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005285 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005286 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00005287 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00005288 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00005289 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00005290 CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005291 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005292 MVT::f32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00005293 else
5294 assert(0 && "Unexpected conversion");
5295
Evan Chengbf535fc2007-04-27 07:33:31 +00005296 MVT::ValueType SCVT = SignedConv.getValueType();
5297 if (SCVT != DestTy) {
5298 // Destination type needs to be expanded as well. The FADD now we are
5299 // constructing will be expanded into a libcall.
5300 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmand9d874b2008-03-05 01:08:17 +00005301 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5302 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Chengbf535fc2007-04-27 07:33:31 +00005303 SignedConv, SignedConv.getValue(1));
5304 }
5305 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5306 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005307 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005308 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005309
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005310 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand9d874b2008-03-05 01:08:17 +00005311 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005312 default: assert(0 && "This action not implemented for this operation!");
5313 case TargetLowering::Legal:
5314 case TargetLowering::Expand:
5315 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005316 case TargetLowering::Custom: {
5317 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5318 Source), DAG);
5319 if (NV.Val)
5320 return LegalizeOp(NV);
5321 break; // The target decided this was legal after all
5322 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005323 }
5324
Chris Lattner153587e2005-05-12 07:00:44 +00005325 // Expand the source, then glue it back together for the call. We must expand
5326 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohmand6819da2008-03-11 01:59:03 +00005327 if (ExpandSource) {
5328 SDOperand SrcLo, SrcHi;
5329 ExpandOp(Source, SrcLo, SrcHi);
5330 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5331 }
Chris Lattner153587e2005-05-12 07:00:44 +00005332
Evan Cheng31cbddf2007-01-12 02:11:51 +00005333 RTLIB::Libcall LC;
Evan Cheng86e476b2008-04-01 01:50:16 +00005334 if (SourceVT == MVT::i32) {
5335 if (DestTy == MVT::f32)
Evan Cheng4cabe4b2008-04-01 02:00:09 +00005336 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng86e476b2008-04-01 01:50:16 +00005337 else {
5338 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5339 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
5340 }
5341 } else if (SourceVT == MVT::i64) {
Dan Gohmand9d874b2008-03-05 01:08:17 +00005342 if (DestTy == MVT::f32)
5343 LC = RTLIB::SINTTOFP_I64_F32;
Dan Gohmand6819da2008-03-11 01:59:03 +00005344 else if (DestTy == MVT::f64)
Dan Gohmand9d874b2008-03-05 01:08:17 +00005345 LC = RTLIB::SINTTOFP_I64_F64;
Dan Gohmand6819da2008-03-11 01:59:03 +00005346 else if (DestTy == MVT::f80)
5347 LC = RTLIB::SINTTOFP_I64_F80;
5348 else {
5349 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5350 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dan Gohmand9d874b2008-03-05 01:08:17 +00005351 }
5352 } else if (SourceVT == MVT::i128) {
5353 if (DestTy == MVT::f32)
5354 LC = RTLIB::SINTTOFP_I128_F32;
5355 else if (DestTy == MVT::f64)
5356 LC = RTLIB::SINTTOFP_I128_F64;
5357 else if (DestTy == MVT::f80)
5358 LC = RTLIB::SINTTOFP_I128_F80;
5359 else {
5360 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5361 LC = RTLIB::SINTTOFP_I128_PPCF128;
5362 }
5363 } else {
5364 assert(0 && "Unknown int value type");
Chris Lattner06bbeb62005-05-11 19:02:11 +00005365 }
Chris Lattner462505f2006-02-13 09:18:02 +00005366
Evan Chengbf535fc2007-04-27 07:33:31 +00005367 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00005368 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohmanf4300952008-03-10 23:03:31 +00005369 SDOperand HiPart;
Duncan Sands844d55a2008-04-12 17:14:18 +00005370 SDOperand Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart);
Evan Cheng86e476b2008-04-01 01:50:16 +00005371 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmanf4300952008-03-10 23:03:31 +00005372 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5373 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005374}
Misha Brukman835702a2005-04-21 22:36:52 +00005375
Chris Lattner689bdcc2006-01-28 08:25:58 +00005376/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5377/// INT_TO_FP operation of the specified operand when the target requests that
5378/// we expand it. At this point, we know that the result and operand types are
5379/// legal for the target.
5380SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5381 SDOperand Op0,
5382 MVT::ValueType DestVT) {
5383 if (Op0.getValueType() == MVT::i32) {
5384 // simple 32-bit [signed|unsigned] integer to float/double expansion
5385
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005386 // Get the stack frame index of a 8 byte buffer.
5387 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5388
Chris Lattner689bdcc2006-01-28 08:25:58 +00005389 // word offset constant for Hi/Lo address computation
5390 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5391 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00005392 SDOperand Hi = StackSlot;
5393 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5394 if (TLI.isLittleEndian())
5395 std::swap(Hi, Lo);
5396
Chris Lattner689bdcc2006-01-28 08:25:58 +00005397 // if signed map to unsigned space
5398 SDOperand Op0Mapped;
5399 if (isSigned) {
5400 // constant used to invert sign bit (signed to unsigned mapping)
5401 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5402 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5403 } else {
5404 Op0Mapped = Op0;
5405 }
5406 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00005407 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005408 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005409 // initial hi portion of constructed double
5410 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5411 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00005412 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005413 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00005414 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005415 // FP constant to bias correct the final result
5416 SDOperand Bias = DAG.getConstantFP(isSigned ?
5417 BitsToDouble(0x4330000080000000ULL)
5418 : BitsToDouble(0x4330000000000000ULL),
5419 MVT::f64);
5420 // subtract the bias
5421 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5422 // final result
5423 SDOperand Result;
5424 // handle final rounding
5425 if (DestVT == MVT::f64) {
5426 // do nothing
5427 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00005428 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner72733e52008-01-17 07:00:52 +00005429 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5430 DAG.getIntPtrConstant(0));
Dale Johannesen7f724e92007-09-16 16:51:49 +00005431 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5432 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005433 }
5434 return Result;
5435 }
5436 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5437 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5438
Scott Michela6729e82008-03-10 15:42:14 +00005439 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005440 DAG.getConstant(0, Op0.getValueType()),
5441 ISD::SETLT);
Chris Lattner72733e52008-01-17 07:00:52 +00005442 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005443 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5444 SignSet, Four, Zero);
5445
5446 // If the sign bit of the integer is set, the large number will be treated
5447 // as a negative number. To counteract this, the dynamic code adds an
5448 // offset depending on the data type.
5449 uint64_t FF;
5450 switch (Op0.getValueType()) {
5451 default: assert(0 && "Unsupported integer type!");
5452 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5453 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5454 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5455 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5456 }
5457 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005458 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005459
5460 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5461 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5462 SDOperand FudgeInReg;
5463 if (DestVT == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005464 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005465 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005466 else {
Dan Gohman2d489b52008-02-06 22:27:42 +00005467 FudgeInReg =
5468 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5469 DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005470 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005471 MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005472 }
5473
5474 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5475}
5476
5477/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5478/// *INT_TO_FP operation of the specified operand when the target requests that
5479/// we promote it. At this point, we know that the result and operand types are
5480/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5481/// operation that takes a larger input.
5482SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5483 MVT::ValueType DestVT,
5484 bool isSigned) {
5485 // First step, figure out the appropriate *INT_TO_FP operation to use.
5486 MVT::ValueType NewInTy = LegalOp.getValueType();
5487
5488 unsigned OpToUse = 0;
5489
5490 // Scan for the appropriate larger type to use.
5491 while (1) {
5492 NewInTy = (MVT::ValueType)(NewInTy+1);
5493 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5494
5495 // If the target supports SINT_TO_FP of this type, use it.
5496 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5497 default: break;
5498 case TargetLowering::Legal:
5499 if (!TLI.isTypeLegal(NewInTy))
5500 break; // Can't use this datatype.
5501 // FALL THROUGH.
5502 case TargetLowering::Custom:
5503 OpToUse = ISD::SINT_TO_FP;
5504 break;
5505 }
5506 if (OpToUse) break;
5507 if (isSigned) continue;
5508
5509 // If the target supports UINT_TO_FP of this type, use it.
5510 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5511 default: break;
5512 case TargetLowering::Legal:
5513 if (!TLI.isTypeLegal(NewInTy))
5514 break; // Can't use this datatype.
5515 // FALL THROUGH.
5516 case TargetLowering::Custom:
5517 OpToUse = ISD::UINT_TO_FP;
5518 break;
5519 }
5520 if (OpToUse) break;
5521
5522 // Otherwise, try a larger type.
5523 }
5524
5525 // Okay, we found the operation and type to use. Zero extend our input to the
5526 // desired type then run the operation on it.
5527 return DAG.getNode(OpToUse, DestVT,
5528 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5529 NewInTy, LegalOp));
5530}
5531
5532/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5533/// FP_TO_*INT operation of the specified operand when the target requests that
5534/// we promote it. At this point, we know that the result and operand types are
5535/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5536/// operation that returns a larger result.
5537SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5538 MVT::ValueType DestVT,
5539 bool isSigned) {
5540 // First step, figure out the appropriate FP_TO*INT operation to use.
5541 MVT::ValueType NewOutTy = DestVT;
5542
5543 unsigned OpToUse = 0;
5544
5545 // Scan for the appropriate larger type to use.
5546 while (1) {
5547 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5548 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5549
5550 // If the target supports FP_TO_SINT returning this type, use it.
5551 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5552 default: break;
5553 case TargetLowering::Legal:
5554 if (!TLI.isTypeLegal(NewOutTy))
5555 break; // Can't use this datatype.
5556 // FALL THROUGH.
5557 case TargetLowering::Custom:
5558 OpToUse = ISD::FP_TO_SINT;
5559 break;
5560 }
5561 if (OpToUse) break;
5562
5563 // If the target supports FP_TO_UINT of this type, use it.
5564 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5565 default: break;
5566 case TargetLowering::Legal:
5567 if (!TLI.isTypeLegal(NewOutTy))
5568 break; // Can't use this datatype.
5569 // FALL THROUGH.
5570 case TargetLowering::Custom:
5571 OpToUse = ISD::FP_TO_UINT;
5572 break;
5573 }
5574 if (OpToUse) break;
5575
5576 // Otherwise, try a larger type.
5577 }
5578
Chris Lattnerf81d5882007-11-24 07:07:01 +00005579
5580 // Okay, we found the operation and type to use.
5581 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5582
5583 // If the operation produces an invalid type, it must be custom lowered. Use
5584 // the target lowering hooks to expand it. Just keep the low part of the
5585 // expanded operation, we know that we're truncating anyway.
5586 if (getTypeAction(NewOutTy) == Expand) {
5587 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5588 assert(Operation.Val && "Didn't return anything");
5589 }
5590
5591 // Truncate the result of the extended FP_TO_*INT operation to the desired
5592 // size.
5593 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005594}
5595
5596/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5597///
5598SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5599 MVT::ValueType VT = Op.getValueType();
5600 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5601 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5602 switch (VT) {
5603 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5604 case MVT::i16:
5605 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5606 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5607 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5608 case MVT::i32:
5609 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5610 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5611 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5612 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5613 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5614 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5615 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5616 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5617 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5618 case MVT::i64:
5619 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5620 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5621 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5622 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5623 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5624 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5625 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5626 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5627 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5628 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5629 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5630 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5631 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5632 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5633 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5634 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5635 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5636 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5637 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5638 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5639 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5640 }
5641}
5642
5643/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5644///
5645SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5646 switch (Opc) {
5647 default: assert(0 && "Cannot expand this yet!");
5648 case ISD::CTPOP: {
5649 static const uint64_t mask[6] = {
5650 0x5555555555555555ULL, 0x3333333333333333ULL,
5651 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5652 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5653 };
5654 MVT::ValueType VT = Op.getValueType();
5655 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005656 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005657 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5658 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5659 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5660 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5661 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5662 DAG.getNode(ISD::AND, VT,
5663 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5664 }
5665 return Op;
5666 }
5667 case ISD::CTLZ: {
5668 // for now, we do this:
5669 // x = x | (x >> 1);
5670 // x = x | (x >> 2);
5671 // ...
5672 // x = x | (x >>16);
5673 // x = x | (x >>32); // for 64-bit input
5674 // return popcount(~x);
5675 //
5676 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5677 MVT::ValueType VT = Op.getValueType();
5678 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005679 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005680 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5681 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5682 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5683 }
5684 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5685 return DAG.getNode(ISD::CTPOP, VT, Op);
5686 }
5687 case ISD::CTTZ: {
5688 // for now, we use: { return popcount(~x & (x - 1)); }
5689 // unless the target has ctlz but not ctpop, in which case we use:
5690 // { return 32 - nlz(~x & (x-1)); }
5691 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5692 MVT::ValueType VT = Op.getValueType();
5693 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5694 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5695 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5696 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5697 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5698 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5699 TLI.isOperationLegal(ISD::CTLZ, VT))
5700 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005701 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005702 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5703 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5704 }
5705 }
5706}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005707
Chris Lattnerdc750592005-01-07 07:47:09 +00005708/// ExpandOp - Expand the specified SDOperand into its two component pieces
5709/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5710/// LegalizeNodes map is filled in for any results that are not expanded, the
5711/// ExpandedNodes map is filled in for any results that are expanded, and the
5712/// Lo/Hi values are returned.
5713void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5714 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005715 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005716 SDNode *Node = Op.Val;
5717 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005718 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005719 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005720 "Cannot expand to FP value or to larger int value!");
5721
Chris Lattner1a570f12005-09-02 20:32:45 +00005722 // See if we already expanded it.
Roman Levensteina3ee1a32008-04-16 16:15:27 +00005723 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005724 = ExpandedNodes.find(Op);
5725 if (I != ExpandedNodes.end()) {
5726 Lo = I->second.first;
5727 Hi = I->second.second;
5728 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005729 }
5730
Chris Lattnerdc750592005-01-07 07:47:09 +00005731 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005732 case ISD::CopyFromReg:
5733 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005734 case ISD::FP_ROUND_INREG:
5735 if (VT == MVT::ppcf128 &&
5736 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5737 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005738 SDOperand SrcLo, SrcHi, Src;
5739 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5740 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5741 SDOperand Result = TLI.LowerOperation(
5742 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005743 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5744 Lo = Result.Val->getOperand(0);
5745 Hi = Result.Val->getOperand(1);
5746 break;
5747 }
5748 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005749 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005750#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005751 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005752#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005753 assert(0 && "Do not know how to expand this operator!");
5754 abort();
Dan Gohman66272a52008-02-27 01:52:30 +00005755 case ISD::EXTRACT_ELEMENT:
5756 ExpandOp(Node->getOperand(0), Lo, Hi);
5757 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5758 return ExpandOp(Hi, Lo, Hi);
Dan Gohmane5e32ec2008-02-27 19:44:57 +00005759 return ExpandOp(Lo, Lo, Hi);
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005760 case ISD::EXTRACT_VECTOR_ELT:
5761 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5762 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5763 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5764 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005765 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005766 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005767 Lo = DAG.getNode(ISD::UNDEF, NVT);
5768 Hi = DAG.getNode(ISD::UNDEF, NVT);
5769 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005770 case ISD::Constant: {
Dan Gohmanf2bbfa32008-03-03 22:20:46 +00005771 unsigned NVTBits = MVT::getSizeInBits(NVT);
5772 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5773 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5774 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005775 break;
5776 }
Evan Cheng47833a12006-12-12 21:32:44 +00005777 case ISD::ConstantFP: {
5778 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005779 if (CFP->getValueType(0) == MVT::ppcf128) {
5780 APInt api = CFP->getValueAPF().convertToAPInt();
5781 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5782 MVT::f64);
5783 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5784 MVT::f64);
5785 break;
5786 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005787 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005788 if (getTypeAction(Lo.getValueType()) == Expand)
5789 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005790 break;
5791 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005792 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005793 // Return the operands.
5794 Lo = Node->getOperand(0);
5795 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005796 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00005797
5798 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00005799 if (Node->getNumValues() == 1) {
5800 ExpandOp(Op.getOperand(0), Lo, Hi);
5801 break;
5802 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005803 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5804 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5805 Op.getValue(1).getValueType() == MVT::Other &&
5806 "unhandled MERGE_VALUES");
5807 ExpandOp(Op.getOperand(0), Lo, Hi);
5808 // Remember that we legalized the chain.
5809 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5810 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005811
5812 case ISD::SIGN_EXTEND_INREG:
5813 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005814 // sext_inreg the low part if needed.
5815 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5816
5817 // The high part gets the sign extension from the lo-part. This handles
5818 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005819 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5820 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5821 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005822 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005823
Nate Begeman2fba8a32006-01-14 03:14:10 +00005824 case ISD::BSWAP: {
5825 ExpandOp(Node->getOperand(0), Lo, Hi);
5826 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5827 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5828 Lo = TempLo;
5829 break;
5830 }
5831
Chris Lattner55e9cde2005-05-11 04:51:16 +00005832 case ISD::CTPOP:
5833 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005834 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5835 DAG.getNode(ISD::CTPOP, NVT, Lo),
5836 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005837 Hi = DAG.getConstant(0, NVT);
5838 break;
5839
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005840 case ISD::CTLZ: {
5841 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005842 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005843 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5844 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Scott Michela6729e82008-03-10 15:42:14 +00005845 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005846 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005847 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5848 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5849
5850 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5851 Hi = DAG.getConstant(0, NVT);
5852 break;
5853 }
5854
5855 case ISD::CTTZ: {
5856 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005857 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005858 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5859 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Scott Michela6729e82008-03-10 15:42:14 +00005860 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005861 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005862 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5863 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5864
5865 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5866 Hi = DAG.getConstant(0, NVT);
5867 break;
5868 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005869
Nate Begemane74795c2006-01-25 18:21:52 +00005870 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005871 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5872 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005873 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5874 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5875
5876 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005877 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005878 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005879 if (TLI.isBigEndian())
Nate Begemane74795c2006-01-25 18:21:52 +00005880 std::swap(Lo, Hi);
5881 break;
5882 }
5883
Chris Lattnerdc750592005-01-07 07:47:09 +00005884 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005885 LoadSDNode *LD = cast<LoadSDNode>(Node);
5886 SDOperand Ch = LD->getChain(); // Legalize the chain.
5887 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5888 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005889 int SVOffset = LD->getSrcValueOffset();
5890 unsigned Alignment = LD->getAlignment();
5891 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005892
Evan Chenge71fe34d2006-10-09 20:57:25 +00005893 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005894 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5895 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005896 if (VT == MVT::f32 || VT == MVT::f64) {
5897 // f32->i32 or f64->i64 one to one expansion.
5898 // Remember that we legalized the chain.
5899 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005900 // Recursively expand the new load.
5901 if (getTypeAction(NVT) == Expand)
5902 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005903 break;
5904 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005905
Evan Chenge71fe34d2006-10-09 20:57:25 +00005906 // Increment the pointer to the other half.
5907 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5908 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00005909 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005910 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005911 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005912 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5913 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005914
Evan Chenge71fe34d2006-10-09 20:57:25 +00005915 // Build a factor node to remember that this load is independent of the
5916 // other one.
5917 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5918 Hi.getValue(1));
5919
5920 // Remember that we legalized the chain.
5921 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005922 if (TLI.isBigEndian())
Evan Chenge71fe34d2006-10-09 20:57:25 +00005923 std::swap(Lo, Hi);
5924 } else {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005925 MVT::ValueType EVT = LD->getMemoryVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005926
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005927 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5928 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005929 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5930 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005931 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005932 // Remember that we legalized the chain.
5933 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5934 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5935 break;
5936 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005937
5938 if (EVT == NVT)
5939 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005940 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005941 else
5942 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005943 SVOffset, EVT, isVolatile,
5944 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005945
5946 // Remember that we legalized the chain.
5947 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5948
5949 if (ExtType == ISD::SEXTLOAD) {
5950 // The high part is obtained by SRA'ing all but one of the bits of the
5951 // lo part.
5952 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5953 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5954 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5955 } else if (ExtType == ISD::ZEXTLOAD) {
5956 // The high part is just a zero.
5957 Hi = DAG.getConstant(0, NVT);
5958 } else /* if (ExtType == ISD::EXTLOAD) */ {
5959 // The high part is undefined.
5960 Hi = DAG.getNode(ISD::UNDEF, NVT);
5961 }
5962 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005963 break;
5964 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005965 case ISD::AND:
5966 case ISD::OR:
5967 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5968 SDOperand LL, LH, RL, RH;
5969 ExpandOp(Node->getOperand(0), LL, LH);
5970 ExpandOp(Node->getOperand(1), RL, RH);
5971 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5972 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5973 break;
5974 }
5975 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005976 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005977 ExpandOp(Node->getOperand(1), LL, LH);
5978 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005979 if (getTypeAction(NVT) == Expand)
5980 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005981 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005982 if (VT != MVT::f32)
5983 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005984 break;
5985 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005986 case ISD::SELECT_CC: {
5987 SDOperand TL, TH, FL, FH;
5988 ExpandOp(Node->getOperand(2), TL, TH);
5989 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005990 if (getTypeAction(NVT) == Expand)
5991 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005992 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5993 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005994 if (VT != MVT::f32)
5995 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5996 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005997 break;
5998 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005999 case ISD::ANY_EXTEND:
6000 // The low part is any extension of the input (which degenerates to a copy).
6001 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6002 // The high part is undefined.
6003 Hi = DAG.getNode(ISD::UNDEF, NVT);
6004 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00006005 case ISD::SIGN_EXTEND: {
6006 // The low part is just a sign extension of the input (which degenerates to
6007 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006008 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006009
Chris Lattnerdc750592005-01-07 07:47:09 +00006010 // The high part is obtained by SRA'ing all but one of the bits of the lo
6011 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00006012 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006013 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6014 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00006015 break;
6016 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006017 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00006018 // The low part is just a zero extension of the input (which degenerates to
6019 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006020 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006021
Chris Lattnerdc750592005-01-07 07:47:09 +00006022 // The high part is just a zero.
6023 Hi = DAG.getConstant(0, NVT);
6024 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00006025
Chris Lattner59b27fa372007-02-13 23:55:16 +00006026 case ISD::TRUNCATE: {
6027 // The input value must be larger than this value. Expand *it*.
6028 SDOperand NewLo;
6029 ExpandOp(Node->getOperand(0), NewLo, Hi);
6030
6031 // The low part is now either the right size, or it is closer. If not the
6032 // right size, make an illegal truncate so we recursively expand it.
6033 if (NewLo.getValueType() != Node->getValueType(0))
6034 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6035 ExpandOp(NewLo, Lo, Hi);
6036 break;
6037 }
6038
Chris Lattner36e663d2005-12-23 00:16:34 +00006039 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006040 SDOperand Tmp;
6041 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6042 // If the target wants to, allow it to lower this itself.
6043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6044 case Expand: assert(0 && "cannot expand FP!");
6045 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6046 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6047 }
6048 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6049 }
6050
Evan Cheng4eee7242006-12-09 02:42:38 +00006051 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00006052 if (VT == MVT::f32 || VT == MVT::f64) {
6053 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00006054 if (getTypeAction(NVT) == Expand)
6055 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00006056 break;
6057 }
6058
6059 // If source operand will be expanded to the same type as VT, i.e.
6060 // i64 <- f64, i32 <- f32, expand the source operand instead.
6061 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6062 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6063 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006064 break;
6065 }
6066
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006067 // Turn this into a load/store pair by default.
6068 if (Tmp.Val == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00006069 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006070
Chris Lattner36e663d2005-12-23 00:16:34 +00006071 ExpandOp(Tmp, Lo, Hi);
6072 break;
6073 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006074
Chris Lattnerf81d5882007-11-24 07:07:01 +00006075 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00006076 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6077 TargetLowering::Custom &&
6078 "Must custom expand ReadCycleCounter");
Chris Lattnerf81d5882007-11-24 07:07:01 +00006079 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6080 assert(Tmp.Val && "Node must be custom expanded!");
6081 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner44c28c22005-11-20 22:56:56 +00006082 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00006083 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006084 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006085 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006086
Andrew Lenharth357061a2008-03-05 01:15:49 +00006087 case ISD::ATOMIC_LCS: {
6088 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6089 assert(Tmp.Val && "Node must be custom expanded!");
6090 ExpandOp(Tmp.getValue(0), Lo, Hi);
6091 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6092 LegalizeOp(Tmp.getValue(1)));
6093 break;
6094 }
6095
6096
6097
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006098 // These operators cannot be expanded directly, emit them as calls to
6099 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006100 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006101 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00006102 SDOperand Op;
6103 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6104 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006105 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6106 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00006107 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006108
Chris Lattner941d84a2005-07-30 01:40:57 +00006109 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6110
Chris Lattnerfe68d752005-07-29 00:33:32 +00006111 // Now that the custom expander is done, expand the result, which is still
6112 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006113 if (Op.Val) {
6114 ExpandOp(Op, Lo, Hi);
6115 break;
6116 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006117 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006118
Dale Johannesenc0154c02007-10-05 20:04:43 +00006119 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf4300952008-03-10 23:03:31 +00006120 if (VT == MVT::i64) {
6121 if (Node->getOperand(0).getValueType() == MVT::f32)
6122 LC = RTLIB::FPTOSINT_F32_I64;
6123 else if (Node->getOperand(0).getValueType() == MVT::f64)
6124 LC = RTLIB::FPTOSINT_F64_I64;
6125 else if (Node->getOperand(0).getValueType() == MVT::f80)
6126 LC = RTLIB::FPTOSINT_F80_I64;
6127 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6128 LC = RTLIB::FPTOSINT_PPCF128_I64;
Duncan Sands844d55a2008-04-12 17:14:18 +00006129 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf4300952008-03-10 23:03:31 +00006130 } else if (VT == MVT::i128) {
6131 if (Node->getOperand(0).getValueType() == MVT::f32)
6132 LC = RTLIB::FPTOSINT_F32_I128;
6133 else if (Node->getOperand(0).getValueType() == MVT::f64)
6134 LC = RTLIB::FPTOSINT_F64_I128;
6135 else if (Node->getOperand(0).getValueType() == MVT::f80)
6136 LC = RTLIB::FPTOSINT_F80_I128;
6137 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6138 LC = RTLIB::FPTOSINT_PPCF128_I128;
Duncan Sands844d55a2008-04-12 17:14:18 +00006139 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf4300952008-03-10 23:03:31 +00006140 } else {
6141 assert(0 && "Unexpected uint-to-fp conversion!");
6142 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006143 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006144 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006145
Evan Cheng31cbddf2007-01-12 02:11:51 +00006146 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006147 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006148 SDOperand Op;
6149 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6150 case Expand: assert(0 && "cannot expand FP!");
6151 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6152 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6153 }
6154
6155 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6156
6157 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006158 if (Op.Val) {
6159 ExpandOp(Op, Lo, Hi);
6160 break;
6161 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006162 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006163
Evan Chengfd11ef42007-10-05 01:09:32 +00006164 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmanf4300952008-03-10 23:03:31 +00006165 if (VT == MVT::i64) {
6166 if (Node->getOperand(0).getValueType() == MVT::f32)
6167 LC = RTLIB::FPTOUINT_F32_I64;
6168 else if (Node->getOperand(0).getValueType() == MVT::f64)
6169 LC = RTLIB::FPTOUINT_F64_I64;
6170 else if (Node->getOperand(0).getValueType() == MVT::f80)
6171 LC = RTLIB::FPTOUINT_F80_I64;
6172 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6173 LC = RTLIB::FPTOUINT_PPCF128_I64;
Duncan Sands844d55a2008-04-12 17:14:18 +00006174 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf4300952008-03-10 23:03:31 +00006175 } else if (VT == MVT::i128) {
6176 if (Node->getOperand(0).getValueType() == MVT::f32)
6177 LC = RTLIB::FPTOUINT_F32_I128;
6178 else if (Node->getOperand(0).getValueType() == MVT::f64)
6179 LC = RTLIB::FPTOUINT_F64_I128;
6180 else if (Node->getOperand(0).getValueType() == MVT::f80)
6181 LC = RTLIB::FPTOUINT_F80_I128;
6182 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6183 LC = RTLIB::FPTOUINT_PPCF128_I128;
Duncan Sands844d55a2008-04-12 17:14:18 +00006184 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Dan Gohmanf4300952008-03-10 23:03:31 +00006185 } else {
6186 assert(0 && "Unexpected uint-to-fp conversion!");
6187 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006188 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006189 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006190
Evan Cheng870e4f82006-01-09 18:31:59 +00006191 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006192 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006193 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006194 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006195 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006196 Op = TLI.LowerOperation(Op, DAG);
6197 if (Op.Val) {
6198 // Now that the custom expander is done, expand the result, which is
6199 // still VT.
6200 ExpandOp(Op, Lo, Hi);
6201 break;
6202 }
6203 }
6204
Chris Lattner72b503b2006-09-13 03:50:39 +00006205 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6206 // this X << 1 as X+X.
6207 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00006208 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner72b503b2006-09-13 03:50:39 +00006209 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6210 SDOperand LoOps[2], HiOps[3];
6211 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6212 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6213 LoOps[1] = LoOps[0];
6214 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6215
6216 HiOps[1] = HiOps[0];
6217 HiOps[2] = Lo.getValue(1);
6218 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6219 break;
6220 }
6221 }
6222
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006223 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006224 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006225 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006226
6227 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006228 TargetLowering::LegalizeAction Action =
6229 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6230 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6231 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006232 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006233 break;
6234 }
6235
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006236 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006237 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006238 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006239 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006240
Evan Cheng870e4f82006-01-09 18:31:59 +00006241 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006242 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006243 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006244 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006245 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006246 Op = TLI.LowerOperation(Op, DAG);
6247 if (Op.Val) {
6248 // Now that the custom expander is done, expand the result, which is
6249 // still VT.
6250 ExpandOp(Op, Lo, Hi);
6251 break;
6252 }
6253 }
6254
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006255 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006256 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006257 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006258
6259 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006260 TargetLowering::LegalizeAction Action =
6261 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6262 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6263 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006264 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006265 break;
6266 }
6267
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006268 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006269 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006270 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006271 }
6272
6273 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006274 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006275 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006276 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006277 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006278 Op = TLI.LowerOperation(Op, DAG);
6279 if (Op.Val) {
6280 // Now that the custom expander is done, expand the result, which is
6281 // still VT.
6282 ExpandOp(Op, Lo, Hi);
6283 break;
6284 }
6285 }
6286
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006287 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006288 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006289 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006290
6291 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006292 TargetLowering::LegalizeAction Action =
6293 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6294 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6295 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006296 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006297 break;
6298 }
6299
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006300 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006301 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006302 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006303 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006304
Misha Brukman835702a2005-04-21 22:36:52 +00006305 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006306 case ISD::SUB: {
6307 // If the target wants to custom expand this, let them.
6308 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6309 TargetLowering::Custom) {
6310 Op = TLI.LowerOperation(Op, DAG);
6311 if (Op.Val) {
6312 ExpandOp(Op, Lo, Hi);
6313 break;
6314 }
6315 }
6316
6317 // Expand the subcomponents.
6318 SDOperand LHSL, LHSH, RHSL, RHSH;
6319 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6320 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00006321 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6322 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006323 LoOps[0] = LHSL;
6324 LoOps[1] = RHSL;
6325 HiOps[0] = LHSH;
6326 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00006327 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00006328 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006329 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006330 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006331 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00006332 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006333 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006334 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006335 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00006336 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006337 }
Chris Lattner2135bc02007-05-17 18:15:41 +00006338
6339 case ISD::ADDC:
6340 case ISD::SUBC: {
6341 // Expand the subcomponents.
6342 SDOperand LHSL, LHSH, RHSL, RHSH;
6343 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6344 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6345 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6346 SDOperand LoOps[2] = { LHSL, RHSL };
6347 SDOperand HiOps[3] = { LHSH, RHSH };
6348
6349 if (Node->getOpcode() == ISD::ADDC) {
6350 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6351 HiOps[2] = Lo.getValue(1);
6352 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6353 } else {
6354 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6355 HiOps[2] = Lo.getValue(1);
6356 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6357 }
6358 // Remember that we legalized the flag.
6359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6360 break;
6361 }
6362 case ISD::ADDE:
6363 case ISD::SUBE: {
6364 // Expand the subcomponents.
6365 SDOperand LHSL, LHSH, RHSL, RHSH;
6366 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6367 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6368 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6369 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6370 SDOperand HiOps[3] = { LHSH, RHSH };
6371
6372 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6373 HiOps[2] = Lo.getValue(1);
6374 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6375
6376 // Remember that we legalized the flag.
6377 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6378 break;
6379 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006380 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006381 // If the target wants to custom expand this, let them.
6382 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00006383 SDOperand New = TLI.LowerOperation(Op, DAG);
6384 if (New.Val) {
6385 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006386 break;
6387 }
6388 }
6389
Evan Chenge93762d2006-09-01 18:17:58 +00006390 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6391 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00006392 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6393 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6394 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00006395 SDOperand LL, LH, RL, RH;
6396 ExpandOp(Node->getOperand(0), LL, LH);
6397 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman1f372ed2008-02-25 21:11:39 +00006398 unsigned OuterBitSize = Op.getValueSizeInBits();
6399 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohmana1603612007-10-08 18:33:35 +00006400 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6401 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman272e2342008-03-10 20:42:19 +00006402 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6403 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6404 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohmana1603612007-10-08 18:33:35 +00006405 // The inputs are both zero-extended.
6406 if (HasUMUL_LOHI) {
6407 // We can emit a umul_lohi.
6408 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6409 Hi = SDOperand(Lo.Val, 1);
6410 break;
6411 }
6412 if (HasMULHU) {
6413 // We can emit a mulhu+mul.
6414 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6415 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6416 break;
6417 }
Dan Gohmana1603612007-10-08 18:33:35 +00006418 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00006419 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohmana1603612007-10-08 18:33:35 +00006420 // The input values are both sign-extended.
6421 if (HasSMUL_LOHI) {
6422 // We can emit a smul_lohi.
6423 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6424 Hi = SDOperand(Lo.Val, 1);
6425 break;
6426 }
6427 if (HasMULHS) {
6428 // We can emit a mulhs+mul.
6429 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6430 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6431 break;
6432 }
6433 }
6434 if (HasUMUL_LOHI) {
6435 // Lo,Hi = umul LHS, RHS.
6436 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6437 DAG.getVTList(NVT, NVT), LL, RL);
6438 Lo = UMulLOHI;
6439 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00006440 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6441 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6442 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6443 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00006444 break;
Nate Begeman43144a22005-08-30 02:44:00 +00006445 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00006446 if (HasMULHU) {
6447 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6448 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6449 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6450 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6451 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6452 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6453 break;
6454 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006455 }
Evan Chenge93762d2006-09-01 18:17:58 +00006456
Dan Gohmana1603612007-10-08 18:33:35 +00006457 // If nothing else, we can make a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006458 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00006459 break;
6460 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006461 case ISD::SDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00006462 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006463 break;
6464 case ISD::UDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00006465 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006466 break;
6467 case ISD::SREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00006468 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006469 break;
6470 case ISD::UREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00006471 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006472 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00006473
Evan Cheng4eee7242006-12-09 02:42:38 +00006474 case ISD::FADD:
Duncan Sands844d55a2008-04-12 17:14:18 +00006475 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6476 RTLIB::ADD_F64,
6477 RTLIB::ADD_F80,
6478 RTLIB::ADD_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006479 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006480 break;
6481 case ISD::FSUB:
Duncan Sands844d55a2008-04-12 17:14:18 +00006482 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6483 RTLIB::SUB_F64,
6484 RTLIB::SUB_F80,
6485 RTLIB::SUB_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006486 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006487 break;
6488 case ISD::FMUL:
Duncan Sands844d55a2008-04-12 17:14:18 +00006489 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6490 RTLIB::MUL_F64,
6491 RTLIB::MUL_F80,
6492 RTLIB::MUL_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006493 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006494 break;
6495 case ISD::FDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00006496 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6497 RTLIB::DIV_F64,
6498 RTLIB::DIV_F80,
6499 RTLIB::DIV_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006500 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006501 break;
6502 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006503 if (VT == MVT::ppcf128) {
6504 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6505 Node->getOperand(0).getValueType()==MVT::f64);
6506 const uint64_t zero = 0;
6507 if (Node->getOperand(0).getValueType()==MVT::f32)
6508 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6509 else
6510 Hi = Node->getOperand(0);
6511 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6512 break;
6513 }
Duncan Sands844d55a2008-04-12 17:14:18 +00006514 Lo = ExpandLibCall(RTLIB::FPEXT_F32_F64, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006515 break;
6516 case ISD::FP_ROUND:
Duncan Sands844d55a2008-04-12 17:14:18 +00006517 Lo = ExpandLibCall(RTLIB::FPROUND_F64_F32, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006518 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006519 case ISD::FPOWI:
Duncan Sands844d55a2008-04-12 17:14:18 +00006520 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32,
6521 RTLIB::POWI_F64,
6522 RTLIB::POWI_F80,
6523 RTLIB::POWI_PPCF128),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006524 Node, false, Hi);
6525 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006526 case ISD::FSQRT:
6527 case ISD::FSIN:
6528 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006529 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006530 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006531 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00006532 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6533 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006534 break;
6535 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00006536 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6537 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006538 break;
6539 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00006540 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6541 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006542 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006543 default: assert(0 && "Unreachable!");
6544 }
Duncan Sands844d55a2008-04-12 17:14:18 +00006545 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006546 break;
6547 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006548 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006549 if (VT == MVT::ppcf128) {
6550 SDOperand Tmp;
6551 ExpandOp(Node->getOperand(0), Lo, Tmp);
6552 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6553 // lo = hi==fabs(hi) ? lo : -lo;
6554 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6555 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6556 DAG.getCondCode(ISD::SETEQ));
6557 break;
6558 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006559 SDOperand Mask = (VT == MVT::f64)
6560 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6561 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6562 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6563 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6564 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6565 if (getTypeAction(NVT) == Expand)
6566 ExpandOp(Lo, Lo, Hi);
6567 break;
6568 }
6569 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006570 if (VT == MVT::ppcf128) {
6571 ExpandOp(Node->getOperand(0), Lo, Hi);
6572 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6573 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6574 break;
6575 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006576 SDOperand Mask = (VT == MVT::f64)
6577 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6578 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6579 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6580 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6581 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6582 if (getTypeAction(NVT) == Expand)
6583 ExpandOp(Lo, Lo, Hi);
6584 break;
6585 }
Evan Cheng003feb02007-01-04 21:56:39 +00006586 case ISD::FCOPYSIGN: {
6587 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6588 if (getTypeAction(NVT) == Expand)
6589 ExpandOp(Lo, Lo, Hi);
6590 break;
6591 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006592 case ISD::SINT_TO_FP:
6593 case ISD::UINT_TO_FP: {
6594 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6595 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen12c76db2008-03-18 17:28:38 +00006596
6597 // Promote the operand if needed. Do this before checking for
6598 // ppcf128 so conversions of i16 and i8 work.
6599 if (getTypeAction(SrcVT) == Promote) {
6600 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6601 Tmp = isSigned
6602 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6603 DAG.getValueType(SrcVT))
6604 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6605 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6606 SrcVT = Node->getOperand(0).getValueType();
6607 }
6608
Dan Gohmanf4300952008-03-10 23:03:31 +00006609 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman432e4a62008-02-25 21:39:34 +00006610 static const uint64_t zero = 0;
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006611 if (isSigned) {
6612 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6613 Node->getOperand(0)));
6614 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6615 } else {
Dan Gohman432e4a62008-02-25 21:39:34 +00006616 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006617 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6618 Node->getOperand(0)));
6619 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6620 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006621 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006622 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6623 DAG.getConstant(0, MVT::i32),
6624 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6625 DAG.getConstantFP(
6626 APFloat(APInt(128, 2, TwoE32)),
6627 MVT::ppcf128)),
6628 Hi,
6629 DAG.getCondCode(ISD::SETLT)),
6630 Lo, Hi);
6631 }
6632 break;
6633 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006634 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6635 // si64->ppcf128 done by libcall, below
Dan Gohman432e4a62008-02-25 21:39:34 +00006636 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006637 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6638 Lo, Hi);
6639 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6640 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6641 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6642 DAG.getConstant(0, MVT::i64),
6643 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6644 DAG.getConstantFP(
6645 APFloat(APInt(128, 2, TwoE64)),
6646 MVT::ppcf128)),
6647 Hi,
6648 DAG.getCondCode(ISD::SETLT)),
6649 Lo, Hi);
6650 break;
6651 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006652
Dan Gohmanf4300952008-03-10 23:03:31 +00006653 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6654 Node->getOperand(0));
Evan Cheng86e476b2008-04-01 01:50:16 +00006655 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng611abc02008-04-01 01:51:26 +00006656 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng86e476b2008-04-01 01:50:16 +00006657 ExpandOp(Lo, Lo, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006658 break;
6659 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006660 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006661
Chris Lattnerac12f682005-12-21 18:02:52 +00006662 // Make sure the resultant values have been legalized themselves, unless this
6663 // is a type that requires multi-step expansion.
6664 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6665 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006666 if (Hi.Val)
6667 // Don't legalize the high part if it is expanded to a single node.
6668 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006669 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006670
6671 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006672 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006673 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006674}
6675
Dan Gohmana8665142007-06-25 16:23:39 +00006676/// SplitVectorOp - Given an operand of vector type, break it down into
6677/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006678void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6679 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006680 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006681 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006682 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006683 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00006684
Dan Gohman60028182007-09-24 15:54:53 +00006685 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begemanbd117f02007-11-15 21:15:26 +00006686
6687 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6688 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6689
6690 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6691 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6692
Chris Lattner32206f52006-03-18 01:44:44 +00006693 // See if we already split it.
6694 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6695 = SplitNodes.find(Op);
6696 if (I != SplitNodes.end()) {
6697 Lo = I->second.first;
6698 Hi = I->second.second;
6699 return;
6700 }
6701
6702 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006703 default:
6704#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006705 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006706#endif
6707 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00006708 case ISD::UNDEF:
6709 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6710 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6711 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006712 case ISD::BUILD_PAIR:
6713 Lo = Node->getOperand(0);
6714 Hi = Node->getOperand(1);
6715 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006716 case ISD::INSERT_VECTOR_ELT: {
6717 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6718 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6719 SDOperand ScalarOp = Node->getOperand(1);
Nate Begemanbd117f02007-11-15 21:15:26 +00006720 if (Index < NewNumElts_Lo)
6721 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Nate Begeman63eb03f2008-03-14 00:53:31 +00006722 DAG.getIntPtrConstant(Index));
Dan Gohmana90183e2007-09-28 23:53:40 +00006723 else
Nate Begemanbd117f02007-11-15 21:15:26 +00006724 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
Nate Begeman63eb03f2008-03-14 00:53:31 +00006725 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
Dan Gohmana90183e2007-09-28 23:53:40 +00006726 break;
6727 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006728 case ISD::VECTOR_SHUFFLE: {
6729 // Build the low part.
6730 SDOperand Mask = Node->getOperand(2);
6731 SmallVector<SDOperand, 8> Ops;
6732 MVT::ValueType PtrVT = TLI.getPointerTy();
6733
6734 // Insert all of the elements from the input that are needed. We use
6735 // buildvector of extractelement here because the input vectors will have
6736 // to be legalized, so this makes the code simpler.
6737 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Nate Begeman63eb03f2008-03-14 00:53:31 +00006738 SDOperand IdxNode = Mask.getOperand(i);
6739 if (IdxNode.getOpcode() == ISD::UNDEF) {
6740 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6741 continue;
6742 }
6743 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006744 SDOperand InVec = Node->getOperand(0);
6745 if (Idx >= NumElements) {
6746 InVec = Node->getOperand(1);
6747 Idx -= NumElements;
6748 }
6749 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6750 DAG.getConstant(Idx, PtrVT)));
6751 }
6752 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6753 Ops.clear();
6754
6755 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Nate Begeman63eb03f2008-03-14 00:53:31 +00006756 SDOperand IdxNode = Mask.getOperand(i);
6757 if (IdxNode.getOpcode() == ISD::UNDEF) {
6758 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6759 continue;
6760 }
6761 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006762 SDOperand InVec = Node->getOperand(0);
6763 if (Idx >= NumElements) {
6764 InVec = Node->getOperand(1);
6765 Idx -= NumElements;
6766 }
6767 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6768 DAG.getConstant(Idx, PtrVT)));
6769 }
6770 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6771 break;
6772 }
Dan Gohmana8665142007-06-25 16:23:39 +00006773 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006774 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00006775 Node->op_begin()+NewNumElts_Lo);
6776 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006777
Nate Begemanbd117f02007-11-15 21:15:26 +00006778 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00006779 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006780 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006781 break;
6782 }
Dan Gohmana8665142007-06-25 16:23:39 +00006783 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00006784 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00006785 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6786 if (NewNumSubvectors == 1) {
6787 Lo = Node->getOperand(0);
6788 Hi = Node->getOperand(1);
6789 } else {
6790 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6791 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00006792 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006793
Dan Gohmana8665142007-06-25 16:23:39 +00006794 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6795 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006796 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00006797 }
Dan Gohman26455c42007-06-13 15:12:02 +00006798 break;
6799 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006800 case ISD::SELECT: {
6801 SDOperand Cond = Node->getOperand(0);
6802
6803 SDOperand LL, LH, RL, RH;
6804 SplitVectorOp(Node->getOperand(1), LL, LH);
6805 SplitVectorOp(Node->getOperand(2), RL, RH);
6806
6807 if (MVT::isVector(Cond.getValueType())) {
6808 // Handle a vector merge.
6809 SDOperand CL, CH;
6810 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00006811 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6812 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006813 } else {
6814 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00006815 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6816 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006817 }
6818 break;
6819 }
Dan Gohmana8665142007-06-25 16:23:39 +00006820 case ISD::ADD:
6821 case ISD::SUB:
6822 case ISD::MUL:
6823 case ISD::FADD:
6824 case ISD::FSUB:
6825 case ISD::FMUL:
6826 case ISD::SDIV:
6827 case ISD::UDIV:
6828 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006829 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006830 case ISD::AND:
6831 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00006832 case ISD::XOR:
6833 case ISD::UREM:
6834 case ISD::SREM:
6835 case ISD::FREM: {
Chris Lattner32206f52006-03-18 01:44:44 +00006836 SDOperand LL, LH, RL, RH;
6837 SplitVectorOp(Node->getOperand(0), LL, LH);
6838 SplitVectorOp(Node->getOperand(1), RL, RH);
6839
Nate Begemanbd117f02007-11-15 21:15:26 +00006840 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6841 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006842 break;
6843 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006844 case ISD::FPOWI: {
6845 SDOperand L, H;
6846 SplitVectorOp(Node->getOperand(0), L, H);
6847
Nate Begemanbd117f02007-11-15 21:15:26 +00006848 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6849 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00006850 break;
6851 }
6852 case ISD::CTTZ:
6853 case ISD::CTLZ:
6854 case ISD::CTPOP:
6855 case ISD::FNEG:
6856 case ISD::FABS:
6857 case ISD::FSQRT:
6858 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00006859 case ISD::FCOS:
6860 case ISD::FP_TO_SINT:
6861 case ISD::FP_TO_UINT:
6862 case ISD::SINT_TO_FP:
6863 case ISD::UINT_TO_FP: {
Dan Gohman2a7de412007-10-11 23:57:53 +00006864 SDOperand L, H;
6865 SplitVectorOp(Node->getOperand(0), L, H);
6866
Nate Begemanbd117f02007-11-15 21:15:26 +00006867 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6868 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00006869 break;
6870 }
Dan Gohmana8665142007-06-25 16:23:39 +00006871 case ISD::LOAD: {
6872 LoadSDNode *LD = cast<LoadSDNode>(Node);
6873 SDOperand Ch = LD->getChain();
6874 SDOperand Ptr = LD->getBasePtr();
6875 const Value *SV = LD->getSrcValue();
6876 int SVOffset = LD->getSrcValueOffset();
6877 unsigned Alignment = LD->getAlignment();
6878 bool isVolatile = LD->isVolatile();
6879
Nate Begemanbd117f02007-11-15 21:15:26 +00006880 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6881 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006882 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00006883 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006884 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006885 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begemanbd117f02007-11-15 21:15:26 +00006886 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006887
6888 // Build a factor node to remember that this load is independent of the
6889 // other one.
6890 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6891 Hi.getValue(1));
6892
6893 // Remember that we legalized the chain.
6894 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006895 break;
6896 }
Dan Gohmana8665142007-06-25 16:23:39 +00006897 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006898 // We know the result is a vector. The input may be either a vector or a
6899 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006900 SDOperand InOp = Node->getOperand(0);
6901 if (!MVT::isVector(InOp.getValueType()) ||
6902 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6903 // The input is a scalar or single-element vector.
6904 // Lower to a store/load so that it can be split.
6905 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006906 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman54d3b5a2008-02-11 18:58:42 +00006907 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006908
Evan Chengdf9ac472006-10-05 23:01:46 +00006909 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006910 InOp, Ptr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006911 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006912 FI->getIndex());
6913 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006914 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006915 FI->getIndex());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006916 }
Dan Gohman0de76942007-06-29 00:09:08 +00006917 // Split the vector and convert each of the pieces now.
6918 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006919 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6920 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00006921 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006922 }
Chris Lattner32206f52006-03-18 01:44:44 +00006923 }
6924
6925 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006926 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006927 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006928 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006929}
6930
6931
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006932/// ScalarizeVectorOp - Given an operand of single-element vector type
6933/// (e.g. v1f32), convert it into the equivalent operation that returns a
6934/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006935SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6936 assert(MVT::isVector(Op.getValueType()) &&
6937 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006938 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006939 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6940 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006941
Dan Gohmana8665142007-06-25 16:23:39 +00006942 // See if we already scalarized it.
6943 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6944 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006945
6946 SDOperand Result;
6947 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006948 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006949#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006950 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006951#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006952 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6953 case ISD::ADD:
6954 case ISD::FADD:
6955 case ISD::SUB:
6956 case ISD::FSUB:
6957 case ISD::MUL:
6958 case ISD::FMUL:
6959 case ISD::SDIV:
6960 case ISD::UDIV:
6961 case ISD::FDIV:
6962 case ISD::SREM:
6963 case ISD::UREM:
6964 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006965 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006966 case ISD::AND:
6967 case ISD::OR:
6968 case ISD::XOR:
6969 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006970 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006971 ScalarizeVectorOp(Node->getOperand(0)),
6972 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006973 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006974 case ISD::FNEG:
6975 case ISD::FABS:
6976 case ISD::FSQRT:
6977 case ISD::FSIN:
6978 case ISD::FCOS:
6979 Result = DAG.getNode(Node->getOpcode(),
6980 NewVT,
6981 ScalarizeVectorOp(Node->getOperand(0)));
6982 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006983 case ISD::FPOWI:
6984 Result = DAG.getNode(Node->getOpcode(),
6985 NewVT,
6986 ScalarizeVectorOp(Node->getOperand(0)),
6987 Node->getOperand(1));
6988 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006989 case ISD::LOAD: {
6990 LoadSDNode *LD = cast<LoadSDNode>(Node);
6991 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6992 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006993
Dan Gohmana8665142007-06-25 16:23:39 +00006994 const Value *SV = LD->getSrcValue();
6995 int SVOffset = LD->getSrcValueOffset();
6996 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6997 LD->isVolatile(), LD->getAlignment());
6998
Chris Lattner32206f52006-03-18 01:44:44 +00006999 // Remember that we legalized the chain.
7000 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7001 break;
7002 }
Dan Gohmana8665142007-06-25 16:23:39 +00007003 case ISD::BUILD_VECTOR:
7004 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00007005 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007006 case ISD::INSERT_VECTOR_ELT:
7007 // Returning the inserted scalar element.
7008 Result = Node->getOperand(1);
7009 break;
7010 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00007011 assert(Node->getOperand(0).getValueType() == NewVT &&
7012 "Concat of non-legal vectors not yet supported!");
7013 Result = Node->getOperand(0);
7014 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007015 case ISD::VECTOR_SHUFFLE: {
7016 // Figure out if the scalar is the LHS or RHS and return it.
7017 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7018 if (cast<ConstantSDNode>(EltNum)->getValue())
7019 Result = ScalarizeVectorOp(Node->getOperand(1));
7020 else
7021 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00007022 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007023 }
7024 case ISD::EXTRACT_SUBVECTOR:
7025 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00007026 assert(Result.getValueType() == NewVT);
7027 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007028 case ISD::BIT_CONVERT:
7029 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00007030 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007031 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00007032 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00007033 ScalarizeVectorOp(Op.getOperand(1)),
7034 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00007035 break;
Chris Lattner32206f52006-03-18 01:44:44 +00007036 }
7037
7038 if (TLI.isTypeLegal(NewVT))
7039 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00007040 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7041 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00007042 return Result;
7043}
7044
Chris Lattnerdc750592005-01-07 07:47:09 +00007045
7046// SelectionDAG::Legalize - This is the entry point for the file.
7047//
Chris Lattner4add7e32005-01-23 04:42:50 +00007048void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00007049 if (ViewLegalizeDAGs) viewGraph();
7050
Chris Lattnerdc750592005-01-07 07:47:09 +00007051 /// run - This is the main entry point to this class.
7052 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00007053 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00007054}
7055