blob: 8cdfcfa2acfd0c2bf0effbd4b346126dacb71c31 [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
38//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000051class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000052 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
Chris Lattner462505f2006-02-13 09:18:02 +000055 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Chris Lattner462505f2006-02-13 09:18:02 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
Chris Lattnerdc750592005-01-07 07:47:09 +000067 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000068 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000070 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000071 };
Chris Lattner462505f2006-02-13 09:18:02 +000072
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000076 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000077
Chris Lattnerdc750592005-01-07 07:47:09 +000078 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000082
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000087
Chris Lattner32206f52006-03-18 01:44:44 +000088 /// ExpandedNodes - For nodes that need to be expanded this map indicates
89 /// which which operands are the expanded version of the input. This allows
90 /// us to avoid expanding the same node more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000092
Chris Lattner32206f52006-03-18 01:44:44 +000093 /// SplitNodes - For vector nodes that need to be split, this map indicates
94 /// which which operands are the split version of the input. This allows us
95 /// to avoid splitting the same node more than once.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattner32206f52006-03-18 01:44:44 +000097
Dan Gohmana8665142007-06-25 16:23:39 +000098 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000100 /// processed to the result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000102
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000103 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000104 LegalizedNodes.insert(std::make_pair(From, To));
105 // If someone requests legalization of the new node, return itself.
106 if (From != To)
107 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000108 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000109 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman38740a92008-07-07 17:46:23 +0000110 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000111 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000112 // If someone requests legalization of the new node, return itself.
113 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000114 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000115
Chris Lattnerdc750592005-01-07 07:47:09 +0000116public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000117 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000118
Chris Lattnerdc750592005-01-07 07:47:09 +0000119 /// getTypeAction - Return how we should legalize values of this type, either
120 /// it is already legal or we need to expand it into multiple registers of
121 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands13237ac2008-06-06 12:08:01 +0000122 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000123 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124 }
125
126 /// isTypeLegal - Return true if this type is legal on this target.
127 ///
Duncan Sands13237ac2008-06-06 12:08:01 +0000128 bool isTypeLegal(MVT VT) const {
Chris Lattnerdc750592005-01-07 07:47:09 +0000129 return getTypeAction(VT) == Legal;
130 }
131
Chris Lattnerdc750592005-01-07 07:47:09 +0000132 void LegalizeDAG();
133
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000134private:
Dan Gohmana8665142007-06-25 16:23:39 +0000135 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000136 /// appropriate for its type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000137 void HandleOp(SDValue Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000138
139 /// LegalizeOp - We know that the specified value has a legal type.
140 /// Recursively ensure that the operands have legal types, then return the
141 /// result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000142 SDValue LegalizeOp(SDValue O);
Chris Lattner32206f52006-03-18 01:44:44 +0000143
Dan Gohman2a7de412007-10-11 23:57:53 +0000144 /// UnrollVectorOp - We know that the given vector has a legal type, however
145 /// the operation it performs is not legal and is an operation that we have
146 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
147 /// operating on each element individually.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000148 SDValue UnrollVectorOp(SDValue O);
Nate Begeman6f94f612008-04-25 18:07:40 +0000149
150 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
151 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
152 /// is necessary to spill the vector being inserted into to memory, perform
153 /// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000154 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
155 SDValue Idx);
Dan Gohman2a7de412007-10-11 23:57:53 +0000156
Chris Lattner32206f52006-03-18 01:44:44 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000162 SDValue PromoteOp(SDValue O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000163
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000164 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner32206f52006-03-18 01:44:44 +0000165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000170 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattner32206f52006-03-18 01:44:44 +0000171
Dan Gohmana8665142007-06-25 16:23:39 +0000172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000174 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattner32206f52006-03-18 01:44:44 +0000175
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000179 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattner32206f52006-03-18 01:44:44 +0000180
Duncan Sandsb0e39382008-07-21 10:20:31 +0000181 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner6be79822006-04-04 17:23:26 +0000182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000190 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner6be79822006-04-04 17:23:26 +0000191
Chris Lattner4488f0c2006-07-26 23:55:56 +0000192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000194
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000195 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000196
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000197 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
198 SDValue &Hi);
199 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000200
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000201 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
202 SDValue ExpandBUILD_VECTOR(SDNode *Node);
203 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
204 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
205 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
206 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000207
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000208 SDValue ExpandBSWAP(SDValue Op);
209 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
210 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
211 SDValue &Lo, SDValue &Hi);
212 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
213 SDValue &Lo, SDValue &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000214
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000215 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
216 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattnerdc750592005-01-07 07:47:09 +0000217};
218}
219
Chris Lattner6be79822006-04-04 17:23:26 +0000220/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
221/// specified mask and type. Targets can specify exactly which masks they
222/// support and the code generator is tasked with not creating illegal masks.
223///
224/// Note that this will also return true for shuffles that are promoted to a
225/// different type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000226SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner6be79822006-04-04 17:23:26 +0000227 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
228 default: return 0;
229 case TargetLowering::Legal:
230 case TargetLowering::Custom:
231 break;
232 case TargetLowering::Promote: {
233 // If this is promoted to a different type, convert the shuffle mask and
234 // ask if it is legal in the promoted type!
Duncan Sands13237ac2008-06-06 12:08:01 +0000235 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsb0e39382008-07-21 10:20:31 +0000236 MVT EltVT = NVT.getVectorElementType();
Chris Lattner6be79822006-04-04 17:23:26 +0000237
238 // If we changed # elements, change the shuffle mask.
239 unsigned NumEltsGrowth =
Duncan Sands13237ac2008-06-06 12:08:01 +0000240 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner6be79822006-04-04 17:23:26 +0000241 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
242 if (NumEltsGrowth > 1) {
243 // Renumber the elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000244 SmallVector<SDValue, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000245 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000246 SDValue InOp = Mask.getOperand(i);
Chris Lattner6be79822006-04-04 17:23:26 +0000247 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
248 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsb0e39382008-07-21 10:20:31 +0000249 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner6be79822006-04-04 17:23:26 +0000250 else {
251 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
Duncan Sandsb0e39382008-07-21 10:20:31 +0000252 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner6be79822006-04-04 17:23:26 +0000253 }
254 }
255 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000256 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000257 }
258 VT = NVT;
259 break;
260 }
261 }
262 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
263}
264
Chris Lattner4add7e32005-01-23 04:42:50 +0000265SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
266 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
267 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000268 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000269 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000270}
271
Chris Lattnere31adc82007-06-18 21:28:10 +0000272/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
273/// contains all of a nodes operands before it contains the node.
274static void ComputeTopDownOrdering(SelectionDAG &DAG,
275 SmallVector<SDNode*, 64> &Order) {
276
277 DenseMap<SDNode*, unsigned> Visited;
278 std::vector<SDNode*> Worklist;
279 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000280
Chris Lattnere31adc82007-06-18 21:28:10 +0000281 // Compute ordering from all of the leaves in the graphs, those (like the
282 // entry node) that have no operands.
283 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
284 E = DAG.allnodes_end(); I != E; ++I) {
285 if (I->getNumOperands() == 0) {
286 Visited[I] = 0 - 1U;
287 Worklist.push_back(I);
288 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000289 }
290
Chris Lattnere31adc82007-06-18 21:28:10 +0000291 while (!Worklist.empty()) {
292 SDNode *N = Worklist.back();
293 Worklist.pop_back();
294
295 if (++Visited[N] != N->getNumOperands())
296 continue; // Haven't visited all operands yet
297
298 Order.push_back(N);
299
300 // Now that we have N in, add anything that uses it if all of their operands
301 // are now done.
Dan Gohman91e5dcb2008-07-27 20:43:25 +0000302 Worklist.insert(Worklist.end(), N->use_begin(), N->use_end());
Chris Lattnere31adc82007-06-18 21:28:10 +0000303 }
304
305 assert(Order.size() == Visited.size() &&
Dan Gohman3792c472008-06-20 17:15:19 +0000306 Order.size() == DAG.allnodes_size() &&
Chris Lattnere31adc82007-06-18 21:28:10 +0000307 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000308}
309
Chris Lattner44fe26f2005-07-29 00:11:56 +0000310
Chris Lattnerdc750592005-01-07 07:47:09 +0000311void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000312 LastCALLSEQ_END = DAG.getEntryNode();
313 IsLegalizingCall = false;
314
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000315 // The legalize process is inherently a bottom-up recursive process (users
316 // legalize their uses before themselves). Given infinite stack space, we
317 // could just start legalizing on the root and traverse the whole graph. In
318 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000319 // blocks. To avoid this problem, compute an ordering of the nodes where each
320 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000321 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000322 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000323
Chris Lattner32206f52006-03-18 01:44:44 +0000324 for (unsigned i = 0, e = Order.size(); i != e; ++i)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000325 HandleOp(SDValue(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000326
327 // Finally, it's possible the root changed. Get the new root.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000328 SDValue OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000329 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
330 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000331
332 ExpandedNodes.clear();
333 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000334 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000335 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000336 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000337
338 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000339 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000340}
341
Chris Lattner462505f2006-02-13 09:18:02 +0000342
343/// FindCallEndFromCallStart - Given a chained node that is part of a call
344/// sequence, find the CALLSEQ_END node that terminates the call sequence.
345static SDNode *FindCallEndFromCallStart(SDNode *Node) {
346 if (Node->getOpcode() == ISD::CALLSEQ_END)
347 return Node;
348 if (Node->use_empty())
349 return 0; // No CallSeqEnd
350
351 // The chain is usually at the end.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000352 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner462505f2006-02-13 09:18:02 +0000353 if (TheChain.getValueType() != MVT::Other) {
354 // Sometimes it's at the beginning.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000355 TheChain = SDValue(Node, 0);
Chris Lattner462505f2006-02-13 09:18:02 +0000356 if (TheChain.getValueType() != MVT::Other) {
357 // Otherwise, hunt for it.
358 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
359 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000360 TheChain = SDValue(Node, i);
Chris Lattner462505f2006-02-13 09:18:02 +0000361 break;
362 }
363
364 // Otherwise, we walked into a node without a chain.
365 if (TheChain.getValueType() != MVT::Other)
366 return 0;
367 }
368 }
369
370 for (SDNode::use_iterator UI = Node->use_begin(),
371 E = Node->use_end(); UI != E; ++UI) {
372
373 // Make sure to only follow users of our token chain.
Dan Gohman91e5dcb2008-07-27 20:43:25 +0000374 SDNode *User = *UI;
Chris Lattner462505f2006-02-13 09:18:02 +0000375 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
376 if (User->getOperand(i) == TheChain)
377 if (SDNode *Result = FindCallEndFromCallStart(User))
378 return Result;
379 }
380 return 0;
381}
382
383/// FindCallStartFromCallEnd - Given a chained node that is part of a call
384/// sequence, find the CALLSEQ_START node that initiates the call sequence.
385static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
386 assert(Node && "Didn't find callseq_start for a call??");
387 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
388
389 assert(Node->getOperand(0).getValueType() == MVT::Other &&
390 "Node doesn't have a token chain argument!");
391 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
392}
393
394/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
395/// see if any uses can reach Dest. If no dest operands can get to dest,
396/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000397///
398/// Keep track of the nodes we fine that actually do lead to Dest in
399/// NodesLeadingTo. This avoids retraversing them exponential number of times.
400///
401bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000402 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000403 if (N == Dest) return true; // N certainly leads to Dest :)
404
Chris Lattner4488f0c2006-07-26 23:55:56 +0000405 // If we've already processed this node and it does lead to Dest, there is no
406 // need to reprocess it.
407 if (NodesLeadingTo.count(N)) return true;
408
Chris Lattner462505f2006-02-13 09:18:02 +0000409 // If the first result of this node has been already legalized, then it cannot
410 // reach N.
411 switch (getTypeAction(N->getValueType(0))) {
412 case Legal:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000413 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000414 break;
415 case Promote:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000416 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000417 break;
418 case Expand:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000419 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner462505f2006-02-13 09:18:02 +0000420 break;
421 }
422
423 // Okay, this node has not already been legalized. Check and legalize all
424 // operands. If none lead to Dest, then we can legalize this node.
425 bool OperandsLeadToDest = false;
426 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
427 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000428 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000429
Chris Lattner4488f0c2006-07-26 23:55:56 +0000430 if (OperandsLeadToDest) {
431 NodesLeadingTo.insert(N);
432 return true;
433 }
Chris Lattner462505f2006-02-13 09:18:02 +0000434
435 // Okay, this node looks safe, legalize it and return false.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000436 HandleOp(SDValue(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000437 return false;
438}
439
Dan Gohmana8665142007-06-25 16:23:39 +0000440/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000441/// appropriate for its type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000442void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000443 MVT VT = Op.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000444 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000445 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000446 case Legal: (void)LegalizeOp(Op); break;
447 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000448 case Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +0000449 if (!VT.isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +0000450 // If this is an illegal scalar, expand it into its two component
451 // pieces.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000452 SDValue X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000453 if (Op.getOpcode() == ISD::TargetConstant)
454 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000455 ExpandOp(Op, X, Y);
Duncan Sands13237ac2008-06-06 12:08:01 +0000456 } else if (VT.getVectorNumElements() == 1) {
Dan Gohmana8665142007-06-25 16:23:39 +0000457 // If this is an illegal single element vector, convert it to a
458 // scalar operation.
459 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000460 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000461 // Otherwise, this is an illegal multiple element vector.
462 // Split it in half and legalize both parts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000463 SDValue X, Y;
Dan Gohmana8665142007-06-25 16:23:39 +0000464 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000465 }
466 break;
467 }
468}
Chris Lattner462505f2006-02-13 09:18:02 +0000469
Evan Cheng22cf8992006-12-13 20:57:08 +0000470/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
471/// a load from the constant pool.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000472static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000473 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000474 bool Extend = false;
475
476 // If a FP immediate is precise when represented as a float and if the
477 // target can do an extending load from float to double, we put it into
478 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000479 // double. This shrinks FP constants and canonicalizes them for targets where
480 // an FP extending load is the same cost as a normal load (such as on the x87
481 // fp stack or PPC FP unit).
Duncan Sands13237ac2008-06-06 12:08:01 +0000482 MVT VT = CFP->getValueType(0);
Chris Lattner3b187622008-04-20 00:41:09 +0000483 ConstantFP *LLVMC = ConstantFP::get(CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000484 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000485 if (VT!=MVT::f64 && VT!=MVT::f32)
486 assert(0 && "Invalid type expansion");
Dan Gohman10f7d852008-03-11 00:11:06 +0000487 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Cheng38caf772008-03-04 08:05:30 +0000488 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000489 }
490
Duncan Sands13237ac2008-06-06 12:08:01 +0000491 MVT OrigVT = VT;
492 MVT SVT = VT;
Evan Cheng38caf772008-03-04 08:05:30 +0000493 while (SVT != MVT::f32) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000494 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Cheng38caf772008-03-04 08:05:30 +0000495 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
496 // Only do this if the target has a native EXTLOAD instruction from
497 // smaller type.
Evan Cheng0a62cb42008-03-05 01:30:59 +0000498 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattner3dc38992008-03-05 06:46:58 +0000499 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000500 const Type *SType = SVT.getTypeForMVT();
Evan Cheng38caf772008-03-04 08:05:30 +0000501 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
502 VT = SVT;
503 Extend = true;
504 }
Evan Cheng47833a12006-12-12 21:32:44 +0000505 }
506
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000507 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng38caf772008-03-04 08:05:30 +0000508 if (Extend)
509 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman16d4bc32008-02-07 18:41:25 +0000510 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Cheng38caf772008-03-04 08:05:30 +0000511 0, VT);
512 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
513 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng47833a12006-12-12 21:32:44 +0000514}
515
Chris Lattner462505f2006-02-13 09:18:02 +0000516
Evan Cheng003feb02007-01-04 21:56:39 +0000517/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
518/// operations.
519static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000520SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
521 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000522 MVT VT = Node->getValueType(0);
523 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000524 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
525 "fcopysign expansion only supported for f32 and f64");
Duncan Sands13237ac2008-06-06 12:08:01 +0000526 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000527
Evan Cheng003feb02007-01-04 21:56:39 +0000528 // First get the sign bit of second operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000529 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000530 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
531 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000532 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000533 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000534 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000535 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands13237ac2008-06-06 12:08:01 +0000536 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng003feb02007-01-04 21:56:39 +0000537 if (SizeDiff > 0) {
538 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
539 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
540 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattner87909d02008-07-10 23:46:13 +0000541 } else if (SizeDiff < 0) {
542 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
543 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
544 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
545 }
Evan Cheng3b841dd2007-01-05 21:31:51 +0000546
547 // Clear the sign bit of first operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000548 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng3b841dd2007-01-05 21:31:51 +0000549 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
550 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
551 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000552 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000553 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
554
555 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000556 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
557 return Result;
558}
559
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000560/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
561static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000562SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
563 TargetLowering &TLI) {
564 SDValue Chain = ST->getChain();
565 SDValue Ptr = ST->getBasePtr();
566 SDValue Val = ST->getValue();
Duncan Sands13237ac2008-06-06 12:08:01 +0000567 MVT VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000568 int Alignment = ST->getAlignment();
569 int SVOffset = ST->getSrcValueOffset();
Duncan Sands13237ac2008-06-06 12:08:01 +0000570 if (ST->getMemoryVT().isFloatingPoint() ||
571 ST->getMemoryVT().isVector()) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000572 // Expand to a bitconvert of the value to the integer type of the
573 // same size, then a (misaligned) int store.
Duncan Sands13237ac2008-06-06 12:08:01 +0000574 MVT intVT;
575 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000576 intVT = MVT::i128;
Duncan Sands13237ac2008-06-06 12:08:01 +0000577 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000578 intVT = MVT::i64;
579 else if (VT==MVT::f32)
580 intVT = MVT::i32;
581 else
Dale Johannesenc4c3de22008-02-28 18:36:51 +0000582 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000583
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000584 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000585 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
586 SVOffset, ST->isVolatile(), Alignment);
587 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000588 assert(ST->getMemoryVT().isInteger() &&
589 !ST->getMemoryVT().isVector() &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000590 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000591 // Get the half-size VT
Duncan Sands13237ac2008-06-06 12:08:01 +0000592 MVT NewStoredVT =
593 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
594 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000595 int IncrementSize = NumBits / 8;
596
597 // Divide the stored value in two parts.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000598 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
599 SDValue Lo = Val;
600 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000601
602 // Store the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000603 SDValue Store1, Store2;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000604 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
605 ST->getSrcValue(), SVOffset, NewStoredVT,
606 ST->isVolatile(), Alignment);
607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
608 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000609 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000610 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
611 ST->getSrcValue(), SVOffset + IncrementSize,
612 NewStoredVT, ST->isVolatile(), Alignment);
613
614 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
615}
616
617/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
618static
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000619SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
620 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000621 int SVOffset = LD->getSrcValueOffset();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000622 SDValue Chain = LD->getChain();
623 SDValue Ptr = LD->getBasePtr();
Duncan Sands13237ac2008-06-06 12:08:01 +0000624 MVT VT = LD->getValueType(0);
625 MVT LoadedVT = LD->getMemoryVT();
626 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000627 // Expand to a (misaligned) integer load of the same size,
Dale Johannesenbf76a082008-02-27 22:36:00 +0000628 // then bitconvert to floating point or vector.
Duncan Sands13237ac2008-06-06 12:08:01 +0000629 MVT intVT;
630 if (LoadedVT.is128BitVector() ||
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000631 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000632 intVT = MVT::i128;
Duncan Sands13237ac2008-06-06 12:08:01 +0000633 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000634 intVT = MVT::i64;
Chris Lattner09c03932007-11-19 21:38:03 +0000635 else if (LoadedVT == MVT::f32)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000636 intVT = MVT::i32;
637 else
Dale Johannesenbf76a082008-02-27 22:36:00 +0000638 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000639
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000640 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000641 SVOffset, LD->isVolatile(),
642 LD->getAlignment());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000643 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands13237ac2008-06-06 12:08:01 +0000644 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000645 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
646
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000647 SDValue Ops[] = { Result, Chain };
Duncan Sands739a0542008-07-02 17:40:58 +0000648 return DAG.getMergeValues(Ops, 2);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000649 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000650 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattner09c03932007-11-19 21:38:03 +0000651 "Unaligned load of unsupported type.");
652
Dale Johannesenbf76a082008-02-27 22:36:00 +0000653 // Compute the new VT that is half the size of the old one. This is an
654 // integer MVT.
Duncan Sands13237ac2008-06-06 12:08:01 +0000655 unsigned NumBits = LoadedVT.getSizeInBits();
656 MVT NewLoadedVT;
657 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattner09c03932007-11-19 21:38:03 +0000658 NumBits >>= 1;
659
660 unsigned Alignment = LD->getAlignment();
661 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000662 ISD::LoadExtType HiExtType = LD->getExtensionType();
663
664 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
665 if (HiExtType == ISD::NON_EXTLOAD)
666 HiExtType = ISD::ZEXTLOAD;
667
668 // Load the value in two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000669 SDValue Lo, Hi;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000670 if (TLI.isLittleEndian()) {
671 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
672 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
673 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
674 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
675 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
676 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000677 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000678 } else {
679 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
680 NewLoadedVT,LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, 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 }
687
688 // aggregate the two parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000689 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
690 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000691 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
692
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000693 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000694 Hi.getValue(1));
695
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000696 SDValue Ops[] = { Result, TF };
Duncan Sands739a0542008-07-02 17:40:58 +0000697 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000698}
Evan Cheng003feb02007-01-04 21:56:39 +0000699
Dan Gohman2a7de412007-10-11 23:57:53 +0000700/// UnrollVectorOp - We know that the given vector has a legal type, however
701/// the operation it performs is not legal and is an operation that we have
702/// no way of lowering. "Unroll" the vector, splitting out the scalars and
703/// operating on each element individually.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000704SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000705 MVT VT = Op.getValueType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000706 assert(isTypeLegal(VT) &&
707 "Caller should expand or promote operands that are not legal!");
708 assert(Op.Val->getNumValues() == 1 &&
709 "Can't unroll a vector with multiple results!");
Duncan Sands13237ac2008-06-06 12:08:01 +0000710 unsigned NE = VT.getVectorNumElements();
711 MVT EltVT = VT.getVectorElementType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000712
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000713 SmallVector<SDValue, 8> Scalars;
714 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman2a7de412007-10-11 23:57:53 +0000715 for (unsigned i = 0; i != NE; ++i) {
716 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000717 SDValue Operand = Op.getOperand(j);
Duncan Sands13237ac2008-06-06 12:08:01 +0000718 MVT OperandVT = Operand.getValueType();
719 if (OperandVT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +0000720 // A vector operand; extract a single element.
Duncan Sands13237ac2008-06-06 12:08:01 +0000721 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman2a7de412007-10-11 23:57:53 +0000722 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
723 OperandEltVT,
724 Operand,
725 DAG.getConstant(i, MVT::i32));
726 } else {
727 // A scalar operand; just use it as is.
728 Operands[j] = Operand;
729 }
730 }
731 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
732 &Operands[0], Operands.size()));
733 }
734
735 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
736}
737
Duncan Sands53c954f2008-01-10 10:28:30 +0000738/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands13237ac2008-06-06 12:08:01 +0000739static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands53c954f2008-01-10 10:28:30 +0000740 RTLIB::Libcall Call_F32,
741 RTLIB::Libcall Call_F64,
742 RTLIB::Libcall Call_F80,
743 RTLIB::Libcall Call_PPCF128) {
744 return
745 VT == MVT::f32 ? Call_F32 :
746 VT == MVT::f64 ? Call_F64 :
747 VT == MVT::f80 ? Call_F80 :
748 VT == MVT::ppcf128 ? Call_PPCF128 :
749 RTLIB::UNKNOWN_LIBCALL;
750}
751
Nate Begeman6f94f612008-04-25 18:07:40 +0000752/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
753/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
754/// is necessary to spill the vector being inserted into to memory, perform
755/// the insert there, and then read the result back.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000756SDValue SelectionDAGLegalize::
757PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
758 SDValue Tmp1 = Vec;
759 SDValue Tmp2 = Val;
760 SDValue Tmp3 = Idx;
Nate Begeman6f94f612008-04-25 18:07:40 +0000761
762 // If the target doesn't support this, we have to spill the input vector
763 // to a temporary stack slot, update the element, then reload it. This is
764 // badness. We could also load the value into a vector register (either
765 // with a "move to register" or "extload into register" instruction, then
766 // permute it into place, if the idx is a constant and if the idx is
767 // supported by the target.
Duncan Sands13237ac2008-06-06 12:08:01 +0000768 MVT VT = Tmp1.getValueType();
769 MVT EltVT = VT.getVectorElementType();
770 MVT IdxVT = Tmp3.getValueType();
771 MVT PtrVT = TLI.getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000772 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000773
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000774 int SPFI = cast<FrameIndexSDNode>(StackPtr.Val)->getIndex();
Nate Begeman6f94f612008-04-25 18:07:40 +0000775
776 // Store the vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000777 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000778 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000779
780 // Truncate or zero extend offset to target pointer type.
Duncan Sands11dd4242008-06-08 20:54:56 +0000781 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman6f94f612008-04-25 18:07:40 +0000782 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
783 // Add the offset to the index.
Duncan Sands13237ac2008-06-06 12:08:01 +0000784 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman6f94f612008-04-25 18:07:40 +0000785 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000786 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman6f94f612008-04-25 18:07:40 +0000787 // Store the scalar value.
788 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000789 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman6f94f612008-04-25 18:07:40 +0000790 // Load the updated vector.
Dan Gohman02c7c6c2008-07-11 22:44:52 +0000791 return DAG.getLoad(VT, Ch, StackPtr,
792 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman6f94f612008-04-25 18:07:40 +0000793}
794
Dan Gohmanff727882007-07-13 20:14:11 +0000795/// LegalizeOp - We know that the specified value has a legal type, and
796/// that its operands are legal. Now ensure that the operation itself
797/// is legal, recursively ensuring that the operands' operations remain
798/// legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000799SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000800 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
801 return Op;
802
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000803 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000804 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000805 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000806
Chris Lattnerdc750592005-01-07 07:47:09 +0000807 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000808 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000809 if (Node->getNumValues() > 1) {
810 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000811 if (getTypeAction(Node->getValueType(i)) != Legal) {
812 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000813 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000814 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000815 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000816 }
817 }
818
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000819 // Note that LegalizeOp may be reentered even from single-use nodes, which
820 // means that we always must cache transformed nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000821 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000822 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000823
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000824 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
825 SDValue Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000826 bool isCustom = false;
827
Chris Lattnerdc750592005-01-07 07:47:09 +0000828 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000829 case ISD::FrameIndex:
830 case ISD::EntryToken:
831 case ISD::Register:
832 case ISD::BasicBlock:
833 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000834 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000835 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000836 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000837 case ISD::TargetConstantPool:
838 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000839 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000840 case ISD::TargetExternalSymbol:
841 case ISD::VALUETYPE:
842 case ISD::SRCVALUE:
Dan Gohman2d489b52008-02-06 22:27:42 +0000843 case ISD::MEMOPERAND:
Chris Lattnereb637512006-01-28 08:31:04 +0000844 case ISD::CONDCODE:
Duncan Sandsd97eea32008-03-21 09:14:45 +0000845 case ISD::ARG_FLAGS:
Chris Lattnereb637512006-01-28 08:31:04 +0000846 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000847 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000848 "This must be legal!");
849 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000850 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000851 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
852 // If this is a target node, legalize it by legalizing the operands then
853 // passing it through.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000854 SmallVector<SDValue, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000855 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000856 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000857
Chris Lattner97af9d52006-08-08 01:09:31 +0000858 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000859
860 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
861 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
862 return Result.getValue(Op.ResNo);
863 }
864 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000865#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000866 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000867#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000868 assert(0 && "Do not know how to legalize this operator!");
869 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000870 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000871 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000872 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000873 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000874 case ISD::ConstantPool:
875 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000876 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
877 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000878 case TargetLowering::Custom:
879 Tmp1 = TLI.LowerOperation(Op, DAG);
880 if (Tmp1.Val) Result = Tmp1;
881 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000882 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000883 break;
884 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000885 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000886 case ISD::FRAMEADDR:
887 case ISD::RETURNADDR:
888 // The only option for these nodes is to custom lower them. If the target
889 // does not custom lower them, then return zero.
890 Tmp1 = TLI.LowerOperation(Op, DAG);
891 if (Tmp1.Val)
892 Result = Tmp1;
893 else
894 Result = DAG.getConstant(0, TLI.getPointerTy());
895 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000896 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands13237ac2008-06-06 12:08:01 +0000897 MVT VT = Node->getValueType(0);
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000898 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
899 default: assert(0 && "This action is not supported yet!");
900 case TargetLowering::Custom:
901 Result = TLI.LowerOperation(Op, DAG);
902 if (Result.Val) break;
903 // Fall Thru
904 case TargetLowering::Legal:
905 Result = DAG.getConstant(0, VT);
906 break;
907 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000908 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000909 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000910 case ISD::EXCEPTIONADDR: {
911 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands13237ac2008-06-06 12:08:01 +0000912 MVT VT = Node->getValueType(0);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000913 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000916 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000917 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000918 }
919 break;
920 case TargetLowering::Custom:
921 Result = TLI.LowerOperation(Op, DAG);
922 if (Result.Val) break;
923 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000924 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000925 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands739a0542008-07-02 17:40:58 +0000926 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000927 break;
928 }
929 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000930 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000931 if (Result.Val->getNumValues() == 1) break;
932
933 assert(Result.Val->getNumValues() == 2 &&
934 "Cannot return more than two values!");
935
936 // Since we produced two values, make sure to remember that we
937 // legalized both of them.
938 Tmp1 = LegalizeOp(Result);
939 Tmp2 = LegalizeOp(Result.getValue(1));
940 AddLegalizedOperand(Op.getValue(0), Tmp1);
941 AddLegalizedOperand(Op.getValue(1), Tmp2);
942 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +0000943 case ISD::EHSELECTION: {
944 Tmp1 = LegalizeOp(Node->getOperand(0));
945 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +0000946 MVT VT = Node->getValueType(0);
Jim Laskey644af6b2007-02-28 20:43:58 +0000947 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
948 default: assert(0 && "This action is not supported yet!");
949 case TargetLowering::Expand: {
950 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000951 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +0000952 }
953 break;
954 case TargetLowering::Custom:
955 Result = TLI.LowerOperation(Op, DAG);
956 if (Result.Val) break;
957 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000958 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000959 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands739a0542008-07-02 17:40:58 +0000960 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +0000961 break;
962 }
963 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000964 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000965 if (Result.Val->getNumValues() == 1) break;
966
967 assert(Result.Val->getNumValues() == 2 &&
968 "Cannot return more than two values!");
969
970 // Since we produced two values, make sure to remember that we
971 // legalized both of them.
972 Tmp1 = LegalizeOp(Result);
973 Tmp2 = LegalizeOp(Result.getValue(1));
974 AddLegalizedOperand(Op.getValue(0), Tmp1);
975 AddLegalizedOperand(Op.getValue(1), Tmp2);
976 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000977 case ISD::EH_RETURN: {
Duncan Sands13237ac2008-06-06 12:08:01 +0000978 MVT VT = Node->getValueType(0);
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000979 // The only "good" option for this node is to custom lower it.
980 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
981 default: assert(0 && "This action is not supported at all!");
982 case TargetLowering::Custom:
983 Result = TLI.LowerOperation(Op, DAG);
984 if (Result.Val) break;
985 // Fall Thru
986 case TargetLowering::Legal:
987 // Target does not know, how to lower this, lower to noop
988 Result = LegalizeOp(Node->getOperand(0));
989 break;
990 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000991 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000992 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000993 case ISD::AssertSext:
994 case ISD::AssertZext:
995 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000997 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000998 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000999 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001000 Result = Node->getOperand(Op.ResNo);
1001 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +00001002 case ISD::CopyFromReg:
1003 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001004 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001005 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001007 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001008 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001009 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001010 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1012 } else {
1013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1014 }
Chris Lattnere3c67e92005-12-18 15:27:43 +00001015 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1016 }
Chris Lattnereb6614d2005-01-28 06:27:38 +00001017 // Since CopyFromReg produces two values, make sure to remember that we
1018 // legalized both of them.
1019 AddLegalizedOperand(Op.getValue(0), Result);
1020 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1021 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +00001022 case ISD::UNDEF: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001023 MVT VT = Op.getValueType();
Nate Begemancda9aa72005-04-01 22:34:39 +00001024 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +00001025 default: assert(0 && "This action is not supported yet!");
1026 case TargetLowering::Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +00001027 if (VT.isInteger())
Nate Begemancda9aa72005-04-01 22:34:39 +00001028 Result = DAG.getConstant(0, VT);
Duncan Sands13237ac2008-06-06 12:08:01 +00001029 else if (VT.isFloatingPoint())
1030 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf04d37d2007-09-26 17:26:49 +00001031 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00001032 else
1033 assert(0 && "Unknown value type!");
1034 break;
Nate Begeman69d39432005-04-02 00:41:14 +00001035 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +00001036 break;
1037 }
1038 break;
1039 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001040
Chris Lattnere55d1712006-03-28 00:40:33 +00001041 case ISD::INTRINSIC_W_CHAIN:
1042 case ISD::INTRINSIC_WO_CHAIN:
1043 case ISD::INTRINSIC_VOID: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001044 SmallVector<SDValue, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001045 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1046 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001047 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001048
1049 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001050 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001051 TargetLowering::Custom) {
1052 Tmp3 = TLI.LowerOperation(Result, DAG);
1053 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001054 }
1055
1056 if (Result.Val->getNumValues() == 1) break;
1057
1058 // Must have return value and chain result.
1059 assert(Result.Val->getNumValues() == 2 &&
1060 "Cannot return more than two values!");
1061
1062 // Since loads produce two values, make sure to remember that we
1063 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001064 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1065 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001066 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +00001067 }
Chris Lattner435b4022005-11-29 06:21:05 +00001068
Dan Gohman5c73a882008-06-30 20:59:49 +00001069 case ISD::DBG_STOPPOINT:
1070 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner435b4022005-11-29 06:21:05 +00001071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1072
Dan Gohman5c73a882008-06-30 20:59:49 +00001073 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner435b4022005-11-29 06:21:05 +00001074 case TargetLowering::Promote:
1075 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001076 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +00001077 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001078 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohmanfb19f942008-07-01 00:05:16 +00001079 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001080
Dan Gohman5c73a882008-06-30 20:59:49 +00001081 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskeyc56315c2007-01-26 21:22:28 +00001082 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman5c73a882008-06-30 20:59:49 +00001083 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1084 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001085
Dan Gohman5c73a882008-06-30 20:59:49 +00001086 unsigned Line = DSP->getLine();
1087 unsigned Col = DSP->getColumn();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001088
1089 if (useDEBUG_LOC) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001090 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng34ef1db2008-07-08 20:06:39 +00001091 DAG.getConstant(Col, MVT::i32),
1092 DAG.getConstant(SrcFile, MVT::i32) };
1093 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001094 } else {
Evan Cheng263070e2008-02-01 02:05:57 +00001095 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohmanfb19f942008-07-01 00:05:16 +00001096 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001097 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001098 } else {
1099 Result = Tmp1; // chain
1100 }
Chris Lattner435b4022005-11-29 06:21:05 +00001101 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001102 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001103 case TargetLowering::Legal: {
1104 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1105 if (Action == Legal && Tmp1 == Node->getOperand(0))
1106 break;
1107
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001108 SmallVector<SDValue, 8> Ops;
Evan Cheng34ef1db2008-07-08 20:06:39 +00001109 Ops.push_back(Tmp1);
1110 if (Action == Legal) {
1111 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1112 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1113 } else {
1114 // Otherwise promote them.
1115 Ops.push_back(PromoteOp(Node->getOperand(1)));
1116 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner435b4022005-11-29 06:21:05 +00001117 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001118 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1119 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1120 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001121 break;
1122 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001123 }
Chris Lattner435b4022005-11-29 06:21:05 +00001124 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001125
1126 case ISD::DECLARE:
1127 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1128 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1129 default: assert(0 && "This action is not supported yet!");
1130 case TargetLowering::Legal:
1131 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1132 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1133 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1135 break;
Chris Lattner9824ffe2008-02-28 05:53:40 +00001136 case TargetLowering::Expand:
1137 Result = LegalizeOp(Node->getOperand(0));
1138 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001139 }
1140 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001141
1142 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001143 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001144 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001145 default: assert(0 && "This action is not supported yet!");
Evan Cheng34ef1db2008-07-08 20:06:39 +00001146 case TargetLowering::Legal: {
1147 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001148 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng34ef1db2008-07-08 20:06:39 +00001149 if (Action == Legal && Tmp1 == Node->getOperand(0))
1150 break;
1151 if (Action == Legal) {
1152 Tmp2 = Node->getOperand(1);
1153 Tmp3 = Node->getOperand(2);
1154 Tmp4 = Node->getOperand(3);
1155 } else {
1156 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1157 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1158 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1159 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001160 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001161 break;
1162 }
Evan Cheng34ef1db2008-07-08 20:06:39 +00001163 }
Jim Laskey762e9ec2006-01-05 01:25:28 +00001164 break;
1165
Dan Gohmanfb19f942008-07-01 00:05:16 +00001166 case ISD::DBG_LABEL:
1167 case ISD::EH_LABEL:
1168 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1169 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001170 default: assert(0 && "This action is not supported yet!");
1171 case TargetLowering::Legal:
1172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohmanfb19f942008-07-01 00:05:16 +00001173 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskey7c462762005-12-16 22:45:29 +00001174 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001175 case TargetLowering::Expand:
1176 Result = LegalizeOp(Node->getOperand(0));
1177 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001178 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001179 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001180
Evan Cheng95cf6612008-03-08 00:58:38 +00001181 case ISD::PREFETCH:
1182 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1183 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1184 default: assert(0 && "This action is not supported yet!");
1185 case TargetLowering::Legal:
1186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1187 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1188 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1189 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1191 break;
1192 case TargetLowering::Expand:
1193 // It's a noop.
1194 Result = LegalizeOp(Node->getOperand(0));
1195 break;
1196 }
1197 break;
1198
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001199 case ISD::MEMBARRIER: {
1200 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001201 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1202 default: assert(0 && "This action is not supported yet!");
1203 case TargetLowering::Legal: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001204 SDValue Ops[6];
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001205 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands96658d02008-02-27 08:53:44 +00001206 for (int x = 1; x < 6; ++x) {
1207 Ops[x] = Node->getOperand(x);
1208 if (!isTypeLegal(Ops[x].getValueType()))
1209 Ops[x] = PromoteOp(Ops[x]);
1210 }
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001211 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1212 break;
1213 }
1214 case TargetLowering::Expand:
1215 //There is no libgcc call for this op
1216 Result = Node->getOperand(0); // Noop
1217 break;
1218 }
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001219 break;
1220 }
1221
Mon P Wang6a490372008-06-25 08:15:39 +00001222 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang3e583932008-05-05 19:05:59 +00001223 unsigned int num_operands = 4;
1224 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001225 SDValue Ops[4];
Mon P Wang3e583932008-05-05 19:05:59 +00001226 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharthd032c332008-03-01 21:52:34 +00001227 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang3e583932008-05-05 19:05:59 +00001228 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1229
1230 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1231 default: assert(0 && "This action is not supported yet!");
1232 case TargetLowering::Custom:
1233 Result = TLI.LowerOperation(Result, DAG);
1234 break;
1235 case TargetLowering::Legal:
1236 break;
1237 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1239 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Mon P Wang3e583932008-05-05 19:05:59 +00001240 return Result.getValue(Op.ResNo);
Duncan Sands93e180342008-07-04 11:47:58 +00001241 }
Mon P Wang6a490372008-06-25 08:15:39 +00001242 case ISD::ATOMIC_LOAD_ADD:
1243 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang3e583932008-05-05 19:05:59 +00001244 case ISD::ATOMIC_LOAD_AND:
1245 case ISD::ATOMIC_LOAD_OR:
1246 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharthf88d50b2008-06-14 05:48:15 +00001247 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang3e583932008-05-05 19:05:59 +00001248 case ISD::ATOMIC_LOAD_MIN:
1249 case ISD::ATOMIC_LOAD_MAX:
1250 case ISD::ATOMIC_LOAD_UMIN:
1251 case ISD::ATOMIC_LOAD_UMAX:
1252 case ISD::ATOMIC_SWAP: {
1253 unsigned int num_operands = 3;
1254 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001255 SDValue Ops[3];
Mon P Wang3e583932008-05-05 19:05:59 +00001256 for (unsigned int x = 0; x < num_operands; ++x)
1257 Ops[x] = LegalizeOp(Node->getOperand(x));
1258 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands93e180342008-07-04 11:47:58 +00001259
Andrew Lenharthd032c332008-03-01 21:52:34 +00001260 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth95528942008-02-21 06:45:13 +00001261 default: assert(0 && "This action is not supported yet!");
Andrew Lenharthd032c332008-03-01 21:52:34 +00001262 case TargetLowering::Custom:
1263 Result = TLI.LowerOperation(Result, DAG);
1264 break;
Mon P Wang3e583932008-05-05 19:05:59 +00001265 case TargetLowering::Expand:
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001266 Result = SDValue(TLI.ReplaceNodeResults(Op.Val, DAG),0);
Mon P Wang3e583932008-05-05 19:05:59 +00001267 break;
Andrew Lenharthd032c332008-03-01 21:52:34 +00001268 case TargetLowering::Legal:
Andrew Lenharth95528942008-02-21 06:45:13 +00001269 break;
1270 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001271 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1272 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Andrew Lenharthd032c332008-03-01 21:52:34 +00001273 return Result.getValue(Op.ResNo);
Duncan Sands93e180342008-07-04 11:47:58 +00001274 }
Scott Michel9d09c5c2007-08-08 23:23:31 +00001275 case ISD::Constant: {
1276 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1277 unsigned opAction =
1278 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1279
Chris Lattnerdc750592005-01-07 07:47:09 +00001280 // We know we don't need to expand constants here, constants only have one
1281 // value and we check that it is fine above.
1282
Scott Michel9d09c5c2007-08-08 23:23:31 +00001283 if (opAction == TargetLowering::Custom) {
1284 Tmp1 = TLI.LowerOperation(Result, DAG);
1285 if (Tmp1.Val)
1286 Result = Tmp1;
1287 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001288 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001289 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001290 case ISD::ConstantFP: {
1291 // Spill FP immediates to the constant pool if the target cannot directly
1292 // codegen them. Targets often have some immediate values that can be
1293 // efficiently generated into an FP register without a load. We explicitly
1294 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001295 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1296
Chris Lattner758b0ac2006-01-29 06:26:56 +00001297 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1298 default: assert(0 && "This action is not supported yet!");
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001299 case TargetLowering::Legal:
1300 break;
Chris Lattner758b0ac2006-01-29 06:26:56 +00001301 case TargetLowering::Custom:
1302 Tmp3 = TLI.LowerOperation(Result, DAG);
1303 if (Tmp3.Val) {
1304 Result = Tmp3;
1305 break;
1306 }
1307 // FALLTHROUGH
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001308 case TargetLowering::Expand: {
1309 // Check to see if this FP immediate is already legal.
1310 bool isLegal = false;
1311 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1312 E = TLI.legal_fpimm_end(); I != E; ++I) {
1313 if (CFP->isExactlyValue(*I)) {
1314 isLegal = true;
1315 break;
1316 }
1317 }
1318 // If this is a legal constant, turn it into a TargetConstantFP node.
1319 if (isLegal)
1320 break;
Evan Cheng3766fc602006-12-12 22:19:28 +00001321 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001322 }
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001323 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001324 break;
1325 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001326 case ISD::TokenFactor:
1327 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001328 Tmp1 = LegalizeOp(Node->getOperand(0));
1329 Tmp2 = LegalizeOp(Node->getOperand(1));
1330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1331 } else if (Node->getNumOperands() == 3) {
1332 Tmp1 = LegalizeOp(Node->getOperand(0));
1333 Tmp2 = LegalizeOp(Node->getOperand(1));
1334 Tmp3 = LegalizeOp(Node->getOperand(2));
1335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001336 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001337 SmallVector<SDValue, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001338 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001339 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1340 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001341 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001342 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001343 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001344
1345 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001346 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001347 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001348 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1349 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001350 // A call within a calling sequence must be legalized to something
1351 // other than the normal CALLSEQ_END. Violating this gets Legalize
1352 // into an infinite loop.
1353 assert ((!IsLegalizingCall ||
1354 Node->getOpcode() != ISD::CALL ||
1355 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1356 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001357
1358 // The number of incoming and outgoing values should match; unless the final
1359 // outgoing value is a flag.
1360 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1361 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1362 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1363 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001364 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001365
Chris Lattneraaa23d92006-05-16 22:53:20 +00001366 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001367 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001368 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendlingf359fed2007-11-13 00:44:25 +00001369 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1370 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001371 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001372 if (Op.ResNo == i)
1373 Tmp2 = Tmp1;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001374 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001375 }
1376 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001377 case ISD::EXTRACT_SUBREG: {
1378 Tmp1 = LegalizeOp(Node->getOperand(0));
1379 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1380 assert(idx && "Operand must be a constant");
1381 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1382 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1383 }
1384 break;
1385 case ISD::INSERT_SUBREG: {
1386 Tmp1 = LegalizeOp(Node->getOperand(0));
1387 Tmp2 = LegalizeOp(Node->getOperand(1));
1388 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1389 assert(idx && "Operand must be a constant");
1390 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1391 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1392 }
1393 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001394 case ISD::BUILD_VECTOR:
1395 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001396 default: assert(0 && "This action is not supported yet!");
1397 case TargetLowering::Custom:
1398 Tmp3 = TLI.LowerOperation(Result, DAG);
1399 if (Tmp3.Val) {
1400 Result = Tmp3;
1401 break;
1402 }
1403 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001404 case TargetLowering::Expand:
1405 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001406 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001407 }
Chris Lattner29b23012006-03-19 01:17:20 +00001408 break;
1409 case ISD::INSERT_VECTOR_ELT:
1410 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner29b23012006-03-19 01:17:20 +00001411 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman735ab3c2008-02-13 06:43:04 +00001412
1413 // The type of the value to insert may not be legal, even though the vector
1414 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1415 // here.
1416 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1417 default: assert(0 && "Cannot expand insert element operand");
1418 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1419 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1420 }
Chris Lattner29b23012006-03-19 01:17:20 +00001421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1422
1423 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1424 Node->getValueType(0))) {
1425 default: assert(0 && "This action is not supported yet!");
1426 case TargetLowering::Legal:
1427 break;
1428 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001429 Tmp4 = TLI.LowerOperation(Result, DAG);
1430 if (Tmp4.Val) {
1431 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001432 break;
1433 }
1434 // FALLTHROUGH
1435 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001436 // If the insert index is a constant, codegen this as a scalar_to_vector,
1437 // then a shuffle that inserts it into the right position in the vector.
1438 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman735ab3c2008-02-13 06:43:04 +00001439 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1440 // match the element type of the vector being created.
1441 if (Tmp2.getValueType() ==
Duncan Sands13237ac2008-06-06 12:08:01 +00001442 Op.getValueType().getVectorElementType()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001443 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman735ab3c2008-02-13 06:43:04 +00001444 Tmp1.getValueType(), Tmp2);
1445
Duncan Sands13237ac2008-06-06 12:08:01 +00001446 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1447 MVT ShufMaskVT =
1448 MVT::getIntVectorWithNumElements(NumElts);
1449 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman735ab3c2008-02-13 06:43:04 +00001450
1451 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1452 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1453 // elt 0 of the RHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001454 SmallVector<SDValue, 8> ShufOps;
Nate Begeman735ab3c2008-02-13 06:43:04 +00001455 for (unsigned i = 0; i != NumElts; ++i) {
1456 if (i != InsertPos->getValue())
1457 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1458 else
1459 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1460 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001461 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman735ab3c2008-02-13 06:43:04 +00001462 &ShufOps[0], ShufOps.size());
1463
1464 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1465 Tmp1, ScVec, ShufMask);
1466 Result = LegalizeOp(Result);
1467 break;
Chris Lattner326870b2006-04-17 19:21:01 +00001468 }
Chris Lattner326870b2006-04-17 19:21:01 +00001469 }
Nate Begeman6f94f612008-04-25 18:07:40 +00001470 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner29b23012006-03-19 01:17:20 +00001471 break;
1472 }
1473 }
1474 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001475 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001476 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1477 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1478 break;
1479 }
1480
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001481 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1482 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1483 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1484 Node->getValueType(0))) {
1485 default: assert(0 && "This action is not supported yet!");
1486 case TargetLowering::Legal:
1487 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001488 case TargetLowering::Custom:
1489 Tmp3 = TLI.LowerOperation(Result, DAG);
1490 if (Tmp3.Val) {
1491 Result = Tmp3;
1492 break;
1493 }
1494 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001495 case TargetLowering::Expand:
1496 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001497 break;
1498 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001499 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001500 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001501 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1502 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1503 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1504
1505 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001506 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1507 default: assert(0 && "Unknown operation action!");
1508 case TargetLowering::Legal:
1509 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1510 "vector shuffle should not be created if not legal!");
1511 break;
1512 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001513 Tmp3 = TLI.LowerOperation(Result, DAG);
1514 if (Tmp3.Val) {
1515 Result = Tmp3;
1516 break;
1517 }
1518 // FALLTHROUGH
1519 case TargetLowering::Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001520 MVT VT = Node->getValueType(0);
1521 MVT EltVT = VT.getVectorElementType();
1522 MVT PtrVT = TLI.getPointerTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001523 SDValue Mask = Node->getOperand(2);
Evan Cheng9fa89592006-04-05 06:07:11 +00001524 unsigned NumElems = Mask.getNumOperands();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001525 SmallVector<SDValue,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001526 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001527 SDValue Arg = Mask.getOperand(i);
Evan Cheng9fa89592006-04-05 06:07:11 +00001528 if (Arg.getOpcode() == ISD::UNDEF) {
1529 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1530 } else {
1531 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1532 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1533 if (Idx < NumElems)
1534 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1535 DAG.getConstant(Idx, PtrVT)));
1536 else
1537 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1538 DAG.getConstant(Idx - NumElems, PtrVT)));
1539 }
1540 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001541 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001542 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001543 }
Chris Lattner6be79822006-04-04 17:23:26 +00001544 case TargetLowering::Promote: {
1545 // Change base type to a different vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00001546 MVT OVT = Node->getValueType(0);
1547 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner6be79822006-04-04 17:23:26 +00001548
1549 // Cast the two input vectors.
1550 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1551 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1552
1553 // Convert the shuffle mask to the right # elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001554 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Chris Lattner6be79822006-04-04 17:23:26 +00001555 assert(Tmp3.Val && "Shuffle not legal?");
1556 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1557 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1558 break;
1559 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001560 }
1561 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001562
1563 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001564 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001565 Tmp2 = LegalizeOp(Node->getOperand(1));
1566 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001567 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001568 break;
1569
Dan Gohmana8665142007-06-25 16:23:39 +00001570 case ISD::EXTRACT_SUBVECTOR:
1571 Tmp1 = Node->getOperand(0);
1572 Tmp2 = LegalizeOp(Node->getOperand(1));
1573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1574 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001575 break;
1576
Chris Lattner462505f2006-02-13 09:18:02 +00001577 case ISD::CALLSEQ_START: {
1578 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1579
1580 // Recursively Legalize all of the inputs of the call end that do not lead
1581 // to this call start. This ensures that any libcalls that need be inserted
1582 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001583 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001584 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001585 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1586 NodesLeadingTo);
1587 }
Chris Lattner462505f2006-02-13 09:18:02 +00001588
1589 // Now that we legalized all of the inputs (which may have inserted
1590 // libcalls) create the new CALLSEQ_START node.
1591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1592
1593 // Merge in the last call, to ensure that this call start after the last
1594 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001595 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001596 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1597 Tmp1 = LegalizeOp(Tmp1);
1598 }
Chris Lattner462505f2006-02-13 09:18:02 +00001599
1600 // Do not try to legalize the target-specific arguments (#1+).
1601 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001602 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001603 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001604 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001605 }
1606
1607 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001608 AddLegalizedOperand(Op.getValue(0), Result);
1609 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1610 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1611
Chris Lattner462505f2006-02-13 09:18:02 +00001612 // Now that the callseq_start and all of the non-call nodes above this call
1613 // sequence have been legalized, legalize the call itself. During this
1614 // process, no libcalls can/will be inserted, guaranteeing that no calls
1615 // can overlap.
1616 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner462505f2006-02-13 09:18:02 +00001617 // Note that we are selecting this call!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001618 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner462505f2006-02-13 09:18:02 +00001619 IsLegalizingCall = true;
1620
1621 // Legalize the call, starting from the CALLSEQ_END.
1622 LegalizeOp(LastCALLSEQ_END);
1623 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1624 return Result;
1625 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001626 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001627 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1628 // will cause this node to be legalized as well as handling libcalls right.
1629 if (LastCALLSEQ_END.Val != Node) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001630 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1631 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001632 assert(I != LegalizedNodes.end() &&
1633 "Legalizing the call start should have legalized this node!");
1634 return I->second;
1635 }
1636
1637 // Otherwise, the call start has been legalized and everything is going
1638 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001640 // Do not try to legalize the target-specific arguments (#1+), except for
1641 // an optional flag input.
1642 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1643 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001644 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001645 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001646 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001647 }
1648 } else {
1649 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1650 if (Tmp1 != Node->getOperand(0) ||
1651 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001652 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001653 Ops[0] = Tmp1;
1654 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001655 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001656 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001657 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001658 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001659 // This finishes up call legalization.
1660 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001661
1662 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001663 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner8e2ee732006-02-14 00:55:02 +00001664 if (Node->getNumValues() == 2)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001665 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattner8e2ee732006-02-14 00:55:02 +00001666 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001667 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands13237ac2008-06-06 12:08:01 +00001668 MVT VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001669 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1670 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1671 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001673
Chris Lattnerd02b0542006-01-28 10:58:55 +00001674 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001675 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001676 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001677 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001678 case TargetLowering::Expand: {
1679 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1680 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1681 " not tell us which reg is the stack pointer!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001682 SDValue Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001683
1684 // Chain the dynamic stack allocation so that it doesn't modify the stack
1685 // pointer when other instructions are using the stack.
1686 Chain = DAG.getCALLSEQ_START(Chain,
1687 DAG.getConstant(0, TLI.getPointerTy()));
1688
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001689 SDValue Size = Tmp2.getOperand(1);
1690 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng631ccc62007-08-16 23:50:06 +00001691 Chain = SP.getValue(1);
1692 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1693 unsigned StackAlign =
1694 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1695 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001696 SP = DAG.getNode(ISD::AND, VT, SP,
1697 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001698 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001699 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1700
1701 Tmp2 =
1702 DAG.getCALLSEQ_END(Chain,
1703 DAG.getConstant(0, TLI.getPointerTy()),
1704 DAG.getConstant(0, TLI.getPointerTy()),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001705 SDValue());
Bill Wendlingf359fed2007-11-13 00:44:25 +00001706
Chris Lattnerd02b0542006-01-28 10:58:55 +00001707 Tmp1 = LegalizeOp(Tmp1);
1708 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001709 break;
1710 }
1711 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001712 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1713 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001714 Tmp1 = LegalizeOp(Tmp3);
1715 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001716 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001717 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001718 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001719 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001720 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001721 // Since this op produce two values, make sure to remember that we
1722 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001723 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1724 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001725 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001726 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001727 case ISD::INLINEASM: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001728 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001729 bool Changed = false;
1730 // Legalize all of the operands of the inline asm, in case they are nodes
1731 // that need to be expanded or something. Note we skip the asm string and
1732 // all of the TargetConstant flags.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001733 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001734 Changed = Op != Ops[0];
1735 Ops[0] = Op;
1736
1737 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1738 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1739 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1740 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001741 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001742 if (Op != Ops[i]) {
1743 Changed = true;
1744 Ops[i] = Op;
1745 }
1746 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001747 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001748
1749 if (HasInFlag) {
1750 Op = LegalizeOp(Ops.back());
1751 Changed |= Op != Ops.back();
1752 Ops.back() = Op;
1753 }
1754
1755 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001756 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001757
1758 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001759 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1760 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattner476e67b2006-01-26 22:24:51 +00001761 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001762 }
Chris Lattner68a12142005-01-07 22:12:08 +00001763 case ISD::BR:
1764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001765 // Ensure that libcalls are emitted before a branch.
1766 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1767 Tmp1 = LegalizeOp(Tmp1);
1768 LastCALLSEQ_END = DAG.getEntryNode();
1769
Chris Lattnerd02b0542006-01-28 10:58:55 +00001770 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001771 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001772 case ISD::BRIND:
1773 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1774 // Ensure that libcalls are emitted before a branch.
1775 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1776 Tmp1 = LegalizeOp(Tmp1);
1777 LastCALLSEQ_END = DAG.getEntryNode();
1778
1779 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1780 default: assert(0 && "Indirect target must be legal type (pointer)!");
1781 case Legal:
1782 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1783 break;
1784 }
1785 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1786 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001787 case ISD::BR_JT:
1788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1789 // Ensure that libcalls are emitted before a branch.
1790 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1791 Tmp1 = LegalizeOp(Tmp1);
1792 LastCALLSEQ_END = DAG.getEntryNode();
1793
1794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1796
1797 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1798 default: assert(0 && "This action is not supported yet!");
1799 case TargetLowering::Legal: break;
1800 case TargetLowering::Custom:
1801 Tmp1 = TLI.LowerOperation(Result, DAG);
1802 if (Tmp1.Val) Result = Tmp1;
1803 break;
1804 case TargetLowering::Expand: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001805 SDValue Chain = Result.getOperand(0);
1806 SDValue Table = Result.getOperand(1);
1807 SDValue Index = Result.getOperand(2);
Evan Cheng84a28d42006-10-30 08:00:44 +00001808
Duncan Sands13237ac2008-06-06 12:08:01 +00001809 MVT PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001810 MachineFunction &MF = DAG.getMachineFunction();
1811 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001812 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001813 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001814
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001815 SDValue LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001816 switch (EntrySize) {
1817 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001818 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001819 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001820 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001821 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskey70323a82006-12-14 19:17:33 +00001822 }
1823
Evan Cheng797d56f2007-11-09 01:32:10 +00001824 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001825 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001826 // For PIC, the sequence is:
1827 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001828 // RelocBase can be JumpTable, GOT or some sort of global base.
1829 if (PTy != MVT::i32)
1830 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1831 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1832 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001833 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001834 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001835 }
1836 }
1837 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001838 case ISD::BRCOND:
1839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001840 // Ensure that libcalls are emitted before a return.
1841 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1842 Tmp1 = LegalizeOp(Tmp1);
1843 LastCALLSEQ_END = DAG.getEntryNode();
1844
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001845 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1846 case Expand: assert(0 && "It's impossible to expand bools");
1847 case Legal:
1848 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1849 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00001850 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001851 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001852
1853 // The top bits of the promoted condition are not necessarily zero, ensure
1854 // that the value is properly zero extended.
Dan Gohman1f372ed2008-02-25 21:11:39 +00001855 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00001856 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman1f372ed2008-02-25 21:11:39 +00001857 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerdb189382006-11-27 04:39:56 +00001858 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001859 break;
1860 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00001861 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001862
1863 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001864 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001865
1866 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1867 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001868 case TargetLowering::Legal: break;
1869 case TargetLowering::Custom:
1870 Tmp1 = TLI.LowerOperation(Result, DAG);
1871 if (Tmp1.Val) Result = Tmp1;
1872 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001873 case TargetLowering::Expand:
1874 // Expand brcond's setcc into its constituent parts and create a BR_CC
1875 // Node.
1876 if (Tmp2.getOpcode() == ISD::SETCC) {
1877 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1878 Tmp2.getOperand(0), Tmp2.getOperand(1),
1879 Node->getOperand(2));
1880 } else {
1881 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1882 DAG.getCondCode(ISD::SETNE), Tmp2,
1883 DAG.getConstant(0, Tmp2.getValueType()),
1884 Node->getOperand(2));
1885 }
1886 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001887 }
1888 break;
1889 case ISD::BR_CC:
1890 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001891 // Ensure that libcalls are emitted before a branch.
1892 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1893 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001894 Tmp2 = Node->getOperand(2); // LHS
1895 Tmp3 = Node->getOperand(3); // RHS
1896 Tmp4 = Node->getOperand(1); // CC
1897
1898 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001899 LastCALLSEQ_END = DAG.getEntryNode();
1900
Nate Begeman7e7f4392006-02-01 07:19:44 +00001901 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1902 // the LHS is a legal SETCC itself. In this case, we need to compare
1903 // the result against zero to select between true and false values.
1904 if (Tmp3.Val == 0) {
1905 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1906 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001907 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001908
1909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1910 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001911
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001912 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1913 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
1916 Tmp4 = TLI.LowerOperation(Result, DAG);
1917 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001918 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001919 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001920 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001921 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001922 LoadSDNode *LD = cast<LoadSDNode>(Node);
1923 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1924 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001925
Evan Chenge71fe34d2006-10-09 20:57:25 +00001926 ISD::LoadExtType ExtType = LD->getExtensionType();
1927 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands13237ac2008-06-06 12:08:01 +00001928 MVT VT = Node->getValueType(0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00001929 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1930 Tmp3 = Result.getValue(0);
1931 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001932
Evan Chenge71fe34d2006-10-09 20:57:25 +00001933 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1934 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001935 case TargetLowering::Legal:
1936 // If this is an unaligned load and the target doesn't support it,
1937 // expand it.
1938 if (!TLI.allowsUnalignedMemoryAccesses()) {
1939 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00001940 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001941 if (LD->getAlignment() < ABIAlignment){
1942 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1943 TLI);
1944 Tmp3 = Result.getOperand(0);
1945 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001946 Tmp3 = LegalizeOp(Tmp3);
1947 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001948 }
1949 }
1950 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001951 case TargetLowering::Custom:
1952 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1953 if (Tmp1.Val) {
1954 Tmp3 = LegalizeOp(Tmp1);
1955 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001956 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001957 break;
1958 case TargetLowering::Promote: {
1959 // Only promote a load of vector type to another.
Duncan Sands13237ac2008-06-06 12:08:01 +00001960 assert(VT.isVector() && "Cannot promote this load!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001961 // Change base type to a different vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00001962 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Chenge71fe34d2006-10-09 20:57:25 +00001963
1964 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001965 LD->getSrcValueOffset(),
1966 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001967 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1968 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001969 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001970 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001971 }
1972 // Since loads produce two values, make sure to remember that we
1973 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001974 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1975 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Evan Chenge71fe34d2006-10-09 20:57:25 +00001976 return Op.ResNo ? Tmp4 : Tmp3;
1977 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00001978 MVT SrcVT = LD->getMemoryVT();
1979 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sands95d46ef2008-01-23 20:39:46 +00001980 int SVOffset = LD->getSrcValueOffset();
1981 unsigned Alignment = LD->getAlignment();
1982 bool isVolatile = LD->isVolatile();
1983
Duncan Sands13237ac2008-06-06 12:08:01 +00001984 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sands95d46ef2008-01-23 20:39:46 +00001985 // Some targets pretend to have an i1 loading operation, and actually
1986 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1987 // bits are guaranteed to be zero; it helps the optimizers understand
1988 // that these bits are zero. It is also useful for EXTLOAD, since it
1989 // tells the optimizers that those bits are undefined. It would be
1990 // nice to have an effective generic way of getting these benefits...
1991 // Until such a way is found, don't insist on promoting i1 here.
1992 (SrcVT != MVT::i1 ||
1993 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1994 // Promote to a byte-sized load if not loading an integral number of
1995 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands13237ac2008-06-06 12:08:01 +00001996 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1997 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001998 SDValue Ch;
Duncan Sands95d46ef2008-01-23 20:39:46 +00001999
2000 // The extra bits are guaranteed to be zero, since we stored them that
2001 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2002
2003 ISD::LoadExtType NewExtType =
2004 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2005
2006 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2007 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2008 NVT, isVolatile, Alignment);
2009
2010 Ch = Result.getValue(1); // The chain.
2011
2012 if (ExtType == ISD::SEXTLOAD)
2013 // Having the top bits zero doesn't help when sign extending.
2014 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2015 Result, DAG.getValueType(SrcVT));
2016 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2017 // All the top bits are guaranteed to be zero - inform the optimizers.
2018 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2019 DAG.getValueType(SrcVT));
2020
2021 Tmp1 = LegalizeOp(Result);
2022 Tmp2 = LegalizeOp(Ch);
2023 } else if (SrcWidth & (SrcWidth - 1)) {
2024 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands13237ac2008-06-06 12:08:01 +00002025 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sands95d46ef2008-01-23 20:39:46 +00002026 "Unsupported extload!");
2027 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2028 assert(RoundWidth < SrcWidth);
2029 unsigned ExtraWidth = SrcWidth - RoundWidth;
2030 assert(ExtraWidth < RoundWidth);
2031 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2032 "Load size not an integral number of bytes!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002033 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2034 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002035 SDValue Lo, Hi, Ch;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002036 unsigned IncrementSize;
2037
2038 if (TLI.isLittleEndian()) {
2039 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2040 // Load the bottom RoundWidth bits.
2041 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2042 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2043 Alignment);
2044
2045 // Load the remaining ExtraWidth bits.
2046 IncrementSize = RoundWidth / 8;
2047 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2048 DAG.getIntPtrConstant(IncrementSize));
2049 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2050 LD->getSrcValue(), SVOffset + IncrementSize,
2051 ExtraVT, isVolatile,
2052 MinAlign(Alignment, IncrementSize));
2053
2054 // Build a factor node to remember that this load is independent of the
2055 // other one.
2056 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2057 Hi.getValue(1));
2058
2059 // Move the top bits to the right place.
2060 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2061 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2062
2063 // Join the hi and lo parts.
2064 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002065 } else {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002066 // Big endian - avoid unaligned loads.
2067 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2068 // Load the top RoundWidth bits.
2069 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2070 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2071 Alignment);
2072
2073 // Load the remaining ExtraWidth bits.
2074 IncrementSize = RoundWidth / 8;
2075 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2076 DAG.getIntPtrConstant(IncrementSize));
2077 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2078 LD->getSrcValue(), SVOffset + IncrementSize,
2079 ExtraVT, isVolatile,
2080 MinAlign(Alignment, IncrementSize));
2081
2082 // Build a factor node to remember that this load is independent of the
2083 // other one.
2084 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2085 Hi.getValue(1));
2086
2087 // Move the top bits to the right place.
2088 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2089 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2090
2091 // Join the hi and lo parts.
2092 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2093 }
2094
2095 Tmp1 = LegalizeOp(Result);
2096 Tmp2 = LegalizeOp(Ch);
2097 } else {
2098 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2099 default: assert(0 && "This action is not supported yet!");
2100 case TargetLowering::Custom:
2101 isCustom = true;
2102 // FALLTHROUGH
2103 case TargetLowering::Legal:
2104 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2105 Tmp1 = Result.getValue(0);
2106 Tmp2 = Result.getValue(1);
2107
2108 if (isCustom) {
2109 Tmp3 = TLI.LowerOperation(Result, DAG);
2110 if (Tmp3.Val) {
2111 Tmp1 = LegalizeOp(Tmp3);
2112 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2113 }
2114 } else {
2115 // If this is an unaligned load and the target doesn't support it,
2116 // expand it.
2117 if (!TLI.allowsUnalignedMemoryAccesses()) {
2118 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002119 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sands95d46ef2008-01-23 20:39:46 +00002120 if (LD->getAlignment() < ABIAlignment){
2121 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2122 TLI);
2123 Tmp1 = Result.getOperand(0);
2124 Tmp2 = Result.getOperand(1);
2125 Tmp1 = LegalizeOp(Tmp1);
2126 Tmp2 = LegalizeOp(Tmp2);
2127 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002128 }
2129 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002130 break;
2131 case TargetLowering::Expand:
2132 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2133 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002134 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sands95d46ef2008-01-23 20:39:46 +00002135 LD->getSrcValueOffset(),
2136 LD->isVolatile(), LD->getAlignment());
2137 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2138 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2139 Tmp2 = LegalizeOp(Load.getValue(1));
2140 break;
2141 }
2142 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2143 // Turn the unsupported load into an EXTLOAD followed by an explicit
2144 // zero/sign extend inreg.
2145 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2146 Tmp1, Tmp2, LD->getSrcValue(),
2147 LD->getSrcValueOffset(), SrcVT,
2148 LD->isVolatile(), LD->getAlignment());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002149 SDValue ValRes;
Duncan Sands95d46ef2008-01-23 20:39:46 +00002150 if (ExtType == ISD::SEXTLOAD)
2151 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2152 Result, DAG.getValueType(SrcVT));
2153 else
2154 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2155 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2156 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002157 break;
2158 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002159 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002160
Evan Chenge71fe34d2006-10-09 20:57:25 +00002161 // Since loads produce two values, make sure to remember that we legalized
2162 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002163 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2164 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002165 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002166 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002167 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002168 case ISD::EXTRACT_ELEMENT: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002169 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5172ce62005-10-19 00:06:56 +00002170 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002171 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00002172 case Legal:
2173 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2174 // 1 -> Hi
2175 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands13237ac2008-06-06 12:08:01 +00002176 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5172ce62005-10-19 00:06:56 +00002177 TLI.getShiftAmountTy()));
2178 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2179 } else {
2180 // 0 -> Lo
2181 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2182 Node->getOperand(0));
2183 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002184 break;
2185 case Expand:
2186 // Get both the low and high parts.
2187 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2188 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2189 Result = Tmp2; // 1 -> Hi
2190 else
2191 Result = Tmp1; // 0 -> Lo
2192 break;
2193 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002194 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00002195 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002196
2197 case ISD::CopyToReg:
2198 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00002199
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002200 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00002201 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00002202 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00002203 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002204 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002206 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002207 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002208 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002209 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2211 Tmp3);
2212 } else {
2213 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002214 }
2215
2216 // Since this produces two values, make sure to remember that we legalized
2217 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002218 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2219 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002220 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00002221 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002222 break;
2223
2224 case ISD::RET:
2225 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002226
2227 // Ensure that libcalls are emitted before a return.
2228 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2229 Tmp1 = LegalizeOp(Tmp1);
2230 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002231
Chris Lattnerdc750592005-01-07 07:47:09 +00002232 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00002233 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00002234 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00002235 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002236 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00002237 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00002238 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00002239 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002240 case Expand:
Duncan Sands13237ac2008-06-06 12:08:01 +00002241 if (!Tmp2.getValueType().isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002242 SDValue Lo, Hi;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002243 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00002244
2245 // Big endian systems want the hi reg first.
Duncan Sands7377f5f2008-02-11 10:37:04 +00002246 if (TLI.isBigEndian())
Chris Lattner13780ac2007-03-06 20:01:06 +00002247 std::swap(Lo, Hi);
2248
Evan Cheng3432ab92006-12-11 19:27:14 +00002249 if (Hi.Val)
2250 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2251 else
2252 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00002253 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002254 } else {
2255 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002256 int InIx = Tmp2.ResNo;
Duncan Sands13237ac2008-06-06 12:08:01 +00002257 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2258 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002259
Dan Gohmana8665142007-06-25 16:23:39 +00002260 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002261 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002262 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002263 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002264 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002265 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002266 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002267 } else if (NumElems == 1) {
2268 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002269 Tmp2 = ScalarizeVectorOp(Tmp2);
2270 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002271 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002272
2273 // FIXME: Returns of gcc generic vectors smaller than a legal type
2274 // should be returned in integer registers!
2275
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002276 // The scalarized value type may not be legal, e.g. it might require
2277 // promotion or expansion. Relegalize the return.
2278 Result = LegalizeOp(Result);
2279 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002280 // FIXME: Returns of gcc generic vectors larger than a legal vector
2281 // type should be returned by reference!
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002282 SDValue Lo, Hi;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002283 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002284 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002285 Result = LegalizeOp(Result);
2286 }
2287 }
Misha Brukman835702a2005-04-21 22:36:52 +00002288 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002289 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002290 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002292 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002293 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002294 }
2295 break;
2296 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002297 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002298 break;
2299 default: { // ret <values>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002300 SmallVector<SDValue, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002301 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002302 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002303 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2304 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002305 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002306 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002307 break;
2308 case Expand: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002309 SDValue Lo, Hi;
Duncan Sands13237ac2008-06-06 12:08:01 +00002310 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002311 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002312 ExpandOp(Node->getOperand(i), Lo, Hi);
2313 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002314 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00002315 if (Hi.Val) {
2316 NewValues.push_back(Hi);
2317 NewValues.push_back(Node->getOperand(i+1));
2318 }
Misha Brukman835702a2005-04-21 22:36:52 +00002319 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002320 }
2321 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002322 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002323 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002324
2325 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002326 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002327 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002328 Result = DAG.getNode(ISD::RET, MVT::Other,
2329 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002330 break;
2331 }
2332 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002333
Chris Lattner4d1ea712006-01-29 21:02:23 +00002334 if (Result.getOpcode() == ISD::RET) {
2335 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2336 default: assert(0 && "This action is not supported yet!");
2337 case TargetLowering::Legal: break;
2338 case TargetLowering::Custom:
2339 Tmp1 = TLI.LowerOperation(Result, DAG);
2340 if (Tmp1.Val) Result = Tmp1;
2341 break;
2342 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002343 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002344 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002345 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002346 StoreSDNode *ST = cast<StoreSDNode>(Node);
2347 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2348 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002349 int SVOffset = ST->getSrcValueOffset();
2350 unsigned Alignment = ST->getAlignment();
2351 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002352
Evan Chengab51cf22006-10-13 21:14:26 +00002353 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002354 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2355 // FIXME: We shouldn't do this for TargetConstantFP's.
2356 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2357 // to phase ordering between legalized code and the dag combiner. This
2358 // probably means that we need to integrate dag combiner and legalizer
2359 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002360 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002361 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002362 if (CFP->getValueType(0) == MVT::f32 &&
2363 getTypeAction(MVT::i32) == Legal) {
Dan Gohman10f7d852008-03-11 00:11:06 +00002364 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2365 convertToAPInt().zextOrTrunc(32),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002366 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002367 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2368 SVOffset, isVolatile, Alignment);
2369 break;
2370 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002371 // If this target supports 64-bit registers, do a single 64-bit store.
2372 if (getTypeAction(MVT::i64) == Legal) {
2373 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman10f7d852008-03-11 00:11:06 +00002374 zextOrTrunc(64), MVT::i64);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002375 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2376 SVOffset, isVolatile, Alignment);
2377 break;
Duncan Sands8651e9c2008-06-13 19:07:40 +00002378 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002379 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2380 // stores. If the target supports neither 32- nor 64-bits, this
2381 // xform is certainly not worth it.
Dan Gohman10f7d852008-03-11 00:11:06 +00002382 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002383 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2384 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00002385 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002386
2387 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2388 SVOffset, isVolatile, Alignment);
2389 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002390 DAG.getIntPtrConstant(4));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002391 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002392 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002393
2394 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2395 break;
2396 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002397 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002398 }
2399
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002400 switch (getTypeAction(ST->getMemoryVT())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002401 case Legal: {
2402 Tmp3 = LegalizeOp(ST->getValue());
2403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2404 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002405
Duncan Sands13237ac2008-06-06 12:08:01 +00002406 MVT VT = Tmp3.getValueType();
Evan Chengab51cf22006-10-13 21:14:26 +00002407 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2408 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002409 case TargetLowering::Legal:
2410 // If this is an unaligned store and the target doesn't support it,
2411 // expand it.
2412 if (!TLI.allowsUnalignedMemoryAccesses()) {
2413 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002414 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002415 if (ST->getAlignment() < ABIAlignment)
2416 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2417 TLI);
2418 }
2419 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002420 case TargetLowering::Custom:
2421 Tmp1 = TLI.LowerOperation(Result, DAG);
2422 if (Tmp1.Val) Result = Tmp1;
2423 break;
2424 case TargetLowering::Promote:
Duncan Sands13237ac2008-06-06 12:08:01 +00002425 assert(VT.isVector() && "Unknown legal promote case!");
Evan Chengab51cf22006-10-13 21:14:26 +00002426 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2427 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2428 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002429 ST->getSrcValue(), SVOffset, isVolatile,
2430 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002431 break;
2432 }
2433 break;
2434 }
2435 case Promote:
2436 // Truncate the value and store the result.
2437 Tmp3 = PromoteOp(ST->getValue());
2438 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002439 SVOffset, ST->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00002440 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002441 break;
2442
2443 case Expand:
2444 unsigned IncrementSize = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002445 SDValue Lo, Hi;
Evan Chengab51cf22006-10-13 21:14:26 +00002446
2447 // If this is a vector type, then we have to calculate the increment as
2448 // the product of the element size in bytes, and the number of elements
2449 // in the high half of the vector.
Duncan Sands13237ac2008-06-06 12:08:01 +00002450 if (ST->getValue().getValueType().isVector()) {
Evan Chengab51cf22006-10-13 21:14:26 +00002451 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002452 int InIx = ST->getValue().ResNo;
Duncan Sands13237ac2008-06-06 12:08:01 +00002453 MVT InVT = InVal->getValueType(InIx);
2454 unsigned NumElems = InVT.getVectorNumElements();
2455 MVT EVT = InVT.getVectorElementType();
Evan Chengab51cf22006-10-13 21:14:26 +00002456
Dan Gohmana8665142007-06-25 16:23:39 +00002457 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002458 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00002459 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002460 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002461 // Turn this into a normal store of the vector type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002462 Tmp3 = LegalizeOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002463 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002464 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002465 Result = LegalizeOp(Result);
2466 break;
2467 } else if (NumElems == 1) {
2468 // Turn this into a normal store of the scalar type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002469 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002470 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002471 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002472 // The scalarized value type may not be legal, e.g. it might require
2473 // promotion or expansion. Relegalize the scalar store.
2474 Result = LegalizeOp(Result);
2475 break;
2476 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002477 SplitVectorOp(ST->getValue(), Lo, Hi);
Duncan Sands13237ac2008-06-06 12:08:01 +00002478 IncrementSize = Lo.Val->getValueType(0).getVectorNumElements() *
2479 EVT.getSizeInBits()/8;
Evan Chengab51cf22006-10-13 21:14:26 +00002480 }
2481 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002482 ExpandOp(ST->getValue(), Lo, Hi);
Duncan Sands13237ac2008-06-06 12:08:01 +00002483 IncrementSize = Hi.Val ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002484
Duncan Sands7377f5f2008-02-11 10:37:04 +00002485 if (TLI.isBigEndian())
Evan Chengab51cf22006-10-13 21:14:26 +00002486 std::swap(Lo, Hi);
2487 }
2488
2489 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002490 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002491
2492 if (Hi.Val == NULL) {
2493 // Must be int <-> float one-to-one expansion.
2494 Result = Lo;
2495 break;
2496 }
2497
Evan Chengab51cf22006-10-13 21:14:26 +00002498 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002499 DAG.getIntPtrConstant(IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00002500 assert(isTypeLegal(Tmp2.getValueType()) &&
2501 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002502 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002503 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002504 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002505 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002506 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2507 break;
2508 }
2509 } else {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002510 switch (getTypeAction(ST->getValue().getValueType())) {
2511 case Legal:
2512 Tmp3 = LegalizeOp(ST->getValue());
2513 break;
2514 case Promote:
2515 // We can promote the value, the truncstore will still take care of it.
2516 Tmp3 = PromoteOp(ST->getValue());
2517 break;
2518 case Expand:
2519 // Just store the low part. This may become a non-trunc store, so make
2520 // sure to use getTruncStore, not UpdateNodeOperands below.
2521 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2522 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2523 SVOffset, MVT::i8, isVolatile, Alignment);
2524 }
Evan Chengab51cf22006-10-13 21:14:26 +00002525
Duncan Sands13237ac2008-06-06 12:08:01 +00002526 MVT StVT = ST->getMemoryVT();
2527 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands88de26c2008-01-22 07:17:34 +00002528
Duncan Sands13237ac2008-06-06 12:08:01 +00002529 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands88de26c2008-01-22 07:17:34 +00002530 // Promote to a byte-sized store with upper bits zero if not
2531 // storing an integral number of bytes. For example, promote
2532 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands13237ac2008-06-06 12:08:01 +00002533 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands88de26c2008-01-22 07:17:34 +00002534 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2535 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2536 SVOffset, NVT, isVolatile, Alignment);
2537 } else if (StWidth & (StWidth - 1)) {
2538 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands13237ac2008-06-06 12:08:01 +00002539 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands88de26c2008-01-22 07:17:34 +00002540 "Unsupported truncstore!");
2541 unsigned RoundWidth = 1 << Log2_32(StWidth);
2542 assert(RoundWidth < StWidth);
2543 unsigned ExtraWidth = StWidth - RoundWidth;
2544 assert(ExtraWidth < RoundWidth);
2545 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2546 "Store size not an integral number of bytes!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002547 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2548 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002549 SDValue Lo, Hi;
Duncan Sands88de26c2008-01-22 07:17:34 +00002550 unsigned IncrementSize;
2551
2552 if (TLI.isLittleEndian()) {
2553 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2554 // Store the bottom RoundWidth bits.
2555 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2556 SVOffset, RoundVT,
2557 isVolatile, Alignment);
2558
2559 // Store the remaining ExtraWidth bits.
2560 IncrementSize = RoundWidth / 8;
2561 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2562 DAG.getIntPtrConstant(IncrementSize));
2563 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2564 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2565 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2566 SVOffset + IncrementSize, ExtraVT, isVolatile,
2567 MinAlign(Alignment, IncrementSize));
2568 } else {
2569 // Big endian - avoid unaligned stores.
2570 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2571 // Store the top RoundWidth bits.
2572 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2573 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2574 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2575 RoundVT, isVolatile, Alignment);
2576
2577 // Store the remaining ExtraWidth bits.
2578 IncrementSize = RoundWidth / 8;
2579 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2580 DAG.getIntPtrConstant(IncrementSize));
2581 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2582 SVOffset + IncrementSize, ExtraVT, isVolatile,
2583 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002584 }
Duncan Sands88de26c2008-01-22 07:17:34 +00002585
2586 // The order of the stores doesn't matter.
2587 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2588 } else {
2589 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2590 Tmp2 != ST->getBasePtr())
2591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2592 ST->getOffset());
2593
2594 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2595 default: assert(0 && "This action is not supported yet!");
2596 case TargetLowering::Legal:
2597 // If this is an unaligned store and the target doesn't support it,
2598 // expand it.
2599 if (!TLI.allowsUnalignedMemoryAccesses()) {
2600 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands13237ac2008-06-06 12:08:01 +00002601 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands88de26c2008-01-22 07:17:34 +00002602 if (ST->getAlignment() < ABIAlignment)
2603 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2604 TLI);
2605 }
2606 break;
2607 case TargetLowering::Custom:
2608 Result = TLI.LowerOperation(Result, DAG);
2609 break;
2610 case Expand:
2611 // TRUNCSTORE:i16 i32 -> STORE i16
2612 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2613 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2614 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2615 isVolatile, Alignment);
2616 break;
2617 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00002618 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002619 }
2620 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002621 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002622 case ISD::PCMARKER:
2623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002625 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002626 case ISD::STACKSAVE:
2627 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002628 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2629 Tmp1 = Result.getValue(0);
2630 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002631
Chris Lattnerb3266452006-01-13 02:50:02 +00002632 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2633 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002634 case TargetLowering::Legal: break;
2635 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002636 Tmp3 = TLI.LowerOperation(Result, DAG);
2637 if (Tmp3.Val) {
2638 Tmp1 = LegalizeOp(Tmp3);
2639 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002640 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002641 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002642 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002643 // Expand to CopyFromReg if the target set
2644 // StackPointerRegisterToSaveRestore.
2645 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002646 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002647 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002648 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002649 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002650 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2651 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002652 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002653 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002654 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002655
2656 // Since stacksave produce two values, make sure to remember that we
2657 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002658 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2659 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002660 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002661
Chris Lattnerb3266452006-01-13 02:50:02 +00002662 case ISD::STACKRESTORE:
2663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002666
2667 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2668 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002669 case TargetLowering::Legal: break;
2670 case TargetLowering::Custom:
2671 Tmp1 = TLI.LowerOperation(Result, DAG);
2672 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002673 break;
2674 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002675 // Expand to CopyToReg if the target set
2676 // StackPointerRegisterToSaveRestore.
2677 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2678 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2679 } else {
2680 Result = Tmp1;
2681 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002682 break;
2683 }
2684 break;
2685
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002686 case ISD::READCYCLECOUNTER:
2687 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002688 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002689 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2690 Node->getValueType(0))) {
2691 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002692 case TargetLowering::Legal:
2693 Tmp1 = Result.getValue(0);
2694 Tmp2 = Result.getValue(1);
2695 break;
Evan Cheng69739932006-11-29 08:26:18 +00002696 case TargetLowering::Custom:
2697 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002698 Tmp1 = LegalizeOp(Result.getValue(0));
2699 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002700 break;
2701 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002702
2703 // Since rdcc produce two values, make sure to remember that we legalized
2704 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002705 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2706 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002707 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002708
Chris Lattner39c67442005-01-14 22:08:15 +00002709 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002710 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2711 case Expand: assert(0 && "It's impossible to expand bools");
2712 case Legal:
2713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2714 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002715 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002716 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002717 // Make sure the condition is either zero or one.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002718 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002719 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002720 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattner3abb6362006-11-28 01:03:30 +00002721 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002722 break;
2723 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002724 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002725 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002726 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002727
Chris Lattnerd02b0542006-01-28 10:58:55 +00002728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002729
Nate Begeman987121a2005-08-23 04:29:48 +00002730 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002731 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002732 case TargetLowering::Legal: break;
2733 case TargetLowering::Custom: {
2734 Tmp1 = TLI.LowerOperation(Result, DAG);
2735 if (Tmp1.Val) Result = Tmp1;
2736 break;
2737 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002738 case TargetLowering::Expand:
2739 if (Tmp1.getOpcode() == ISD::SETCC) {
2740 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2741 Tmp2, Tmp3,
2742 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2743 } else {
2744 Result = DAG.getSelectCC(Tmp1,
2745 DAG.getConstant(0, Tmp1.getValueType()),
2746 Tmp2, Tmp3, ISD::SETNE);
2747 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002748 break;
2749 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002750 MVT NVT =
Chris Lattner3c0dd462005-01-16 07:29:19 +00002751 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2752 unsigned ExtOp, TruncOp;
Duncan Sands13237ac2008-06-06 12:08:01 +00002753 if (Tmp2.getValueType().isVector()) {
Evan Chengbe8a8932006-04-12 16:33:18 +00002754 ExtOp = ISD::BIT_CONVERT;
2755 TruncOp = ISD::BIT_CONVERT;
Duncan Sands13237ac2008-06-06 12:08:01 +00002756 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002757 ExtOp = ISD::ANY_EXTEND;
2758 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002759 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002760 ExtOp = ISD::FP_EXTEND;
2761 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002762 }
2763 // Promote each of the values to the new type.
2764 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2765 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2766 // Perform the larger operation, then round down.
2767 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner72733e52008-01-17 07:00:52 +00002768 if (TruncOp != ISD::FP_ROUND)
2769 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2770 else
2771 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2772 DAG.getIntPtrConstant(0));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002773 break;
2774 }
2775 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002776 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002777 case ISD::SELECT_CC: {
2778 Tmp1 = Node->getOperand(0); // LHS
2779 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002780 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2781 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002782 SDValue CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002783
Nate Begeman7e7f4392006-02-01 07:19:44 +00002784 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2785
2786 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2787 // the LHS is a legal SETCC itself. In this case, we need to compare
2788 // the result against zero to select between true and false values.
2789 if (Tmp2.Val == 0) {
2790 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2791 CC = DAG.getCondCode(ISD::SETNE);
2792 }
2793 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2794
2795 // Everything is legal, see if we should expand this op or something.
2796 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2797 default: assert(0 && "This action is not supported yet!");
2798 case TargetLowering::Legal: break;
2799 case TargetLowering::Custom:
2800 Tmp1 = TLI.LowerOperation(Result, DAG);
2801 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002802 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002803 }
2804 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002805 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002806 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002807 Tmp1 = Node->getOperand(0);
2808 Tmp2 = Node->getOperand(1);
2809 Tmp3 = Node->getOperand(2);
2810 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2811
2812 // If we had to Expand the SetCC operands into a SELECT node, then it may
2813 // not always be possible to return a true LHS & RHS. In this case, just
2814 // return the value we legalized, returned in the LHS
2815 if (Tmp2.Val == 0) {
2816 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002817 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002818 }
Nate Begeman987121a2005-08-23 04:29:48 +00002819
Chris Lattnerf263a232006-01-30 22:43:50 +00002820 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002821 default: assert(0 && "Cannot handle this action for SETCC yet!");
2822 case TargetLowering::Custom:
2823 isCustom = true;
2824 // FALLTHROUGH.
2825 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002826 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002827 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002828 Tmp4 = TLI.LowerOperation(Result, DAG);
2829 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002830 }
Nate Begeman987121a2005-08-23 04:29:48 +00002831 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002832 case TargetLowering::Promote: {
2833 // First step, figure out the appropriate operation to use.
2834 // Allow SETCC to not be supported for all legal data types
2835 // Mostly this targets FP
Duncan Sands13237ac2008-06-06 12:08:01 +00002836 MVT NewInTy = Node->getOperand(0).getValueType();
2837 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002838
2839 // Scan for the appropriate larger type to use.
2840 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002841 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002842
Duncan Sands13237ac2008-06-06 12:08:01 +00002843 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002844 "Fell off of the edge of the integer world");
Duncan Sands13237ac2008-06-06 12:08:01 +00002845 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002846 "Fell off of the edge of the floating point world");
2847
2848 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002849 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002850 break;
2851 }
Duncan Sands13237ac2008-06-06 12:08:01 +00002852 if (NewInTy.isInteger())
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002853 assert(0 && "Cannot promote Legal Integer SETCC yet");
2854 else {
2855 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2856 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2857 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002858 Tmp1 = LegalizeOp(Tmp1);
2859 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002861 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002862 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002863 }
Nate Begeman987121a2005-08-23 04:29:48 +00002864 case TargetLowering::Expand:
2865 // Expand a setcc node into a select_cc of the same condition, lhs, and
2866 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands13237ac2008-06-06 12:08:01 +00002867 MVT VT = Node->getValueType(0);
Nate Begeman987121a2005-08-23 04:29:48 +00002868 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2869 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002870 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002871 break;
2872 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002873 break;
Nate Begemancfcb5602008-05-12 19:40:03 +00002874 case ISD::VSETCC: {
2875 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2876 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002877 SDValue CC = Node->getOperand(2);
Nate Begemancfcb5602008-05-12 19:40:03 +00002878
2879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
2880
2881 // Everything is legal, see if we should expand this op or something.
2882 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
2883 default: assert(0 && "This action is not supported yet!");
2884 case TargetLowering::Legal: break;
2885 case TargetLowering::Custom:
2886 Tmp1 = TLI.LowerOperation(Result, DAG);
2887 if (Tmp1.Val) Result = Tmp1;
2888 break;
2889 }
2890 break;
2891 }
Chris Lattner5385db52005-05-09 20:23:03 +00002892
Chris Lattner4157c412005-04-02 04:00:59 +00002893 case ISD::SHL_PARTS:
2894 case ISD::SRA_PARTS:
2895 case ISD::SRL_PARTS: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002896 SmallVector<SDValue, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002897 bool Changed = false;
2898 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2899 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2900 Changed |= Ops.back() != Node->getOperand(i);
2901 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002902 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002903 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002904
Evan Cheng870e4f82006-01-09 18:31:59 +00002905 switch (TLI.getOperationAction(Node->getOpcode(),
2906 Node->getValueType(0))) {
2907 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002908 case TargetLowering::Legal: break;
2909 case TargetLowering::Custom:
2910 Tmp1 = TLI.LowerOperation(Result, DAG);
2911 if (Tmp1.Val) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002912 SDValue Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002913 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002914 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002915 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Evan Cheng870e4f82006-01-09 18:31:59 +00002916 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002917 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002918 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002919 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002920 return RetVal;
2921 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002922 break;
2923 }
2924
Chris Lattner13fe99c2005-04-02 05:00:07 +00002925 // Since these produce multiple values, make sure to remember that we
2926 // legalized all of them.
2927 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002928 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Chris Lattner13fe99c2005-04-02 05:00:07 +00002929 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002930 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002931
2932 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002933 case ISD::ADD:
2934 case ISD::SUB:
2935 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002936 case ISD::MULHS:
2937 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002938 case ISD::UDIV:
2939 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002940 case ISD::AND:
2941 case ISD::OR:
2942 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002943 case ISD::SHL:
2944 case ISD::SRL:
2945 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002946 case ISD::FADD:
2947 case ISD::FSUB:
2948 case ISD::FMUL:
2949 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002950 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002951 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002952 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2953 case Expand: assert(0 && "Not possible");
2954 case Legal:
2955 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2956 break;
2957 case Promote:
2958 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2959 break;
2960 }
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002961
2962 if ((Node->getOpcode() == ISD::SHL ||
2963 Node->getOpcode() == ISD::SRL ||
2964 Node->getOpcode() == ISD::SRA) &&
2965 !Node->getValueType(0).isVector()) {
2966 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
2967 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
2968 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
2969 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
2970 }
2971
Chris Lattnerd02b0542006-01-28 10:58:55 +00002972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002973
Andrew Lenharth72594262005-12-24 23:42:32 +00002974 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002975 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002976 case TargetLowering::Legal: break;
2977 case TargetLowering::Custom:
2978 Tmp1 = TLI.LowerOperation(Result, DAG);
Nate Begemand63495f2008-07-29 18:28:31 +00002979 if (Tmp1.Val)
2980 // FIXME: these braces are correct, but breaks CellSPU codegen.
2981 //{
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002982 Result = Tmp1;
2983 break;
Nate Begemand63495f2008-07-29 18:28:31 +00002984 //}
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002985 // Fall through if the custom lower can't deal with the operation
Chris Lattner87f08092006-04-02 03:57:31 +00002986 case TargetLowering::Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00002987 MVT VT = Op.getValueType();
Dan Gohmana1603612007-10-08 18:33:35 +00002988
2989 // See if multiply or divide can be lowered using two-result operations.
2990 SDVTList VTs = DAG.getVTList(VT, VT);
2991 if (Node->getOpcode() == ISD::MUL) {
2992 // We just need the low half of the multiply; try both the signed
2993 // and unsigned forms. If the target supports both SMUL_LOHI and
2994 // UMUL_LOHI, form a preference by checking which forms of plain
2995 // MULH it supports.
2996 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2997 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2998 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2999 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3000 unsigned OpToUse = 0;
3001 if (HasSMUL_LOHI && !HasMULHS) {
3002 OpToUse = ISD::SMUL_LOHI;
3003 } else if (HasUMUL_LOHI && !HasMULHU) {
3004 OpToUse = ISD::UMUL_LOHI;
3005 } else if (HasSMUL_LOHI) {
3006 OpToUse = ISD::SMUL_LOHI;
3007 } else if (HasUMUL_LOHI) {
3008 OpToUse = ISD::UMUL_LOHI;
3009 }
3010 if (OpToUse) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003011 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003012 break;
3013 }
3014 }
3015 if (Node->getOpcode() == ISD::MULHS &&
3016 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003017 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003018 break;
3019 }
3020 if (Node->getOpcode() == ISD::MULHU &&
3021 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003022 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003023 break;
3024 }
3025 if (Node->getOpcode() == ISD::SDIV &&
3026 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003027 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003028 break;
3029 }
3030 if (Node->getOpcode() == ISD::UDIV &&
3031 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003032 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohmana1603612007-10-08 18:33:35 +00003033 break;
3034 }
3035
Dan Gohman2a7de412007-10-11 23:57:53 +00003036 // Check to see if we have a libcall for this operator.
3037 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3038 bool isSigned = false;
3039 switch (Node->getOpcode()) {
3040 case ISD::UDIV:
3041 case ISD::SDIV:
3042 if (VT == MVT::i32) {
3043 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00003044 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00003045 isSigned = Node->getOpcode() == ISD::SDIV;
3046 }
3047 break;
3048 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00003049 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3050 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00003051 break;
3052 default: break;
3053 }
3054 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003055 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003056 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003057 break;
3058 }
3059
Duncan Sands13237ac2008-06-06 12:08:01 +00003060 assert(Node->getValueType(0).isVector() &&
Chris Lattner87f08092006-04-02 03:57:31 +00003061 "Cannot expand this binary operator!");
3062 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00003063 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00003064 break;
3065 }
Evan Cheng119266e2006-04-12 21:20:24 +00003066 case TargetLowering::Promote: {
3067 switch (Node->getOpcode()) {
3068 default: assert(0 && "Do not know how to promote this BinOp!");
3069 case ISD::AND:
3070 case ISD::OR:
3071 case ISD::XOR: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003072 MVT OVT = Node->getValueType(0);
3073 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3074 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Cheng119266e2006-04-12 21:20:24 +00003075 // Bit convert each of the values to the new type.
3076 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3077 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3078 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3079 // Bit convert the result back the original type.
3080 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3081 break;
3082 }
3083 }
3084 }
Andrew Lenharth72594262005-12-24 23:42:32 +00003085 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003086 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003087
Dan Gohman12334ac2007-10-05 14:17:22 +00003088 case ISD::SMUL_LOHI:
3089 case ISD::UMUL_LOHI:
3090 case ISD::SDIVREM:
3091 case ISD::UDIVREM:
3092 // These nodes will only be produced by target-specific lowering, so
3093 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00003094 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00003095 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00003096
3097 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3098 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3099 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00003100 break;
3101
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003102 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3103 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3104 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3105 case Expand: assert(0 && "Not possible");
3106 case Legal:
3107 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3108 break;
3109 case Promote:
3110 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3111 break;
3112 }
3113
3114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3115
3116 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3117 default: assert(0 && "Operation not supported");
3118 case TargetLowering::Custom:
3119 Tmp1 = TLI.LowerOperation(Result, DAG);
3120 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00003121 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003122 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00003123 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00003124 // If this target supports fabs/fneg natively and select is cheap,
3125 // do this efficiently.
3126 if (!TLI.isSelectExpensive() &&
3127 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3128 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00003129 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00003130 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00003131 // Get the sign bit of the RHS.
Duncan Sands13237ac2008-06-06 12:08:01 +00003132 MVT IVT =
Chris Lattner994d8e62006-03-13 06:08:38 +00003133 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003134 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00003135 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner994d8e62006-03-13 06:08:38 +00003136 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3137 // Get the absolute value of the result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003138 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner994d8e62006-03-13 06:08:38 +00003139 // Select between the nabs and abs value based on the sign bit of
3140 // the input.
3141 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3142 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3143 AbsVal),
3144 AbsVal);
3145 Result = LegalizeOp(Result);
3146 break;
3147 }
3148
3149 // Otherwise, do bitwise ops!
Duncan Sands13237ac2008-06-06 12:08:01 +00003150 MVT NVT =
Evan Cheng003feb02007-01-04 21:56:39 +00003151 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3152 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3153 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3154 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003155 break;
3156 }
Evan Cheng003feb02007-01-04 21:56:39 +00003157 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003158 break;
3159
Nate Begeman5965bd12006-02-17 05:43:56 +00003160 case ISD::ADDC:
3161 case ISD::SUBC:
3162 Tmp1 = LegalizeOp(Node->getOperand(0));
3163 Tmp2 = LegalizeOp(Node->getOperand(1));
3164 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3165 // Since this produces two values, make sure to remember that we legalized
3166 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003167 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3168 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman5965bd12006-02-17 05:43:56 +00003169 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00003170
Nate Begeman5965bd12006-02-17 05:43:56 +00003171 case ISD::ADDE:
3172 case ISD::SUBE:
3173 Tmp1 = LegalizeOp(Node->getOperand(0));
3174 Tmp2 = LegalizeOp(Node->getOperand(1));
3175 Tmp3 = LegalizeOp(Node->getOperand(2));
3176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3177 // Since this produces two values, make sure to remember that we legalized
3178 // both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003179 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3180 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman5965bd12006-02-17 05:43:56 +00003181 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00003182
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003183 case ISD::BUILD_PAIR: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003184 MVT PairTy = Node->getValueType(0);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003185 // TODO: handle the case where the Lo and Hi operands are not of legal type
3186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3187 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3188 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003189 case TargetLowering::Promote:
3190 case TargetLowering::Custom:
3191 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003192 case TargetLowering::Legal:
3193 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3194 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3195 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003196 case TargetLowering::Expand:
3197 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3198 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3199 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00003200 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003201 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003202 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003203 break;
3204 }
3205 break;
3206 }
3207
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003208 case ISD::UREM:
3209 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003210 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003211 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3212 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003213
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003214 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003215 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3216 case TargetLowering::Custom:
3217 isCustom = true;
3218 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003219 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003220 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003221 if (isCustom) {
3222 Tmp1 = TLI.LowerOperation(Result, DAG);
3223 if (Tmp1.Val) Result = Tmp1;
3224 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003225 break;
Dan Gohmana1603612007-10-08 18:33:35 +00003226 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00003227 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00003228 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands13237ac2008-06-06 12:08:01 +00003229 MVT VT = Node->getValueType(0);
Dan Gohmana1603612007-10-08 18:33:35 +00003230
3231 // See if remainder can be lowered using two-result operations.
3232 SDVTList VTs = DAG.getVTList(VT, VT);
3233 if (Node->getOpcode() == ISD::SREM &&
3234 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003235 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003236 break;
3237 }
3238 if (Node->getOpcode() == ISD::UREM &&
3239 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003240 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00003241 break;
3242 }
3243
Duncan Sands13237ac2008-06-06 12:08:01 +00003244 if (VT.isInteger()) {
Dan Gohmana1603612007-10-08 18:33:35 +00003245 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003246 TargetLowering::Legal) {
3247 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00003248 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003249 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3250 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands13237ac2008-06-06 12:08:01 +00003251 } else if (VT.isVector()) {
Dan Gohman08143e32007-11-05 23:35:22 +00003252 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003253 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00003254 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003255 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00003256 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3257 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003258 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003259 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003260 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003261 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00003262 assert(VT.isFloatingPoint() &&
Dan Gohmanccfc0282007-11-06 22:11:54 +00003263 "remainder op must have integer or floating-point type");
Duncan Sands13237ac2008-06-06 12:08:01 +00003264 if (VT.isVector()) {
Dan Gohman08143e32007-11-05 23:35:22 +00003265 Result = LegalizeOp(UnrollVectorOp(Op));
3266 } else {
3267 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003268 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3269 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003270 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003271 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman08143e32007-11-05 23:35:22 +00003272 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003273 }
3274 break;
3275 }
Dan Gohmana1603612007-10-08 18:33:35 +00003276 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003277 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003278 case ISD::VAARG: {
3279 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3280 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3281
Duncan Sands13237ac2008-06-06 12:08:01 +00003282 MVT VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003283 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3284 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003285 case TargetLowering::Custom:
3286 isCustom = true;
3287 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003288 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003289 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3290 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003291 Tmp1 = Result.getValue(1);
3292
3293 if (isCustom) {
3294 Tmp2 = TLI.LowerOperation(Result, DAG);
3295 if (Tmp2.Val) {
3296 Result = LegalizeOp(Tmp2);
3297 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3298 }
3299 }
Nate Begemane74795c2006-01-25 18:21:52 +00003300 break;
3301 case TargetLowering::Expand: {
Dan Gohman2d489b52008-02-06 22:27:42 +00003302 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003303 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003304 // Increment the pointer, VAList, to the next vaarg
3305 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands13237ac2008-06-06 12:08:01 +00003306 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begemane74795c2006-01-25 18:21:52 +00003307 TLI.getPointerTy()));
3308 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00003309 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003310 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003311 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003312 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003313 Result = LegalizeOp(Result);
3314 break;
3315 }
3316 }
3317 // Since VAARG produces two values, make sure to remember that we
3318 // legalized both of them.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003319 AddLegalizedOperand(SDValue(Node, 0), Result);
3320 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003321 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003322 }
3323
3324 case ISD::VACOPY:
3325 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3326 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3327 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3328
3329 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3330 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003331 case TargetLowering::Custom:
3332 isCustom = true;
3333 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003334 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3336 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003337 if (isCustom) {
3338 Tmp1 = TLI.LowerOperation(Result, DAG);
3339 if (Tmp1.Val) Result = Tmp1;
3340 }
Nate Begemane74795c2006-01-25 18:21:52 +00003341 break;
3342 case TargetLowering::Expand:
3343 // This defaults to loading a pointer from the input and storing it to the
3344 // output, returning the chain.
Dan Gohman2d489b52008-02-06 22:27:42 +00003345 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3346 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman9752a8f32008-04-17 02:09:26 +00003347 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3348 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003349 break;
3350 }
3351 break;
3352
3353 case ISD::VAEND:
3354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3355 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3356
3357 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3358 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003359 case TargetLowering::Custom:
3360 isCustom = true;
3361 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003362 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003364 if (isCustom) {
3365 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3366 if (Tmp1.Val) Result = Tmp1;
3367 }
Nate Begemane74795c2006-01-25 18:21:52 +00003368 break;
3369 case TargetLowering::Expand:
3370 Result = Tmp1; // Default to a no-op, return the chain
3371 break;
3372 }
3373 break;
3374
3375 case ISD::VASTART:
3376 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3377 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3378
Chris Lattnerd02b0542006-01-28 10:58:55 +00003379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3380
Nate Begemane74795c2006-01-25 18:21:52 +00003381 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3382 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003383 case TargetLowering::Legal: break;
3384 case TargetLowering::Custom:
3385 Tmp1 = TLI.LowerOperation(Result, DAG);
3386 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003387 break;
3388 }
3389 break;
3390
Nate Begeman1b8121b2006-01-11 21:21:00 +00003391 case ISD::ROTL:
3392 case ISD::ROTR:
3393 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3394 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003395 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003396 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3397 default:
3398 assert(0 && "ROTL/ROTR legalize operation not supported");
3399 break;
3400 case TargetLowering::Legal:
3401 break;
3402 case TargetLowering::Custom:
3403 Tmp1 = TLI.LowerOperation(Result, DAG);
3404 if (Tmp1.Val) Result = Tmp1;
3405 break;
3406 case TargetLowering::Promote:
3407 assert(0 && "Do not know how to promote ROTL/ROTR");
3408 break;
3409 case TargetLowering::Expand:
3410 assert(0 && "Do not know how to expand ROTL/ROTR");
3411 break;
3412 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003413 break;
3414
Nate Begeman2fba8a32006-01-14 03:14:10 +00003415 case ISD::BSWAP:
3416 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3417 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003418 case TargetLowering::Custom:
3419 assert(0 && "Cannot custom legalize this yet!");
3420 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003421 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003422 break;
3423 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003424 MVT OVT = Tmp1.getValueType();
3425 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3426 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begeman2fba8a32006-01-14 03:14:10 +00003427
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003428 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3429 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3430 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3431 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3432 break;
3433 }
3434 case TargetLowering::Expand:
3435 Result = ExpandBSWAP(Tmp1);
3436 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003437 }
3438 break;
3439
Andrew Lenharth5e177822005-05-03 17:19:30 +00003440 case ISD::CTPOP:
3441 case ISD::CTTZ:
3442 case ISD::CTLZ:
3443 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3444 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003445 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003446 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003447 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003448 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003449 TargetLowering::Custom) {
3450 Tmp1 = TLI.LowerOperation(Result, DAG);
3451 if (Tmp1.Val) {
3452 Result = Tmp1;
3453 }
Scott Michel34e2d222007-07-30 21:00:31 +00003454 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003455 break;
3456 case TargetLowering::Promote: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003457 MVT OVT = Tmp1.getValueType();
3458 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003459
3460 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003461 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3462 // Perform the larger operation, then subtract if needed.
3463 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003464 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003465 case ISD::CTPOP:
3466 Result = Tmp1;
3467 break;
3468 case ISD::CTTZ:
3469 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00003470 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00003471 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003472 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003473 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00003474 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003475 break;
3476 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003477 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003478 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00003479 DAG.getConstant(NVT.getSizeInBits() -
3480 OVT.getSizeInBits(), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003481 break;
3482 }
3483 break;
3484 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003485 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003486 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003487 break;
3488 }
3489 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003490
Chris Lattner13fe99c2005-04-02 05:00:07 +00003491 // Unary operators
3492 case ISD::FABS:
3493 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003494 case ISD::FSQRT:
3495 case ISD::FSIN:
3496 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003497 Tmp1 = LegalizeOp(Node->getOperand(0));
3498 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003499 case TargetLowering::Promote:
3500 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003501 isCustom = true;
3502 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003503 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003504 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003505 if (isCustom) {
3506 Tmp1 = TLI.LowerOperation(Result, DAG);
3507 if (Tmp1.Val) Result = Tmp1;
3508 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003509 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003510 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003511 switch (Node->getOpcode()) {
3512 default: assert(0 && "Unreachable!");
3513 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003514 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3515 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003516 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003517 break;
Chris Lattner80026402005-04-30 04:43:14 +00003518 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003519 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands13237ac2008-06-06 12:08:01 +00003520 MVT VT = Node->getValueType(0);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003521 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003522 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman63eb03f2008-03-14 00:53:31 +00003523 ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003524 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3525 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003526 break;
3527 }
3528 case ISD::FSQRT:
3529 case ISD::FSIN:
3530 case ISD::FCOS: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003531 MVT VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003532
3533 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands13237ac2008-06-06 12:08:01 +00003534 if (VT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +00003535 Result = LegalizeOp(UnrollVectorOp(Op));
3536 break;
3537 }
3538
Evan Cheng31cbddf2007-01-12 02:11:51 +00003539 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003540 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003541 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003542 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3543 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003544 break;
3545 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003546 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3547 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003548 break;
3549 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003550 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3551 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003552 break;
Chris Lattner80026402005-04-30 04:43:14 +00003553 default: assert(0 && "Unreachable!");
3554 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003555 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003556 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003557 break;
3558 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003559 }
3560 break;
3561 }
3562 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003563 case ISD::FPOWI: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003564 MVT VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003565
3566 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands13237ac2008-06-06 12:08:01 +00003567 if (VT.isVector()) {
Dan Gohman2a7de412007-10-11 23:57:53 +00003568 Result = LegalizeOp(UnrollVectorOp(Op));
3569 break;
3570 }
3571
3572 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003573 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3574 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003575 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003576 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003577 break;
3578 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003579 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003580 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003581 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3582 Node->getValueType(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00003583 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +00003584 // The input has to be a vector type, we have to either scalarize it, pack
3585 // it, or convert it based on whether the input vector type is legal.
3586 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003587 int InIx = Node->getOperand(0).ResNo;
Duncan Sands13237ac2008-06-06 12:08:01 +00003588 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3589 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohmana8665142007-06-25 16:23:39 +00003590
3591 // Figure out if there is a simple type corresponding to this Vector
3592 // type. If so, convert to the vector type.
Duncan Sands13237ac2008-06-06 12:08:01 +00003593 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00003594 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003595 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003596 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3597 LegalizeOp(Node->getOperand(0)));
3598 break;
3599 } else if (NumElems == 1) {
3600 // Turn this into a bit convert of the scalar input.
3601 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3602 ScalarizeVectorOp(Node->getOperand(0)));
3603 break;
3604 } else {
3605 // FIXME: UNIMP! Store then reload
3606 assert(0 && "Cast from unsupported vector type not implemented yet!");
3607 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003608 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003609 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3610 Node->getOperand(0).getValueType())) {
3611 default: assert(0 && "Unknown operation action!");
3612 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003613 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3614 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003615 break;
3616 case TargetLowering::Legal:
3617 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003618 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003619 break;
3620 }
3621 }
3622 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003623
Chris Lattner13fe99c2005-04-02 05:00:07 +00003624 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003625 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003626 case ISD::UINT_TO_FP: {
3627 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003628 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3629 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003630 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003631 Node->getOperand(0).getValueType())) {
3632 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003633 case TargetLowering::Custom:
3634 isCustom = true;
3635 // FALLTHROUGH
3636 case TargetLowering::Legal:
3637 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003638 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003639 if (isCustom) {
3640 Tmp1 = TLI.LowerOperation(Result, DAG);
3641 if (Tmp1.Val) Result = Tmp1;
3642 }
3643 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003644 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003645 Result = ExpandLegalINT_TO_FP(isSigned,
3646 LegalizeOp(Node->getOperand(0)),
3647 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003648 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003649 case TargetLowering::Promote:
3650 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3651 Node->getValueType(0),
3652 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003653 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003654 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003655 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003656 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003657 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3658 Node->getValueType(0), Node->getOperand(0));
3659 break;
3660 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003661 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003662 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003663 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3664 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003665 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003666 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3667 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003668 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003669 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3670 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003671 break;
3672 }
3673 break;
3674 }
3675 case ISD::TRUNCATE:
3676 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3677 case Legal:
3678 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003679 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003680 break;
3681 case Expand:
3682 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3683
3684 // Since the result is legal, we should just be able to truncate the low
3685 // part of the source.
3686 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3687 break;
3688 case Promote:
3689 Result = PromoteOp(Node->getOperand(0));
3690 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3691 break;
3692 }
3693 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003694
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003695 case ISD::FP_TO_SINT:
3696 case ISD::FP_TO_UINT:
3697 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3698 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003699 Tmp1 = LegalizeOp(Node->getOperand(0));
3700
Chris Lattner44fe26f2005-07-29 00:11:56 +00003701 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3702 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003703 case TargetLowering::Custom:
3704 isCustom = true;
3705 // FALLTHROUGH
3706 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003707 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003708 if (isCustom) {
3709 Tmp1 = TLI.LowerOperation(Result, DAG);
3710 if (Tmp1.Val) Result = Tmp1;
3711 }
3712 break;
3713 case TargetLowering::Promote:
3714 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3715 Node->getOpcode() == ISD::FP_TO_SINT);
3716 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003717 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003718 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003719 SDValue True, False;
Duncan Sands13237ac2008-06-06 12:08:01 +00003720 MVT VT = Node->getOperand(0).getValueType();
3721 MVT NVT = Node->getValueType(0);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003722 const uint64_t zero[] = {0, 0};
Duncan Sands13237ac2008-06-06 12:08:01 +00003723 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3724 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohman837a6dc2008-02-29 01:44:25 +00003725 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003726 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003727 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begeman36853ee2005-08-14 01:20:53 +00003728 Node->getOperand(0), Tmp2, ISD::SETLT);
3729 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3730 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003731 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003732 Tmp2));
3733 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman837a6dc2008-02-29 01:44:25 +00003734 DAG.getConstant(x, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003735 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3736 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003737 } else {
3738 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3739 }
3740 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003741 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003742 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003743 case Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003744 MVT VT = Op.getValueType();
3745 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003746 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003747 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner72733e52008-01-17 07:00:52 +00003748 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3749 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3750 Node->getOperand(0), DAG.getValueType(MVT::f64));
3751 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3752 DAG.getIntPtrConstant(1));
3753 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3754 } else {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003755 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3756 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3757 Tmp2 = DAG.getConstantFP(apf, OVT);
3758 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3759 // FIXME: generated code sucks.
3760 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3761 DAG.getNode(ISD::ADD, MVT::i32,
3762 DAG.getNode(ISD::FP_TO_SINT, VT,
3763 DAG.getNode(ISD::FSUB, OVT,
3764 Node->getOperand(0), Tmp2)),
3765 DAG.getConstant(0x80000000, MVT::i32)),
3766 DAG.getNode(ISD::FP_TO_SINT, VT,
3767 Node->getOperand(0)),
3768 DAG.getCondCode(ISD::SETGE));
3769 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003770 break;
3771 }
Dan Gohmanf4300952008-03-10 23:03:31 +00003772 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sands77a3d052008-07-17 02:36:29 +00003773 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3774 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3775 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003776 SDValue Dummy;
Duncan Sands844d55a2008-04-12 17:14:18 +00003777 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003778 break;
3779 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003780 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003781 Tmp1 = PromoteOp(Node->getOperand(0));
3782 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3783 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003784 break;
3785 }
3786 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003787
Chris Lattner91d86242008-01-16 06:57:07 +00003788 case ISD::FP_EXTEND: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003789 MVT DstVT = Op.getValueType();
3790 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner72733e52008-01-17 07:00:52 +00003791 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3792 // The only other way we can lower this is to turn it into a STORE,
3793 // LOAD pair, targetting a temporary location (a stack slot).
3794 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3795 break;
Chris Lattner91d86242008-01-16 06:57:07 +00003796 }
3797 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3798 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3799 case Legal:
3800 Tmp1 = LegalizeOp(Node->getOperand(0));
3801 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3802 break;
3803 case Promote:
3804 Tmp1 = PromoteOp(Node->getOperand(0));
3805 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3806 break;
3807 }
3808 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003809 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003810 case ISD::FP_ROUND: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003811 MVT DstVT = Op.getValueType();
3812 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner72733e52008-01-17 07:00:52 +00003813 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3814 if (SrcVT == MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003815 SDValue Lo;
Dale Johannesen949e5a22008-01-20 01:18:38 +00003816 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner72733e52008-01-17 07:00:52 +00003817 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen949e5a22008-01-20 01:18:38 +00003818 if (DstVT!=MVT::f64)
3819 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner72733e52008-01-17 07:00:52 +00003820 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003821 }
Chris Lattner72733e52008-01-17 07:00:52 +00003822 // The only other way we can lower this is to turn it into a STORE,
3823 // LOAD pair, targetting a temporary location (a stack slot).
3824 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3825 break;
Dale Johannesena2b3c172007-07-03 00:53:03 +00003826 }
Chris Lattner91d86242008-01-16 06:57:07 +00003827 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3828 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3829 case Legal:
3830 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003832 break;
3833 case Promote:
3834 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003835 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3836 Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003837 break;
3838 }
3839 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003840 }
Chris Lattner7753f172005-09-02 00:18:10 +00003841 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003842 case ISD::ZERO_EXTEND:
3843 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003844 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003845 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003846 case Legal:
3847 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michelbe940422008-04-30 00:26:38 +00003848 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michela3cefea2008-02-15 23:05:48 +00003849 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3850 TargetLowering::Custom) {
Scott Michelbe940422008-04-30 00:26:38 +00003851 Tmp1 = TLI.LowerOperation(Result, DAG);
3852 if (Tmp1.Val) Result = Tmp1;
Scott Michela3cefea2008-02-15 23:05:48 +00003853 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003854 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003855 case Promote:
3856 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003857 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003858 Tmp1 = PromoteOp(Node->getOperand(0));
3859 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003860 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003861 case ISD::ZERO_EXTEND:
3862 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003863 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003864 Result = DAG.getZeroExtendInReg(Result,
3865 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003866 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003867 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003868 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003869 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003870 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003871 Result,
3872 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003873 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003874 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003875 }
3876 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003877 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003878 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003879 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands13237ac2008-06-06 12:08:01 +00003880 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003881
3882 // If this operation is not supported, convert it to a shl/shr or load/store
3883 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003884 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3885 default: assert(0 && "This action not supported for this op yet!");
3886 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003887 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003888 break;
3889 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003890 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003891 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003892 // NOTE: we could fall back on load/store here too for targets without
3893 // SAR. However, it is doubtful that any exist.
Duncan Sands13237ac2008-06-06 12:08:01 +00003894 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3895 ExtraVT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003896 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003897 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3898 Node->getOperand(0), ShiftCst);
3899 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3900 Result, ShiftCst);
3901 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003902 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003903 // EXTLOAD pair, targetting a temporary location (a stack slot).
3904
3905 // NOTE: there is a choice here between constantly creating new stack
3906 // slots and always reusing the same one. We currently always create
3907 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003908 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3909 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00003910 } else {
3911 assert(0 && "Unknown op");
3912 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003913 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003914 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003915 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003916 }
Duncan Sands644f9172007-07-27 12:58:54 +00003917 case ISD::TRAMPOLINE: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003918 SDValue Ops[6];
Duncan Sands644f9172007-07-27 12:58:54 +00003919 for (unsigned i = 0; i != 6; ++i)
3920 Ops[i] = LegalizeOp(Node->getOperand(i));
3921 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3922 // The only option for this node is to custom lower it.
3923 Result = TLI.LowerOperation(Result, DAG);
3924 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003925
3926 // Since trampoline produces two values, make sure to remember that we
3927 // legalized both of them.
3928 Tmp1 = LegalizeOp(Result.getValue(1));
3929 Result = LegalizeOp(Result);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003930 AddLegalizedOperand(SDValue(Node, 0), Result);
3931 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Duncan Sands86e01192007-09-11 14:10:23 +00003932 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003933 }
Dan Gohmanfd3e3002008-05-14 00:43:10 +00003934 case ISD::FLT_ROUNDS_: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003935 MVT VT = Node->getValueType(0);
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00003936 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3937 default: assert(0 && "This action not supported for this op yet!");
3938 case TargetLowering::Custom:
3939 Result = TLI.LowerOperation(Op, DAG);
3940 if (Result.Val) break;
3941 // Fall Thru
3942 case TargetLowering::Legal:
3943 // If this operation is not supported, lower it to constant 1
3944 Result = DAG.getConstant(1, VT);
3945 break;
3946 }
Dan Gohmanecb77382008-05-12 16:07:15 +00003947 break;
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00003948 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00003949 case ISD::TRAP: {
Duncan Sands13237ac2008-06-06 12:08:01 +00003950 MVT VT = Node->getValueType(0);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003951 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3952 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00003953 case TargetLowering::Legal:
3954 Tmp1 = LegalizeOp(Node->getOperand(0));
3955 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3956 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003957 case TargetLowering::Custom:
3958 Result = TLI.LowerOperation(Op, DAG);
3959 if (Result.Val) break;
3960 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00003961 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003962 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00003963 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003964 TargetLowering::ArgListTy Args;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003965 std::pair<SDValue,SDValue> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00003966 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3967 false, false, false, CallingConv::C, false,
Chris Lattnerec224882008-01-15 22:09:33 +00003968 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3969 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003970 Result = CallResult.second;
3971 break;
3972 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00003973 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003974 }
Chris Lattner99222f72005-01-15 07:15:18 +00003975 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003976
Chris Lattner101ea662006-04-08 04:13:17 +00003977 assert(Result.getValueType() == Op.getValueType() &&
3978 "Bad legalization!");
3979
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003980 // Make sure that the generated code is itself legal.
3981 if (Result != Op)
3982 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003983
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003984 // Note that LegalizeOp may be reentered even from single-use nodes, which
3985 // means that we always must cache transformed nodes.
3986 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003987 return Result;
3988}
3989
Chris Lattner4d978642005-01-15 22:16:26 +00003990/// PromoteOp - Given an operation that produces a value in an invalid type,
3991/// promote it to compute the value into a larger type. The produced value will
3992/// have the correct bits for the low portion of the register, but no guarantee
3993/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003994SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003995 MVT VT = Op.getValueType();
3996 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003997 assert(getTypeAction(VT) == Promote &&
3998 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands11dd4242008-06-08 20:54:56 +00003999 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004000 "Cannot promote to smaller type!");
4001
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004002 SDValue Tmp1, Tmp2, Tmp3;
4003 SDValue Result;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004004 SDNode *Node = Op.Val;
4005
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004006 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00004007 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004008
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004009 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00004010 case ISD::CopyFromReg:
4011 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004012 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004013#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004014 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004015#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004016 assert(0 && "Do not know how to promote this operator!");
4017 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004018 case ISD::UNDEF:
4019 Result = DAG.getNode(ISD::UNDEF, NVT);
4020 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004021 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00004022 if (VT != MVT::i1)
4023 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4024 else
4025 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004026 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4027 break;
4028 case ISD::ConstantFP:
4029 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4030 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4031 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00004032
Chris Lattner2cb338d2005-01-18 02:59:52 +00004033 case ISD::SETCC:
Scott Michela6729e82008-03-10 15:42:14 +00004034 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman63eb03f2008-03-14 00:53:31 +00004035 && "SetCC type is not legal??");
Scott Michela6729e82008-03-10 15:42:14 +00004036 Result = DAG.getNode(ISD::SETCC,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004037 TLI.getSetCCResultType(Node->getOperand(0)),
4038 Node->getOperand(0), Node->getOperand(1),
4039 Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00004040 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00004041
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004042 case ISD::TRUNCATE:
4043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4044 case Legal:
4045 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands11dd4242008-06-08 20:54:56 +00004046 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004047 "This truncation doesn't make sense!");
Duncan Sands11dd4242008-06-08 20:54:56 +00004048 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004049 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4050 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00004051 case Promote:
4052 // The truncation is not required, because we don't guarantee anything
4053 // about high bits anyway.
4054 Result = PromoteOp(Node->getOperand(0));
4055 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004056 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00004057 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4058 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00004059 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004060 }
4061 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004062 case ISD::SIGN_EXTEND:
4063 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00004064 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00004065 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4066 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4067 case Legal:
4068 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004069 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004070 break;
4071 case Promote:
4072 // Promote the reg if it's smaller.
4073 Result = PromoteOp(Node->getOperand(0));
4074 // The high bits are not guaranteed to be anything. Insert an extend.
4075 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00004076 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004077 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00004078 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00004079 Result = DAG.getZeroExtendInReg(Result,
4080 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00004081 break;
4082 }
4083 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004084 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00004085 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4086 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00004087 Result = PromoteOp(Result);
4088 break;
4089
Chris Lattner4d978642005-01-15 22:16:26 +00004090 case ISD::FP_EXTEND:
4091 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4092 case ISD::FP_ROUND:
4093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4094 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4095 case Promote: assert(0 && "Unreachable with 2 FP types!");
4096 case Legal:
Chris Lattner72733e52008-01-17 07:00:52 +00004097 if (Node->getConstantOperandVal(1) == 0) {
4098 // Input is legal? Do an FP_ROUND_INREG.
4099 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4100 DAG.getValueType(VT));
4101 } else {
4102 // Just remove the truncate, it isn't affecting the value.
4103 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4104 Node->getOperand(1));
4105 }
Chris Lattner4d978642005-01-15 22:16:26 +00004106 break;
4107 }
4108 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004109 case ISD::SINT_TO_FP:
4110 case ISD::UINT_TO_FP:
4111 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4112 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00004113 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004114 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004115 break;
4116
4117 case Promote:
4118 Result = PromoteOp(Node->getOperand(0));
4119 if (Node->getOpcode() == ISD::SINT_TO_FP)
4120 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004121 Result,
4122 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00004123 else
Chris Lattner0e852af2005-04-13 02:38:47 +00004124 Result = DAG.getZeroExtendInReg(Result,
4125 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00004126 // No extra round required here.
4127 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00004128 break;
4129 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00004130 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4131 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00004132 // Round if we cannot tolerate excess precision.
4133 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004134 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4135 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00004136 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004137 }
Chris Lattner4d978642005-01-15 22:16:26 +00004138 break;
4139
Chris Lattner268d4572005-12-09 17:32:47 +00004140 case ISD::SIGN_EXTEND_INREG:
4141 Result = PromoteOp(Node->getOperand(0));
4142 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4143 Node->getOperand(1));
4144 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004145 case ISD::FP_TO_SINT:
4146 case ISD::FP_TO_UINT:
4147 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4148 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00004149 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004150 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00004151 break;
4152 case Promote:
4153 // The input result is prerounded, so we don't have to do anything
4154 // special.
4155 Tmp1 = PromoteOp(Node->getOperand(0));
4156 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004157 }
Nate Begeman36853ee2005-08-14 01:20:53 +00004158 // If we're promoting a UINT to a larger size, check to see if the new node
4159 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4160 // we can use that instead. This allows us to generate better code for
4161 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4162 // legal, such as PowerPC.
4163 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004164 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00004165 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4166 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00004167 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4168 } else {
4169 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4170 }
Chris Lattner4d978642005-01-15 22:16:26 +00004171 break;
4172
Chris Lattner13fe99c2005-04-02 05:00:07 +00004173 case ISD::FABS:
4174 case ISD::FNEG:
4175 Tmp1 = PromoteOp(Node->getOperand(0));
4176 assert(Tmp1.getValueType() == NVT);
4177 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4178 // NOTE: we do not have to do any extra rounding here for
4179 // NoExcessFPPrecision, because we know the input will have the appropriate
4180 // precision, and these operations don't modify precision at all.
4181 break;
4182
Chris Lattner9d6fa982005-04-28 21:44:33 +00004183 case ISD::FSQRT:
4184 case ISD::FSIN:
4185 case ISD::FCOS:
4186 Tmp1 = PromoteOp(Node->getOperand(0));
4187 assert(Tmp1.getValueType() == NVT);
4188 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004189 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004190 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4191 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00004192 break;
4193
Chris Lattnerca401aa2007-03-03 23:43:21 +00004194 case ISD::FPOWI: {
4195 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4196 // directly as well, which may be better.
4197 Tmp1 = PromoteOp(Node->getOperand(0));
4198 assert(Tmp1.getValueType() == NVT);
4199 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4200 if (NoExcessFPPrecision)
4201 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4202 DAG.getValueType(VT));
4203 break;
4204 }
4205
Mon P Wang6a490372008-06-25 08:15:39 +00004206 case ISD::ATOMIC_CMP_SWAP: {
4207 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharth95528942008-02-21 06:45:13 +00004208 Tmp2 = PromoteOp(Node->getOperand(2));
4209 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang6a490372008-06-25 08:15:39 +00004210 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4211 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanaa01afd2008-06-25 16:07:49 +00004212 AtomNode->getSrcValue(),
Mon P Wang6a490372008-06-25 08:15:39 +00004213 AtomNode->getAlignment());
Andrew Lenharth95528942008-02-21 06:45:13 +00004214 // Remember that we legalized the chain.
4215 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4216 break;
4217 }
Mon P Wang6a490372008-06-25 08:15:39 +00004218 case ISD::ATOMIC_LOAD_ADD:
4219 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang3e583932008-05-05 19:05:59 +00004220 case ISD::ATOMIC_LOAD_AND:
4221 case ISD::ATOMIC_LOAD_OR:
4222 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharthf88d50b2008-06-14 05:48:15 +00004223 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang3e583932008-05-05 19:05:59 +00004224 case ISD::ATOMIC_LOAD_MIN:
4225 case ISD::ATOMIC_LOAD_MAX:
4226 case ISD::ATOMIC_LOAD_UMIN:
4227 case ISD::ATOMIC_LOAD_UMAX:
Andrew Lenharth95528942008-02-21 06:45:13 +00004228 case ISD::ATOMIC_SWAP: {
Mon P Wang6a490372008-06-25 08:15:39 +00004229 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharth95528942008-02-21 06:45:13 +00004230 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang6a490372008-06-25 08:15:39 +00004231 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4232 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanaa01afd2008-06-25 16:07:49 +00004233 AtomNode->getSrcValue(),
Mon P Wang6a490372008-06-25 08:15:39 +00004234 AtomNode->getAlignment());
Andrew Lenharth95528942008-02-21 06:45:13 +00004235 // Remember that we legalized the chain.
4236 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4237 break;
4238 }
4239
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004240 case ISD::AND:
4241 case ISD::OR:
4242 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004243 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00004244 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004245 case ISD::MUL:
4246 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00004247 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004248 // that too is okay if they are integer operations.
4249 Tmp1 = PromoteOp(Node->getOperand(0));
4250 Tmp2 = PromoteOp(Node->getOperand(1));
4251 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4252 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00004253 break;
4254 case ISD::FADD:
4255 case ISD::FSUB:
4256 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004257 Tmp1 = PromoteOp(Node->getOperand(0));
4258 Tmp2 = PromoteOp(Node->getOperand(1));
4259 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4260 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4261
4262 // Floating point operations will give excess precision that we may not be
4263 // able to tolerate. If we DO allow excess precision, just leave it,
4264 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004265 // FIXME: Why would we need to round FP ops more than integer ones?
4266 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004267 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004268 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4269 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004270 break;
4271
Chris Lattner4d978642005-01-15 22:16:26 +00004272 case ISD::SDIV:
4273 case ISD::SREM:
4274 // These operators require that their input be sign extended.
4275 Tmp1 = PromoteOp(Node->getOperand(0));
4276 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00004277 if (NVT.isInteger()) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004278 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4279 DAG.getValueType(VT));
4280 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4281 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004282 }
4283 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4284
4285 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands13237ac2008-06-06 12:08:01 +00004286 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004287 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4288 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004289 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004290 case ISD::FDIV:
4291 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004292 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004293 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004294 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004295 case Expand: assert(0 && "not implemented");
4296 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4297 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004298 }
4299 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004300 case Expand: assert(0 && "not implemented");
4301 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4302 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004303 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004304 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4305
4306 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004307 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004308 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4309 DAG.getValueType(VT));
4310 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004311
4312 case ISD::UDIV:
4313 case ISD::UREM:
4314 // These operators require that their input be zero extended.
4315 Tmp1 = PromoteOp(Node->getOperand(0));
4316 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands13237ac2008-06-06 12:08:01 +00004317 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004318 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4319 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004320 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4321 break;
4322
4323 case ISD::SHL:
4324 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004325 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004326 break;
4327 case ISD::SRA:
4328 // The input value must be properly sign extended.
4329 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004330 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4331 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004332 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004333 break;
4334 case ISD::SRL:
4335 // The input value must be properly zero extended.
4336 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004337 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004338 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004339 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004340
4341 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004342 Tmp1 = Node->getOperand(0); // Get the chain.
4343 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004344 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4345 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands93e180342008-07-04 11:47:58 +00004346 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman595ec732006-01-28 03:14:31 +00004347 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +00004348 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004349 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004350 // Increment the pointer, VAList, to the next vaarg
4351 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands13237ac2008-06-06 12:08:01 +00004352 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman595ec732006-01-28 03:14:31 +00004353 TLI.getPointerTy()));
4354 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00004355 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004356 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004357 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004358 }
4359 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004360 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004361 break;
4362
Evan Chenge71fe34d2006-10-09 20:57:25 +00004363 case ISD::LOAD: {
4364 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004365 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4366 ? ISD::EXTLOAD : LD->getExtensionType();
4367 Result = DAG.getExtLoad(ExtType, NVT,
4368 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004369 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00004370 LD->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00004371 LD->isVolatile(),
4372 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004373 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004374 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004375 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004376 }
Scott Micheld831cc42008-06-02 22:18:03 +00004377 case ISD::SELECT: {
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004378 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4379 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Micheld831cc42008-06-02 22:18:03 +00004380
Duncan Sands13237ac2008-06-06 12:08:01 +00004381 MVT VT2 = Tmp2.getValueType();
Scott Micheld831cc42008-06-02 22:18:03 +00004382 assert(VT2 == Tmp3.getValueType()
Scott Michela7d86492008-06-03 19:13:20 +00004383 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4384 // Ensure that the resulting node is at least the same size as the operands'
4385 // value types, because we cannot assume that TLI.getSetCCValueType() is
4386 // constant.
4387 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004388 break;
Scott Micheld831cc42008-06-02 22:18:03 +00004389 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004390 case ISD::SELECT_CC:
4391 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4392 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4393 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004394 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004395 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004396 case ISD::BSWAP:
4397 Tmp1 = Node->getOperand(0);
4398 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4399 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4400 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004401 DAG.getConstant(NVT.getSizeInBits() -
4402 VT.getSizeInBits(),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004403 TLI.getShiftAmountTy()));
4404 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004405 case ISD::CTPOP:
4406 case ISD::CTTZ:
4407 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004408 // Zero extend the argument
4409 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004410 // Perform the larger operation, then subtract if needed.
4411 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004412 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004413 case ISD::CTPOP:
4414 Result = Tmp1;
4415 break;
4416 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004417 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00004418 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004419 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohman1796f1f2007-05-18 17:52:13 +00004420 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004421 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands13237ac2008-06-06 12:08:01 +00004422 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004423 break;
4424 case ISD::CTLZ:
4425 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004426 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands13237ac2008-06-06 12:08:01 +00004427 DAG.getConstant(NVT.getSizeInBits() -
4428 VT.getSizeInBits(), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004429 break;
4430 }
4431 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004432 case ISD::EXTRACT_SUBVECTOR:
4433 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004434 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004435 case ISD::EXTRACT_VECTOR_ELT:
4436 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4437 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004438 }
4439
4440 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004441
4442 // Make sure the result is itself legal.
4443 Result = LegalizeOp(Result);
4444
4445 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004446 AddPromotedOperand(Op, Result);
4447 return Result;
4448}
Chris Lattnerdc750592005-01-07 07:47:09 +00004449
Dan Gohmana8665142007-06-25 16:23:39 +00004450/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4451/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4452/// based on the vector type. The return type of this matches the element type
4453/// of the vector, which may not be legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004454SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004455 // We know that operand #0 is the Vec vector. If the index is a constant
4456 // or if the invec is a supported hardware type, we can use it. Otherwise,
4457 // lower to a store then an indexed load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004458 SDValue Vec = Op.getOperand(0);
4459 SDValue Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004460
Duncan Sands13237ac2008-06-06 12:08:01 +00004461 MVT TVT = Vec.getValueType();
4462 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner6f423252006-03-31 17:55:51 +00004463
Dan Gohmana8665142007-06-25 16:23:39 +00004464 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4465 default: assert(0 && "This action is not supported yet!");
4466 case TargetLowering::Custom: {
4467 Vec = LegalizeOp(Vec);
4468 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004469 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Dan Gohmana8665142007-06-25 16:23:39 +00004470 if (Tmp3.Val)
4471 return Tmp3;
4472 break;
4473 }
4474 case TargetLowering::Legal:
4475 if (isTypeLegal(TVT)) {
4476 Vec = LegalizeOp(Vec);
4477 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004478 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004479 }
4480 break;
4481 case TargetLowering::Expand:
4482 break;
4483 }
4484
4485 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004486 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004487 Op = ScalarizeVectorOp(Vec);
4488 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begemanef337672008-01-29 02:24:00 +00004489 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmana8665142007-06-25 16:23:39 +00004490 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004491 SDValue Lo, Hi;
Chris Lattner6f423252006-03-31 17:55:51 +00004492 SplitVectorOp(Vec, Lo, Hi);
Nate Begemanef337672008-01-29 02:24:00 +00004493 if (CIdx->getValue() < NumLoElts) {
Chris Lattner6f423252006-03-31 17:55:51 +00004494 Vec = Lo;
4495 } else {
4496 Vec = Hi;
Nate Begemanef337672008-01-29 02:24:00 +00004497 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmana8665142007-06-25 16:23:39 +00004498 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004499 }
Dan Gohmana8665142007-06-25 16:23:39 +00004500
Chris Lattner6f423252006-03-31 17:55:51 +00004501 // It's now an extract from the appropriate high or low part. Recurse.
4502 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004503 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004504 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004505 // Store the value to a temporary stack slot, then LOAD the scalar
4506 // element back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004507 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4508 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohmana8665142007-06-25 16:23:39 +00004509
4510 // Add the offset to the index.
Duncan Sands13237ac2008-06-06 12:08:01 +00004511 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohmana8665142007-06-25 16:23:39 +00004512 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4513 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004514
Duncan Sands11dd4242008-06-08 20:54:56 +00004515 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004516 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004517 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004518 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004519
Dan Gohmana8665142007-06-25 16:23:39 +00004520 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4521
4522 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004523 }
Dan Gohmana8665142007-06-25 16:23:39 +00004524 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004525}
4526
Dan Gohmana8665142007-06-25 16:23:39 +00004527/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004528/// we assume the operation can be split if it is not already legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004529SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004530 // We know that operand #0 is the Vec vector. For now we assume the index
4531 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004532 SDValue Vec = Op.getOperand(0);
4533 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman26455c42007-06-13 15:12:02 +00004534
Duncan Sands13237ac2008-06-06 12:08:01 +00004535 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman26455c42007-06-13 15:12:02 +00004536
Duncan Sands13237ac2008-06-06 12:08:01 +00004537 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman26455c42007-06-13 15:12:02 +00004538 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004539 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004540 }
4541
4542 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004543 SDValue Lo, Hi;
Dan Gohman26455c42007-06-13 15:12:02 +00004544 SplitVectorOp(Vec, Lo, Hi);
4545 if (CIdx->getValue() < NumElems/2) {
4546 Vec = Lo;
4547 } else {
4548 Vec = Hi;
4549 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4550 }
4551
4552 // It's now an extract from the appropriate high or low part. Recurse.
4553 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004554 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004555}
4556
Nate Begeman7e7f4392006-02-01 07:19:44 +00004557/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4558/// with condition CC on the current target. This usually involves legalizing
4559/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4560/// there may be no choice but to create a new SetCC node to represent the
4561/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004562/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4563void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4564 SDValue &RHS,
4565 SDValue &CC) {
4566 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004567
4568 switch (getTypeAction(LHS.getValueType())) {
4569 case Legal:
4570 Tmp1 = LegalizeOp(LHS); // LHS
4571 Tmp2 = LegalizeOp(RHS); // RHS
4572 break;
4573 case Promote:
4574 Tmp1 = PromoteOp(LHS); // LHS
4575 Tmp2 = PromoteOp(RHS); // RHS
4576
4577 // If this is an FP compare, the operands have already been extended.
Duncan Sands13237ac2008-06-06 12:08:01 +00004578 if (LHS.getValueType().isInteger()) {
4579 MVT VT = LHS.getValueType();
4580 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman7e7f4392006-02-01 07:19:44 +00004581
4582 // Otherwise, we have to insert explicit sign or zero extends. Note
4583 // that we could insert sign extends for ALL conditions, but zero extend
4584 // is cheaper on many machines (an AND instead of two shifts), so prefer
4585 // it.
4586 switch (cast<CondCodeSDNode>(CC)->get()) {
4587 default: assert(0 && "Unknown integer comparison!");
4588 case ISD::SETEQ:
4589 case ISD::SETNE:
4590 case ISD::SETUGE:
4591 case ISD::SETUGT:
4592 case ISD::SETULE:
4593 case ISD::SETULT:
4594 // ALL of these operations will work if we either sign or zero extend
4595 // the operands (including the unsigned comparisons!). Zero extend is
4596 // usually a simpler/cheaper operation, so prefer it.
4597 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4598 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4599 break;
4600 case ISD::SETGE:
4601 case ISD::SETGT:
4602 case ISD::SETLT:
4603 case ISD::SETLE:
4604 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4605 DAG.getValueType(VT));
4606 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4607 DAG.getValueType(VT));
4608 break;
4609 }
4610 }
4611 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004612 case Expand: {
Duncan Sands13237ac2008-06-06 12:08:01 +00004613 MVT VT = LHS.getValueType();
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004614 if (VT == MVT::f32 || VT == MVT::f64) {
4615 // Expand into one or more soft-fp libcall(s).
Evan Cheng4c609ab2008-07-01 21:35:46 +00004616 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004617 switch (cast<CondCodeSDNode>(CC)->get()) {
4618 case ISD::SETEQ:
4619 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004620 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004621 break;
4622 case ISD::SETNE:
4623 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004624 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004625 break;
4626 case ISD::SETGE:
4627 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004628 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004629 break;
4630 case ISD::SETLT:
4631 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004632 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004633 break;
4634 case ISD::SETLE:
4635 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004636 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004637 break;
4638 case ISD::SETGT:
4639 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004640 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004641 break;
4642 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004643 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4644 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004645 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004646 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004647 break;
4648 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004649 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004650 switch (cast<CondCodeSDNode>(CC)->get()) {
4651 case ISD::SETONE:
4652 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004653 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004654 // Fallthrough
4655 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004656 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004657 break;
4658 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004659 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004660 break;
4661 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004662 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004663 break;
4664 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004665 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004666 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004667 case ISD::SETUEQ:
4668 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004669 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004670 default: assert(0 && "Unsupported FP setcc!");
4671 }
4672 }
Duncan Sands1ae6ef82008-06-30 10:19:09 +00004673
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004674 SDValue Dummy;
4675 SDValue Ops[2] = { LHS, RHS };
Duncan Sands739a0542008-07-02 17:40:58 +00004676 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004677 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004678 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004679 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004680 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michela6729e82008-03-10 15:42:14 +00004681 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004682 CC);
Duncan Sands739a0542008-07-02 17:40:58 +00004683 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004684 false /*sign irrelevant*/, Dummy);
Scott Michela6729e82008-03-10 15:42:14 +00004685 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004686 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004687 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004688 Tmp2 = SDValue();
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004689 }
Evan Chengd8b83e12008-07-07 07:18:09 +00004690 LHS = LegalizeOp(Tmp1);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004691 RHS = Tmp2;
4692 return;
4693 }
4694
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004695 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004696 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004697 ExpandOp(RHS, RHSLo, RHSHi);
4698 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4699
4700 if (VT==MVT::ppcf128) {
4701 // FIXME: This generated code sucks. We want to generate
4702 // FCMP crN, hi1, hi2
4703 // BNE crN, L:
4704 // FCMP crN, lo1, lo2
4705 // The following can be improved, but not that much.
Scott Michela6729e82008-03-10 15:42:14 +00004706 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4707 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004708 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00004709 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4710 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004711 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4712 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004713 Tmp2 = SDValue();
Dale Johannesenf864ac92007-10-06 01:24:11 +00004714 break;
4715 }
4716
4717 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004718 case ISD::SETEQ:
4719 case ISD::SETNE:
4720 if (RHSLo == RHSHi)
4721 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4722 if (RHSCST->isAllOnesValue()) {
4723 // Comparison to -1.
4724 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4725 Tmp2 = RHSLo;
4726 break;
4727 }
4728
4729 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4730 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4731 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4732 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4733 break;
4734 default:
4735 // If this is a comparison of the sign bit, just look at the top part.
4736 // X > -1, x < 0
4737 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4738 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00004739 CST->isNullValue()) || // X < 0
Nate Begeman7e7f4392006-02-01 07:19:44 +00004740 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4741 CST->isAllOnesValue())) { // X > -1
4742 Tmp1 = LHSHi;
4743 Tmp2 = RHSHi;
4744 break;
4745 }
4746
4747 // FIXME: This generated code sucks.
4748 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004749 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004750 default: assert(0 && "Unknown integer setcc!");
4751 case ISD::SETLT:
4752 case ISD::SETULT: LowCC = ISD::SETULT; break;
4753 case ISD::SETGT:
4754 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4755 case ISD::SETLE:
4756 case ISD::SETULE: LowCC = ISD::SETULE; break;
4757 case ISD::SETGE:
4758 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4759 }
4760
4761 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4762 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4763 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4764
4765 // NOTE: on targets without efficient SELECT of bools, we can always use
4766 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004767 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michela6729e82008-03-10 15:42:14 +00004768 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004769 LowCC, false, DagCombineInfo);
Evan Cheng93049452007-02-08 22:16:19 +00004770 if (!Tmp1.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004771 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4772 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00004773 CCCode, false, DagCombineInfo);
4774 if (!Tmp2.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004775 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004776 RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004777
4778 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4779 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohmanb72127a2008-03-13 22:13:53 +00004780 if ((Tmp1C && Tmp1C->isNullValue()) ||
4781 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng93049452007-02-08 22:16:19 +00004782 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4783 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohmanb72127a2008-03-13 22:13:53 +00004784 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng93049452007-02-08 22:16:19 +00004785 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4786 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4787 // low part is known false, returns high part.
4788 // For LE / GE, if high part is known false, ignore the low part.
4789 // For LT / GT, if high part is known true, ignore the low part.
4790 Tmp1 = Tmp2;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004791 Tmp2 = SDValue();
Evan Cheng93049452007-02-08 22:16:19 +00004792 } else {
Scott Michela6729e82008-03-10 15:42:14 +00004793 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00004794 ISD::SETEQ, false, DagCombineInfo);
4795 if (!Result.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004796 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman63eb03f2008-03-14 00:53:31 +00004797 ISD::SETEQ);
Evan Cheng93049452007-02-08 22:16:19 +00004798 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4799 Result, Tmp1, Tmp2));
4800 Tmp1 = Result;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004801 Tmp2 = SDValue();
Evan Cheng93049452007-02-08 22:16:19 +00004802 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004803 }
4804 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004805 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004806 LHS = Tmp1;
4807 RHS = Tmp2;
4808}
4809
Chris Lattner87bc3e72008-01-16 07:45:30 +00004810/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4811/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4812/// a load from the stack slot to DestVT, extending it if needed.
4813/// The resultant code need not be legal.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004814SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
4815 MVT SlotVT,
4816 MVT DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00004817 // Create the stack frame object.
Mon P Wang5c755ff2008-07-05 20:40:31 +00004818 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
4819 SrcOp.getValueType().getTypeForMVT());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004820 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang5c755ff2008-07-05 20:40:31 +00004821
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004822 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004823 int SPFI = StackPtrFI->getIndex();
Mon P Wang5c755ff2008-07-05 20:40:31 +00004824
Duncan Sands13237ac2008-06-06 12:08:01 +00004825 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
4826 unsigned SlotSize = SlotVT.getSizeInBits();
4827 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang5c755ff2008-07-05 20:40:31 +00004828 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
4829 DestVT.getTypeForMVT());
Chris Lattner36e663d2005-12-23 00:16:34 +00004830
Chris Lattner87bc3e72008-01-16 07:45:30 +00004831 // Emit a store to the stack slot. Use a truncstore if the input value is
4832 // later than DestVT.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004833 SDValue Store;
Mon P Wang5c755ff2008-07-05 20:40:31 +00004834
Chris Lattner87bc3e72008-01-16 07:45:30 +00004835 if (SrcSize > SlotSize)
Dan Gohman2d489b52008-02-06 22:27:42 +00004836 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00004837 PseudoSourceValue::getFixedStack(SPFI), 0,
4838 SlotVT, false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004839 else {
4840 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman2d489b52008-02-06 22:27:42 +00004841 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00004842 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang5c755ff2008-07-05 20:40:31 +00004843 false, SrcAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004844 }
4845
Chris Lattner36e663d2005-12-23 00:16:34 +00004846 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004847 if (SlotSize == DestSize)
Mon P Wang5c755ff2008-07-05 20:40:31 +00004848 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004849
4850 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang5c755ff2008-07-05 20:40:31 +00004851 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
4852 false, DestAlign);
Chris Lattner36e663d2005-12-23 00:16:34 +00004853}
4854
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004855SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner6be79822006-04-04 17:23:26 +00004856 // Create a vector sized/aligned stack slot, store the value to element #0,
4857 // then load the whole vector back out.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004858 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00004859
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004860 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004861 int SPFI = StackPtrFI->getIndex();
4862
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004863 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00004864 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00004865 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00004866 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004867}
4868
4869
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004870/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004871/// support the operation, but do support the resultant vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004872SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004873
4874 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004875 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004876 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004877 bool isOnlyLowElement = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004878 SDValue SplatValue = Node->getOperand(0);
Chris Lattner322c8262008-03-09 00:29:42 +00004879
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004880 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattner322c8262008-03-09 00:29:42 +00004881 // and use a bitmask instead of a list of elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004882 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004883 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004884 bool isConstant = true;
4885 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4886 SplatValue.getOpcode() != ISD::UNDEF)
4887 isConstant = false;
4888
Evan Cheng1d2e9952006-03-24 01:17:21 +00004889 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004890 SDValue V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004891 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004892 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004893 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004894 if (SplatValue != V)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004895 SplatValue = SDValue(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004896
4897 // If this isn't a constant element or an undef, we can't use a constant
4898 // pool load.
4899 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4900 V.getOpcode() != ISD::UNDEF)
4901 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004902 }
4903
4904 if (isOnlyLowElement) {
4905 // If the low element is an undef too, then this whole things is an undef.
4906 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4907 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4908 // Otherwise, turn this into a scalar_to_vector node.
4909 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4910 Node->getOperand(0));
4911 }
4912
Chris Lattner77e271c2006-03-24 07:29:17 +00004913 // If all elements are constants, create a load from the constant pool.
4914 if (isConstant) {
Duncan Sands13237ac2008-06-06 12:08:01 +00004915 MVT VT = Node->getValueType(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004916 std::vector<Constant*> CV;
4917 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4918 if (ConstantFPSDNode *V =
4919 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Chris Lattner3b187622008-04-20 00:41:09 +00004920 CV.push_back(ConstantFP::get(V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004921 } else if (ConstantSDNode *V =
Chris Lattner3b187622008-04-20 00:41:09 +00004922 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4923 CV.push_back(ConstantInt::get(V->getAPIntValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004924 } else {
4925 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner3b187622008-04-20 00:41:09 +00004926 const Type *OpNTy =
Duncan Sands13237ac2008-06-06 12:08:01 +00004927 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner77e271c2006-03-24 07:29:17 +00004928 CV.push_back(UndefValue::get(OpNTy));
4929 }
4930 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004931 Constant *CP = ConstantVector::get(CV);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004932 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00004933 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004934 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004935 }
4936
Chris Lattner21e68c82006-03-20 01:52:29 +00004937 if (SplatValue.Val) { // Splat of one value?
4938 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands13237ac2008-06-06 12:08:01 +00004939 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004940 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
4941 std::vector<SDValue> ZeroVec(NumElems, Zero);
4942 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004943 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004944
4945 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004946 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004947 // Get the splatted value into the low element of a vector register.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004948 SDValue LowValVec =
Chris Lattner21e68c82006-03-20 01:52:29 +00004949 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4950
4951 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4952 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4953 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4954 SplatMask);
4955 }
4956 }
4957
Evan Cheng1d2e9952006-03-24 01:17:21 +00004958 // If there are only two unique elements, we may be able to turn this into a
4959 // vector shuffle.
4960 if (Values.size() == 2) {
Chris Lattner322c8262008-03-09 00:29:42 +00004961 // Get the two values in deterministic order.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004962 SDValue Val1 = Node->getOperand(1);
4963 SDValue Val2;
4964 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattner322c8262008-03-09 00:29:42 +00004965 if (MI->first != Val1)
4966 Val2 = MI->first;
4967 else
4968 Val2 = (++MI)->first;
4969
4970 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4971 // vector shuffle has the undef vector on the RHS.
4972 if (Val1.getOpcode() == ISD::UNDEF)
4973 std::swap(Val1, Val2);
4974
Evan Cheng1d2e9952006-03-24 01:17:21 +00004975 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands13237ac2008-06-06 12:08:01 +00004976 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4977 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004978 std::vector<SDValue> MaskVec(NumElems);
Chris Lattner322c8262008-03-09 00:29:42 +00004979
4980 // Set elements of the shuffle mask for Val1.
4981 std::vector<unsigned> &Val1Elts = Values[Val1];
4982 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4983 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4984
4985 // Set elements of the shuffle mask for Val2.
4986 std::vector<unsigned> &Val2Elts = Values[Val2];
4987 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4988 if (Val2.getOpcode() != ISD::UNDEF)
4989 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4990 else
4991 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4992
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004993 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004994 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004995
Chris Lattner322c8262008-03-09 00:29:42 +00004996 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004997 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4998 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattner322c8262008-03-09 00:29:42 +00004999 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5000 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005001 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng1d2e9952006-03-24 01:17:21 +00005002
5003 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattner322c8262008-03-09 00:29:42 +00005004 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005005 }
5006 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005007
5008 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5009 // aligned object on the stack, store each element into it, then load
5010 // the result as a vector.
Duncan Sands13237ac2008-06-06 12:08:01 +00005011 MVT VT = Node->getValueType(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005012 // Create the stack frame object.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005013 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005014
5015 // Emit a store of each element to the stack slot.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005016 SmallVector<SDValue, 8> Stores;
Duncan Sands13237ac2008-06-06 12:08:01 +00005017 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005018 // Store (in the right endianness) the elements to memory.
5019 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5020 // Ignore undef elements.
5021 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5022
Chris Lattner8fa445a2006-03-22 01:46:54 +00005023 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005024
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005025 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005026 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5027
Evan Chengdf9ac472006-10-05 23:01:46 +00005028 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00005029 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005030 }
5031
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005032 SDValue StoreChain;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005033 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005034 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5035 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005036 else
5037 StoreChain = DAG.getEntryNode();
5038
5039 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005040 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005041}
5042
Chris Lattner4157c412005-04-02 04:00:59 +00005043void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005044 SDValue Op, SDValue Amt,
5045 SDValue &Lo, SDValue &Hi) {
Chris Lattner4157c412005-04-02 04:00:59 +00005046 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005047 SDValue LHSL, LHSH;
Chris Lattner4157c412005-04-02 04:00:59 +00005048 ExpandOp(Op, LHSL, LHSH);
5049
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005050 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands13237ac2008-06-06 12:08:01 +00005051 MVT VT = LHSL.getValueType();
Chris Lattnerbd887772006-08-14 23:53:35 +00005052 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00005053 Hi = Lo.getValue(1);
5054}
5055
5056
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005057/// ExpandShift - Try to find a clever way to expand this shift operation out to
5058/// smaller elements. If we can't find a way that is more efficient than a
5059/// libcall on this target, return false. Otherwise, return true with the
5060/// low-parts expanded into Lo and Hi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005061bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5062 SDValue &Lo, SDValue &Hi) {
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005063 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5064 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00005065
Duncan Sands13237ac2008-06-06 12:08:01 +00005066 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005067 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands13237ac2008-06-06 12:08:01 +00005068 MVT ShTy = ShAmt.getValueType();
5069 unsigned ShBits = ShTy.getSizeInBits();
5070 unsigned VTBits = Op.getValueType().getSizeInBits();
5071 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanb0674922005-04-06 21:13:14 +00005072
Chris Lattnerfbbe5702007-10-14 20:35:12 +00005073 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00005074 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5075 unsigned Cst = CN->getValue();
5076 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005077 SDValue InL, InH;
Nate Begemanb0674922005-04-06 21:13:14 +00005078 ExpandOp(Op, InL, InH);
5079 switch(Opc) {
5080 case ISD::SHL:
5081 if (Cst > VTBits) {
5082 Lo = DAG.getConstant(0, NVT);
5083 Hi = DAG.getConstant(0, NVT);
5084 } else if (Cst > NVTBits) {
5085 Lo = DAG.getConstant(0, NVT);
5086 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005087 } else if (Cst == NVTBits) {
5088 Lo = DAG.getConstant(0, NVT);
5089 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00005090 } else {
5091 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5092 Hi = DAG.getNode(ISD::OR, NVT,
5093 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5094 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5095 }
5096 return true;
5097 case ISD::SRL:
5098 if (Cst > VTBits) {
5099 Lo = DAG.getConstant(0, NVT);
5100 Hi = DAG.getConstant(0, NVT);
5101 } else if (Cst > NVTBits) {
5102 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5103 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00005104 } else if (Cst == NVTBits) {
5105 Lo = InH;
5106 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00005107 } else {
5108 Lo = DAG.getNode(ISD::OR, NVT,
5109 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5110 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5111 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5112 }
5113 return true;
5114 case ISD::SRA:
5115 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005116 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005117 DAG.getConstant(NVTBits-1, ShTy));
5118 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005119 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005120 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00005121 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005122 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005123 } else if (Cst == NVTBits) {
5124 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00005125 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00005126 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00005127 } else {
5128 Lo = DAG.getNode(ISD::OR, NVT,
5129 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5130 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5131 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5132 }
5133 return true;
5134 }
5135 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00005136
5137 // Okay, the shift amount isn't constant. However, if we can tell that it is
5138 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005139 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5140 APInt KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00005141 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00005142
Dan Gohmanf3057a92008-02-22 01:12:31 +00005143 // If we know that if any of the high bits of the shift amount are one, then
5144 // we can do this as a couple of simple shifts.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005145 if (KnownOne.intersects(Mask)) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005146 // Mask out the high bit, which we know is set.
5147 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman34fc7db2008-02-20 16:57:27 +00005148 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner875ea0c2006-09-20 03:38:48 +00005149
5150 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005151 SDValue InL, InH;
Chris Lattner875ea0c2006-09-20 03:38:48 +00005152 ExpandOp(Op, InL, InH);
5153 switch(Opc) {
5154 case ISD::SHL:
5155 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5156 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5157 return true;
5158 case ISD::SRL:
5159 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5160 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5161 return true;
5162 case ISD::SRA:
5163 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5164 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5165 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5166 return true;
5167 }
5168 }
5169
Dan Gohmanf3057a92008-02-22 01:12:31 +00005170 // If we know that the high bits of the shift amount are all zero, then we can
5171 // do this as a couple of simple shifts.
5172 if ((KnownZero & Mask) == Mask) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005173 // Compute 32-amt.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005174 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner875ea0c2006-09-20 03:38:48 +00005175 DAG.getConstant(NVTBits, Amt.getValueType()),
5176 Amt);
5177
5178 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005179 SDValue InL, InH;
Chris Lattner875ea0c2006-09-20 03:38:48 +00005180 ExpandOp(Op, InL, InH);
5181 switch(Opc) {
5182 case ISD::SHL:
5183 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5184 Hi = DAG.getNode(ISD::OR, NVT,
5185 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5186 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5187 return true;
5188 case ISD::SRL:
5189 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5190 Lo = DAG.getNode(ISD::OR, NVT,
5191 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5192 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5193 return true;
5194 case ISD::SRA:
5195 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5196 Lo = DAG.getNode(ISD::OR, NVT,
5197 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5198 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5199 return true;
5200 }
5201 }
5202
Nate Begemanb0674922005-04-06 21:13:14 +00005203 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005204}
Chris Lattneraac464e2005-01-21 06:05:23 +00005205
Chris Lattner4add7e32005-01-23 04:42:50 +00005206
Chris Lattneraac464e2005-01-21 06:05:23 +00005207// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5208// does not fit into a register, return the lo part and set the hi part to the
5209// by-reg argument. If it does fit into a single register, return the result
5210// and leave the Hi part unset.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005211SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5212 bool isSigned, SDValue &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00005213 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5214 // The input chain to this libcall is the entry node of the function.
5215 // Legalizing the call will automatically add the previous call to the
5216 // dependence.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005217 SDValue InChain = DAG.getEntryNode();
Chris Lattner462505f2006-02-13 09:18:02 +00005218
Chris Lattneraac464e2005-01-21 06:05:23 +00005219 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00005220 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00005221 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005222 MVT ArgVT = Node->getOperand(i).getValueType();
5223 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencere63b6512006-12-31 05:55:36 +00005224 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00005225 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005226 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00005227 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00005228 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005229 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Duncan Sands844d55a2008-04-12 17:14:18 +00005230 TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00005231
Chris Lattner06bbeb62005-05-11 19:02:11 +00005232 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands13237ac2008-06-06 12:08:01 +00005233 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005234 std::pair<SDValue,SDValue> CallInfo =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005235 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5236 false, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00005237
Chris Lattner462505f2006-02-13 09:18:02 +00005238 // Legalize the call sequence, starting with the chain. This will advance
5239 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5240 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5241 LegalizeOp(CallInfo.second);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005242 SDValue Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005243 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005244 default: assert(0 && "Unknown thing");
5245 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005246 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00005247 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005248 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00005249 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00005250 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005251 }
Chris Lattner63022662005-09-02 20:26:58 +00005252 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005253}
5254
Chris Lattner4add7e32005-01-23 04:42:50 +00005255
Evan Chengbf535fc2007-04-27 07:33:31 +00005256/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5257///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005258SDValue SelectionDAGLegalize::
5259ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005260 MVT SourceVT = Source.getValueType();
Dan Gohmand6819da2008-03-11 01:59:03 +00005261 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattneraac464e2005-01-21 06:05:23 +00005262
Evan Cheng0bd72c52008-04-01 02:18:22 +00005263 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5264 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohmanda7897c2008-03-05 02:07:31 +00005265 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005266 // incoming integer is set. To handle this, we dynamically test to see if
5267 // it is set, and, if so, add a fudge factor.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005268 SDValue Hi;
Dan Gohmand6819da2008-03-11 01:59:03 +00005269 if (ExpandSource) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005270 SDValue Lo;
Dan Gohmand6819da2008-03-11 01:59:03 +00005271 ExpandOp(Source, Lo, Hi);
5272 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5273 } else {
5274 // The comparison for the sign bit will use the entire operand.
5275 Hi = Source;
5276 }
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005277
Chris Lattner2a4f7312005-05-13 04:45:13 +00005278 // If this is unsigned, and not supported, first perform the conversion to
5279 // signed, then adjust the result if the sign bit is set.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005280 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner2a4f7312005-05-13 04:45:13 +00005281
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005282 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005283 DAG.getConstant(0, Hi.getValueType()),
5284 ISD::SETLT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005285 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5286 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005287 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00005288 uint64_t FF = 0x5f800000ULL;
5289 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmanda7897c2008-03-05 02:07:31 +00005290 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005291
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005292 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005293 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005294 SDValue FudgeInReg;
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005295 if (DestTy == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005296 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005297 PseudoSourceValue::getConstantPool(), 0);
Duncan Sands11dd4242008-06-08 20:54:56 +00005298 else if (DestTy.bitsGT(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00005299 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00005300 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00005301 CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005302 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005303 MVT::f32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00005304 else
5305 assert(0 && "Unexpected conversion");
5306
Duncan Sands13237ac2008-06-06 12:08:01 +00005307 MVT SCVT = SignedConv.getValueType();
Evan Chengbf535fc2007-04-27 07:33:31 +00005308 if (SCVT != DestTy) {
5309 // Destination type needs to be expanded as well. The FADD now we are
5310 // constructing will be expanded into a libcall.
Duncan Sands13237ac2008-06-06 12:08:01 +00005311 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5312 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand9d874b2008-03-05 01:08:17 +00005313 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Chengbf535fc2007-04-27 07:33:31 +00005314 SignedConv, SignedConv.getValue(1));
5315 }
5316 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5317 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005318 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005319 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005320
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005321 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand9d874b2008-03-05 01:08:17 +00005322 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005323 default: assert(0 && "This action not implemented for this operation!");
5324 case TargetLowering::Legal:
5325 case TargetLowering::Expand:
5326 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005327 case TargetLowering::Custom: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005328 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005329 Source), DAG);
5330 if (NV.Val)
5331 return LegalizeOp(NV);
5332 break; // The target decided this was legal after all
5333 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005334 }
5335
Chris Lattner153587e2005-05-12 07:00:44 +00005336 // Expand the source, then glue it back together for the call. We must expand
5337 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohmand6819da2008-03-11 01:59:03 +00005338 if (ExpandSource) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005339 SDValue SrcLo, SrcHi;
Dan Gohmand6819da2008-03-11 01:59:03 +00005340 ExpandOp(Source, SrcLo, SrcHi);
5341 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5342 }
Chris Lattner153587e2005-05-12 07:00:44 +00005343
Duncan Sands77a3d052008-07-17 02:36:29 +00005344 RTLIB::Libcall LC = isSigned ?
5345 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5346 RTLIB::getUINTTOFP(SourceVT, DestTy);
5347 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5348
Chris Lattner462505f2006-02-13 09:18:02 +00005349 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005350 SDValue HiPart;
5351 SDValue Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart);
Evan Cheng86e476b2008-04-01 01:50:16 +00005352 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmanf4300952008-03-10 23:03:31 +00005353 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5354 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005355}
Misha Brukman835702a2005-04-21 22:36:52 +00005356
Chris Lattner689bdcc2006-01-28 08:25:58 +00005357/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5358/// INT_TO_FP operation of the specified operand when the target requests that
5359/// we expand it. At this point, we know that the result and operand types are
5360/// legal for the target.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005361SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5362 SDValue Op0,
5363 MVT DestVT) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005364 if (Op0.getValueType() == MVT::i32) {
5365 // simple 32-bit [signed|unsigned] integer to float/double expansion
5366
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005367 // Get the stack frame index of a 8 byte buffer.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005368 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005369
Chris Lattner689bdcc2006-01-28 08:25:58 +00005370 // word offset constant for Hi/Lo address computation
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005371 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner689bdcc2006-01-28 08:25:58 +00005372 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005373 SDValue Hi = StackSlot;
5374 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00005375 if (TLI.isLittleEndian())
5376 std::swap(Hi, Lo);
5377
Chris Lattner689bdcc2006-01-28 08:25:58 +00005378 // if signed map to unsigned space
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005379 SDValue Op0Mapped;
Chris Lattner689bdcc2006-01-28 08:25:58 +00005380 if (isSigned) {
5381 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005382 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005383 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5384 } else {
5385 Op0Mapped = Op0;
5386 }
5387 // store the lo of the constructed double - based on integer input
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005388 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005389 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005390 // initial hi portion of constructed double
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005391 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005392 // store the hi of the constructed double - biased exponent
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005393 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005394 // load the constructed double
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005395 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005396 // FP constant to bias correct the final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005397 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner689bdcc2006-01-28 08:25:58 +00005398 BitsToDouble(0x4330000080000000ULL)
5399 : BitsToDouble(0x4330000000000000ULL),
5400 MVT::f64);
5401 // subtract the bias
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005402 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005403 // final result
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005404 SDValue Result;
Chris Lattner689bdcc2006-01-28 08:25:58 +00005405 // handle final rounding
5406 if (DestVT == MVT::f64) {
5407 // do nothing
5408 Result = Sub;
Duncan Sands11dd4242008-06-08 20:54:56 +00005409 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner72733e52008-01-17 07:00:52 +00005410 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5411 DAG.getIntPtrConstant(0));
Duncan Sands11dd4242008-06-08 20:54:56 +00005412 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen7f724e92007-09-16 16:51:49 +00005413 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005414 }
5415 return Result;
5416 }
5417 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005418 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005419
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005420 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005421 DAG.getConstant(0, Op0.getValueType()),
5422 ISD::SETLT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005423 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5424 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005425 SignSet, Four, Zero);
5426
5427 // If the sign bit of the integer is set, the large number will be treated
5428 // as a negative number. To counteract this, the dynamic code adds an
5429 // offset depending on the data type.
5430 uint64_t FF;
Duncan Sands13237ac2008-06-06 12:08:01 +00005431 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005432 default: assert(0 && "Unsupported integer type!");
5433 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5434 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5435 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5436 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5437 }
5438 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005439 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005440
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005441 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattner689bdcc2006-01-28 08:25:58 +00005442 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005443 SDValue FudgeInReg;
Chris Lattner689bdcc2006-01-28 08:25:58 +00005444 if (DestVT == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005445 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005446 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005447 else {
Dan Gohman2d489b52008-02-06 22:27:42 +00005448 FudgeInReg =
5449 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5450 DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005451 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005452 MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005453 }
5454
5455 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5456}
5457
5458/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5459/// *INT_TO_FP operation of the specified operand when the target requests that
5460/// we promote it. At this point, we know that the result and operand types are
5461/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5462/// operation that takes a larger input.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005463SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5464 MVT DestVT,
5465 bool isSigned) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005466 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands13237ac2008-06-06 12:08:01 +00005467 MVT NewInTy = LegalOp.getValueType();
Chris Lattner689bdcc2006-01-28 08:25:58 +00005468
5469 unsigned OpToUse = 0;
5470
5471 // Scan for the appropriate larger type to use.
5472 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005473 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5474 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00005475
5476 // If the target supports SINT_TO_FP of this type, use it.
5477 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5478 default: break;
5479 case TargetLowering::Legal:
5480 if (!TLI.isTypeLegal(NewInTy))
5481 break; // Can't use this datatype.
5482 // FALL THROUGH.
5483 case TargetLowering::Custom:
5484 OpToUse = ISD::SINT_TO_FP;
5485 break;
5486 }
5487 if (OpToUse) break;
5488 if (isSigned) continue;
5489
5490 // If the target supports UINT_TO_FP of this type, use it.
5491 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5492 default: break;
5493 case TargetLowering::Legal:
5494 if (!TLI.isTypeLegal(NewInTy))
5495 break; // Can't use this datatype.
5496 // FALL THROUGH.
5497 case TargetLowering::Custom:
5498 OpToUse = ISD::UINT_TO_FP;
5499 break;
5500 }
5501 if (OpToUse) break;
5502
5503 // Otherwise, try a larger type.
5504 }
5505
5506 // Okay, we found the operation and type to use. Zero extend our input to the
5507 // desired type then run the operation on it.
5508 return DAG.getNode(OpToUse, DestVT,
5509 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5510 NewInTy, LegalOp));
5511}
5512
5513/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5514/// FP_TO_*INT operation of the specified operand when the target requests that
5515/// we promote it. At this point, we know that the result and operand types are
5516/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5517/// operation that returns a larger result.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005518SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5519 MVT DestVT,
5520 bool isSigned) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005521 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands13237ac2008-06-06 12:08:01 +00005522 MVT NewOutTy = DestVT;
Chris Lattner689bdcc2006-01-28 08:25:58 +00005523
5524 unsigned OpToUse = 0;
5525
5526 // Scan for the appropriate larger type to use.
5527 while (1) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005528 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5529 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner689bdcc2006-01-28 08:25:58 +00005530
5531 // If the target supports FP_TO_SINT returning this type, use it.
5532 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5533 default: break;
5534 case TargetLowering::Legal:
5535 if (!TLI.isTypeLegal(NewOutTy))
5536 break; // Can't use this datatype.
5537 // FALL THROUGH.
5538 case TargetLowering::Custom:
5539 OpToUse = ISD::FP_TO_SINT;
5540 break;
5541 }
5542 if (OpToUse) break;
5543
5544 // If the target supports FP_TO_UINT of this type, use it.
5545 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5546 default: break;
5547 case TargetLowering::Legal:
5548 if (!TLI.isTypeLegal(NewOutTy))
5549 break; // Can't use this datatype.
5550 // FALL THROUGH.
5551 case TargetLowering::Custom:
5552 OpToUse = ISD::FP_TO_UINT;
5553 break;
5554 }
5555 if (OpToUse) break;
5556
5557 // Otherwise, try a larger type.
5558 }
5559
Chris Lattnerf81d5882007-11-24 07:07:01 +00005560
5561 // Okay, we found the operation and type to use.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005562 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands93e180342008-07-04 11:47:58 +00005563
Chris Lattnerf81d5882007-11-24 07:07:01 +00005564 // If the operation produces an invalid type, it must be custom lowered. Use
5565 // the target lowering hooks to expand it. Just keep the low part of the
5566 // expanded operation, we know that we're truncating anyway.
5567 if (getTypeAction(NewOutTy) == Expand) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005568 Operation = SDValue(TLI.ReplaceNodeResults(Operation.Val, DAG), 0);
Chris Lattnerf81d5882007-11-24 07:07:01 +00005569 assert(Operation.Val && "Didn't return anything");
5570 }
Duncan Sands93e180342008-07-04 11:47:58 +00005571
Chris Lattnerf81d5882007-11-24 07:07:01 +00005572 // Truncate the result of the extended FP_TO_*INT operation to the desired
5573 // size.
5574 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005575}
5576
5577/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5578///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005579SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005580 MVT VT = Op.getValueType();
5581 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005582 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands13237ac2008-06-06 12:08:01 +00005583 switch (VT.getSimpleVT()) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005584 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5585 case MVT::i16:
5586 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5587 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5588 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5589 case MVT::i32:
5590 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5591 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5592 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5593 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5594 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5595 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5596 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5597 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5598 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5599 case MVT::i64:
5600 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5601 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5602 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5603 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5604 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5605 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5606 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5607 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5608 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5609 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5610 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5611 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5612 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5613 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5614 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5615 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5616 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5617 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5618 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5619 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5620 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5621 }
5622}
5623
5624/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5625///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005626SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00005627 switch (Opc) {
5628 default: assert(0 && "Cannot expand this yet!");
5629 case ISD::CTPOP: {
5630 static const uint64_t mask[6] = {
5631 0x5555555555555555ULL, 0x3333333333333333ULL,
5632 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5633 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5634 };
Duncan Sands13237ac2008-06-06 12:08:01 +00005635 MVT VT = Op.getValueType();
5636 MVT ShVT = TLI.getShiftAmountTy();
5637 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00005638 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5639 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005640 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
5641 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005642 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5643 DAG.getNode(ISD::AND, VT,
5644 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5645 }
5646 return Op;
5647 }
5648 case ISD::CTLZ: {
5649 // for now, we do this:
5650 // x = x | (x >> 1);
5651 // x = x | (x >> 2);
5652 // ...
5653 // x = x | (x >>16);
5654 // x = x | (x >>32); // for 64-bit input
5655 // return popcount(~x);
5656 //
5657 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands13237ac2008-06-06 12:08:01 +00005658 MVT VT = Op.getValueType();
5659 MVT ShVT = TLI.getShiftAmountTy();
5660 unsigned len = VT.getSizeInBits();
Chris Lattner689bdcc2006-01-28 08:25:58 +00005661 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005662 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005663 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5664 }
5665 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5666 return DAG.getNode(ISD::CTPOP, VT, Op);
5667 }
5668 case ISD::CTTZ: {
5669 // for now, we use: { return popcount(~x & (x - 1)); }
5670 // unless the target has ctlz but not ctpop, in which case we use:
5671 // { return 32 - nlz(~x & (x-1)); }
5672 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands13237ac2008-06-06 12:08:01 +00005673 MVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005674 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
5675 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005676 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5677 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5678 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5679 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5680 TLI.isOperationLegal(ISD::CTLZ, VT))
5681 return DAG.getNode(ISD::SUB, VT,
Duncan Sands13237ac2008-06-06 12:08:01 +00005682 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005683 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5684 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5685 }
5686 }
5687}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005688
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005689/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerdc750592005-01-07 07:47:09 +00005690/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5691/// LegalizeNodes map is filled in for any results that are not expanded, the
5692/// ExpandedNodes map is filled in for any results that are expanded, and the
5693/// Lo/Hi values are returned.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005694void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands13237ac2008-06-06 12:08:01 +00005695 MVT VT = Op.getValueType();
5696 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005697 SDNode *Node = Op.Val;
5698 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00005699 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands13237ac2008-06-06 12:08:01 +00005700 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattnerdc750592005-01-07 07:47:09 +00005701
Chris Lattner1a570f12005-09-02 20:32:45 +00005702 // See if we already expanded it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005703 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005704 = ExpandedNodes.find(Op);
5705 if (I != ExpandedNodes.end()) {
5706 Lo = I->second.first;
5707 Hi = I->second.second;
5708 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005709 }
5710
Chris Lattnerdc750592005-01-07 07:47:09 +00005711 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005712 case ISD::CopyFromReg:
5713 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005714 case ISD::FP_ROUND_INREG:
5715 if (VT == MVT::ppcf128 &&
5716 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5717 TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005718 SDValue SrcLo, SrcHi, Src;
Dale Johannesen6472eb62007-10-11 23:32:15 +00005719 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5720 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005721 SDValue Result = TLI.LowerOperation(
Dale Johannesen6472eb62007-10-11 23:32:15 +00005722 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005723 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5724 Lo = Result.Val->getOperand(0);
5725 Hi = Result.Val->getOperand(1);
5726 break;
5727 }
5728 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005729 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005730#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005731 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005732#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005733 assert(0 && "Do not know how to expand this operator!");
5734 abort();
Dan Gohman66272a52008-02-27 01:52:30 +00005735 case ISD::EXTRACT_ELEMENT:
5736 ExpandOp(Node->getOperand(0), Lo, Hi);
5737 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5738 return ExpandOp(Hi, Lo, Hi);
Dan Gohmane5e32ec2008-02-27 19:44:57 +00005739 return ExpandOp(Lo, Lo, Hi);
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005740 case ISD::EXTRACT_VECTOR_ELT:
5741 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5742 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5743 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5744 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005745 case ISD::UNDEF:
5746 Lo = DAG.getNode(ISD::UNDEF, NVT);
5747 Hi = DAG.getNode(ISD::UNDEF, NVT);
5748 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005749 case ISD::Constant: {
Duncan Sands13237ac2008-06-06 12:08:01 +00005750 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohmanf2bbfa32008-03-03 22:20:46 +00005751 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5752 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5753 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005754 break;
5755 }
Evan Cheng47833a12006-12-12 21:32:44 +00005756 case ISD::ConstantFP: {
5757 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005758 if (CFP->getValueType(0) == MVT::ppcf128) {
5759 APInt api = CFP->getValueAPF().convertToAPInt();
5760 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5761 MVT::f64);
5762 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5763 MVT::f64);
5764 break;
5765 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005766 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005767 if (getTypeAction(Lo.getValueType()) == Expand)
5768 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005769 break;
5770 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005771 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005772 // Return the operands.
5773 Lo = Node->getOperand(0);
5774 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005775 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00005776
5777 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00005778 if (Node->getNumValues() == 1) {
5779 ExpandOp(Op.getOperand(0), Lo, Hi);
5780 break;
5781 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005782 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5783 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5784 Op.getValue(1).getValueType() == MVT::Other &&
5785 "unhandled MERGE_VALUES");
5786 ExpandOp(Op.getOperand(0), Lo, Hi);
5787 // Remember that we legalized the chain.
5788 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5789 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005790
5791 case ISD::SIGN_EXTEND_INREG:
5792 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005793 // sext_inreg the low part if needed.
5794 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5795
5796 // The high part gets the sign extension from the lo-part. This handles
5797 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005798 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands13237ac2008-06-06 12:08:01 +00005799 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005800 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005801 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005802
Nate Begeman2fba8a32006-01-14 03:14:10 +00005803 case ISD::BSWAP: {
5804 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005805 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begeman2fba8a32006-01-14 03:14:10 +00005806 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5807 Lo = TempLo;
5808 break;
5809 }
5810
Chris Lattner55e9cde2005-05-11 04:51:16 +00005811 case ISD::CTPOP:
5812 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005813 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5814 DAG.getNode(ISD::CTPOP, NVT, Lo),
5815 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005816 Hi = DAG.getConstant(0, NVT);
5817 break;
5818
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005819 case ISD::CTLZ: {
5820 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005821 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005822 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
5823 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5824 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005825 ISD::SETNE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005826 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005827 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5828
5829 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5830 Hi = DAG.getConstant(0, NVT);
5831 break;
5832 }
5833
5834 case ISD::CTTZ: {
5835 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005836 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005837 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
5838 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5839 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005840 ISD::SETNE);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005841 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005842 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5843
5844 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5845 Hi = DAG.getConstant(0, NVT);
5846 break;
5847 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005848
Nate Begemane74795c2006-01-25 18:21:52 +00005849 case ISD::VAARG: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005850 SDValue Ch = Node->getOperand(0); // Legalize the chain.
5851 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005852 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5853 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5854
5855 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005856 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005857 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005858 if (TLI.isBigEndian())
Nate Begemane74795c2006-01-25 18:21:52 +00005859 std::swap(Lo, Hi);
5860 break;
5861 }
5862
Chris Lattnerdc750592005-01-07 07:47:09 +00005863 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005864 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005865 SDValue Ch = LD->getChain(); // Legalize the chain.
5866 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005867 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005868 int SVOffset = LD->getSrcValueOffset();
5869 unsigned Alignment = LD->getAlignment();
5870 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005871
Evan Chenge71fe34d2006-10-09 20:57:25 +00005872 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005873 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5874 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005875 if (VT == MVT::f32 || VT == MVT::f64) {
5876 // f32->i32 or f64->i64 one to one expansion.
5877 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005878 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005879 // Recursively expand the new load.
5880 if (getTypeAction(NVT) == Expand)
5881 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005882 break;
5883 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005884
Evan Chenge71fe34d2006-10-09 20:57:25 +00005885 // Increment the pointer to the other half.
Duncan Sands13237ac2008-06-06 12:08:01 +00005886 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Chenge71fe34d2006-10-09 20:57:25 +00005887 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00005888 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005889 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005890 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005891 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5892 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005893
Evan Chenge71fe34d2006-10-09 20:57:25 +00005894 // Build a factor node to remember that this load is independent of the
5895 // other one.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005896 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Chenge71fe34d2006-10-09 20:57:25 +00005897 Hi.getValue(1));
5898
5899 // Remember that we legalized the chain.
5900 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005901 if (TLI.isBigEndian())
Evan Chenge71fe34d2006-10-09 20:57:25 +00005902 std::swap(Lo, Hi);
5903 } else {
Duncan Sands13237ac2008-06-06 12:08:01 +00005904 MVT EVT = LD->getMemoryVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005905
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005906 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5907 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005908 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005909 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005910 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005911 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005912 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Chenge370e0e2006-12-13 03:19:57 +00005913 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5914 break;
5915 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005916
5917 if (EVT == NVT)
5918 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005919 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005920 else
5921 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005922 SVOffset, EVT, isVolatile,
5923 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005924
5925 // Remember that we legalized the chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005926 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Chenge71fe34d2006-10-09 20:57:25 +00005927
5928 if (ExtType == ISD::SEXTLOAD) {
5929 // The high part is obtained by SRA'ing all but one of the bits of the
5930 // lo part.
Duncan Sands13237ac2008-06-06 12:08:01 +00005931 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Chenge71fe34d2006-10-09 20:57:25 +00005932 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5933 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5934 } else if (ExtType == ISD::ZEXTLOAD) {
5935 // The high part is just a zero.
5936 Hi = DAG.getConstant(0, NVT);
5937 } else /* if (ExtType == ISD::EXTLOAD) */ {
5938 // The high part is undefined.
5939 Hi = DAG.getNode(ISD::UNDEF, NVT);
5940 }
5941 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005942 break;
5943 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005944 case ISD::AND:
5945 case ISD::OR:
5946 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005947 SDValue LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005948 ExpandOp(Node->getOperand(0), LL, LH);
5949 ExpandOp(Node->getOperand(1), RL, RH);
5950 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5951 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5952 break;
5953 }
5954 case ISD::SELECT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005955 SDValue LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005956 ExpandOp(Node->getOperand(1), LL, LH);
5957 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005958 if (getTypeAction(NVT) == Expand)
5959 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005960 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005961 if (VT != MVT::f32)
5962 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005963 break;
5964 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005965 case ISD::SELECT_CC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005966 SDValue TL, TH, FL, FH;
Nate Begemane5b86d72005-08-10 20:51:12 +00005967 ExpandOp(Node->getOperand(2), TL, TH);
5968 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005969 if (getTypeAction(NVT) == Expand)
5970 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005971 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5972 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005973 if (VT != MVT::f32)
5974 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5975 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005976 break;
5977 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005978 case ISD::ANY_EXTEND:
5979 // The low part is any extension of the input (which degenerates to a copy).
5980 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5981 // The high part is undefined.
5982 Hi = DAG.getNode(ISD::UNDEF, NVT);
5983 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005984 case ISD::SIGN_EXTEND: {
5985 // The low part is just a sign extension of the input (which degenerates to
5986 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005987 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005988
Chris Lattnerdc750592005-01-07 07:47:09 +00005989 // The high part is obtained by SRA'ing all but one of the bits of the lo
5990 // part.
Duncan Sands13237ac2008-06-06 12:08:01 +00005991 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005992 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5993 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005994 break;
5995 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005996 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005997 // The low part is just a zero extension of the input (which degenerates to
5998 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005999 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006000
Chris Lattnerdc750592005-01-07 07:47:09 +00006001 // The high part is just a zero.
6002 Hi = DAG.getConstant(0, NVT);
6003 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00006004
Chris Lattner59b27fa372007-02-13 23:55:16 +00006005 case ISD::TRUNCATE: {
6006 // The input value must be larger than this value. Expand *it*.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006007 SDValue NewLo;
Chris Lattner59b27fa372007-02-13 23:55:16 +00006008 ExpandOp(Node->getOperand(0), NewLo, Hi);
6009
6010 // The low part is now either the right size, or it is closer. If not the
6011 // right size, make an illegal truncate so we recursively expand it.
6012 if (NewLo.getValueType() != Node->getValueType(0))
6013 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6014 ExpandOp(NewLo, Lo, Hi);
6015 break;
6016 }
6017
Chris Lattner36e663d2005-12-23 00:16:34 +00006018 case ISD::BIT_CONVERT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006019 SDValue Tmp;
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006020 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6021 // If the target wants to, allow it to lower this itself.
6022 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6023 case Expand: assert(0 && "cannot expand FP!");
6024 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6025 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6026 }
6027 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6028 }
6029
Evan Cheng4eee7242006-12-09 02:42:38 +00006030 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00006031 if (VT == MVT::f32 || VT == MVT::f64) {
6032 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00006033 if (getTypeAction(NVT) == Expand)
6034 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00006035 break;
6036 }
6037
6038 // If source operand will be expanded to the same type as VT, i.e.
6039 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands13237ac2008-06-06 12:08:01 +00006040 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng0076ca02006-12-12 19:53:13 +00006041 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6042 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006043 break;
6044 }
6045
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006046 // Turn this into a load/store pair by default.
6047 if (Tmp.Val == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00006048 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006049
Chris Lattner36e663d2005-12-23 00:16:34 +00006050 ExpandOp(Tmp, Lo, Hi);
6051 break;
6052 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006053
Chris Lattnerf81d5882007-11-24 07:07:01 +00006054 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00006055 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6056 TargetLowering::Custom &&
6057 "Must custom expand ReadCycleCounter");
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006058 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006059 assert(Tmp.Val && "Node must be custom expanded!");
6060 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006061 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00006062 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006063 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006064 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006065
Mon P Wang6a490372008-06-25 08:15:39 +00006066 case ISD::ATOMIC_CMP_SWAP: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006067 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Andrew Lenharth357061a2008-03-05 01:15:49 +00006068 assert(Tmp.Val && "Node must be custom expanded!");
6069 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006070 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Andrew Lenharth357061a2008-03-05 01:15:49 +00006071 LegalizeOp(Tmp.getValue(1)));
6072 break;
6073 }
6074
6075
6076
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006077 // These operators cannot be expanded directly, emit them as calls to
6078 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006079 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006080 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006081 SDValue Op;
Chris Lattner941d84a2005-07-30 01:40:57 +00006082 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6083 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006084 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6085 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00006086 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006087
Chris Lattner941d84a2005-07-30 01:40:57 +00006088 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6089
Chris Lattnerfe68d752005-07-29 00:33:32 +00006090 // Now that the custom expander is done, expand the result, which is still
6091 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006092 if (Op.Val) {
6093 ExpandOp(Op, Lo, Hi);
6094 break;
6095 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006096 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006097
Duncan Sands77a3d052008-07-17 02:36:29 +00006098 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6099 VT);
6100 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6101 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006102 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006103 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006104
Evan Cheng31cbddf2007-01-12 02:11:51 +00006105 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006106 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006107 SDValue Op;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006108 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6109 case Expand: assert(0 && "cannot expand FP!");
6110 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6111 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6112 }
6113
6114 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6115
6116 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006117 if (Op.Val) {
6118 ExpandOp(Op, Lo, Hi);
6119 break;
6120 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006121 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006122
Duncan Sands77a3d052008-07-17 02:36:29 +00006123 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6124 VT);
6125 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6126 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006127 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006128 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006129
Evan Cheng870e4f82006-01-09 18:31:59 +00006130 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006131 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006132 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006133 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006134 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006135 Op = TLI.LowerOperation(Op, DAG);
6136 if (Op.Val) {
6137 // Now that the custom expander is done, expand the result, which is
6138 // still VT.
6139 ExpandOp(Op, Lo, Hi);
6140 break;
6141 }
6142 }
6143
Chris Lattner72b503b2006-09-13 03:50:39 +00006144 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6145 // this X << 1 as X+X.
6146 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00006147 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner72b503b2006-09-13 03:50:39 +00006148 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006149 SDValue LoOps[2], HiOps[3];
Chris Lattner72b503b2006-09-13 03:50:39 +00006150 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6151 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6152 LoOps[1] = LoOps[0];
6153 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6154
6155 HiOps[1] = HiOps[0];
6156 HiOps[2] = Lo.getValue(1);
6157 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6158 break;
6159 }
6160 }
6161
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006162 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006163 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006164 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006165
6166 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006167 TargetLowering::LegalizeAction Action =
6168 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6169 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6170 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006171 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006172 break;
6173 }
6174
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006175 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006176 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006177 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006178 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006179
Evan Cheng870e4f82006-01-09 18:31:59 +00006180 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006181 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006182 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006183 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006184 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006185 Op = TLI.LowerOperation(Op, DAG);
6186 if (Op.Val) {
6187 // Now that the custom expander is done, expand the result, which is
6188 // still VT.
6189 ExpandOp(Op, Lo, Hi);
6190 break;
6191 }
6192 }
6193
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006194 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006195 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006196 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006197
6198 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006199 TargetLowering::LegalizeAction Action =
6200 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6201 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6202 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006203 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006204 break;
6205 }
6206
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006207 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006208 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006209 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006210 }
6211
6212 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006213 // If the target wants custom lowering, do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006214 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006215 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006216 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006217 Op = TLI.LowerOperation(Op, DAG);
6218 if (Op.Val) {
6219 // Now that the custom expander is done, expand the result, which is
6220 // still VT.
6221 ExpandOp(Op, Lo, Hi);
6222 break;
6223 }
6224 }
6225
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006226 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006227 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006228 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006229
6230 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006231 TargetLowering::LegalizeAction Action =
6232 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6233 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6234 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006235 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006236 break;
6237 }
6238
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006239 // Otherwise, emit a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006240 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006241 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006242 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006243
Misha Brukman835702a2005-04-21 22:36:52 +00006244 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006245 case ISD::SUB: {
6246 // If the target wants to custom expand this, let them.
6247 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6248 TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006249 SDValue Result = TLI.LowerOperation(Op, DAG);
Duncan Sands5fb92e52008-06-22 09:42:16 +00006250 if (Result.Val) {
6251 ExpandOp(Result, Lo, Hi);
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006252 break;
6253 }
6254 }
6255
6256 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006257 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006258 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6259 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00006260 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006261 SDValue LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006262 LoOps[0] = LHSL;
6263 LoOps[1] = RHSL;
6264 HiOps[0] = LHSH;
6265 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00006266 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00006267 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006268 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006269 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006270 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00006271 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006272 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006273 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006274 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00006275 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006276 }
Chris Lattner2135bc02007-05-17 18:15:41 +00006277
6278 case ISD::ADDC:
6279 case ISD::SUBC: {
6280 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006281 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner2135bc02007-05-17 18:15:41 +00006282 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6283 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6284 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006285 SDValue LoOps[2] = { LHSL, RHSL };
6286 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattner2135bc02007-05-17 18:15:41 +00006287
6288 if (Node->getOpcode() == ISD::ADDC) {
6289 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6290 HiOps[2] = Lo.getValue(1);
6291 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6292 } else {
6293 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6294 HiOps[2] = Lo.getValue(1);
6295 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6296 }
6297 // Remember that we legalized the flag.
6298 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6299 break;
6300 }
6301 case ISD::ADDE:
6302 case ISD::SUBE: {
6303 // Expand the subcomponents.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006304 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner2135bc02007-05-17 18:15:41 +00006305 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6306 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6307 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006308 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6309 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattner2135bc02007-05-17 18:15:41 +00006310
6311 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6312 HiOps[2] = Lo.getValue(1);
6313 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6314
6315 // Remember that we legalized the flag.
6316 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6317 break;
6318 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006319 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006320 // If the target wants to custom expand this, let them.
6321 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006322 SDValue New = TLI.LowerOperation(Op, DAG);
Chris Lattnere50f5d12006-09-16 05:08:34 +00006323 if (New.Val) {
6324 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006325 break;
6326 }
6327 }
6328
Evan Chenge93762d2006-09-01 18:17:58 +00006329 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6330 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00006331 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6332 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6333 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006334 SDValue LL, LH, RL, RH;
Nate Begemanadd0c632005-04-11 03:01:51 +00006335 ExpandOp(Node->getOperand(0), LL, LH);
6336 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman1f372ed2008-02-25 21:11:39 +00006337 unsigned OuterBitSize = Op.getValueSizeInBits();
6338 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohmana1603612007-10-08 18:33:35 +00006339 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6340 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman272e2342008-03-10 20:42:19 +00006341 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6342 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6343 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohmana1603612007-10-08 18:33:35 +00006344 // The inputs are both zero-extended.
6345 if (HasUMUL_LOHI) {
6346 // We can emit a umul_lohi.
6347 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006348 Hi = SDValue(Lo.Val, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00006349 break;
6350 }
6351 if (HasMULHU) {
6352 // We can emit a mulhu+mul.
6353 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6354 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6355 break;
6356 }
Dan Gohmana1603612007-10-08 18:33:35 +00006357 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00006358 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohmana1603612007-10-08 18:33:35 +00006359 // The input values are both sign-extended.
6360 if (HasSMUL_LOHI) {
6361 // We can emit a smul_lohi.
6362 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006363 Hi = SDValue(Lo.Val, 1);
Dan Gohmana1603612007-10-08 18:33:35 +00006364 break;
6365 }
6366 if (HasMULHS) {
6367 // We can emit a mulhs+mul.
6368 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6369 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6370 break;
6371 }
6372 }
6373 if (HasUMUL_LOHI) {
6374 // Lo,Hi = umul LHS, RHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006375 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohmana1603612007-10-08 18:33:35 +00006376 DAG.getVTList(NVT, NVT), LL, RL);
6377 Lo = UMulLOHI;
6378 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00006379 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6380 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6381 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6382 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00006383 break;
Nate Begeman43144a22005-08-30 02:44:00 +00006384 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00006385 if (HasMULHU) {
6386 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6387 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6388 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6389 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6390 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6391 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6392 break;
6393 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006394 }
Evan Chenge93762d2006-09-01 18:17:58 +00006395
Dan Gohmana1603612007-10-08 18:33:35 +00006396 // If nothing else, we can make a libcall.
Duncan Sands844d55a2008-04-12 17:14:18 +00006397 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00006398 break;
6399 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006400 case ISD::SDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00006401 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006402 break;
6403 case ISD::UDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00006404 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006405 break;
6406 case ISD::SREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00006407 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006408 break;
6409 case ISD::UREM:
Duncan Sands844d55a2008-04-12 17:14:18 +00006410 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006411 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00006412
Evan Cheng4eee7242006-12-09 02:42:38 +00006413 case ISD::FADD:
Duncan Sands844d55a2008-04-12 17:14:18 +00006414 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6415 RTLIB::ADD_F64,
6416 RTLIB::ADD_F80,
6417 RTLIB::ADD_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006418 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006419 break;
6420 case ISD::FSUB:
Duncan Sands844d55a2008-04-12 17:14:18 +00006421 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6422 RTLIB::SUB_F64,
6423 RTLIB::SUB_F80,
6424 RTLIB::SUB_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006425 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006426 break;
6427 case ISD::FMUL:
Duncan Sands844d55a2008-04-12 17:14:18 +00006428 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6429 RTLIB::MUL_F64,
6430 RTLIB::MUL_F80,
6431 RTLIB::MUL_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006432 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006433 break;
6434 case ISD::FDIV:
Duncan Sands844d55a2008-04-12 17:14:18 +00006435 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6436 RTLIB::DIV_F64,
6437 RTLIB::DIV_F80,
6438 RTLIB::DIV_PPCF128),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006439 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006440 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00006441 case ISD::FP_EXTEND: {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006442 if (VT == MVT::ppcf128) {
6443 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6444 Node->getOperand(0).getValueType()==MVT::f64);
6445 const uint64_t zero = 0;
6446 if (Node->getOperand(0).getValueType()==MVT::f32)
6447 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6448 else
6449 Hi = Node->getOperand(0);
6450 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6451 break;
6452 }
Duncan Sands77a3d052008-07-17 02:36:29 +00006453 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6454 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6455 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006456 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00006457 }
6458 case ISD::FP_ROUND: {
6459 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6460 VT);
6461 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6462 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006463 break;
Duncan Sands77a3d052008-07-17 02:36:29 +00006464 }
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006465 case ISD::FPOWI:
Duncan Sands844d55a2008-04-12 17:14:18 +00006466 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32,
6467 RTLIB::POWI_F64,
6468 RTLIB::POWI_F80,
6469 RTLIB::POWI_PPCF128),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006470 Node, false, Hi);
6471 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006472 case ISD::FSQRT:
6473 case ISD::FSIN:
6474 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006475 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006476 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006477 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00006478 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6479 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006480 break;
6481 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00006482 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6483 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006484 break;
6485 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00006486 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6487 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006488 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006489 default: assert(0 && "Unreachable!");
6490 }
Duncan Sands844d55a2008-04-12 17:14:18 +00006491 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006492 break;
6493 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006494 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006495 if (VT == MVT::ppcf128) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006496 SDValue Tmp;
Dale Johannesen61c574f2007-10-12 19:02:17 +00006497 ExpandOp(Node->getOperand(0), Lo, Tmp);
6498 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6499 // lo = hi==fabs(hi) ? lo : -lo;
6500 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6501 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6502 DAG.getCondCode(ISD::SETEQ));
6503 break;
6504 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006505 SDValue Mask = (VT == MVT::f64)
Evan Cheng388cbbf2006-12-16 00:52:40 +00006506 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6507 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6508 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6509 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6510 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6511 if (getTypeAction(NVT) == Expand)
6512 ExpandOp(Lo, Lo, Hi);
6513 break;
6514 }
6515 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006516 if (VT == MVT::ppcf128) {
6517 ExpandOp(Node->getOperand(0), Lo, Hi);
6518 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6519 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6520 break;
6521 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006522 SDValue Mask = (VT == MVT::f64)
Evan Cheng388cbbf2006-12-16 00:52:40 +00006523 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6524 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6525 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6526 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6527 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6528 if (getTypeAction(NVT) == Expand)
6529 ExpandOp(Lo, Lo, Hi);
6530 break;
6531 }
Evan Cheng003feb02007-01-04 21:56:39 +00006532 case ISD::FCOPYSIGN: {
6533 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6534 if (getTypeAction(NVT) == Expand)
6535 ExpandOp(Lo, Lo, Hi);
6536 break;
6537 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006538 case ISD::SINT_TO_FP:
6539 case ISD::UINT_TO_FP: {
6540 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands13237ac2008-06-06 12:08:01 +00006541 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen12c76db2008-03-18 17:28:38 +00006542
6543 // Promote the operand if needed. Do this before checking for
6544 // ppcf128 so conversions of i16 and i8 work.
6545 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006546 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesen12c76db2008-03-18 17:28:38 +00006547 Tmp = isSigned
6548 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6549 DAG.getValueType(SrcVT))
6550 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6551 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6552 SrcVT = Node->getOperand(0).getValueType();
6553 }
6554
Dan Gohmanf4300952008-03-10 23:03:31 +00006555 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohman432e4a62008-02-25 21:39:34 +00006556 static const uint64_t zero = 0;
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006557 if (isSigned) {
6558 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6559 Node->getOperand(0)));
6560 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6561 } else {
Dan Gohman432e4a62008-02-25 21:39:34 +00006562 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006563 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6564 Node->getOperand(0)));
6565 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6566 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006567 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006568 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6569 DAG.getConstant(0, MVT::i32),
6570 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6571 DAG.getConstantFP(
6572 APFloat(APInt(128, 2, TwoE32)),
6573 MVT::ppcf128)),
6574 Hi,
6575 DAG.getCondCode(ISD::SETLT)),
6576 Lo, Hi);
6577 }
6578 break;
6579 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006580 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6581 // si64->ppcf128 done by libcall, below
Dan Gohman432e4a62008-02-25 21:39:34 +00006582 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006583 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6584 Lo, Hi);
6585 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6586 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6587 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6588 DAG.getConstant(0, MVT::i64),
6589 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6590 DAG.getConstantFP(
6591 APFloat(APInt(128, 2, TwoE64)),
6592 MVT::ppcf128)),
6593 Hi,
6594 DAG.getCondCode(ISD::SETLT)),
6595 Lo, Hi);
6596 break;
6597 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006598
Dan Gohmanf4300952008-03-10 23:03:31 +00006599 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6600 Node->getOperand(0));
Evan Cheng86e476b2008-04-01 01:50:16 +00006601 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng611abc02008-04-01 01:51:26 +00006602 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng86e476b2008-04-01 01:50:16 +00006603 ExpandOp(Lo, Lo, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006604 break;
6605 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006606 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006607
Chris Lattnerac12f682005-12-21 18:02:52 +00006608 // Make sure the resultant values have been legalized themselves, unless this
6609 // is a type that requires multi-step expansion.
6610 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6611 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006612 if (Hi.Val)
6613 // Don't legalize the high part if it is expanded to a single node.
6614 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006615 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006616
6617 // Remember in a map if the values will be reused later.
Dan Gohman38740a92008-07-07 17:46:23 +00006618 bool isNew =
6619 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng870e4f82006-01-09 18:31:59 +00006620 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006621}
6622
Dan Gohmana8665142007-06-25 16:23:39 +00006623/// SplitVectorOp - Given an operand of vector type, break it down into
6624/// two smaller values, still of vector type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006625void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
6626 SDValue &Hi) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006627 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006628 SDNode *Node = Op.Val;
Duncan Sands13237ac2008-06-06 12:08:01 +00006629 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattner32206f52006-03-18 01:44:44 +00006630 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00006631
Duncan Sands13237ac2008-06-06 12:08:01 +00006632 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begemanbd117f02007-11-15 21:15:26 +00006633
6634 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6635 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6636
Duncan Sands13237ac2008-06-06 12:08:01 +00006637 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
6638 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006639
Chris Lattner32206f52006-03-18 01:44:44 +00006640 // See if we already split it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006641 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner32206f52006-03-18 01:44:44 +00006642 = SplitNodes.find(Op);
6643 if (I != SplitNodes.end()) {
6644 Lo = I->second.first;
6645 Hi = I->second.second;
6646 return;
6647 }
6648
6649 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006650 default:
6651#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006652 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006653#endif
6654 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00006655 case ISD::UNDEF:
6656 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6657 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6658 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006659 case ISD::BUILD_PAIR:
6660 Lo = Node->getOperand(0);
6661 Hi = Node->getOperand(1);
6662 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006663 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman6f94f612008-04-25 18:07:40 +00006664 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
6665 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6666 unsigned Index = Idx->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006667 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman6f94f612008-04-25 18:07:40 +00006668 if (Index < NewNumElts_Lo)
6669 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
6670 DAG.getIntPtrConstant(Index));
6671 else
6672 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6673 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
6674 break;
6675 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006676 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman6f94f612008-04-25 18:07:40 +00006677 Node->getOperand(1),
6678 Node->getOperand(2));
6679 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohmana90183e2007-09-28 23:53:40 +00006680 break;
6681 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006682 case ISD::VECTOR_SHUFFLE: {
6683 // Build the low part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006684 SDValue Mask = Node->getOperand(2);
6685 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00006686 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006687
6688 // Insert all of the elements from the input that are needed. We use
6689 // buildvector of extractelement here because the input vectors will have
6690 // to be legalized, so this makes the code simpler.
6691 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006692 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman63eb03f2008-03-14 00:53:31 +00006693 if (IdxNode.getOpcode() == ISD::UNDEF) {
6694 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6695 continue;
6696 }
6697 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006698 SDValue InVec = Node->getOperand(0);
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006699 if (Idx >= NumElements) {
6700 InVec = Node->getOperand(1);
6701 Idx -= NumElements;
6702 }
6703 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6704 DAG.getConstant(Idx, PtrVT)));
6705 }
6706 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6707 Ops.clear();
6708
6709 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006710 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman63eb03f2008-03-14 00:53:31 +00006711 if (IdxNode.getOpcode() == ISD::UNDEF) {
6712 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6713 continue;
6714 }
6715 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006716 SDValue InVec = Node->getOperand(0);
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006717 if (Idx >= NumElements) {
6718 InVec = Node->getOperand(1);
6719 Idx -= NumElements;
6720 }
6721 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6722 DAG.getConstant(Idx, PtrVT)));
6723 }
Mon P Wang73343502008-07-25 01:30:26 +00006724 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006725 break;
6726 }
Dan Gohmana8665142007-06-25 16:23:39 +00006727 case ISD::BUILD_VECTOR: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006728 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00006729 Node->op_begin()+NewNumElts_Lo);
6730 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006731
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006732 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00006733 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006734 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006735 break;
6736 }
Dan Gohmana8665142007-06-25 16:23:39 +00006737 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00006738 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00006739 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6740 if (NewNumSubvectors == 1) {
6741 Lo = Node->getOperand(0);
6742 Hi = Node->getOperand(1);
6743 } else {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006744 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Dan Gohmana8665142007-06-25 16:23:39 +00006745 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00006746 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006747
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006748 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohmana8665142007-06-25 16:23:39 +00006749 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006750 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00006751 }
Dan Gohman26455c42007-06-13 15:12:02 +00006752 break;
6753 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006754 case ISD::SELECT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006755 SDValue Cond = Node->getOperand(0);
Dan Gohman8f518b982007-10-17 14:48:28 +00006756
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006757 SDValue LL, LH, RL, RH;
Dan Gohman8f518b982007-10-17 14:48:28 +00006758 SplitVectorOp(Node->getOperand(1), LL, LH);
6759 SplitVectorOp(Node->getOperand(2), RL, RH);
6760
Duncan Sands13237ac2008-06-06 12:08:01 +00006761 if (Cond.getValueType().isVector()) {
Dan Gohman8f518b982007-10-17 14:48:28 +00006762 // Handle a vector merge.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006763 SDValue CL, CH;
Dan Gohman8f518b982007-10-17 14:48:28 +00006764 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00006765 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6766 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006767 } else {
6768 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00006769 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6770 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006771 }
6772 break;
6773 }
Chris Lattner9d3740e2008-06-30 02:43:01 +00006774 case ISD::SELECT_CC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006775 SDValue CondLHS = Node->getOperand(0);
6776 SDValue CondRHS = Node->getOperand(1);
6777 SDValue CondCode = Node->getOperand(4);
Chris Lattner9d3740e2008-06-30 02:43:01 +00006778
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006779 SDValue LL, LH, RL, RH;
Chris Lattner9d3740e2008-06-30 02:43:01 +00006780 SplitVectorOp(Node->getOperand(2), LL, LH);
6781 SplitVectorOp(Node->getOperand(3), RL, RH);
6782
6783 // Handle a simple select with vector operands.
6784 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
6785 LL, RL, CondCode);
6786 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
6787 LH, RH, CondCode);
6788 break;
6789 }
Nate Begemancfcb5602008-05-12 19:40:03 +00006790 case ISD::VSETCC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006791 SDValue LL, LH, RL, RH;
Nate Begemancfcb5602008-05-12 19:40:03 +00006792 SplitVectorOp(Node->getOperand(0), LL, LH);
6793 SplitVectorOp(Node->getOperand(1), RL, RH);
6794 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
6795 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
6796 break;
6797 }
Dan Gohmana8665142007-06-25 16:23:39 +00006798 case ISD::ADD:
6799 case ISD::SUB:
6800 case ISD::MUL:
6801 case ISD::FADD:
6802 case ISD::FSUB:
6803 case ISD::FMUL:
6804 case ISD::SDIV:
6805 case ISD::UDIV:
6806 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006807 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006808 case ISD::AND:
6809 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00006810 case ISD::XOR:
6811 case ISD::UREM:
6812 case ISD::SREM:
6813 case ISD::FREM: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006814 SDValue LL, LH, RL, RH;
Chris Lattner32206f52006-03-18 01:44:44 +00006815 SplitVectorOp(Node->getOperand(0), LL, LH);
6816 SplitVectorOp(Node->getOperand(1), RL, RH);
6817
Nate Begemanbd117f02007-11-15 21:15:26 +00006818 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6819 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006820 break;
6821 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006822 case ISD::FPOWI: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006823 SDValue L, H;
Dan Gohman2a7de412007-10-11 23:57:53 +00006824 SplitVectorOp(Node->getOperand(0), L, H);
6825
Nate Begemanbd117f02007-11-15 21:15:26 +00006826 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6827 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00006828 break;
6829 }
6830 case ISD::CTTZ:
6831 case ISD::CTLZ:
6832 case ISD::CTPOP:
6833 case ISD::FNEG:
6834 case ISD::FABS:
6835 case ISD::FSQRT:
6836 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00006837 case ISD::FCOS:
6838 case ISD::FP_TO_SINT:
6839 case ISD::FP_TO_UINT:
6840 case ISD::SINT_TO_FP:
6841 case ISD::UINT_TO_FP: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006842 SDValue L, H;
Dan Gohman2a7de412007-10-11 23:57:53 +00006843 SplitVectorOp(Node->getOperand(0), L, H);
6844
Nate Begemanbd117f02007-11-15 21:15:26 +00006845 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6846 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00006847 break;
6848 }
Dan Gohmana8665142007-06-25 16:23:39 +00006849 case ISD::LOAD: {
6850 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006851 SDValue Ch = LD->getChain();
6852 SDValue Ptr = LD->getBasePtr();
Dan Gohmana8665142007-06-25 16:23:39 +00006853 const Value *SV = LD->getSrcValue();
6854 int SVOffset = LD->getSrcValueOffset();
6855 unsigned Alignment = LD->getAlignment();
6856 bool isVolatile = LD->isVolatile();
6857
Nate Begemanbd117f02007-11-15 21:15:26 +00006858 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Duncan Sands13237ac2008-06-06 12:08:01 +00006859 unsigned IncrementSize = NewNumElts_Lo * NewEltVT.getSizeInBits()/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006860 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00006861 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006862 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006863 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begemanbd117f02007-11-15 21:15:26 +00006864 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006865
6866 // Build a factor node to remember that this load is independent of the
6867 // other one.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006868 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattner32206f52006-03-18 01:44:44 +00006869 Hi.getValue(1));
6870
6871 // Remember that we legalized the chain.
6872 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006873 break;
6874 }
Dan Gohmana8665142007-06-25 16:23:39 +00006875 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006876 // We know the result is a vector. The input may be either a vector or a
6877 // scalar value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006878 SDValue InOp = Node->getOperand(0);
Duncan Sands13237ac2008-06-06 12:08:01 +00006879 if (!InOp.getValueType().isVector() ||
6880 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman0de76942007-06-29 00:09:08 +00006881 // The input is a scalar or single-element vector.
6882 // Lower to a store/load so that it can be split.
6883 // FIXME: this could be improved probably.
Mon P Wang97432f42008-07-15 05:28:34 +00006884 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
6885 Op.getValueType().getTypeForMVT());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006886 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Dan Gohman02c7c6c2008-07-11 22:44:52 +00006887 int FI = cast<FrameIndexSDNode>(Ptr.Val)->getIndex();
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006888
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006889 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006890 InOp, Ptr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00006891 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00006892 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman02c7c6c2008-07-11 22:44:52 +00006893 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006894 }
Dan Gohman0de76942007-06-29 00:09:08 +00006895 // Split the vector and convert each of the pieces now.
6896 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006897 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6898 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00006899 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006900 }
Chris Lattner32206f52006-03-18 01:44:44 +00006901 }
6902
6903 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006904 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006905 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006906 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006907}
6908
6909
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006910/// ScalarizeVectorOp - Given an operand of single-element vector type
6911/// (e.g. v1f32), convert it into the equivalent operation that returns a
6912/// scalar (e.g. f32) value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006913SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands13237ac2008-06-06 12:08:01 +00006914 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006915 SDNode *Node = Op.Val;
Duncan Sands13237ac2008-06-06 12:08:01 +00006916 MVT NewVT = Op.getValueType().getVectorElementType();
6917 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006918
Dan Gohmana8665142007-06-25 16:23:39 +00006919 // See if we already scalarized it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006920 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohmana8665142007-06-25 16:23:39 +00006921 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006922
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006923 SDValue Result;
Chris Lattner32206f52006-03-18 01:44:44 +00006924 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006925 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006926#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006927 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006928#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006929 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6930 case ISD::ADD:
6931 case ISD::FADD:
6932 case ISD::SUB:
6933 case ISD::FSUB:
6934 case ISD::MUL:
6935 case ISD::FMUL:
6936 case ISD::SDIV:
6937 case ISD::UDIV:
6938 case ISD::FDIV:
6939 case ISD::SREM:
6940 case ISD::UREM:
6941 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006942 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006943 case ISD::AND:
6944 case ISD::OR:
6945 case ISD::XOR:
6946 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006947 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006948 ScalarizeVectorOp(Node->getOperand(0)),
6949 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006950 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006951 case ISD::FNEG:
6952 case ISD::FABS:
6953 case ISD::FSQRT:
6954 case ISD::FSIN:
6955 case ISD::FCOS:
6956 Result = DAG.getNode(Node->getOpcode(),
6957 NewVT,
6958 ScalarizeVectorOp(Node->getOperand(0)));
6959 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006960 case ISD::FPOWI:
6961 Result = DAG.getNode(Node->getOpcode(),
6962 NewVT,
6963 ScalarizeVectorOp(Node->getOperand(0)),
6964 Node->getOperand(1));
6965 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006966 case ISD::LOAD: {
6967 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006968 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6969 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006970
Dan Gohmana8665142007-06-25 16:23:39 +00006971 const Value *SV = LD->getSrcValue();
6972 int SVOffset = LD->getSrcValueOffset();
6973 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6974 LD->isVolatile(), LD->getAlignment());
6975
Chris Lattner32206f52006-03-18 01:44:44 +00006976 // Remember that we legalized the chain.
6977 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6978 break;
6979 }
Dan Gohmana8665142007-06-25 16:23:39 +00006980 case ISD::BUILD_VECTOR:
6981 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006982 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006983 case ISD::INSERT_VECTOR_ELT:
6984 // Returning the inserted scalar element.
6985 Result = Node->getOperand(1);
6986 break;
6987 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006988 assert(Node->getOperand(0).getValueType() == NewVT &&
6989 "Concat of non-legal vectors not yet supported!");
6990 Result = Node->getOperand(0);
6991 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006992 case ISD::VECTOR_SHUFFLE: {
6993 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006994 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00006995 if (cast<ConstantSDNode>(EltNum)->getValue())
6996 Result = ScalarizeVectorOp(Node->getOperand(1));
6997 else
6998 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006999 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007000 }
7001 case ISD::EXTRACT_SUBVECTOR:
7002 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00007003 assert(Result.getValueType() == NewVT);
7004 break;
Evan Cheng9ac36312008-05-16 17:19:05 +00007005 case ISD::BIT_CONVERT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007006 SDValue Op0 = Op.getOperand(0);
Duncan Sands13237ac2008-06-06 12:08:01 +00007007 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng9ac36312008-05-16 17:19:05 +00007008 Op0 = ScalarizeVectorOp(Op0);
7009 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00007010 break;
Evan Cheng9ac36312008-05-16 17:19:05 +00007011 }
Dan Gohmana8665142007-06-25 16:23:39 +00007012 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00007013 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00007014 ScalarizeVectorOp(Op.getOperand(1)),
7015 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00007016 break;
Chris Lattner9d3740e2008-06-30 02:43:01 +00007017 case ISD::SELECT_CC:
7018 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7019 Node->getOperand(1),
7020 ScalarizeVectorOp(Op.getOperand(2)),
7021 ScalarizeVectorOp(Op.getOperand(3)),
7022 Node->getOperand(4));
7023 break;
Nate Begemanb87e63a2008-05-12 23:09:43 +00007024 case ISD::VSETCC: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007025 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7026 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begemanb87e63a2008-05-12 23:09:43 +00007027 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7028 Op.getOperand(2));
7029 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7030 DAG.getConstant(-1ULL, NewVT),
7031 DAG.getConstant(0ULL, NewVT));
7032 break;
7033 }
Chris Lattner32206f52006-03-18 01:44:44 +00007034 }
7035
7036 if (TLI.isTypeLegal(NewVT))
7037 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00007038 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7039 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00007040 return Result;
7041}
7042
Chris Lattnerdc750592005-01-07 07:47:09 +00007043
7044// SelectionDAG::Legalize - This is the entry point for the file.
7045//
Chris Lattner4add7e32005-01-23 04:42:50 +00007046void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00007047 /// run - This is the main entry point to this class.
7048 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00007049 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00007050}
7051