blob: e1ced4862ce97e38a84602d3967da3478cd5df1a [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000031#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000032#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000035#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000036#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000037#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000039#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000065 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000066
Chris Lattner6831a812006-02-13 09:18:02 +000067 /// IsLegalizingCall - This member is used *only* for purposes of providing
Scott Michelfdc40a02009-02-17 22:15:04 +000068 /// helpful assertions that a libcall isn't created while another call is
Chris Lattner6831a812006-02-13 09:18:02 +000069 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
Scott Michelfdc40a02009-02-17 22:15:04 +000071
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Scott Michelfdc40a02009-02-17 22:15:04 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Dan Gohman475871a2008-07-27 21:46:04 +000088 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000089 LegalizedNodes.insert(std::make_pair(From, To));
90 // If someone requests legalization of the new node, return itself.
91 if (From != To)
92 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000093 }
94
Chris Lattner3e928bb2005-01-07 07:47:09 +000095public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000096 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000101 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000102 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 }
104
105 /// isTypeLegal - Return true if this type is legal on this target.
106 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000107 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 return getTypeAction(VT) == Legal;
109 }
110
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 void LegalizeDAG();
112
Chris Lattner456a93a2006-01-28 07:39:30 +0000113private:
Dan Gohman7f321562007-06-25 16:23:39 +0000114 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000115 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000116 void HandleOp(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Chris Lattnerc7029802006-03-18 01:44:44 +0000118 /// LegalizeOp - We know that the specified value has a legal type.
119 /// Recursively ensure that the operands have legal types, then return the
120 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Nate Begeman68679912008-04-25 18:07:40 +0000123 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
124 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
125 /// is necessary to spill the vector being inserted into to memory, perform
126 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000128 SDValue Idx, DebugLoc dl);
129 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
130 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000131
Mon P Wangf007a8b2008-11-06 05:31:54 +0000132 /// Useful 16 element vector type that is used to pass operands for widening.
Scott Michelfdc40a02009-02-17 22:15:04 +0000133 typedef SmallVector<SDValue, 16> SDValueVector;
134
Nate Begeman5a5ca152009-04-29 05:20:52 +0000135 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
136 /// performs the same shuffe in terms of order or result bytes, but on a type
137 /// whose vector element type is narrower than the original shuffle type.
138 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
139 SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
140 SDValue N1, SDValue N2,
141 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000142
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000143 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000144 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000145
Dale Johannesenbb5da912009-02-02 20:41:04 +0000146 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
147 DebugLoc dl);
148 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
149 DebugLoc dl);
150 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
151 DebugLoc dl) {
152 LegalizeSetCCOperands(LHS, RHS, CC, dl);
153 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000154 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Eli Friedman47b41f72009-05-27 02:21:29 +0000156 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000157 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
158 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
159 RTLIB::Libcall Call_PPCF128);
160 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, RTLIB::Libcall Call_I16,
161 RTLIB::Libcall Call_I32, RTLIB::Libcall Call_I64,
162 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000163
Dale Johannesen8a782a22009-02-02 22:12:50 +0000164 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue ExpandBUILD_VECTOR(SDNode *Node);
166 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000167 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
168 DebugLoc dl);
169 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
170 DebugLoc dl);
171 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
172 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000173
Dale Johannesen8a782a22009-02-02 22:12:50 +0000174 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
175 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000176
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000177 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman8c377c72009-05-27 01:25:56 +0000178
179 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
180 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000181};
182}
183
Nate Begeman5a5ca152009-04-29 05:20:52 +0000184/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
185/// performs the same shuffe in terms of order or result bytes, but on a type
186/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000187/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000188SDValue
189SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
190 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000191 SmallVectorImpl<int> &Mask) const {
192 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000193 unsigned NumMaskElts = VT.getVectorNumElements();
194 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000196
Nate Begeman9008ca62009-04-27 18:41:29 +0000197 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
198
199 if (NumEltsGrowth == 1)
200 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
201
202 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000203 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000204 int Idx = Mask[i];
205 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
206 if (Idx < 0)
207 NewMask.push_back(-1);
208 else
209 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000210 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000211 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000212 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000213 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
214 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000215}
216
Bill Wendling5aa49772009-02-24 02:35:30 +0000217SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000218 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000219 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000220 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000221 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223}
224
Chris Lattner3e928bb2005-01-07 07:47:09 +0000225void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000226 LastCALLSEQ_END = DAG.getEntryNode();
227 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000228
Chris Lattnerab510a72005-10-02 17:49:46 +0000229 // The legalize process is inherently a bottom-up recursive process (users
230 // legalize their uses before themselves). Given infinite stack space, we
231 // could just start legalizing on the root and traverse the whole graph. In
232 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000233 // blocks. To avoid this problem, compute an ordering of the nodes where each
234 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000235 DAG.AssignTopologicalOrder();
236 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
237 E = prior(DAG.allnodes_end()); I != next(E); ++I)
238 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000239
240 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000241 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000242 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
243 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000244
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245 LegalizedNodes.clear();
246
247 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000248 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000249}
250
Chris Lattner6831a812006-02-13 09:18:02 +0000251
252/// FindCallEndFromCallStart - Given a chained node that is part of a call
253/// sequence, find the CALLSEQ_END node that terminates the call sequence.
254static SDNode *FindCallEndFromCallStart(SDNode *Node) {
255 if (Node->getOpcode() == ISD::CALLSEQ_END)
256 return Node;
257 if (Node->use_empty())
258 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000259
Chris Lattner6831a812006-02-13 09:18:02 +0000260 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000262 if (TheChain.getValueType() != MVT::Other) {
263 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000264 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000265 if (TheChain.getValueType() != MVT::Other) {
266 // Otherwise, hunt for it.
267 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
268 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000269 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000270 break;
271 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
273 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000274 if (TheChain.getValueType() != MVT::Other)
275 return 0;
276 }
277 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Chris Lattner6831a812006-02-13 09:18:02 +0000279 for (SDNode::use_iterator UI = Node->use_begin(),
280 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Chris Lattner6831a812006-02-13 09:18:02 +0000282 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000283 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000284 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
285 if (User->getOperand(i) == TheChain)
286 if (SDNode *Result = FindCallEndFromCallStart(User))
287 return Result;
288 }
289 return 0;
290}
291
Scott Michelfdc40a02009-02-17 22:15:04 +0000292/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000293/// sequence, find the CALLSEQ_START node that initiates the call sequence.
294static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
295 assert(Node && "Didn't find callseq_start for a call??");
296 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000297
Chris Lattner6831a812006-02-13 09:18:02 +0000298 assert(Node->getOperand(0).getValueType() == MVT::Other &&
299 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000300 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000301}
302
303/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000304/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000305/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000306///
307/// Keep track of the nodes we fine that actually do lead to Dest in
308/// NodesLeadingTo. This avoids retraversing them exponential number of times.
309///
310bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000311 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000312 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000313
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000314 // If we've already processed this node and it does lead to Dest, there is no
315 // need to reprocess it.
316 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000317
Chris Lattner6831a812006-02-13 09:18:02 +0000318 // If the first result of this node has been already legalized, then it cannot
319 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000320 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000321
Chris Lattner6831a812006-02-13 09:18:02 +0000322 // Okay, this node has not already been legalized. Check and legalize all
323 // operands. If none lead to Dest, then we can legalize this node.
324 bool OperandsLeadToDest = false;
325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
326 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000327 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000328
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000329 if (OperandsLeadToDest) {
330 NodesLeadingTo.insert(N);
331 return true;
332 }
Chris Lattner6831a812006-02-13 09:18:02 +0000333
334 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000335 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000336 return false;
337}
338
Mon P Wang0c397192008-10-30 08:01:45 +0000339/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000340/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000341void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000342 // Don't touch TargetConstants
343 if (Op.getOpcode() == ISD::TargetConstant)
344 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000345 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000346 // We should never see any illegal result types here.
347 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Eli Friedman74807f22009-05-26 08:55:52 +0000348 (void)LegalizeOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000349}
Chris Lattner6831a812006-02-13 09:18:02 +0000350
Evan Cheng9f877882006-12-13 20:57:08 +0000351/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
352/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000353static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000354 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000355 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000356 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000357
358 // If a FP immediate is precise when represented as a float and if the
359 // target can do an extending load from float to double, we put it into
360 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000361 // double. This shrinks FP constants and canonicalizes them for targets where
362 // an FP extending load is the same cost as a normal load (such as on the x87
363 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000364 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000365 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000366 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000367 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000368 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000369 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000370 }
371
Duncan Sands83ec4b62008-06-06 12:08:01 +0000372 MVT OrigVT = VT;
373 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000374 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000375 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000376 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
377 // Only do this if the target has a native EXTLOAD instruction from
378 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000379 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000380 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000381 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000382 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
383 VT = SVT;
384 Extend = true;
385 }
Evan Cheng00495212006-12-12 21:32:44 +0000386 }
387
Dan Gohman475871a2008-07-27 21:46:04 +0000388 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000389 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000390 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000391 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000392 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000393 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000394 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000395 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000396 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000397}
398
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000399/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
400static
Dan Gohman475871a2008-07-27 21:46:04 +0000401SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000402 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue Chain = ST->getChain();
404 SDValue Ptr = ST->getBasePtr();
405 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000406 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000407 int Alignment = ST->getAlignment();
408 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000409 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000410 if (ST->getMemoryVT().isFloatingPoint() ||
411 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000412 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
413 if (TLI.isTypeLegal(intVT)) {
414 // Expand to a bitconvert of the value to the integer type of the
415 // same size, then a (misaligned) int store.
416 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000417 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
418 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000419 SVOffset, ST->isVolatile(), Alignment);
420 } else {
421 // Do a (aligned) store to a stack slot, then copy from the stack slot
422 // to the final destination using (unaligned) integer loads and stores.
423 MVT StoredVT = ST->getMemoryVT();
424 MVT RegVT =
425 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
426 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
427 unsigned RegBytes = RegVT.getSizeInBits() / 8;
428 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000429
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000430 // Make sure the stack slot is also aligned for the register type.
431 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
432
433 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000434 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000435 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000436 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
437 SmallVector<SDValue, 8> Stores;
438 unsigned Offset = 0;
439
440 // Do all but one copies using the full register width.
441 for (unsigned i = 1; i < NumRegs; i++) {
442 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000443 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000444 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000445 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000446 ST->getSrcValue(), SVOffset + Offset,
447 ST->isVolatile(),
448 MinAlign(ST->getAlignment(), Offset)));
449 // Increment the pointers.
450 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000451 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000452 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000453 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000454 }
455
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000456 // The last store may be partial. Do a truncating store. On big-endian
457 // machines this requires an extending load from the stack slot to ensure
458 // that the bits are in the right place.
459 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000460
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000461 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000462 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000463 NULL, 0, MemVT);
464
Dale Johannesenbb5da912009-02-02 20:41:04 +0000465 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000466 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000467 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000468 MinAlign(ST->getAlignment(), Offset)));
469 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000470 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000471 Stores.size());
472 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000473 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000474 assert(ST->getMemoryVT().isInteger() &&
475 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000476 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000477 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000478 MVT NewStoredVT =
479 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
480 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000481 int IncrementSize = NumBits / 8;
482
483 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
485 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000486 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000487
488 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000489 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000490 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000491 ST->getSrcValue(), SVOffset, NewStoredVT,
492 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000493 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000494 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000495 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000496 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000497 ST->getSrcValue(), SVOffset + IncrementSize,
498 NewStoredVT, ST->isVolatile(), Alignment);
499
Dale Johannesenbb5da912009-02-02 20:41:04 +0000500 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501}
502
503/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
504static
Dan Gohman475871a2008-07-27 21:46:04 +0000505SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000506 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000507 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000508 SDValue Chain = LD->getChain();
509 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000510 MVT VT = LD->getValueType(0);
511 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000513 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000514 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
515 if (TLI.isTypeLegal(intVT)) {
516 // Expand to a (misaligned) integer load of the same size,
517 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000519 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000520 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000522 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000523 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000524
Duncan Sands05e11fa2008-12-12 21:47:02 +0000525 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000526 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000527 } else {
528 // Copy the value to a (aligned) stack slot using (unaligned) integer
529 // loads and stores, then do a (aligned) load from the stack slot.
530 MVT RegVT = TLI.getRegisterType(intVT);
531 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
532 unsigned RegBytes = RegVT.getSizeInBits() / 8;
533 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
534
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000535 // Make sure the stack slot is also aligned for the register type.
536 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
537
Duncan Sands05e11fa2008-12-12 21:47:02 +0000538 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
539 SmallVector<SDValue, 8> Stores;
540 SDValue StackPtr = StackBase;
541 unsigned Offset = 0;
542
543 // Do all but one copies using the full register width.
544 for (unsigned i = 1; i < NumRegs; i++) {
545 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000546 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000547 SVOffset + Offset, LD->isVolatile(),
548 MinAlign(LD->getAlignment(), Offset));
549 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000550 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000551 NULL, 0));
552 // Increment the pointers.
553 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000554 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
555 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000556 Increment);
557 }
558
559 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000560 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000561 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000562 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000563 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000564 MinAlign(LD->getAlignment(), Offset));
565 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000566 // On big-endian machines this requires a truncating store to ensure
567 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000568 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000569 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000570
571 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000572 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000573 Stores.size());
574
575 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000576 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000577 NULL, 0, LoadedVT);
578
579 // Callers expect a MERGE_VALUES node.
580 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000581 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000582 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000583 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000584 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000585 "Unaligned load of unsupported type.");
586
Dale Johannesen8155d642008-02-27 22:36:00 +0000587 // Compute the new VT that is half the size of the old one. This is an
588 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000589 unsigned NumBits = LoadedVT.getSizeInBits();
590 MVT NewLoadedVT;
591 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000592 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000593
Chris Lattnere400af82007-11-19 21:38:03 +0000594 unsigned Alignment = LD->getAlignment();
595 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000596 ISD::LoadExtType HiExtType = LD->getExtensionType();
597
598 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
599 if (HiExtType == ISD::NON_EXTLOAD)
600 HiExtType = ISD::ZEXTLOAD;
601
602 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000603 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000604 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000605 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000606 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000607 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000608 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000609 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000610 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000611 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000612 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000613 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000614 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000615 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000616 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000617 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000619 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000620 }
621
622 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000624 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
625 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000626
Dale Johannesenbb5da912009-02-02 20:41:04 +0000627 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000628 Hi.getValue(1));
629
Dan Gohman475871a2008-07-27 21:46:04 +0000630 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000631 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632}
Evan Cheng912095b2007-01-04 21:56:39 +0000633
Nate Begeman68679912008-04-25 18:07:40 +0000634/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
635/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
636/// is necessary to spill the vector being inserted into to memory, perform
637/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000638SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000639PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
640 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000641 SDValue Tmp1 = Vec;
642 SDValue Tmp2 = Val;
643 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000644
Nate Begeman68679912008-04-25 18:07:40 +0000645 // If the target doesn't support this, we have to spill the input vector
646 // to a temporary stack slot, update the element, then reload it. This is
647 // badness. We could also load the value into a vector register (either
648 // with a "move to register" or "extload into register" instruction, then
649 // permute it into place, if the idx is a constant and if the idx is
650 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000651 MVT VT = Tmp1.getValueType();
652 MVT EltVT = VT.getVectorElementType();
653 MVT IdxVT = Tmp3.getValueType();
654 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000655 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000656
Gabor Greifba36cb52008-08-28 21:40:38 +0000657 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000658
659 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000660 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000661 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000662
663 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000664 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000665 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000666 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000667 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000668 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
669 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000670 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000671 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000672 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000673 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000674 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000675 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000676}
677
Mon P Wange9f10152008-12-09 05:46:39 +0000678
Eli Friedman3f727d62009-05-27 02:16:40 +0000679SDValue SelectionDAGLegalize::
680ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
681 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
682 // SCALAR_TO_VECTOR requires that the type of the value being inserted
683 // match the element type of the vector being created, except for
684 // integers in which case the inserted value can be over width.
685 MVT EltVT = Vec.getValueType().getVectorElementType();
686 if (Val.getValueType() == EltVT ||
687 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
688 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
689 Vec.getValueType(), Val);
690
691 unsigned NumElts = Vec.getValueType().getVectorNumElements();
692 // We generate a shuffle of InVec and ScVec, so the shuffle mask
693 // should be 0,1,2,3,4,5... with the appropriate element replaced with
694 // elt 0 of the RHS.
695 SmallVector<int, 8> ShufOps;
696 for (unsigned i = 0; i != NumElts; ++i)
697 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
698
699 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
700 &ShufOps[0]);
701 }
702 }
703 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
704}
705
Dan Gohmana3466152007-07-13 20:14:11 +0000706/// LegalizeOp - We know that the specified value has a legal type, and
707/// that its operands are legal. Now ensure that the operation itself
708/// is legal, recursively ensuring that the operands' operations remain
709/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000710SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000711 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
712 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000713
Gabor Greifba36cb52008-08-28 21:40:38 +0000714 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000715 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000716
Eli Friedman1fde9c52009-05-24 02:46:31 +0000717 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
718 assert(getTypeAction(Node->getValueType(i)) == Legal &&
719 "Unexpected illegal type!");
720
721 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
722 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
723 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
724 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725
Chris Lattner45982da2005-05-12 16:53:42 +0000726 // Note that LegalizeOp may be reentered even from single-use nodes, which
727 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000728 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000729 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000730
Dan Gohman475871a2008-07-27 21:46:04 +0000731 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
732 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000733 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000734
Eli Friedman8c377c72009-05-27 01:25:56 +0000735 // Figure out the correct action; the way to query this varies by opcode
736 TargetLowering::LegalizeAction Action;
737 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000738 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000739 case ISD::INTRINSIC_W_CHAIN:
740 case ISD::INTRINSIC_WO_CHAIN:
741 case ISD::INTRINSIC_VOID:
742 case ISD::VAARG:
743 case ISD::STACKSAVE:
744 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
745 break;
746 case ISD::SINT_TO_FP:
747 case ISD::UINT_TO_FP:
748 case ISD::EXTRACT_VECTOR_ELT:
749 Action = TLI.getOperationAction(Node->getOpcode(),
750 Node->getOperand(0).getValueType());
751 break;
752 case ISD::FP_ROUND_INREG:
753 case ISD::SIGN_EXTEND_INREG: {
754 MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
755 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
756 break;
757 }
758 case ISD::LOAD:
759 case ISD::STORE:
760 case ISD::BR_CC:
761 case ISD::FORMAL_ARGUMENTS:
762 case ISD::CALL:
763 case ISD::CALLSEQ_START:
764 case ISD::CALLSEQ_END:
765 case ISD::SELECT_CC:
766 case ISD::SETCC:
Eli Friedman8c377c72009-05-27 01:25:56 +0000767 // These instructions have properties that aren't modeled in the
768 // generic codepath
769 SimpleFinishLegalizing = false;
770 break;
771 case ISD::EXTRACT_ELEMENT:
772 case ISD::FLT_ROUNDS_:
773 case ISD::SADDO:
774 case ISD::SSUBO:
775 case ISD::UADDO:
776 case ISD::USUBO:
777 case ISD::SMULO:
778 case ISD::UMULO:
779 case ISD::FPOWI:
780 case ISD::MERGE_VALUES:
781 case ISD::EH_RETURN:
782 case ISD::FRAME_TO_ARGS_OFFSET:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000783 // These operations lie about being legal: when they claim to be legal,
784 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000785 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
786 if (Action == TargetLowering::Legal)
787 Action = TargetLowering::Expand;
788 break;
789 case ISD::TRAMPOLINE:
790 case ISD::FRAMEADDR:
791 case ISD::RETURNADDR:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000792 // These operations lie about being legal: they must always be
793 // custom-lowered.
Eli Friedman8c377c72009-05-27 01:25:56 +0000794 Action = TargetLowering::Custom;
795 break;
796 case ISD::BUILD_VECTOR:
797 // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize
798 // its operands first!
799 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000800 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000801 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000802 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000803 Action = TargetLowering::Legal;
804 } else {
805 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000806 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000807 break;
808 }
809
810 if (SimpleFinishLegalizing) {
811 SmallVector<SDValue, 8> Ops, ResultVals;
812 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
813 Ops.push_back(LegalizeOp(Node->getOperand(i)));
814 switch (Node->getOpcode()) {
815 default: break;
816 case ISD::BR:
817 case ISD::BRIND:
818 case ISD::BR_JT:
819 case ISD::BR_CC:
820 case ISD::BRCOND:
821 case ISD::RET:
822 // Branches tweak the chain to include LastCALLSEQ_END
823 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
824 LastCALLSEQ_END);
825 Ops[0] = LegalizeOp(Ops[0]);
826 LastCALLSEQ_END = DAG.getEntryNode();
827 break;
828 case ISD::SHL:
829 case ISD::SRL:
830 case ISD::SRA:
831 case ISD::ROTL:
832 case ISD::ROTR:
833 // Legalizing shifts/rotates requires adjusting the shift amount
834 // to the appropriate width.
835 if (!Ops[1].getValueType().isVector())
836 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
837 break;
838 }
839
840 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
841 Ops.size());
842 switch (Action) {
843 case TargetLowering::Legal:
844 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
845 ResultVals.push_back(Result.getValue(i));
846 break;
847 case TargetLowering::Custom:
848 // FIXME: The handling for custom lowering with multiple results is
849 // a complete mess.
850 Tmp1 = TLI.LowerOperation(Result, DAG);
851 if (Tmp1.getNode()) {
852 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
853 if (e == 1)
854 ResultVals.push_back(Tmp1);
855 else
856 ResultVals.push_back(Tmp1.getValue(i));
857 }
858 break;
859 }
860
861 // FALL THROUGH
862 case TargetLowering::Expand:
863 ExpandNode(Result.getNode(), ResultVals);
864 break;
865 case TargetLowering::Promote:
866 PromoteNode(Result.getNode(), ResultVals);
867 break;
868 }
869 if (!ResultVals.empty()) {
870 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
871 if (ResultVals[i] != SDValue(Node, i))
872 ResultVals[i] = LegalizeOp(ResultVals[i]);
873 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
874 }
875 return ResultVals[Op.getResNo()];
876 }
877 }
878
879 switch (Node->getOpcode()) {
880 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000881#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000882 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000883#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000884 assert(0 && "Do not know how to legalize this operator!");
885 abort();
Dan Gohman7f460202008-06-30 20:59:49 +0000886 case ISD::DBG_STOPPOINT:
887 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000889
Dan Gohman7f460202008-06-30 20:59:49 +0000890 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000891 case TargetLowering::Promote:
892 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000893 case TargetLowering::Expand: {
894 DwarfWriter *DW = DAG.getDwarfWriter();
895 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
896 MVT::Other);
897 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Bill Wendling92c1e122009-02-13 02:16:35 +0000899 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
900 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
901 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
902 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000903
Bill Wendling92c1e122009-02-13 02:16:35 +0000904 unsigned Line = DSP->getLine();
905 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000906
Bill Wendling98a366d2009-04-29 23:29:43 +0000907 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000908 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
909 // won't hurt anything.
910 if (useDEBUG_LOC) {
911 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +0000912 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000913 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +0000914 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
915 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000916 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +0000917 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
918 }
Bill Wendling92c1e122009-02-13 02:16:35 +0000919 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000920 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +0000921 }
922 } else {
923 Result = Tmp1; // chain
924 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000925 break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000926 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000927 case TargetLowering::Custom:
928 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000929 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000930 break;
Evan Cheng71e86852008-07-08 20:06:39 +0000931 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +0000932 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +0000933 break;
934
Dan Gohman475871a2008-07-27 21:46:04 +0000935 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +0000936 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +0000937 Ops.push_back(Node->getOperand(1)); // line # must be legal.
938 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +0000939 Ops.push_back(Node->getOperand(3)); // filename must be legal.
940 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
941 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000942 break;
943 }
Evan Cheng71e86852008-07-08 20:06:39 +0000944 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000945 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000946 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000947 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000948 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000949 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +0000951 // A call within a calling sequence must be legalized to something
952 // other than the normal CALLSEQ_END. Violating this gets Legalize
953 // into an infinite loop.
954 assert ((!IsLegalizingCall ||
955 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000956 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +0000957 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000958
959 // The number of incoming and outgoing values should match; unless the final
960 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +0000961 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
962 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
963 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000964 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +0000965 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000966
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000967 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000968 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +0000969 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
970 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000971 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +0000972 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000973 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +0000974 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +0000975 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +0000976 }
977 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +0000978 case ISD::BUILD_VECTOR:
979 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000980 default: assert(0 && "This action is not supported yet!");
981 case TargetLowering::Custom:
982 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000983 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000984 Result = Tmp3;
985 break;
986 }
987 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000988 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +0000990 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000991 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000992 break;
Nate Begeman9008ca62009-04-27 18:41:29 +0000993 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +0000994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
995 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +0000996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
997 MVT VT = Result.getValueType();
998
Nate Begeman5a5ca152009-04-29 05:20:52 +0000999 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001000 SmallVector<int, 8> Mask;
1001 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001002
1003 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001004 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001005 default: assert(0 && "Unknown operation action!");
1006 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001007 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001008 "vector shuffle should not be created if not legal!");
1009 break;
1010 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001011 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001012 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001013 Result = Tmp3;
1014 break;
1015 }
1016 // FALLTHROUGH
1017 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001018 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001019 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001020 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001021 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001022 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001023 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001024 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001025 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001026 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001027 if (Idx < NumElems)
1028 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1029 DAG.getIntPtrConstant(Idx)));
1030 else
1031 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1032 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001033 }
Evan Chenga87008d2009-02-25 22:49:59 +00001034 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001035 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001036 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001037 case TargetLowering::Promote: {
1038 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001039 MVT OVT = Node->getValueType(0);
1040 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001041
1042 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001043 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1044 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Chris Lattner4352cc92006-04-04 17:23:26 +00001046 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001047 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001048 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001049 break;
1050 }
Chris Lattner87100e02006-03-20 01:52:29 +00001051 }
1052 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001053 }
Mon P Wang0c397192008-10-30 08:01:45 +00001054 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001055 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001056 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001057 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1058 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1059 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1060
1061 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1062 Node->getValueType(0))) {
1063 default: assert(0 && "Unknown operation action!");
1064 case TargetLowering::Legal:
1065 break;
1066 case TargetLowering::Custom:
1067 Tmp3 = TLI.LowerOperation(Result, DAG);
1068 if (Tmp3.getNode()) {
1069 Result = Tmp3;
1070 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001071 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001072 // FALLTHROUGH
1073 case TargetLowering::Expand: {
1074 // Use extract/insert/build vector for now. We might try to be
1075 // more clever later.
1076 MVT PtrVT = TLI.getPointerTy();
1077 SmallVector<SDValue, 8> Ops;
1078 unsigned NumOperands = Node->getNumOperands();
1079 for (unsigned i=0; i < NumOperands; ++i) {
1080 SDValue SubOp = Node->getOperand(i);
1081 MVT VVT = SubOp.getNode()->getValueType(0);
1082 MVT EltVT = VVT.getVectorElementType();
1083 unsigned NumSubElem = VVT.getVectorNumElements();
1084 for (unsigned j=0; j < NumSubElem; ++j) {
1085 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1086 DAG.getConstant(j, PtrVT)));
1087 }
1088 }
1089 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1090 Node->getValueType(0),
1091 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001092 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001093 }
1094 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001095 }
1096
Chris Lattner6831a812006-02-13 09:18:02 +00001097 case ISD::CALLSEQ_START: {
1098 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001099
Chris Lattner6831a812006-02-13 09:18:02 +00001100 // Recursively Legalize all of the inputs of the call end that do not lead
1101 // to this call start. This ensures that any libcalls that need be inserted
1102 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001103 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001104 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001105 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001106 NodesLeadingTo);
1107 }
Chris Lattner6831a812006-02-13 09:18:02 +00001108
1109 // Now that we legalized all of the inputs (which may have inserted
1110 // libcalls) create the new CALLSEQ_START node.
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112
1113 // Merge in the last call, to ensure that this call start after the last
1114 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001115 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001116 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001117 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001118 Tmp1 = LegalizeOp(Tmp1);
1119 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001120
Chris Lattner6831a812006-02-13 09:18:02 +00001121 // Do not try to legalize the target-specific arguments (#1+).
1122 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001123 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001124 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001125 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001127
Chris Lattner6831a812006-02-13 09:18:02 +00001128 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001129 AddLegalizedOperand(Op.getValue(0), Result);
1130 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1131 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001132
Chris Lattner6831a812006-02-13 09:18:02 +00001133 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001134 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001135 // process, no libcalls can/will be inserted, guaranteeing that no calls
1136 // can overlap.
1137 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001138 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001139 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001140 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001141
Chris Lattner6831a812006-02-13 09:18:02 +00001142 // Legalize the call, starting from the CALLSEQ_END.
1143 LegalizeOp(LastCALLSEQ_END);
1144 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1145 return Result;
1146 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001147 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001148 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1149 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001150 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001151 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1152 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001153 assert(I != LegalizedNodes.end() &&
1154 "Legalizing the call start should have legalized this node!");
1155 return I->second;
1156 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001157
1158 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001159 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001160 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001161 // Do not try to legalize the target-specific arguments (#1+), except for
1162 // an optional flag input.
1163 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1164 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001166 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001167 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001168 }
1169 } else {
1170 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1171 if (Tmp1 != Node->getOperand(0) ||
1172 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001174 Ops[0] = Tmp1;
1175 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001177 }
Chris Lattner6a542892006-01-24 05:48:21 +00001178 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001179 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001180 // This finishes up call legalization.
1181 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001182
Chris Lattner4b653a02006-02-14 00:55:02 +00001183 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001184 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001185 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001186 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001187 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001188 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001189 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1191 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1192 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001194
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001195 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001196 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001197 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001198 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001199 case TargetLowering::Expand: {
1200 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1201 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1202 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001204
1205 // Chain the dynamic stack allocation so that it doesn't modify the stack
1206 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001207 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001208
Dan Gohman475871a2008-07-27 21:46:04 +00001209 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001210 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001211 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001212 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001213 unsigned StackAlign =
1214 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1215 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001216 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001217 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001218 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001219 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001220
Dale Johannesenca57b842009-02-02 23:46:53 +00001221 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001222 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001223
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001224 Tmp1 = LegalizeOp(Tmp1);
1225 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001226 break;
1227 }
1228 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001229 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001231 Tmp1 = LegalizeOp(Tmp3);
1232 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001233 }
Chris Lattner903d2782006-01-15 08:54:32 +00001234 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001235 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001236 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001237 }
Chris Lattner903d2782006-01-15 08:54:32 +00001238 // Since this op produce two values, make sure to remember that we
1239 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001240 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1241 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001242 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001243 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001244 case ISD::BR_JT:
1245 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1246 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001247 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001248 Tmp1 = LegalizeOp(Tmp1);
1249 LastCALLSEQ_END = DAG.getEntryNode();
1250
1251 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1252 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1253
Scott Michelfdc40a02009-02-17 22:15:04 +00001254 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001255 default: assert(0 && "This action is not supported yet!");
1256 case TargetLowering::Legal: break;
1257 case TargetLowering::Custom:
1258 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001259 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001260 break;
1261 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue Chain = Result.getOperand(0);
1263 SDValue Table = Result.getOperand(1);
1264 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001265
Duncan Sands83ec4b62008-06-06 12:08:01 +00001266 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001267 MachineFunction &MF = DAG.getMachineFunction();
1268 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001269 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001270 Index, DAG.getConstant(EntrySize, PTy));
1271 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001272
Duncan Sands712f7b32008-12-12 08:13:38 +00001273 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001274 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001275 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001276 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001277 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001278 // For PIC, the sequence is:
1279 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001280 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001281 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001282 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001283 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001284 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001285 }
1286 }
1287 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001288 case ISD::BR_CC:
1289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001290 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001291 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001292 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001293 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001294 Tmp3 = Node->getOperand(3); // RHS
1295 Tmp4 = Node->getOperand(1); // CC
1296
Scott Michelfdc40a02009-02-17 22:15:04 +00001297 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001298 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001299 LastCALLSEQ_END = DAG.getEntryNode();
1300
Evan Cheng7f042682008-10-15 02:05:31 +00001301 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001302 // the LHS is a legal SETCC itself. In this case, we need to compare
1303 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001305 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1306 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
1309 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001310 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Chris Lattner181b7a32005-12-17 23:46:46 +00001312 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1313 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001314 case TargetLowering::Legal: break;
1315 case TargetLowering::Custom:
1316 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001317 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001318 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001319 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001320 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001321 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001322 LoadSDNode *LD = cast<LoadSDNode>(Node);
1323 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1324 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001325
Evan Cheng466685d2006-10-09 20:57:25 +00001326 ISD::LoadExtType ExtType = LD->getExtensionType();
1327 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001328 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1330 Tmp3 = Result.getValue(0);
1331 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001332
Evan Cheng466685d2006-10-09 20:57:25 +00001333 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1334 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001335 case TargetLowering::Legal:
1336 // If this is an unaligned load and the target doesn't support it,
1337 // expand it.
1338 if (!TLI.allowsUnalignedMemoryAccesses()) {
1339 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001340 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001341 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001343 TLI);
1344 Tmp3 = Result.getOperand(0);
1345 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001346 Tmp3 = LegalizeOp(Tmp3);
1347 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001348 }
1349 }
1350 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001351 case TargetLowering::Custom:
1352 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001353 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001354 Tmp3 = LegalizeOp(Tmp1);
1355 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001356 }
Evan Cheng466685d2006-10-09 20:57:25 +00001357 break;
1358 case TargetLowering::Promote: {
1359 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001360 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001361 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001362 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001363
Dale Johannesenca57b842009-02-02 23:46:53 +00001364 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001365 LD->getSrcValueOffset(),
1366 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001367 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001368 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001369 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001370 }
Evan Cheng466685d2006-10-09 20:57:25 +00001371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001372 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001373 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001374 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1375 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001376 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001377 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001378 MVT SrcVT = LD->getMemoryVT();
1379 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001380 int SVOffset = LD->getSrcValueOffset();
1381 unsigned Alignment = LD->getAlignment();
1382 bool isVolatile = LD->isVolatile();
1383
Duncan Sands83ec4b62008-06-06 12:08:01 +00001384 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001385 // Some targets pretend to have an i1 loading operation, and actually
1386 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1387 // bits are guaranteed to be zero; it helps the optimizers understand
1388 // that these bits are zero. It is also useful for EXTLOAD, since it
1389 // tells the optimizers that those bits are undefined. It would be
1390 // nice to have an effective generic way of getting these benefits...
1391 // Until such a way is found, don't insist on promoting i1 here.
1392 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001393 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001394 // Promote to a byte-sized load if not loading an integral number of
1395 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001396 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1397 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001398 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001399
1400 // The extra bits are guaranteed to be zero, since we stored them that
1401 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1402
1403 ISD::LoadExtType NewExtType =
1404 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1405
Dale Johannesenca57b842009-02-02 23:46:53 +00001406 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001407 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1408 NVT, isVolatile, Alignment);
1409
1410 Ch = Result.getValue(1); // The chain.
1411
1412 if (ExtType == ISD::SEXTLOAD)
1413 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001414 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001415 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001416 Result, DAG.getValueType(SrcVT));
1417 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1418 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001419 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001420 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001421 DAG.getValueType(SrcVT));
1422
1423 Tmp1 = LegalizeOp(Result);
1424 Tmp2 = LegalizeOp(Ch);
1425 } else if (SrcWidth & (SrcWidth - 1)) {
1426 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001427 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001428 "Unsupported extload!");
1429 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1430 assert(RoundWidth < SrcWidth);
1431 unsigned ExtraWidth = SrcWidth - RoundWidth;
1432 assert(ExtraWidth < RoundWidth);
1433 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1434 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001435 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1436 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001437 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001438 unsigned IncrementSize;
1439
1440 if (TLI.isLittleEndian()) {
1441 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1442 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001443 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1444 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001445 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1446 Alignment);
1447
1448 // Load the remaining ExtraWidth bits.
1449 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001450 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001451 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001452 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001453 LD->getSrcValue(), SVOffset + IncrementSize,
1454 ExtraVT, isVolatile,
1455 MinAlign(Alignment, IncrementSize));
1456
1457 // Build a factor node to remember that this load is independent of the
1458 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001459 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001460 Hi.getValue(1));
1461
1462 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001463 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001464 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1465
1466 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001467 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001468 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001469 // Big endian - avoid unaligned loads.
1470 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1471 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001472 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001473 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1474 Alignment);
1475
1476 // Load the remaining ExtraWidth bits.
1477 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001478 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001479 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001480 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001481 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001482 LD->getSrcValue(), SVOffset + IncrementSize,
1483 ExtraVT, isVolatile,
1484 MinAlign(Alignment, IncrementSize));
1485
1486 // Build a factor node to remember that this load is independent of the
1487 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001488 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001489 Hi.getValue(1));
1490
1491 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001492 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001493 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1494
1495 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001496 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001497 }
1498
1499 Tmp1 = LegalizeOp(Result);
1500 Tmp2 = LegalizeOp(Ch);
1501 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001502 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001503 default: assert(0 && "This action is not supported yet!");
1504 case TargetLowering::Custom:
1505 isCustom = true;
1506 // FALLTHROUGH
1507 case TargetLowering::Legal:
1508 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1509 Tmp1 = Result.getValue(0);
1510 Tmp2 = Result.getValue(1);
1511
1512 if (isCustom) {
1513 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001514 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001515 Tmp1 = LegalizeOp(Tmp3);
1516 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1517 }
1518 } else {
1519 // If this is an unaligned load and the target doesn't support it,
1520 // expand it.
1521 if (!TLI.allowsUnalignedMemoryAccesses()) {
1522 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001523 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001524 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001525 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001526 TLI);
1527 Tmp1 = Result.getOperand(0);
1528 Tmp2 = Result.getOperand(1);
1529 Tmp1 = LegalizeOp(Tmp1);
1530 Tmp2 = LegalizeOp(Tmp2);
1531 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001532 }
1533 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001534 break;
1535 case TargetLowering::Expand:
1536 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1537 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001538 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001539 LD->getSrcValueOffset(),
1540 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001541 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001542 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001543 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1544 Tmp2 = LegalizeOp(Load.getValue(1));
1545 break;
1546 }
1547 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1548 // Turn the unsupported load into an EXTLOAD followed by an explicit
1549 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001550 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001551 Tmp1, Tmp2, LD->getSrcValue(),
1552 LD->getSrcValueOffset(), SrcVT,
1553 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001554 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001555 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001556 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1557 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001558 Result, DAG.getValueType(SrcVT));
1559 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001560 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001561 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1562 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001563 break;
1564 }
Evan Cheng466685d2006-10-09 20:57:25 +00001565 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001566
Evan Cheng466685d2006-10-09 20:57:25 +00001567 // Since loads produce two values, make sure to remember that we legalized
1568 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001569 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1570 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001571 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001572 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001573 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001574 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001575 StoreSDNode *ST = cast<StoreSDNode>(Node);
1576 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1577 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001578 int SVOffset = ST->getSrcValueOffset();
1579 unsigned Alignment = ST->getAlignment();
1580 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001581
Evan Cheng8b2794a2006-10-13 21:14:26 +00001582 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001583 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1584 // FIXME: We shouldn't do this for TargetConstantFP's.
1585 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1586 // to phase ordering between legalized code and the dag combiner. This
1587 // probably means that we need to integrate dag combiner and legalizer
1588 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001589 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001590 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001591 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001592 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001593 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001594 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001595 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001596 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001597 SVOffset, isVolatile, Alignment);
1598 break;
1599 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001600 // If this target supports 64-bit registers, do a single 64-bit store.
1601 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001602 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001603 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001604 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001605 SVOffset, isVolatile, Alignment);
1606 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001607 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001608 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1609 // stores. If the target supports neither 32- nor 64-bits, this
1610 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001611 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001612 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1613 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001614 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001615
Dale Johannesenca57b842009-02-02 23:46:53 +00001616 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001617 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001618 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001619 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001620 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001621 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001622
Dale Johannesenca57b842009-02-02 23:46:53 +00001623 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001624 break;
1625 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001626 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001627 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001628
Eli Friedman957bffa2009-05-24 08:42:01 +00001629 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001630 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001631 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001632 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001633
Duncan Sands83ec4b62008-06-06 12:08:01 +00001634 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001635 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1636 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001637 case TargetLowering::Legal:
1638 // If this is an unaligned store and the target doesn't support it,
1639 // expand it.
1640 if (!TLI.allowsUnalignedMemoryAccesses()) {
1641 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001642 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001643 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001644 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001645 TLI);
1646 }
1647 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001648 case TargetLowering::Custom:
1649 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001650 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001651 break;
1652 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001653 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001654 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001655 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001656 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001657 ST->getSrcValue(), SVOffset, isVolatile,
1658 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001659 break;
1660 }
1661 break;
1662 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001663 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001664 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001665
Duncan Sands83ec4b62008-06-06 12:08:01 +00001666 MVT StVT = ST->getMemoryVT();
1667 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001668
Duncan Sands83ec4b62008-06-06 12:08:01 +00001669 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001670 // Promote to a byte-sized store with upper bits zero if not
1671 // storing an integral number of bytes. For example, promote
1672 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001673 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001674 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1675 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001676 SVOffset, NVT, isVolatile, Alignment);
1677 } else if (StWidth & (StWidth - 1)) {
1678 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001679 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001680 "Unsupported truncstore!");
1681 unsigned RoundWidth = 1 << Log2_32(StWidth);
1682 assert(RoundWidth < StWidth);
1683 unsigned ExtraWidth = StWidth - RoundWidth;
1684 assert(ExtraWidth < RoundWidth);
1685 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1686 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001687 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1688 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001689 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001690 unsigned IncrementSize;
1691
1692 if (TLI.isLittleEndian()) {
1693 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1694 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001695 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001696 SVOffset, RoundVT,
1697 isVolatile, Alignment);
1698
1699 // Store the remaining ExtraWidth bits.
1700 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001701 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001702 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001703 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001704 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001705 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001706 SVOffset + IncrementSize, ExtraVT, isVolatile,
1707 MinAlign(Alignment, IncrementSize));
1708 } else {
1709 // Big endian - avoid unaligned stores.
1710 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1711 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001712 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001713 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001714 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1715 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001716
1717 // Store the remaining ExtraWidth bits.
1718 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001719 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001720 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001721 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001722 SVOffset + IncrementSize, ExtraVT, isVolatile,
1723 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001724 }
Duncan Sands7e857202008-01-22 07:17:34 +00001725
1726 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001727 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001728 } else {
1729 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1730 Tmp2 != ST->getBasePtr())
1731 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1732 ST->getOffset());
1733
1734 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1735 default: assert(0 && "This action is not supported yet!");
1736 case TargetLowering::Legal:
1737 // If this is an unaligned store and the target doesn't support it,
1738 // expand it.
1739 if (!TLI.allowsUnalignedMemoryAccesses()) {
1740 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001741 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001742 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001743 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001744 TLI);
1745 }
1746 break;
1747 case TargetLowering::Custom:
1748 Result = TLI.LowerOperation(Result, DAG);
1749 break;
1750 case Expand:
1751 // TRUNCSTORE:i16 i32 -> STORE i16
1752 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001753 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1754 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1755 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001756 break;
1757 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001758 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001759 }
1760 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001761 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001762 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00001763 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001764 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001765 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001766
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001768
Nate Begemanb942a3d2005-08-23 04:29:48 +00001769 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001770 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001771 case TargetLowering::Legal: break;
1772 case TargetLowering::Custom: {
1773 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001774 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001775 break;
1776 }
Nate Begeman9373a812005-08-10 20:51:12 +00001777 case TargetLowering::Expand:
1778 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001779 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00001780 Tmp2, Tmp3,
1781 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1782 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001783 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00001784 DAG.getConstant(0, Tmp1.getValueType()),
1785 Tmp2, Tmp3, ISD::SETNE);
1786 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001787 break;
1788 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001789 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001790 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1791 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001792 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001793 ExtOp = ISD::BIT_CONVERT;
1794 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001795 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001796 ExtOp = ISD::ANY_EXTEND;
1797 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001798 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001799 ExtOp = ISD::FP_EXTEND;
1800 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001801 }
1802 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00001803 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
1804 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001805 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001806 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00001807 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00001808 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00001809 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001810 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00001811 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001812 break;
1813 }
1814 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001815 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001816 case ISD::SELECT_CC: {
1817 Tmp1 = Node->getOperand(0); // LHS
1818 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001819 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1820 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00001821 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00001822
1823 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001824 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001825
Evan Cheng7f042682008-10-15 02:05:31 +00001826 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001827 // the LHS is a legal SETCC itself. In this case, we need to compare
1828 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001829 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001830 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1831 CC = DAG.getCondCode(ISD::SETNE);
1832 }
1833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1834
1835 // Everything is legal, see if we should expand this op or something.
1836 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1837 default: assert(0 && "This action is not supported yet!");
1838 case TargetLowering::Legal: break;
1839 case TargetLowering::Custom:
1840 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001841 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001842 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001843 }
1844 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001845 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001846 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001847 Tmp1 = Node->getOperand(0);
1848 Tmp2 = Node->getOperand(1);
1849 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00001850 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001851
1852 // If we had to Expand the SetCC operands into a SELECT node, then it may
1853 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00001854 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00001855 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001856 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001857 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001858 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001859
Chris Lattner73e142f2006-01-30 22:43:50 +00001860 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001861 default: assert(0 && "Cannot handle this action for SETCC yet!");
1862 case TargetLowering::Custom:
1863 isCustom = true;
1864 // FALLTHROUGH.
1865 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00001866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001867 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00001868 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001869 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00001870 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001871 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001872 case TargetLowering::Promote: {
1873 // First step, figure out the appropriate operation to use.
1874 // Allow SETCC to not be supported for all legal data types
1875 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00001876 MVT NewInTy = Node->getOperand(0).getValueType();
1877 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00001878
1879 // Scan for the appropriate larger type to use.
1880 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001881 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00001882
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00001884 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001885 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00001886 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00001887
Andrew Lenharthae355752005-11-30 17:12:26 +00001888 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001889 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001890 break;
1891 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001892 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00001893 assert(0 && "Cannot promote Legal Integer SETCC yet");
1894 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00001895 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
1896 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00001897 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001898 Tmp1 = LegalizeOp(Tmp1);
1899 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00001900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00001901 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001902 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001903 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001904 case TargetLowering::Expand:
1905 // Expand a setcc node into a select_cc of the same condition, lhs, and
1906 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00001907 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001908 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00001909 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00001910 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001911 break;
1912 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001913 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001914
1915 // Binary operators
Chris Lattnera09f8482006-03-05 05:09:38 +00001916 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1917 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00001918 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00001919
Chris Lattnera09f8482006-03-05 05:09:38 +00001920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Chris Lattnera09f8482006-03-05 05:09:38 +00001922 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1923 default: assert(0 && "Operation not supported");
1924 case TargetLowering::Custom:
1925 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001926 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001927 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001928 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00001929 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00001930 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00001931 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00001932 "Ugly special-cased code!");
1933 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00001934 SDValue SignBit;
1935 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
1936 if (isTypeLegal(IVT)) {
1937 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
1938 } else {
1939 assert(isTypeLegal(TLI.getPointerTy()) &&
1940 (TLI.getPointerTy() == MVT::i32 ||
1941 TLI.getPointerTy() == MVT::i64) &&
1942 "Legal type for load?!");
1943 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
1944 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
1945 SDValue Ch =
1946 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
1947 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
1948 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
1949 LoadPtr, DAG.getIntPtrConstant(4));
1950 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
1951 Ch, LoadPtr, NULL, 0, MVT::i32);
1952 }
1953 SignBit =
1954 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1955 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1956 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00001957 // Get the absolute value of the result.
1958 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1959 // Select between the nabs and abs value based on the sign bit of
1960 // the input.
1961 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1962 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
1963 AbsVal),
1964 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00001965 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00001966 break;
1967 }
Evan Cheng912095b2007-01-04 21:56:39 +00001968 }
Chris Lattnera09f8482006-03-05 05:09:38 +00001969 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001970 case ISD::VAARG: {
1971 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1972 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1973
Duncan Sands83ec4b62008-06-06 12:08:01 +00001974 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001975 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1976 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001977 case TargetLowering::Custom:
1978 isCustom = true;
1979 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001980 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001981 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1982 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001983 Tmp1 = Result.getValue(1);
1984
1985 if (isCustom) {
1986 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001987 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001988 Result = LegalizeOp(Tmp2);
1989 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1990 }
1991 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001992 break;
1993 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00001994 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00001995 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001996 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00001997 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00001998 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00001999 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002000 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00002001 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00002002 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002003 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00002004 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002005 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002006 Result = LegalizeOp(Result);
2007 break;
2008 }
2009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002010 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00002011 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002012 AddLegalizedOperand(SDValue(Node, 0), Result);
2013 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002014 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002015 }
Bill Wendling74c37652008-12-09 22:08:41 +00002016 case ISD::SADDO:
2017 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002018 MVT VT = Node->getValueType(0);
2019 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2020 default: assert(0 && "This action not supported for this op yet!");
2021 case TargetLowering::Custom:
2022 Result = TLI.LowerOperation(Op, DAG);
2023 if (Result.getNode()) break;
2024 // FALLTHROUGH
2025 case TargetLowering::Legal: {
2026 SDValue LHS = LegalizeOp(Node->getOperand(0));
2027 SDValue RHS = LegalizeOp(Node->getOperand(1));
2028
Scott Michelfdc40a02009-02-17 22:15:04 +00002029 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002030 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002031 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002032 MVT OType = Node->getValueType(1);
2033
Bill Wendlinga6af91a2008-11-25 08:19:22 +00002034 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002035
Bill Wendling740464e2008-11-25 19:40:17 +00002036 // LHSSign -> LHS >= 0
2037 // RHSSign -> RHS >= 0
2038 // SumSign -> Sum >= 0
2039 //
Bill Wendling74c37652008-12-09 22:08:41 +00002040 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00002041 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00002042 // Sub:
2043 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00002044 //
Dale Johannesenca57b842009-02-02 23:46:53 +00002045 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2046 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00002047 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2048 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002049 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002050
Dale Johannesenca57b842009-02-02 23:46:53 +00002051 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2052 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002053
Dale Johannesenca57b842009-02-02 23:46:53 +00002054 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002055
2056 MVT ValueVTs[] = { LHS.getValueType(), OType };
2057 SDValue Ops[] = { Sum, Cmp };
2058
Scott Michelfdc40a02009-02-17 22:15:04 +00002059 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002060 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002061 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002062 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002063 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002064 break;
2065 }
2066 }
2067
2068 break;
2069 }
Bill Wendling74c37652008-12-09 22:08:41 +00002070 case ISD::UADDO:
2071 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00002072 MVT VT = Node->getValueType(0);
2073 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2074 default: assert(0 && "This action not supported for this op yet!");
2075 case TargetLowering::Custom:
2076 Result = TLI.LowerOperation(Op, DAG);
2077 if (Result.getNode()) break;
2078 // FALLTHROUGH
2079 case TargetLowering::Legal: {
2080 SDValue LHS = LegalizeOp(Node->getOperand(0));
2081 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002082
Bill Wendling74c37652008-12-09 22:08:41 +00002083 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002084 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002085 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002086 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00002087 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00002088 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002089 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002090
Bill Wendling41ea7e72008-11-24 19:21:46 +00002091 MVT ValueVTs[] = { LHS.getValueType(), OType };
2092 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002093
Scott Michelfdc40a02009-02-17 22:15:04 +00002094 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002095 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002096 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002097 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002098 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002099 break;
2100 }
2101 }
2102
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002103 break;
2104 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002106
Chris Lattner4ddd2832006-04-08 04:13:17 +00002107 assert(Result.getValueType() == Op.getValueType() &&
2108 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002109
Chris Lattner456a93a2006-01-28 07:39:30 +00002110 // Make sure that the generated code is itself legal.
2111 if (Result != Op)
2112 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002113
Chris Lattner45982da2005-05-12 16:53:42 +00002114 // Note that LegalizeOp may be reentered even from single-use nodes, which
2115 // means that we always must cache transformed nodes.
2116 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002117 return Result;
2118}
2119
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002120SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
2121 SDValue Vec = Op.getOperand(0);
2122 SDValue Idx = Op.getOperand(1);
2123 DebugLoc dl = Op.getDebugLoc();
2124 // Store the value to a temporary stack slot, then LOAD the returned part.
2125 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
2126 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
2127
2128 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00002129 unsigned EltSize =
2130 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002131 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
2132 DAG.getConstant(EltSize, Idx.getValueType()));
2133
2134 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
2135 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
2136 else
2137 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
2138
2139 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
2140
2141 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
2142}
2143
Nate Begeman750ac1b2006-02-01 07:19:44 +00002144/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2145/// with condition CC on the current target. This usually involves legalizing
2146/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2147/// there may be no choice but to create a new SetCC node to represent the
2148/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00002149/// LHS, and the SDValue returned in RHS has a nil SDNode value.
2150void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
2151 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002152 SDValue &CC,
2153 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00002154 LHS = LegalizeOp(LHS);
2155 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002156}
2157
Evan Cheng7f042682008-10-15 02:05:31 +00002158/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
2159/// condition code CC on the current target. This routine assumes LHS and rHS
2160/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
2161/// illegal condition code into AND / OR of multiple SETCC values.
2162void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
2163 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002164 SDValue &CC,
2165 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00002166 MVT OpVT = LHS.getValueType();
2167 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2168 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
2169 default: assert(0 && "Unknown condition code action!");
2170 case TargetLowering::Legal:
2171 // Nothing to do.
2172 break;
2173 case TargetLowering::Expand: {
2174 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
2175 unsigned Opc = 0;
2176 switch (CCCode) {
2177 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00002178 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
2179 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2180 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2181 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2182 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2183 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2184 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2185 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2186 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2187 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2188 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2189 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00002190 // FIXME: Implement more expansions.
2191 }
2192
Dale Johannesenbb5da912009-02-02 20:41:04 +00002193 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
2194 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
2195 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00002196 RHS = SDValue();
2197 CC = SDValue();
2198 break;
2199 }
2200 }
2201}
2202
Chris Lattner1401d152008-01-16 07:45:30 +00002203/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2204/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2205/// a load from the stack slot to DestVT, extending it if needed.
2206/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002207SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2208 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002209 MVT DestVT,
2210 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002211 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002212 unsigned SrcAlign =
2213 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2214 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002215 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002216
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002217 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002218 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002219 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2220
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2222 unsigned SlotSize = SlotVT.getSizeInBits();
2223 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002224 unsigned DestAlign =
2225 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002226
Chris Lattner1401d152008-01-16 07:45:30 +00002227 // Emit a store to the stack slot. Use a truncstore if the input value is
2228 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002229 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002230
Chris Lattner1401d152008-01-16 07:45:30 +00002231 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002232 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002233 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002234 else {
2235 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002236 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002237 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002239
Chris Lattner35481892005-12-23 00:16:34 +00002240 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002241 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002242 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002243
Chris Lattner1401d152008-01-16 07:45:30 +00002244 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002245 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002246 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002247}
2248
Dan Gohman475871a2008-07-27 21:46:04 +00002249SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002250 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002251 // Create a vector sized/aligned stack slot, store the value to element #0,
2252 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002253 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002254
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002255 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002256 int SPFI = StackPtrFI->getIndex();
2257
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002258 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2259 StackPtr,
2260 PseudoSourceValue::getFixedStack(SPFI), 0,
2261 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002262 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002263 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002264}
2265
2266
Chris Lattnerce872152006-03-19 06:31:19 +00002267/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002268/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002269SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002270 unsigned NumElems = Node->getNumOperands();
2271 SDValue SplatValue = Node->getOperand(0);
2272 DebugLoc dl = Node->getDebugLoc();
2273 MVT VT = Node->getValueType(0);
2274 MVT OpVT = SplatValue.getValueType();
2275 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
2277 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002278 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002279 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002280
Dan Gohman475871a2008-07-27 21:46:04 +00002281 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002282 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002283 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002284 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002285 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002286 bool isConstant = true;
2287 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2288 SplatValue.getOpcode() != ISD::UNDEF)
2289 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Evan Cheng033e6812006-03-24 01:17:21 +00002291 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002292 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002293 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002294 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002295 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002296 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002297 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002298
2299 // If this isn't a constant element or an undef, we can't use a constant
2300 // pool load.
2301 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2302 V.getOpcode() != ISD::UNDEF)
2303 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002304 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002305
Chris Lattnerce872152006-03-19 06:31:19 +00002306 if (isOnlyLowElement) {
2307 // If the low element is an undef too, then this whole things is an undef.
2308 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002309 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00002310 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002311 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00002312 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002313
Chris Lattner2eb86532006-03-24 07:29:17 +00002314 // If all elements are constants, create a load from the constant pool.
2315 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002316 std::vector<Constant*> CV;
2317 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002318 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002319 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002320 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002321 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002322 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002323 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00002324 } else {
2325 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002326 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00002327 CV.push_back(UndefValue::get(OpNTy));
2328 }
2329 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002330 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002332 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002333 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002334 PseudoSourceValue::getConstantPool(), 0,
2335 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002337
Gabor Greifba36cb52008-08-28 21:40:38 +00002338 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00002339 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00002340 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00002341
2342 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00002343 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002344 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00002345 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002346 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002347
Chris Lattner87100e02006-03-20 01:52:29 +00002348 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00002349 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
2350 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00002351 }
2352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Evan Cheng033e6812006-03-24 01:17:21 +00002354 // If there are only two unique elements, we may be able to turn this into a
2355 // vector shuffle.
2356 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002357 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00002358 SDValue Val1 = Node->getOperand(1);
2359 SDValue Val2;
2360 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002361 if (MI->first != Val1)
2362 Val2 = MI->first;
2363 else
2364 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00002365
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002366 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002367 // vector shuffle has the undef vector on the RHS.
2368 if (Val1.getOpcode() == ISD::UNDEF)
2369 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002370
Evan Cheng033e6812006-03-24 01:17:21 +00002371 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00002372 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002373
2374 // Set elements of the shuffle mask for Val1.
2375 std::vector<unsigned> &Val1Elts = Values[Val1];
2376 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002377 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002378
2379 // Set elements of the shuffle mask for Val2.
2380 std::vector<unsigned> &Val2Elts = Values[Val2];
2381 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
2382 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00002383 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00002384
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002385 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002386 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00002387 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002388 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
2389 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00002390 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00002391 }
2392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002393
Chris Lattnerce872152006-03-19 06:31:19 +00002394 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
2395 // aligned object on the stack, store each element into it, then load
2396 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00002397 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00002398 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00002399 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
2400 const Value *SV = PseudoSourceValue::getFixedStack(FI);
2401
Chris Lattnerce872152006-03-19 06:31:19 +00002402 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00002403 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002404 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00002405 // Store (in the right endianness) the elements to memory.
2406 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2407 // Ignore undef elements.
2408 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00002409
Chris Lattner841c8822006-03-22 01:46:54 +00002410 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00002411
Dan Gohman475871a2008-07-27 21:46:04 +00002412 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002413 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00002414
Dale Johannesen8a782a22009-02-02 22:12:50 +00002415 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
2416 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00002417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002418
Dan Gohman475871a2008-07-27 21:46:04 +00002419 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00002420 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00002421 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002422 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00002423 else
2424 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002425
Chris Lattnerce872152006-03-19 06:31:19 +00002426 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00002427 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00002428}
2429
Chris Lattner77e77a62005-01-21 06:05:23 +00002430// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2431// does not fit into a register, return the lo part and set the hi part to the
2432// by-reg argument. If it does fit into a single register, return the result
2433// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002434SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002435 bool isSigned) {
Chris Lattner6831a812006-02-13 09:18:02 +00002436 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002437 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002438 // Legalizing the call will automatically add the previous call to the
2439 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002440 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002441
Chris Lattner77e77a62005-01-21 06:05:23 +00002442 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002443 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002444 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002445 MVT ArgVT = Node->getOperand(i).getValueType();
2446 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00002447 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002448 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002449 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002450 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002451 }
Bill Wendling056292f2008-09-16 21:48:12 +00002452 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002453 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002454
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002455 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002456 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002457 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002458 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002459 CallingConv::C, false, Callee, Args, DAG,
2460 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002461
Chris Lattner6831a812006-02-13 09:18:02 +00002462 // Legalize the call sequence, starting with the chain. This will advance
2463 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2464 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2465 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002466 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002467}
2468
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002469SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2470 RTLIB::Libcall Call_F32,
2471 RTLIB::Libcall Call_F64,
2472 RTLIB::Libcall Call_F80,
2473 RTLIB::Libcall Call_PPCF128) {
2474 RTLIB::Libcall LC;
2475 switch (Node->getValueType(0).getSimpleVT()) {
2476 default: assert(0 && "Unexpected request for libcall!");
2477 case MVT::f32: LC = Call_F32; break;
2478 case MVT::f64: LC = Call_F64; break;
2479 case MVT::f80: LC = Call_F80; break;
2480 case MVT::ppcf128: LC = Call_PPCF128; break;
2481 }
2482 return ExpandLibCall(LC, Node, false);
2483}
2484
2485SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2486 RTLIB::Libcall Call_I16,
2487 RTLIB::Libcall Call_I32,
2488 RTLIB::Libcall Call_I64,
2489 RTLIB::Libcall Call_I128) {
2490 RTLIB::Libcall LC;
2491 switch (Node->getValueType(0).getSimpleVT()) {
2492 default: assert(0 && "Unexpected request for libcall!");
2493 case MVT::i16: LC = Call_I16; break;
2494 case MVT::i32: LC = Call_I32; break;
2495 case MVT::i64: LC = Call_I64; break;
2496 case MVT::i128: LC = Call_I128; break;
2497 }
2498 return ExpandLibCall(LC, Node, isSigned);
2499}
2500
Chris Lattner22cde6a2006-01-28 08:25:58 +00002501/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2502/// INT_TO_FP operation of the specified operand when the target requests that
2503/// we expand it. At this point, we know that the result and operand types are
2504/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002505SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2506 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00002507 MVT DestVT,
2508 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002509 if (Op0.getValueType() == MVT::i32) {
2510 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002511
Chris Lattner23594d42008-01-16 07:03:22 +00002512 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00002513 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002514
Chris Lattner22cde6a2006-01-28 08:25:58 +00002515 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002516 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002517 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002518 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002519 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002520 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002521 if (TLI.isLittleEndian())
2522 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002523
Chris Lattner22cde6a2006-01-28 08:25:58 +00002524 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002525 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002526 if (isSigned) {
2527 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00002528 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00002529 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002530 } else {
2531 Op0Mapped = Op0;
2532 }
2533 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002534 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002535 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002536 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00002537 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002538 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00002539 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002540 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00002541 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002542 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002543 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002544 BitsToDouble(0x4330000080000000ULL) :
2545 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002546 MVT::f64);
2547 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00002548 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002549 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002550 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002551 // handle final rounding
2552 if (DestVT == MVT::f64) {
2553 // do nothing
2554 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00002555 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002556 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002557 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002558 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002559 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002560 }
2561 return Result;
2562 }
2563 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00002564 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002565
Dale Johannesenaf435272009-02-02 19:03:57 +00002566 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002567 Op0, DAG.getConstant(0, Op0.getValueType()),
2568 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00002569 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00002570 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002571 SignSet, Four, Zero);
2572
2573 // If the sign bit of the integer is set, the large number will be treated
2574 // as a negative number. To counteract this, the dynamic code adds an
2575 // offset depending on the data type.
2576 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002577 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002578 default: assert(0 && "Unsupported integer type!");
2579 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2580 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2581 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2582 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2583 }
2584 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00002585 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002586
Dan Gohman475871a2008-07-27 21:46:04 +00002587 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002588 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00002589 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00002590 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00002591 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002592 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00002593 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002594 PseudoSourceValue::getConstantPool(), 0,
2595 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002596 else {
Dan Gohman69de1932008-02-06 22:27:42 +00002597 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00002598 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00002599 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00002600 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00002601 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002602 }
2603
Dale Johannesenaf435272009-02-02 19:03:57 +00002604 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002605}
2606
2607/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2608/// *INT_TO_FP operation of the specified operand when the target requests that
2609/// we promote it. At this point, we know that the result and operand types are
2610/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2611/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002612SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
2613 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002614 bool isSigned,
2615 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002616 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002617 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002618
2619 unsigned OpToUse = 0;
2620
2621 // Scan for the appropriate larger type to use.
2622 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
2624 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002625
2626 // If the target supports SINT_TO_FP of this type, use it.
2627 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
2628 default: break;
2629 case TargetLowering::Legal:
2630 if (!TLI.isTypeLegal(NewInTy))
2631 break; // Can't use this datatype.
2632 // FALL THROUGH.
2633 case TargetLowering::Custom:
2634 OpToUse = ISD::SINT_TO_FP;
2635 break;
2636 }
2637 if (OpToUse) break;
2638 if (isSigned) continue;
2639
2640 // If the target supports UINT_TO_FP of this type, use it.
2641 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
2642 default: break;
2643 case TargetLowering::Legal:
2644 if (!TLI.isTypeLegal(NewInTy))
2645 break; // Can't use this datatype.
2646 // FALL THROUGH.
2647 case TargetLowering::Custom:
2648 OpToUse = ISD::UINT_TO_FP;
2649 break;
2650 }
2651 if (OpToUse) break;
2652
2653 // Otherwise, try a larger type.
2654 }
2655
2656 // Okay, we found the operation and type to use. Zero extend our input to the
2657 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002658 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002659 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002660 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002661}
2662
2663/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2664/// FP_TO_*INT operation of the specified operand when the target requests that
2665/// we promote it. At this point, we know that the result and operand types are
2666/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2667/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002668SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
2669 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002670 bool isSigned,
2671 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002672 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002673 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002674
2675 unsigned OpToUse = 0;
2676
2677 // Scan for the appropriate larger type to use.
2678 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
2680 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002681
2682 // If the target supports FP_TO_SINT returning this type, use it.
2683 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
2684 default: break;
2685 case TargetLowering::Legal:
2686 if (!TLI.isTypeLegal(NewOutTy))
2687 break; // Can't use this datatype.
2688 // FALL THROUGH.
2689 case TargetLowering::Custom:
2690 OpToUse = ISD::FP_TO_SINT;
2691 break;
2692 }
2693 if (OpToUse) break;
2694
2695 // If the target supports FP_TO_UINT of this type, use it.
2696 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
2697 default: break;
2698 case TargetLowering::Legal:
2699 if (!TLI.isTypeLegal(NewOutTy))
2700 break; // Can't use this datatype.
2701 // FALL THROUGH.
2702 case TargetLowering::Custom:
2703 OpToUse = ISD::FP_TO_UINT;
2704 break;
2705 }
2706 if (OpToUse) break;
2707
2708 // Otherwise, try a larger type.
2709 }
2710
Scott Michelfdc40a02009-02-17 22:15:04 +00002711
Chris Lattner27a6c732007-11-24 07:07:01 +00002712 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002713 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002714
Chris Lattner27a6c732007-11-24 07:07:01 +00002715 // If the operation produces an invalid type, it must be custom lowered. Use
2716 // the target lowering hooks to expand it. Just keep the low part of the
2717 // expanded operation, we know that we're truncating anyway.
2718 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00002719 SmallVector<SDValue, 2> Results;
2720 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
2721 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
2722 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00002723 }
Duncan Sands126d9072008-07-04 11:47:58 +00002724
Chris Lattner27a6c732007-11-24 07:07:01 +00002725 // Truncate the result of the extended FP_TO_*INT operation to the desired
2726 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002727 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002728}
2729
2730/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2731///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002732SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002733 MVT VT = Op.getValueType();
2734 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002735 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002736 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002737 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2738 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002739 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2740 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2741 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002742 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002743 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2744 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2745 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2746 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2747 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2748 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2749 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2750 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2751 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002752 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002753 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2754 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2755 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2756 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2757 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2758 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2759 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2760 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2761 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2762 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2763 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2764 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2765 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2766 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2767 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2768 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2769 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2770 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2771 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2772 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2773 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002774 }
2775}
2776
2777/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2778///
Scott Michelfdc40a02009-02-17 22:15:04 +00002779SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002780 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002781 switch (Opc) {
2782 default: assert(0 && "Cannot expand this yet!");
2783 case ISD::CTPOP: {
2784 static const uint64_t mask[6] = {
2785 0x5555555555555555ULL, 0x3333333333333333ULL,
2786 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2787 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2788 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00002789 MVT VT = Op.getValueType();
2790 MVT ShVT = TLI.getShiftAmountTy();
2791 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002792 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2793 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002794 unsigned EltSize = VT.isVector() ?
2795 VT.getVectorElementType().getSizeInBits() : len;
2796 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002797 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002798 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00002799 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002800 DAG.getNode(ISD::AND, dl, VT,
2801 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
2802 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002803 }
2804 return Op;
2805 }
2806 case ISD::CTLZ: {
2807 // for now, we do this:
2808 // x = x | (x >> 1);
2809 // x = x | (x >> 2);
2810 // ...
2811 // x = x | (x >>16);
2812 // x = x | (x >>32); // for 64-bit input
2813 // return popcount(~x);
2814 //
2815 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00002816 MVT VT = Op.getValueType();
2817 MVT ShVT = TLI.getShiftAmountTy();
2818 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002819 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002820 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002821 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002822 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002823 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002824 Op = DAG.getNOT(dl, Op, VT);
2825 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002826 }
2827 case ISD::CTTZ: {
2828 // for now, we use: { return popcount(~x & (x - 1)); }
2829 // unless the target has ctlz but not ctpop, in which case we use:
2830 // { return 32 - nlz(~x & (x-1)); }
2831 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00002832 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002833 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2834 DAG.getNOT(dl, Op, VT),
2835 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002836 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002837 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002838 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2839 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002840 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002841 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002842 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2843 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002844 }
2845 }
2846}
Chris Lattnere34b3962005-01-19 04:19:40 +00002847
Eli Friedman8c377c72009-05-27 01:25:56 +00002848void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2849 SmallVectorImpl<SDValue> &Results) {
2850 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002851 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00002852 switch (Node->getOpcode()) {
2853 case ISD::CTPOP:
2854 case ISD::CTLZ:
2855 case ISD::CTTZ:
2856 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2857 Results.push_back(Tmp1);
2858 break;
2859 case ISD::BSWAP:
2860 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
2861 break;
2862 case ISD::FRAMEADDR:
2863 case ISD::RETURNADDR:
2864 case ISD::FRAME_TO_ARGS_OFFSET:
2865 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2866 break;
2867 case ISD::FLT_ROUNDS_:
2868 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2869 break;
2870 case ISD::EH_RETURN:
2871 case ISD::DECLARE:
2872 case ISD::DBG_LABEL:
2873 case ISD::EH_LABEL:
2874 case ISD::PREFETCH:
2875 case ISD::MEMBARRIER:
2876 case ISD::VAEND:
2877 Results.push_back(Node->getOperand(0));
2878 break;
2879 case ISD::MERGE_VALUES:
2880 for (unsigned i = 0; i < Node->getNumValues(); i++)
2881 Results.push_back(Node->getOperand(i));
2882 break;
2883 case ISD::UNDEF: {
2884 MVT VT = Node->getValueType(0);
2885 if (VT.isInteger())
2886 Results.push_back(DAG.getConstant(0, VT));
2887 else if (VT.isFloatingPoint())
2888 Results.push_back(DAG.getConstantFP(0, VT));
2889 else
2890 assert(0 && "Unknown value type!");
2891 break;
2892 }
2893 case ISD::TRAP: {
2894 // If this operation is not supported, lower it to 'abort()' call
2895 TargetLowering::ArgListTy Args;
2896 std::pair<SDValue, SDValue> CallResult =
2897 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
2898 false, false, false, false, CallingConv::C, false,
2899 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
2900 Args, DAG, dl);
2901 Results.push_back(CallResult.second);
2902 break;
2903 }
2904 case ISD::FP_ROUND:
2905 case ISD::BIT_CONVERT:
2906 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2907 Node->getValueType(0), dl);
2908 Results.push_back(Tmp1);
2909 break;
2910 case ISD::FP_EXTEND:
2911 Tmp1 = EmitStackConvert(Node->getOperand(0),
2912 Node->getOperand(0).getValueType(),
2913 Node->getValueType(0), dl);
2914 Results.push_back(Tmp1);
2915 break;
2916 case ISD::SIGN_EXTEND_INREG: {
2917 // NOTE: we could fall back on load/store here too for targets without
2918 // SAR. However, it is doubtful that any exist.
2919 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2920 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
2921 ExtraVT.getSizeInBits();
2922 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
2923 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2924 Node->getOperand(0), ShiftCst);
2925 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2926 Results.push_back(Tmp1);
2927 break;
2928 }
2929 case ISD::FP_ROUND_INREG: {
2930 // The only way we can lower this is to turn it into a TRUNCSTORE,
2931 // EXTLOAD pair, targetting a temporary location (a stack slot).
2932
2933 // NOTE: there is a choice here between constantly creating new stack
2934 // slots and always reusing the same one. We currently always create
2935 // new ones, as reuse may inhibit scheduling.
2936 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2937 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2938 Node->getValueType(0), dl);
2939 Results.push_back(Tmp1);
2940 break;
2941 }
2942 case ISD::SINT_TO_FP:
2943 case ISD::UINT_TO_FP:
2944 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2945 Node->getOperand(0), Node->getValueType(0), dl);
2946 Results.push_back(Tmp1);
2947 break;
2948 case ISD::FP_TO_UINT: {
2949 SDValue True, False;
2950 MVT VT = Node->getOperand(0).getValueType();
2951 MVT NVT = Node->getValueType(0);
2952 const uint64_t zero[] = {0, 0};
2953 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
2954 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2955 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2956 Tmp1 = DAG.getConstantFP(apf, VT);
2957 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2958 Node->getOperand(0),
2959 Tmp1, ISD::SETLT);
2960 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
2961 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2962 DAG.getNode(ISD::FSUB, dl, VT,
2963 Node->getOperand(0), Tmp1));
2964 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2965 DAG.getConstant(x, NVT));
2966 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2967 Results.push_back(Tmp1);
2968 break;
2969 }
2970 case ISD::VACOPY: {
2971 // This defaults to loading a pointer from the input and storing it to the
2972 // output, returning the chain.
2973 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2974 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2975 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
2976 Node->getOperand(2), VS, 0);
2977 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
2978 Results.push_back(Tmp1);
2979 break;
2980 }
2981 case ISD::EXTRACT_VECTOR_ELT:
2982 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2983 // This must be an access of the only element. Return it.
2984 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
2985 Node->getOperand(0));
2986 else
2987 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2988 Results.push_back(Tmp1);
2989 break;
2990 case ISD::EXTRACT_SUBVECTOR:
2991 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
2992 break;
2993 case ISD::SCALAR_TO_VECTOR:
2994 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
2995 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002996 case ISD::INSERT_VECTOR_ELT:
2997 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2998 Node->getOperand(1),
2999 Node->getOperand(2), dl));
3000 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003001 case ISD::EXTRACT_ELEMENT: {
3002 MVT OpTy = Node->getOperand(0).getValueType();
3003 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3004 // 1 -> Hi
3005 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3006 DAG.getConstant(OpTy.getSizeInBits()/2,
3007 TLI.getShiftAmountTy()));
3008 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3009 } else {
3010 // 0 -> Lo
3011 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3012 Node->getOperand(0));
3013 }
3014 Results.push_back(Tmp1);
3015 break;
3016 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003017 case ISD::STACKSAVE:
3018 // Expand to CopyFromReg if the target set
3019 // StackPointerRegisterToSaveRestore.
3020 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3021 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3022 Node->getValueType(0)));
3023 Results.push_back(Results[0].getValue(1));
3024 } else {
3025 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3026 Results.push_back(Node->getOperand(0));
3027 }
3028 break;
3029 case ISD::STACKRESTORE:
3030 // Expand to CopyToReg if the target set
3031 // StackPointerRegisterToSaveRestore.
3032 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3033 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3034 Node->getOperand(1)));
3035 } else {
3036 Results.push_back(Node->getOperand(0));
3037 }
3038 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003039 case ISD::FNEG:
3040 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3041 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3042 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3043 Node->getOperand(0));
3044 Results.push_back(Tmp1);
3045 break;
3046 case ISD::FABS: {
3047 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3048 MVT VT = Node->getValueType(0);
3049 Tmp1 = Node->getOperand(0);
3050 Tmp2 = DAG.getConstantFP(0.0, VT);
3051 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3052 Tmp1, Tmp2, ISD::SETUGT);
3053 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3054 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
3055 Results.push_back(Tmp1);
3056 break;
3057 }
3058 case ISD::FSQRT:
3059 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3060 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
3061 break;
3062 case ISD::FSIN:
3063 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3064 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
3065 break;
3066 case ISD::FCOS:
3067 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3068 RTLIB::COS_F80, RTLIB::COS_PPCF128));
3069 break;
3070 case ISD::FLOG:
3071 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3072 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
3073 break;
3074 case ISD::FLOG2:
3075 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3076 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
3077 break;
3078 case ISD::FLOG10:
3079 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3080 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
3081 break;
3082 case ISD::FEXP:
3083 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3084 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
3085 break;
3086 case ISD::FEXP2:
3087 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3088 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
3089 break;
3090 case ISD::FTRUNC:
3091 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3092 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
3093 break;
3094 case ISD::FFLOOR:
3095 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3096 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
3097 break;
3098 case ISD::FCEIL:
3099 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3100 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
3101 break;
3102 case ISD::FRINT:
3103 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3104 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
3105 break;
3106 case ISD::FNEARBYINT:
3107 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3108 RTLIB::NEARBYINT_F64,
3109 RTLIB::NEARBYINT_F80,
3110 RTLIB::NEARBYINT_PPCF128));
3111 break;
3112 case ISD::FPOWI:
3113 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3114 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
3115 break;
3116 case ISD::FPOW:
3117 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3118 RTLIB::POW_F80, RTLIB::POW_PPCF128));
3119 break;
3120 case ISD::FDIV:
3121 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3122 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
3123 break;
3124 case ISD::FREM:
3125 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3126 RTLIB::REM_F80, RTLIB::REM_PPCF128));
3127 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003128 case ISD::ConstantFP: {
3129 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
3130 // Check to see if this FP immediate is already legal.
3131 bool isLegal = false;
3132 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
3133 E = TLI.legal_fpimm_end(); I != E; ++I) {
3134 if (CFP->isExactlyValue(*I)) {
3135 isLegal = true;
3136 break;
3137 }
3138 }
3139 // If this is a legal constant, turn it into a TargetConstantFP node.
3140 if (isLegal)
3141 Results.push_back(SDValue(Node, 0));
3142 else
3143 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
3144 break;
3145 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003146 case ISD::EHSELECTION: {
3147 unsigned Reg = TLI.getExceptionSelectorRegister();
3148 assert(Reg && "Can't expand to unknown register!");
3149 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3150 Node->getValueType(0)));
3151 Results.push_back(Results[0].getValue(1));
3152 break;
3153 }
3154 case ISD::EXCEPTIONADDR: {
3155 unsigned Reg = TLI.getExceptionAddressRegister();
3156 assert(Reg && "Can't expand to unknown register!");
3157 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3158 Node->getValueType(0)));
3159 Results.push_back(Results[0].getValue(1));
3160 break;
3161 }
3162 case ISD::SUB: {
3163 MVT VT = Node->getValueType(0);
3164 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3165 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3166 "Don't know how to expand this subtraction!");
3167 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3168 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
3169 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3170 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3171 break;
3172 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003173 case ISD::UREM:
3174 case ISD::SREM: {
Eli Friedman26ea8f92009-05-27 07:05:37 +00003175 MVT VT = Node->getValueType(0);
3176 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003177 bool isSigned = Node->getOpcode() == ISD::SREM;
3178 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3179 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3180 Tmp2 = Node->getOperand(0);
3181 Tmp3 = Node->getOperand(1);
3182 if (TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
3183 // X % Y -> X-X/Y*Y
3184 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3185 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3186 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
3187 } else if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3188 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3189 } else if (isSigned) {
3190 Tmp1 = ExpandIntLibCall(Node, true, RTLIB::SREM_I16, RTLIB::SREM_I32,
3191 RTLIB::SREM_I64, RTLIB::SREM_I128);
3192 } else {
Eli Friedman26ea8f92009-05-27 07:05:37 +00003193 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::UREM_I16, RTLIB::UREM_I32,
3194 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003195 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003196 Results.push_back(Tmp1);
3197 break;
3198 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003199 case ISD::UDIV:
3200 case ISD::SDIV: {
3201 bool isSigned = Node->getOpcode() == ISD::SDIV;
3202 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003203 MVT VT = Node->getValueType(0);
3204 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003205 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3206 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3207 Node->getOperand(1));
3208 else if (isSigned)
Eli Friedman26ea8f92009-05-27 07:05:37 +00003209 Tmp1 = ExpandIntLibCall(Node, true, RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3210 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003211 else
3212 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3213 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003214 Results.push_back(Tmp1);
3215 break;
3216 }
3217 case ISD::MULHU:
3218 case ISD::MULHS: {
3219 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3220 ISD::SMUL_LOHI;
3221 MVT VT = Node->getValueType(0);
3222 SDVTList VTs = DAG.getVTList(VT, VT);
3223 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3224 "If this wasn't legal, it shouldn't have been created!");
3225 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3226 Node->getOperand(1));
3227 Results.push_back(Tmp1.getValue(1));
3228 break;
3229 }
3230 case ISD::MUL: {
3231 MVT VT = Node->getValueType(0);
3232 SDVTList VTs = DAG.getVTList(VT, VT);
3233 // See if multiply or divide can be lowered using two-result operations.
3234 // We just need the low half of the multiply; try both the signed
3235 // and unsigned forms. If the target supports both SMUL_LOHI and
3236 // UMUL_LOHI, form a preference by checking which forms of plain
3237 // MULH it supports.
3238 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3239 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3240 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3241 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3242 unsigned OpToUse = 0;
3243 if (HasSMUL_LOHI && !HasMULHS) {
3244 OpToUse = ISD::SMUL_LOHI;
3245 } else if (HasUMUL_LOHI && !HasMULHU) {
3246 OpToUse = ISD::UMUL_LOHI;
3247 } else if (HasSMUL_LOHI) {
3248 OpToUse = ISD::SMUL_LOHI;
3249 } else if (HasUMUL_LOHI) {
3250 OpToUse = ISD::UMUL_LOHI;
3251 }
3252 if (OpToUse) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003253 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3254 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003255 break;
3256 }
3257 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::MUL_I16, RTLIB::MUL_I32,
3258 RTLIB::MUL_I64, RTLIB::MUL_I128);
3259 Results.push_back(Tmp1);
3260 break;
3261 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003262 case ISD::BUILD_PAIR: {
3263 MVT PairTy = Node->getValueType(0);
3264 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3265 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
3266 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
3267 DAG.getConstant(PairTy.getSizeInBits()/2,
3268 TLI.getShiftAmountTy()));
3269 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
3270 break;
3271 }
3272 case ISD::BRCOND:
3273 // Expand brcond's setcc into its constituent parts and create a BR_CC
3274 // Node.
3275 Tmp1 = Node->getOperand(0);
3276 Tmp2 = Node->getOperand(1);
3277 if (Tmp2.getOpcode() == ISD::SETCC) {
3278 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
3279 Tmp1, Tmp2.getOperand(2),
3280 Tmp2.getOperand(0), Tmp2.getOperand(1),
3281 Node->getOperand(2));
3282 } else {
3283 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
3284 DAG.getCondCode(ISD::SETNE), Tmp2,
3285 DAG.getConstant(0, Tmp2.getValueType()),
3286 Node->getOperand(2));
3287 }
3288 Results.push_back(Tmp1);
3289 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003290 case ISD::GLOBAL_OFFSET_TABLE:
3291 case ISD::GlobalAddress:
3292 case ISD::GlobalTLSAddress:
3293 case ISD::ExternalSymbol:
3294 case ISD::ConstantPool:
3295 case ISD::JumpTable:
3296 case ISD::INTRINSIC_W_CHAIN:
3297 case ISD::INTRINSIC_WO_CHAIN:
3298 case ISD::INTRINSIC_VOID:
3299 // FIXME: Custom lowering for these operations shouldn't return null!
3300 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3301 Results.push_back(SDValue(Node, i));
3302 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003303 }
3304}
3305void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3306 SmallVectorImpl<SDValue> &Results) {
3307 MVT OVT = Node->getValueType(0);
3308 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3309 Node->getOpcode() == ISD::SINT_TO_FP) {
3310 OVT = Node->getOperand(0).getValueType();
3311 }
3312 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3313 DebugLoc dl = Node->getDebugLoc();
3314 SDValue Tmp1, Tmp2;
3315 switch (Node->getOpcode()) {
3316 case ISD::CTTZ:
3317 case ISD::CTLZ:
3318 case ISD::CTPOP:
3319 // Zero extend the argument.
3320 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3321 // Perform the larger operation.
3322 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3323 if (Node->getOpcode() == ISD::CTTZ) {
3324 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3325 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3326 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3327 ISD::SETEQ);
3328 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3329 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3330 } else if (Node->getOpcode() == ISD::CTLZ) {
3331 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3332 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3333 DAG.getConstant(NVT.getSizeInBits() -
3334 OVT.getSizeInBits(), NVT));
3335 }
3336 Results.push_back(Tmp1);
3337 break;
3338 case ISD::BSWAP: {
3339 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3340 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3341 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3342 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3343 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3344 Results.push_back(Tmp1);
3345 break;
3346 }
3347 case ISD::FP_TO_UINT:
3348 case ISD::FP_TO_SINT:
3349 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3350 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3351 Results.push_back(Tmp1);
3352 break;
3353 case ISD::UINT_TO_FP:
3354 case ISD::SINT_TO_FP:
3355 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3356 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3357 Results.push_back(Tmp1);
3358 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003359 case ISD::AND:
3360 case ISD::OR:
3361 case ISD::XOR:
3362 assert(OVT.isVector() && "Don't know how to promote scalar logic ops");
3363 // Bit convert each of the values to the new type.
3364 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
3365 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1));
3366 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3367 // Bit convert the result back the original type.
3368 Results.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1));
3369 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003370 }
3371}
3372
Chris Lattner3e928bb2005-01-07 07:47:09 +00003373// SelectionDAG::Legalize - This is the entry point for the file.
3374//
Bill Wendling98a366d2009-04-29 23:29:43 +00003375void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3376 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003377 /// run - This is the main entry point to this class.
3378 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003379 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003380}
3381