blob: 58e51780b0716173dc5628b6e68764df13ebf62c [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);
Chris Lattnercad063f2005-07-16 00:19:57 +0000157
Dale Johannesen8a782a22009-02-02 22:12:50 +0000158 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue ExpandBUILD_VECTOR(SDNode *Node);
160 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000161 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
162 DebugLoc dl);
163 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
164 DebugLoc dl);
165 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
166 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000167
Dale Johannesen8a782a22009-02-02 22:12:50 +0000168 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
169 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000170
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000171 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman8c377c72009-05-27 01:25:56 +0000172
173 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
174 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000175};
176}
177
Nate Begeman5a5ca152009-04-29 05:20:52 +0000178/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
179/// performs the same shuffe in terms of order or result bytes, but on a type
180/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000181/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000182SDValue
183SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
184 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000185 SmallVectorImpl<int> &Mask) const {
186 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000187 unsigned NumMaskElts = VT.getVectorNumElements();
188 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000189 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000190
Nate Begeman9008ca62009-04-27 18:41:29 +0000191 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
192
193 if (NumEltsGrowth == 1)
194 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
195
196 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000197 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000198 int Idx = Mask[i];
199 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
200 if (Idx < 0)
201 NewMask.push_back(-1);
202 else
203 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000204 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000205 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000206 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000207 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
208 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000209}
210
Bill Wendling5aa49772009-02-24 02:35:30 +0000211SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000212 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000213 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000214 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000215 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000216 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000217}
218
Chris Lattner3e928bb2005-01-07 07:47:09 +0000219void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000220 LastCALLSEQ_END = DAG.getEntryNode();
221 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000222
Chris Lattnerab510a72005-10-02 17:49:46 +0000223 // The legalize process is inherently a bottom-up recursive process (users
224 // legalize their uses before themselves). Given infinite stack space, we
225 // could just start legalizing on the root and traverse the whole graph. In
226 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000227 // blocks. To avoid this problem, compute an ordering of the nodes where each
228 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000229 DAG.AssignTopologicalOrder();
230 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
231 E = prior(DAG.allnodes_end()); I != next(E); ++I)
232 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000233
234 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000236 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
237 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000238
Chris Lattner3e928bb2005-01-07 07:47:09 +0000239 LegalizedNodes.clear();
240
241 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000242 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000243}
244
Chris Lattner6831a812006-02-13 09:18:02 +0000245
246/// FindCallEndFromCallStart - Given a chained node that is part of a call
247/// sequence, find the CALLSEQ_END node that terminates the call sequence.
248static SDNode *FindCallEndFromCallStart(SDNode *Node) {
249 if (Node->getOpcode() == ISD::CALLSEQ_END)
250 return Node;
251 if (Node->use_empty())
252 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000253
Chris Lattner6831a812006-02-13 09:18:02 +0000254 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000255 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000256 if (TheChain.getValueType() != MVT::Other) {
257 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000258 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000259 if (TheChain.getValueType() != MVT::Other) {
260 // Otherwise, hunt for it.
261 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
262 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000263 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000264 break;
265 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000266
267 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000268 if (TheChain.getValueType() != MVT::Other)
269 return 0;
270 }
271 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Chris Lattner6831a812006-02-13 09:18:02 +0000273 for (SDNode::use_iterator UI = Node->use_begin(),
274 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
Chris Lattner6831a812006-02-13 09:18:02 +0000276 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000277 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000278 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
279 if (User->getOperand(i) == TheChain)
280 if (SDNode *Result = FindCallEndFromCallStart(User))
281 return Result;
282 }
283 return 0;
284}
285
Scott Michelfdc40a02009-02-17 22:15:04 +0000286/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000287/// sequence, find the CALLSEQ_START node that initiates the call sequence.
288static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
289 assert(Node && "Didn't find callseq_start for a call??");
290 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000291
Chris Lattner6831a812006-02-13 09:18:02 +0000292 assert(Node->getOperand(0).getValueType() == MVT::Other &&
293 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000294 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000295}
296
297/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000298/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000299/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000300///
301/// Keep track of the nodes we fine that actually do lead to Dest in
302/// NodesLeadingTo. This avoids retraversing them exponential number of times.
303///
304bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000305 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000306 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000308 // If we've already processed this node and it does lead to Dest, there is no
309 // need to reprocess it.
310 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000311
Chris Lattner6831a812006-02-13 09:18:02 +0000312 // If the first result of this node has been already legalized, then it cannot
313 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000314 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattner6831a812006-02-13 09:18:02 +0000316 // Okay, this node has not already been legalized. Check and legalize all
317 // operands. If none lead to Dest, then we can legalize this node.
318 bool OperandsLeadToDest = false;
319 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
320 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000321 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000322
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000323 if (OperandsLeadToDest) {
324 NodesLeadingTo.insert(N);
325 return true;
326 }
Chris Lattner6831a812006-02-13 09:18:02 +0000327
328 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000329 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000330 return false;
331}
332
Mon P Wang0c397192008-10-30 08:01:45 +0000333/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000334/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000335void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000336 // Don't touch TargetConstants
337 if (Op.getOpcode() == ISD::TargetConstant)
338 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000339 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000340 // We should never see any illegal result types here.
341 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Eli Friedman74807f22009-05-26 08:55:52 +0000342 (void)LegalizeOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000343}
Chris Lattner6831a812006-02-13 09:18:02 +0000344
Evan Cheng9f877882006-12-13 20:57:08 +0000345/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
346/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000347static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000348 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000349 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000350 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000351
352 // If a FP immediate is precise when represented as a float and if the
353 // target can do an extending load from float to double, we put it into
354 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000355 // double. This shrinks FP constants and canonicalizes them for targets where
356 // an FP extending load is the same cost as a normal load (such as on the x87
357 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000358 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000359 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000360 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000361 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000362 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000363 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000364 }
365
Duncan Sands83ec4b62008-06-06 12:08:01 +0000366 MVT OrigVT = VT;
367 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000368 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000369 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000370 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
371 // Only do this if the target has a native EXTLOAD instruction from
372 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000373 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000374 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000375 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000376 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
377 VT = SVT;
378 Extend = true;
379 }
Evan Cheng00495212006-12-12 21:32:44 +0000380 }
381
Dan Gohman475871a2008-07-27 21:46:04 +0000382 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000383 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000384 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000385 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000386 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000387 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000388 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000389 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000390 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000391}
392
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000393/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
394static
Dan Gohman475871a2008-07-27 21:46:04 +0000395SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000396 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000397 SDValue Chain = ST->getChain();
398 SDValue Ptr = ST->getBasePtr();
399 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000400 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000401 int Alignment = ST->getAlignment();
402 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000403 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000404 if (ST->getMemoryVT().isFloatingPoint() ||
405 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000406 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
407 if (TLI.isTypeLegal(intVT)) {
408 // Expand to a bitconvert of the value to the integer type of the
409 // same size, then a (misaligned) int store.
410 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000411 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
412 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000413 SVOffset, ST->isVolatile(), Alignment);
414 } else {
415 // Do a (aligned) store to a stack slot, then copy from the stack slot
416 // to the final destination using (unaligned) integer loads and stores.
417 MVT StoredVT = ST->getMemoryVT();
418 MVT RegVT =
419 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
420 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
421 unsigned RegBytes = RegVT.getSizeInBits() / 8;
422 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000423
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000424 // Make sure the stack slot is also aligned for the register type.
425 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
426
427 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000428 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000429 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000430 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
431 SmallVector<SDValue, 8> Stores;
432 unsigned Offset = 0;
433
434 // Do all but one copies using the full register width.
435 for (unsigned i = 1; i < NumRegs; i++) {
436 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000437 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000438 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000439 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000440 ST->getSrcValue(), SVOffset + Offset,
441 ST->isVolatile(),
442 MinAlign(ST->getAlignment(), Offset)));
443 // Increment the pointers.
444 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000445 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000446 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000447 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000448 }
449
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000450 // The last store may be partial. Do a truncating store. On big-endian
451 // machines this requires an extending load from the stack slot to ensure
452 // that the bits are in the right place.
453 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000454
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000455 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000456 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000457 NULL, 0, MemVT);
458
Dale Johannesenbb5da912009-02-02 20:41:04 +0000459 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000460 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000461 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000462 MinAlign(ST->getAlignment(), Offset)));
463 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000464 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000465 Stores.size());
466 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000467 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000468 assert(ST->getMemoryVT().isInteger() &&
469 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000470 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000471 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000472 MVT NewStoredVT =
473 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
474 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000475 int IncrementSize = NumBits / 8;
476
477 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000478 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
479 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000480 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000481
482 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000483 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000484 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000485 ST->getSrcValue(), SVOffset, NewStoredVT,
486 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000487 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000488 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000489 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000490 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000491 ST->getSrcValue(), SVOffset + IncrementSize,
492 NewStoredVT, ST->isVolatile(), Alignment);
493
Dale Johannesenbb5da912009-02-02 20:41:04 +0000494 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000495}
496
497/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
498static
Dan Gohman475871a2008-07-27 21:46:04 +0000499SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000500 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000502 SDValue Chain = LD->getChain();
503 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000504 MVT VT = LD->getValueType(0);
505 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000506 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000507 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000508 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
509 if (TLI.isTypeLegal(intVT)) {
510 // Expand to a (misaligned) integer load of the same size,
511 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000513 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000514 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000515 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000516 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000517 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000518
Duncan Sands05e11fa2008-12-12 21:47:02 +0000519 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000520 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000521 } else {
522 // Copy the value to a (aligned) stack slot using (unaligned) integer
523 // loads and stores, then do a (aligned) load from the stack slot.
524 MVT RegVT = TLI.getRegisterType(intVT);
525 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
526 unsigned RegBytes = RegVT.getSizeInBits() / 8;
527 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
528
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000529 // Make sure the stack slot is also aligned for the register type.
530 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
531
Duncan Sands05e11fa2008-12-12 21:47:02 +0000532 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
533 SmallVector<SDValue, 8> Stores;
534 SDValue StackPtr = StackBase;
535 unsigned Offset = 0;
536
537 // Do all but one copies using the full register width.
538 for (unsigned i = 1; i < NumRegs; i++) {
539 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000540 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000541 SVOffset + Offset, LD->isVolatile(),
542 MinAlign(LD->getAlignment(), Offset));
543 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000544 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000545 NULL, 0));
546 // Increment the pointers.
547 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000548 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
549 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000550 Increment);
551 }
552
553 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000554 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000555 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000556 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000557 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000558 MinAlign(LD->getAlignment(), Offset));
559 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000560 // On big-endian machines this requires a truncating store to ensure
561 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000562 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000563 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000564
565 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000566 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000567 Stores.size());
568
569 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000570 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000571 NULL, 0, LoadedVT);
572
573 // Callers expect a MERGE_VALUES node.
574 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000575 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000576 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000577 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000578 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000579 "Unaligned load of unsupported type.");
580
Dale Johannesen8155d642008-02-27 22:36:00 +0000581 // Compute the new VT that is half the size of the old one. This is an
582 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 unsigned NumBits = LoadedVT.getSizeInBits();
584 MVT NewLoadedVT;
585 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000586 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000587
Chris Lattnere400af82007-11-19 21:38:03 +0000588 unsigned Alignment = LD->getAlignment();
589 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000590 ISD::LoadExtType HiExtType = LD->getExtensionType();
591
592 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
593 if (HiExtType == ISD::NON_EXTLOAD)
594 HiExtType = ISD::ZEXTLOAD;
595
596 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000597 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000598 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000599 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000600 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000601 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000602 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000603 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000604 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000605 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000606 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000607 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000608 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000609 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000610 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000611 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000612 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000613 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000614 }
615
616 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000617 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000618 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
619 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000620
Dale Johannesenbb5da912009-02-02 20:41:04 +0000621 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000622 Hi.getValue(1));
623
Dan Gohman475871a2008-07-27 21:46:04 +0000624 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000625 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000626}
Evan Cheng912095b2007-01-04 21:56:39 +0000627
Duncan Sands007f9842008-01-10 10:28:30 +0000628/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000630 RTLIB::Libcall Call_F32,
631 RTLIB::Libcall Call_F64,
632 RTLIB::Libcall Call_F80,
633 RTLIB::Libcall Call_PPCF128) {
634 return
635 VT == MVT::f32 ? Call_F32 :
636 VT == MVT::f64 ? Call_F64 :
637 VT == MVT::f80 ? Call_F80 :
638 VT == MVT::ppcf128 ? Call_PPCF128 :
639 RTLIB::UNKNOWN_LIBCALL;
640}
641
Nate Begeman68679912008-04-25 18:07:40 +0000642/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
643/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
644/// is necessary to spill the vector being inserted into to memory, perform
645/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000646SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000647PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
648 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000649 SDValue Tmp1 = Vec;
650 SDValue Tmp2 = Val;
651 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Nate Begeman68679912008-04-25 18:07:40 +0000653 // If the target doesn't support this, we have to spill the input vector
654 // to a temporary stack slot, update the element, then reload it. This is
655 // badness. We could also load the value into a vector register (either
656 // with a "move to register" or "extload into register" instruction, then
657 // permute it into place, if the idx is a constant and if the idx is
658 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000659 MVT VT = Tmp1.getValueType();
660 MVT EltVT = VT.getVectorElementType();
661 MVT IdxVT = Tmp3.getValueType();
662 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000663 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000664
Gabor Greifba36cb52008-08-28 21:40:38 +0000665 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000666
667 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000668 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000669 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000670
671 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000672 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000673 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000674 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000675 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000676 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
677 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000678 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000679 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000680 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000681 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000682 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000683 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000684}
685
Mon P Wange9f10152008-12-09 05:46:39 +0000686
Eli Friedman3f727d62009-05-27 02:16:40 +0000687SDValue SelectionDAGLegalize::
688ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
689 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
690 // SCALAR_TO_VECTOR requires that the type of the value being inserted
691 // match the element type of the vector being created, except for
692 // integers in which case the inserted value can be over width.
693 MVT EltVT = Vec.getValueType().getVectorElementType();
694 if (Val.getValueType() == EltVT ||
695 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
696 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
697 Vec.getValueType(), Val);
698
699 unsigned NumElts = Vec.getValueType().getVectorNumElements();
700 // We generate a shuffle of InVec and ScVec, so the shuffle mask
701 // should be 0,1,2,3,4,5... with the appropriate element replaced with
702 // elt 0 of the RHS.
703 SmallVector<int, 8> ShufOps;
704 for (unsigned i = 0; i != NumElts; ++i)
705 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
706
707 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
708 &ShufOps[0]);
709 }
710 }
711 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
712}
713
Dan Gohmana3466152007-07-13 20:14:11 +0000714/// LegalizeOp - We know that the specified value has a legal type, and
715/// that its operands are legal. Now ensure that the operation itself
716/// is legal, recursively ensuring that the operands' operations remain
717/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000718SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000719 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
720 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000721
Gabor Greifba36cb52008-08-28 21:40:38 +0000722 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000723 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000724
Eli Friedman1fde9c52009-05-24 02:46:31 +0000725 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
726 assert(getTypeAction(Node->getValueType(i)) == Legal &&
727 "Unexpected illegal type!");
728
729 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
730 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
731 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
732 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000733
Chris Lattner45982da2005-05-12 16:53:42 +0000734 // Note that LegalizeOp may be reentered even from single-use nodes, which
735 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000736 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000737 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000738
Dan Gohman475871a2008-07-27 21:46:04 +0000739 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
740 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000741 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000742
Eli Friedman8c377c72009-05-27 01:25:56 +0000743 // Figure out the correct action; the way to query this varies by opcode
744 TargetLowering::LegalizeAction Action;
745 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000746 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000747 case ISD::INTRINSIC_W_CHAIN:
748 case ISD::INTRINSIC_WO_CHAIN:
749 case ISD::INTRINSIC_VOID:
750 case ISD::VAARG:
751 case ISD::STACKSAVE:
752 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
753 break;
754 case ISD::SINT_TO_FP:
755 case ISD::UINT_TO_FP:
756 case ISD::EXTRACT_VECTOR_ELT:
757 Action = TLI.getOperationAction(Node->getOpcode(),
758 Node->getOperand(0).getValueType());
759 break;
760 case ISD::FP_ROUND_INREG:
761 case ISD::SIGN_EXTEND_INREG: {
762 MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
763 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
764 break;
765 }
766 case ISD::LOAD:
767 case ISD::STORE:
768 case ISD::BR_CC:
769 case ISD::FORMAL_ARGUMENTS:
770 case ISD::CALL:
771 case ISD::CALLSEQ_START:
772 case ISD::CALLSEQ_END:
773 case ISD::SELECT_CC:
774 case ISD::SETCC:
775 case ISD::EXCEPTIONADDR:
776 case ISD::EHSELECTION:
777 // These instructions have properties that aren't modeled in the
778 // generic codepath
779 SimpleFinishLegalizing = false;
780 break;
781 case ISD::EXTRACT_ELEMENT:
782 case ISD::FLT_ROUNDS_:
783 case ISD::SADDO:
784 case ISD::SSUBO:
785 case ISD::UADDO:
786 case ISD::USUBO:
787 case ISD::SMULO:
788 case ISD::UMULO:
789 case ISD::FPOWI:
790 case ISD::MERGE_VALUES:
791 case ISD::EH_RETURN:
792 case ISD::FRAME_TO_ARGS_OFFSET:
793 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
794 if (Action == TargetLowering::Legal)
795 Action = TargetLowering::Expand;
796 break;
797 case ISD::TRAMPOLINE:
798 case ISD::FRAMEADDR:
799 case ISD::RETURNADDR:
800 Action = TargetLowering::Custom;
801 break;
802 case ISD::BUILD_VECTOR:
803 // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize
804 // its operands first!
805 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000806 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000807 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000808 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000809 Action = TargetLowering::Legal;
810 } else {
811 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000812 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000813 break;
814 }
815
816 if (SimpleFinishLegalizing) {
817 SmallVector<SDValue, 8> Ops, ResultVals;
818 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
819 Ops.push_back(LegalizeOp(Node->getOperand(i)));
820 switch (Node->getOpcode()) {
821 default: break;
822 case ISD::BR:
823 case ISD::BRIND:
824 case ISD::BR_JT:
825 case ISD::BR_CC:
826 case ISD::BRCOND:
827 case ISD::RET:
828 // Branches tweak the chain to include LastCALLSEQ_END
829 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
830 LastCALLSEQ_END);
831 Ops[0] = LegalizeOp(Ops[0]);
832 LastCALLSEQ_END = DAG.getEntryNode();
833 break;
834 case ISD::SHL:
835 case ISD::SRL:
836 case ISD::SRA:
837 case ISD::ROTL:
838 case ISD::ROTR:
839 // Legalizing shifts/rotates requires adjusting the shift amount
840 // to the appropriate width.
841 if (!Ops[1].getValueType().isVector())
842 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
843 break;
844 }
845
846 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
847 Ops.size());
848 switch (Action) {
849 case TargetLowering::Legal:
850 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
851 ResultVals.push_back(Result.getValue(i));
852 break;
853 case TargetLowering::Custom:
854 // FIXME: The handling for custom lowering with multiple results is
855 // a complete mess.
856 Tmp1 = TLI.LowerOperation(Result, DAG);
857 if (Tmp1.getNode()) {
858 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
859 if (e == 1)
860 ResultVals.push_back(Tmp1);
861 else
862 ResultVals.push_back(Tmp1.getValue(i));
863 }
864 break;
865 }
866
867 // FALL THROUGH
868 case TargetLowering::Expand:
869 ExpandNode(Result.getNode(), ResultVals);
870 break;
871 case TargetLowering::Promote:
872 PromoteNode(Result.getNode(), ResultVals);
873 break;
874 }
875 if (!ResultVals.empty()) {
876 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
877 if (ResultVals[i] != SDValue(Node, i))
878 ResultVals[i] = LegalizeOp(ResultVals[i]);
879 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
880 }
881 return ResultVals[Op.getResNo()];
882 }
883 }
884
885 switch (Node->getOpcode()) {
886 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000887#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000888 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000889#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000890 assert(0 && "Do not know how to legalize this operator!");
891 abort();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000892 case ISD::EXCEPTIONADDR: {
893 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000895 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
896 default: assert(0 && "This action is not supported yet!");
897 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000898 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000899 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000900 }
901 break;
902 case TargetLowering::Custom:
903 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000904 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000905 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000906 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000907 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000908 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000909 break;
910 }
911 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000912 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000913 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000914
Gabor Greifba36cb52008-08-28 21:40:38 +0000915 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000916 "Cannot return more than two values!");
917
918 // Since we produced two values, make sure to remember that we
919 // legalized both of them.
920 Tmp1 = LegalizeOp(Result);
921 Tmp2 = LegalizeOp(Result.getValue(1));
922 AddLegalizedOperand(Op.getValue(0), Tmp1);
923 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000924 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000925 case ISD::EHSELECTION: {
926 Tmp1 = LegalizeOp(Node->getOperand(0));
927 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000928 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +0000929 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
930 default: assert(0 && "This action is not supported yet!");
931 case TargetLowering::Expand: {
932 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000933 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000934 }
935 break;
936 case TargetLowering::Custom:
937 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000938 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +0000939 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000940 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000941 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000942 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +0000943 break;
944 }
945 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000946 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000948
Gabor Greifba36cb52008-08-28 21:40:38 +0000949 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000950 "Cannot return more than two values!");
951
952 // Since we produced two values, make sure to remember that we
953 // legalized both of them.
954 Tmp1 = LegalizeOp(Result);
955 Tmp2 = LegalizeOp(Result.getValue(1));
956 AddLegalizedOperand(Op.getValue(0), Tmp1);
957 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000958 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner36ce6912005-11-29 06:21:05 +0000959
Dan Gohman7f460202008-06-30 20:59:49 +0000960 case ISD::DBG_STOPPOINT:
961 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000962 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000963
Dan Gohman7f460202008-06-30 20:59:49 +0000964 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000965 case TargetLowering::Promote:
966 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000967 case TargetLowering::Expand: {
968 DwarfWriter *DW = DAG.getDwarfWriter();
969 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
970 MVT::Other);
971 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000972
Bill Wendling92c1e122009-02-13 02:16:35 +0000973 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
974 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
975 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
976 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000977
Bill Wendling92c1e122009-02-13 02:16:35 +0000978 unsigned Line = DSP->getLine();
979 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000980
Bill Wendling98a366d2009-04-29 23:29:43 +0000981 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000982 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
983 // won't hurt anything.
984 if (useDEBUG_LOC) {
985 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +0000986 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000987 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +0000988 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
989 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000990 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +0000991 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
992 }
Bill Wendling92c1e122009-02-13 02:16:35 +0000993 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000994 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +0000995 }
996 } else {
997 Result = Tmp1; // chain
998 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000999 break;
Bill Wendling92c1e122009-02-13 02:16:35 +00001000 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001001 case TargetLowering::Custom:
1002 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001003 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001004 break;
Evan Cheng71e86852008-07-08 20:06:39 +00001005 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +00001006 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001007 break;
1008
Dan Gohman475871a2008-07-27 21:46:04 +00001009 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001010 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +00001011 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1012 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +00001013 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1014 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1015 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001016 break;
1017 }
Evan Cheng71e86852008-07-08 20:06:39 +00001018 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001019 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001020 case ISD::ConstantFP: {
1021 // Spill FP immediates to the constant pool if the target cannot directly
1022 // codegen them. Targets often have some immediate values that can be
1023 // efficiently generated into an FP register without a load. We explicitly
1024 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001025 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1026
Chris Lattner3181a772006-01-29 06:26:56 +00001027 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1028 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001029 case TargetLowering::Legal:
1030 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001031 case TargetLowering::Custom:
1032 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001034 Result = Tmp3;
1035 break;
1036 }
1037 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001038 case TargetLowering::Expand: {
1039 // Check to see if this FP immediate is already legal.
1040 bool isLegal = false;
1041 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1042 E = TLI.legal_fpimm_end(); I != E; ++I) {
1043 if (CFP->isExactlyValue(*I)) {
1044 isLegal = true;
1045 break;
1046 }
1047 }
1048 // If this is a legal constant, turn it into a TargetConstantFP node.
1049 if (isLegal)
1050 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001051 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001052 }
Nate Begemane1795842008-02-14 08:57:00 +00001053 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001054 break;
1055 }
Chris Lattnerfdfded52006-04-12 16:20:43 +00001056 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001057 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001058 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001059 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001060 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001061 // A call within a calling sequence must be legalized to something
1062 // other than the normal CALLSEQ_END. Violating this gets Legalize
1063 // into an infinite loop.
1064 assert ((!IsLegalizingCall ||
1065 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001066 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001067 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001068
1069 // The number of incoming and outgoing values should match; unless the final
1070 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001071 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1072 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1073 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001074 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001075 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001076
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001077 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001078 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001079 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1080 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001081 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001082 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001083 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001084 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001085 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001086 }
1087 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001088 case ISD::BUILD_VECTOR:
1089 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001090 default: assert(0 && "This action is not supported yet!");
1091 case TargetLowering::Custom:
1092 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001093 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001094 Result = Tmp3;
1095 break;
1096 }
1097 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001098 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001099 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001100 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001101 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001102 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001103 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001104 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1105 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001106 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1107 MVT VT = Result.getValueType();
1108
Nate Begeman5a5ca152009-04-29 05:20:52 +00001109 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001110 SmallVector<int, 8> Mask;
1111 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001112
1113 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001114 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001115 default: assert(0 && "Unknown operation action!");
1116 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001117 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001118 "vector shuffle should not be created if not legal!");
1119 break;
1120 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001121 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001122 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001123 Result = Tmp3;
1124 break;
1125 }
1126 // FALLTHROUGH
1127 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001128 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001129 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001130 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001131 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001132 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001133 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001134 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001135 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001136 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001137 if (Idx < NumElems)
1138 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1139 DAG.getIntPtrConstant(Idx)));
1140 else
1141 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1142 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001143 }
Evan Chenga87008d2009-02-25 22:49:59 +00001144 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001145 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001146 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001147 case TargetLowering::Promote: {
1148 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001149 MVT OVT = Node->getValueType(0);
1150 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001151
1152 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001153 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1154 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001155
Chris Lattner4352cc92006-04-04 17:23:26 +00001156 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001157 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001158 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001159 break;
1160 }
Chris Lattner87100e02006-03-20 01:52:29 +00001161 }
1162 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001163 }
Mon P Wang0c397192008-10-30 08:01:45 +00001164 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001165 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001166 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001167 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1168 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1169 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1170
1171 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1172 Node->getValueType(0))) {
1173 default: assert(0 && "Unknown operation action!");
1174 case TargetLowering::Legal:
1175 break;
1176 case TargetLowering::Custom:
1177 Tmp3 = TLI.LowerOperation(Result, DAG);
1178 if (Tmp3.getNode()) {
1179 Result = Tmp3;
1180 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001181 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001182 // FALLTHROUGH
1183 case TargetLowering::Expand: {
1184 // Use extract/insert/build vector for now. We might try to be
1185 // more clever later.
1186 MVT PtrVT = TLI.getPointerTy();
1187 SmallVector<SDValue, 8> Ops;
1188 unsigned NumOperands = Node->getNumOperands();
1189 for (unsigned i=0; i < NumOperands; ++i) {
1190 SDValue SubOp = Node->getOperand(i);
1191 MVT VVT = SubOp.getNode()->getValueType(0);
1192 MVT EltVT = VVT.getVectorElementType();
1193 unsigned NumSubElem = VVT.getVectorNumElements();
1194 for (unsigned j=0; j < NumSubElem; ++j) {
1195 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1196 DAG.getConstant(j, PtrVT)));
1197 }
1198 }
1199 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1200 Node->getValueType(0),
1201 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001202 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001203 }
1204 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001205 }
1206
Chris Lattner6831a812006-02-13 09:18:02 +00001207 case ISD::CALLSEQ_START: {
1208 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001209
Chris Lattner6831a812006-02-13 09:18:02 +00001210 // Recursively Legalize all of the inputs of the call end that do not lead
1211 // to this call start. This ensures that any libcalls that need be inserted
1212 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001213 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001214 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001215 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001216 NodesLeadingTo);
1217 }
Chris Lattner6831a812006-02-13 09:18:02 +00001218
1219 // Now that we legalized all of the inputs (which may have inserted
1220 // libcalls) create the new CALLSEQ_START node.
1221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1222
1223 // Merge in the last call, to ensure that this call start after the last
1224 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001225 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001226 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001227 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001228 Tmp1 = LegalizeOp(Tmp1);
1229 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001230
Chris Lattner6831a812006-02-13 09:18:02 +00001231 // Do not try to legalize the target-specific arguments (#1+).
1232 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001234 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001235 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001236 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001237
Chris Lattner6831a812006-02-13 09:18:02 +00001238 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001239 AddLegalizedOperand(Op.getValue(0), Result);
1240 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1241 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001242
Chris Lattner6831a812006-02-13 09:18:02 +00001243 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001244 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001245 // process, no libcalls can/will be inserted, guaranteeing that no calls
1246 // can overlap.
1247 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001248 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001249 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001250 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001251
Chris Lattner6831a812006-02-13 09:18:02 +00001252 // Legalize the call, starting from the CALLSEQ_END.
1253 LegalizeOp(LastCALLSEQ_END);
1254 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1255 return Result;
1256 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001257 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001258 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1259 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001260 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001261 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1262 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001263 assert(I != LegalizedNodes.end() &&
1264 "Legalizing the call start should have legalized this node!");
1265 return I->second;
1266 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001267
1268 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001269 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001270 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001271 // Do not try to legalize the target-specific arguments (#1+), except for
1272 // an optional flag input.
1273 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1274 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001275 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001276 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001277 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001278 }
1279 } else {
1280 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1281 if (Tmp1 != Node->getOperand(0) ||
1282 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001284 Ops[0] = Tmp1;
1285 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001286 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001287 }
Chris Lattner6a542892006-01-24 05:48:21 +00001288 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001289 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001290 // This finishes up call legalization.
1291 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Chris Lattner4b653a02006-02-14 00:55:02 +00001293 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001294 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001295 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001296 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001297 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001298 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001299 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001300 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1301 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1302 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001303 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001304
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001305 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001306 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001307 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001308 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001309 case TargetLowering::Expand: {
1310 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1311 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1312 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001313 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001314
1315 // Chain the dynamic stack allocation so that it doesn't modify the stack
1316 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001317 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001318
Dan Gohman475871a2008-07-27 21:46:04 +00001319 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001320 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001321 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001322 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001323 unsigned StackAlign =
1324 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1325 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001326 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001327 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001328 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001329 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001330
Dale Johannesenca57b842009-02-02 23:46:53 +00001331 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001332 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001333
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001334 Tmp1 = LegalizeOp(Tmp1);
1335 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001336 break;
1337 }
1338 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001339 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001340 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001341 Tmp1 = LegalizeOp(Tmp3);
1342 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001343 }
Chris Lattner903d2782006-01-15 08:54:32 +00001344 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001345 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001346 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001347 }
Chris Lattner903d2782006-01-15 08:54:32 +00001348 // Since this op produce two values, make sure to remember that we
1349 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001350 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1351 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001352 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001353 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001354 case ISD::BR_JT:
1355 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1356 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001357 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001358 Tmp1 = LegalizeOp(Tmp1);
1359 LastCALLSEQ_END = DAG.getEntryNode();
1360
1361 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1363
Scott Michelfdc40a02009-02-17 22:15:04 +00001364 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001365 default: assert(0 && "This action is not supported yet!");
1366 case TargetLowering::Legal: break;
1367 case TargetLowering::Custom:
1368 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001370 break;
1371 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue Chain = Result.getOperand(0);
1373 SDValue Table = Result.getOperand(1);
1374 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001375
Duncan Sands83ec4b62008-06-06 12:08:01 +00001376 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001377 MachineFunction &MF = DAG.getMachineFunction();
1378 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001379 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001380 Index, DAG.getConstant(EntrySize, PTy));
1381 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001382
Duncan Sands712f7b32008-12-12 08:13:38 +00001383 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001384 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001385 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001386 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001387 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001388 // For PIC, the sequence is:
1389 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001390 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001391 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001392 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001393 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001394 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001395 }
1396 }
1397 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001398 case ISD::BRCOND:
1399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001400 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00001401 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001402 Tmp1 = LegalizeOp(Tmp1);
1403 LastCALLSEQ_END = DAG.getEntryNode();
1404
Eli Friedman957bffa2009-05-24 08:42:01 +00001405 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00001406
1407 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001408 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00001409
1410 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001411 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001412 case TargetLowering::Legal: break;
1413 case TargetLowering::Custom:
1414 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001415 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001416 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001417 case TargetLowering::Expand:
1418 // Expand brcond's setcc into its constituent parts and create a BR_CC
1419 // Node.
1420 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001421 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001422 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00001423 Tmp2.getOperand(0), Tmp2.getOperand(1),
1424 Node->getOperand(2));
1425 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001426 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00001427 DAG.getCondCode(ISD::SETNE), Tmp2,
1428 DAG.getConstant(0, Tmp2.getValueType()),
1429 Node->getOperand(2));
1430 }
1431 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001432 }
1433 break;
1434 case ISD::BR_CC:
1435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001436 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001437 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001438 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001439 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001440 Tmp3 = Node->getOperand(3); // RHS
1441 Tmp4 = Node->getOperand(1); // CC
1442
Scott Michelfdc40a02009-02-17 22:15:04 +00001443 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001444 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001445 LastCALLSEQ_END = DAG.getEntryNode();
1446
Evan Cheng7f042682008-10-15 02:05:31 +00001447 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001448 // the LHS is a legal SETCC itself. In this case, we need to compare
1449 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001450 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001451 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1452 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001453 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001454
1455 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001456 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001457
Chris Lattner181b7a32005-12-17 23:46:46 +00001458 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1459 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001460 case TargetLowering::Legal: break;
1461 case TargetLowering::Custom:
1462 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001463 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001464 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001465 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001466 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001467 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001468 LoadSDNode *LD = cast<LoadSDNode>(Node);
1469 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1470 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001471
Evan Cheng466685d2006-10-09 20:57:25 +00001472 ISD::LoadExtType ExtType = LD->getExtensionType();
1473 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001474 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001475 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1476 Tmp3 = Result.getValue(0);
1477 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001478
Evan Cheng466685d2006-10-09 20:57:25 +00001479 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1480 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001481 case TargetLowering::Legal:
1482 // If this is an unaligned load and the target doesn't support it,
1483 // expand it.
1484 if (!TLI.allowsUnalignedMemoryAccesses()) {
1485 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001486 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001487 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001488 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001489 TLI);
1490 Tmp3 = Result.getOperand(0);
1491 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001492 Tmp3 = LegalizeOp(Tmp3);
1493 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001494 }
1495 }
1496 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001497 case TargetLowering::Custom:
1498 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001500 Tmp3 = LegalizeOp(Tmp1);
1501 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001502 }
Evan Cheng466685d2006-10-09 20:57:25 +00001503 break;
1504 case TargetLowering::Promote: {
1505 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001506 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001507 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001508 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001509
Dale Johannesenca57b842009-02-02 23:46:53 +00001510 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001511 LD->getSrcValueOffset(),
1512 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001513 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001514 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001515 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001516 }
Evan Cheng466685d2006-10-09 20:57:25 +00001517 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001518 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001519 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001520 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1521 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001522 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001523 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001524 MVT SrcVT = LD->getMemoryVT();
1525 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001526 int SVOffset = LD->getSrcValueOffset();
1527 unsigned Alignment = LD->getAlignment();
1528 bool isVolatile = LD->isVolatile();
1529
Duncan Sands83ec4b62008-06-06 12:08:01 +00001530 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001531 // Some targets pretend to have an i1 loading operation, and actually
1532 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1533 // bits are guaranteed to be zero; it helps the optimizers understand
1534 // that these bits are zero. It is also useful for EXTLOAD, since it
1535 // tells the optimizers that those bits are undefined. It would be
1536 // nice to have an effective generic way of getting these benefits...
1537 // Until such a way is found, don't insist on promoting i1 here.
1538 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001539 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001540 // Promote to a byte-sized load if not loading an integral number of
1541 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001542 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1543 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001544 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001545
1546 // The extra bits are guaranteed to be zero, since we stored them that
1547 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1548
1549 ISD::LoadExtType NewExtType =
1550 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1551
Dale Johannesenca57b842009-02-02 23:46:53 +00001552 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001553 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1554 NVT, isVolatile, Alignment);
1555
1556 Ch = Result.getValue(1); // The chain.
1557
1558 if (ExtType == ISD::SEXTLOAD)
1559 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001560 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001561 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001562 Result, DAG.getValueType(SrcVT));
1563 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1564 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001565 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001566 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001567 DAG.getValueType(SrcVT));
1568
1569 Tmp1 = LegalizeOp(Result);
1570 Tmp2 = LegalizeOp(Ch);
1571 } else if (SrcWidth & (SrcWidth - 1)) {
1572 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001573 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001574 "Unsupported extload!");
1575 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1576 assert(RoundWidth < SrcWidth);
1577 unsigned ExtraWidth = SrcWidth - RoundWidth;
1578 assert(ExtraWidth < RoundWidth);
1579 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1580 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001581 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1582 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001583 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001584 unsigned IncrementSize;
1585
1586 if (TLI.isLittleEndian()) {
1587 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1588 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001589 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1590 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001591 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1592 Alignment);
1593
1594 // Load the remaining ExtraWidth bits.
1595 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001596 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001597 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001598 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001599 LD->getSrcValue(), SVOffset + IncrementSize,
1600 ExtraVT, isVolatile,
1601 MinAlign(Alignment, IncrementSize));
1602
1603 // Build a factor node to remember that this load is independent of the
1604 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001605 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001606 Hi.getValue(1));
1607
1608 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001609 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001610 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1611
1612 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001613 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001614 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001615 // Big endian - avoid unaligned loads.
1616 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1617 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001618 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001619 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1620 Alignment);
1621
1622 // Load the remaining ExtraWidth bits.
1623 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001624 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001625 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001626 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001627 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001628 LD->getSrcValue(), SVOffset + IncrementSize,
1629 ExtraVT, isVolatile,
1630 MinAlign(Alignment, IncrementSize));
1631
1632 // Build a factor node to remember that this load is independent of the
1633 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001634 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001635 Hi.getValue(1));
1636
1637 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001638 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001639 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1640
1641 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001642 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001643 }
1644
1645 Tmp1 = LegalizeOp(Result);
1646 Tmp2 = LegalizeOp(Ch);
1647 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001648 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001649 default: assert(0 && "This action is not supported yet!");
1650 case TargetLowering::Custom:
1651 isCustom = true;
1652 // FALLTHROUGH
1653 case TargetLowering::Legal:
1654 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1655 Tmp1 = Result.getValue(0);
1656 Tmp2 = Result.getValue(1);
1657
1658 if (isCustom) {
1659 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001660 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001661 Tmp1 = LegalizeOp(Tmp3);
1662 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1663 }
1664 } else {
1665 // If this is an unaligned load and the target doesn't support it,
1666 // expand it.
1667 if (!TLI.allowsUnalignedMemoryAccesses()) {
1668 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001669 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001670 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001671 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001672 TLI);
1673 Tmp1 = Result.getOperand(0);
1674 Tmp2 = Result.getOperand(1);
1675 Tmp1 = LegalizeOp(Tmp1);
1676 Tmp2 = LegalizeOp(Tmp2);
1677 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001678 }
1679 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001680 break;
1681 case TargetLowering::Expand:
1682 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1683 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001684 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001685 LD->getSrcValueOffset(),
1686 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001687 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001688 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001689 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1690 Tmp2 = LegalizeOp(Load.getValue(1));
1691 break;
1692 }
1693 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1694 // Turn the unsupported load into an EXTLOAD followed by an explicit
1695 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001696 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001697 Tmp1, Tmp2, LD->getSrcValue(),
1698 LD->getSrcValueOffset(), SrcVT,
1699 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001700 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001701 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001702 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1703 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001704 Result, DAG.getValueType(SrcVT));
1705 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001706 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001707 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1708 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001709 break;
1710 }
Evan Cheng466685d2006-10-09 20:57:25 +00001711 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001712
Evan Cheng466685d2006-10-09 20:57:25 +00001713 // Since loads produce two values, make sure to remember that we legalized
1714 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001715 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1716 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001717 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001718 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001719 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001720 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001721 StoreSDNode *ST = cast<StoreSDNode>(Node);
1722 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1723 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001724 int SVOffset = ST->getSrcValueOffset();
1725 unsigned Alignment = ST->getAlignment();
1726 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001727
Evan Cheng8b2794a2006-10-13 21:14:26 +00001728 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001729 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1730 // FIXME: We shouldn't do this for TargetConstantFP's.
1731 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1732 // to phase ordering between legalized code and the dag combiner. This
1733 // probably means that we need to integrate dag combiner and legalizer
1734 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001735 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001736 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001737 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001738 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001739 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001740 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001741 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001742 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001743 SVOffset, isVolatile, Alignment);
1744 break;
1745 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001746 // If this target supports 64-bit registers, do a single 64-bit store.
1747 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001748 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001749 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001750 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001751 SVOffset, isVolatile, Alignment);
1752 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001753 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001754 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1755 // stores. If the target supports neither 32- nor 64-bits, this
1756 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001757 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1759 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001760 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001761
Dale Johannesenca57b842009-02-02 23:46:53 +00001762 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001763 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001764 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001765 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001766 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001767 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001768
Dale Johannesenca57b842009-02-02 23:46:53 +00001769 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001770 break;
1771 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001772 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001773 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001774
Eli Friedman957bffa2009-05-24 08:42:01 +00001775 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001776 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001778 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001779
Duncan Sands83ec4b62008-06-06 12:08:01 +00001780 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001781 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1782 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001783 case TargetLowering::Legal:
1784 // If this is an unaligned store and the target doesn't support it,
1785 // expand it.
1786 if (!TLI.allowsUnalignedMemoryAccesses()) {
1787 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001788 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001789 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001790 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001791 TLI);
1792 }
1793 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001794 case TargetLowering::Custom:
1795 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001796 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001797 break;
1798 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001799 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001800 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001801 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001802 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001803 ST->getSrcValue(), SVOffset, isVolatile,
1804 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001805 break;
1806 }
1807 break;
1808 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001809 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001810 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001811
Duncan Sands83ec4b62008-06-06 12:08:01 +00001812 MVT StVT = ST->getMemoryVT();
1813 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001814
Duncan Sands83ec4b62008-06-06 12:08:01 +00001815 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001816 // Promote to a byte-sized store with upper bits zero if not
1817 // storing an integral number of bytes. For example, promote
1818 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001819 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001820 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1821 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001822 SVOffset, NVT, isVolatile, Alignment);
1823 } else if (StWidth & (StWidth - 1)) {
1824 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001825 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001826 "Unsupported truncstore!");
1827 unsigned RoundWidth = 1 << Log2_32(StWidth);
1828 assert(RoundWidth < StWidth);
1829 unsigned ExtraWidth = StWidth - RoundWidth;
1830 assert(ExtraWidth < RoundWidth);
1831 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1832 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001833 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1834 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001836 unsigned IncrementSize;
1837
1838 if (TLI.isLittleEndian()) {
1839 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1840 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001841 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001842 SVOffset, RoundVT,
1843 isVolatile, Alignment);
1844
1845 // Store the remaining ExtraWidth bits.
1846 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001847 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001848 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001849 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001850 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001851 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001852 SVOffset + IncrementSize, ExtraVT, isVolatile,
1853 MinAlign(Alignment, IncrementSize));
1854 } else {
1855 // Big endian - avoid unaligned stores.
1856 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1857 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001858 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001859 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001860 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1861 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001862
1863 // Store the remaining ExtraWidth bits.
1864 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001865 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001866 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001867 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001868 SVOffset + IncrementSize, ExtraVT, isVolatile,
1869 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001870 }
Duncan Sands7e857202008-01-22 07:17:34 +00001871
1872 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001873 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001874 } else {
1875 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1876 Tmp2 != ST->getBasePtr())
1877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1878 ST->getOffset());
1879
1880 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1881 default: assert(0 && "This action is not supported yet!");
1882 case TargetLowering::Legal:
1883 // If this is an unaligned store and the target doesn't support it,
1884 // expand it.
1885 if (!TLI.allowsUnalignedMemoryAccesses()) {
1886 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001887 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001888 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001889 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001890 TLI);
1891 }
1892 break;
1893 case TargetLowering::Custom:
1894 Result = TLI.LowerOperation(Result, DAG);
1895 break;
1896 case Expand:
1897 // TRUNCSTORE:i16 i32 -> STORE i16
1898 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001899 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1900 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1901 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001902 break;
1903 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001904 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001905 }
1906 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001907 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001908 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00001909 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001910 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001911 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001912
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001913 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001914
Nate Begemanb942a3d2005-08-23 04:29:48 +00001915 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001916 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001917 case TargetLowering::Legal: break;
1918 case TargetLowering::Custom: {
1919 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001920 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001921 break;
1922 }
Nate Begeman9373a812005-08-10 20:51:12 +00001923 case TargetLowering::Expand:
1924 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001925 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00001926 Tmp2, Tmp3,
1927 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1928 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001929 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00001930 DAG.getConstant(0, Tmp1.getValueType()),
1931 Tmp2, Tmp3, ISD::SETNE);
1932 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001933 break;
1934 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001935 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001936 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1937 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001938 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001939 ExtOp = ISD::BIT_CONVERT;
1940 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001941 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001942 ExtOp = ISD::ANY_EXTEND;
1943 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001944 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001945 ExtOp = ISD::FP_EXTEND;
1946 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001947 }
1948 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00001949 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
1950 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001951 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001952 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00001953 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00001954 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00001955 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001956 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00001957 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001958 break;
1959 }
1960 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001961 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001962 case ISD::SELECT_CC: {
1963 Tmp1 = Node->getOperand(0); // LHS
1964 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001965 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1966 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00001968
1969 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001970 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001971
Evan Cheng7f042682008-10-15 02:05:31 +00001972 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001973 // the LHS is a legal SETCC itself. In this case, we need to compare
1974 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001975 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001976 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1977 CC = DAG.getCondCode(ISD::SETNE);
1978 }
1979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1980
1981 // Everything is legal, see if we should expand this op or something.
1982 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1983 default: assert(0 && "This action is not supported yet!");
1984 case TargetLowering::Legal: break;
1985 case TargetLowering::Custom:
1986 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001987 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001988 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001989 }
1990 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001991 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001992 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001993 Tmp1 = Node->getOperand(0);
1994 Tmp2 = Node->getOperand(1);
1995 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00001996 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001997
1998 // If we had to Expand the SetCC operands into a SELECT node, then it may
1999 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00002000 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002001 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002002 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002003 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002004 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002005
Chris Lattner73e142f2006-01-30 22:43:50 +00002006 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002007 default: assert(0 && "Cannot handle this action for SETCC yet!");
2008 case TargetLowering::Custom:
2009 isCustom = true;
2010 // FALLTHROUGH.
2011 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002012 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002013 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002014 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002015 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002016 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002017 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002018 case TargetLowering::Promote: {
2019 // First step, figure out the appropriate operation to use.
2020 // Allow SETCC to not be supported for all legal data types
2021 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002022 MVT NewInTy = Node->getOperand(0).getValueType();
2023 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002024
2025 // Scan for the appropriate larger type to use.
2026 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002027 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002028
Duncan Sands83ec4b62008-06-06 12:08:01 +00002029 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002030 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002032 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00002033
Andrew Lenharthae355752005-11-30 17:12:26 +00002034 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002035 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002036 break;
2037 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002038 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002039 assert(0 && "Cannot promote Legal Integer SETCC yet");
2040 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002041 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
2042 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00002043 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002044 Tmp1 = LegalizeOp(Tmp1);
2045 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002046 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002047 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002048 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002049 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002050 case TargetLowering::Expand:
2051 // Expand a setcc node into a select_cc of the same condition, lhs, and
2052 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002053 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002054 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00002055 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002056 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002057 break;
2058 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002059 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002060
2061 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002062 case ISD::ADD:
2063 case ISD::SUB:
2064 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002065 case ISD::MULHS:
2066 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002067 case ISD::UDIV:
2068 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002069 case ISD::AND:
2070 case ISD::OR:
2071 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002072 case ISD::SHL:
2073 case ISD::SRL:
2074 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002075 case ISD::FADD:
2076 case ISD::FSUB:
2077 case ISD::FMUL:
2078 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002079 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002080 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00002081 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002082
2083 if ((Node->getOpcode() == ISD::SHL ||
2084 Node->getOpcode() == ISD::SRL ||
2085 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00002086 !Node->getValueType(0).isVector())
2087 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
2088
Eli Friedman957bffa2009-05-24 08:42:01 +00002089 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002090
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002091 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002092
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002093 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002094 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002095 case TargetLowering::Legal: break;
2096 case TargetLowering::Custom:
2097 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002098 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002099 Result = Tmp1;
2100 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00002101 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002102 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002103 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002104 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002105
Dan Gohman525178c2007-10-08 18:33:35 +00002106 // See if multiply or divide can be lowered using two-result operations.
2107 SDVTList VTs = DAG.getVTList(VT, VT);
2108 if (Node->getOpcode() == ISD::MUL) {
2109 // We just need the low half of the multiply; try both the signed
2110 // and unsigned forms. If the target supports both SMUL_LOHI and
2111 // UMUL_LOHI, form a preference by checking which forms of plain
2112 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002113 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2114 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2115 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2116 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00002117 unsigned OpToUse = 0;
2118 if (HasSMUL_LOHI && !HasMULHS) {
2119 OpToUse = ISD::SMUL_LOHI;
2120 } else if (HasUMUL_LOHI && !HasMULHU) {
2121 OpToUse = ISD::UMUL_LOHI;
2122 } else if (HasSMUL_LOHI) {
2123 OpToUse = ISD::SMUL_LOHI;
2124 } else if (HasUMUL_LOHI) {
2125 OpToUse = ISD::UMUL_LOHI;
2126 }
2127 if (OpToUse) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002128 Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002129 break;
2130 }
2131 }
2132 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002133 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002134 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002135 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002136 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002137 break;
2138 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002139 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002140 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002141 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
2142 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002143 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002144 break;
2145 }
2146 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002147 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002148 Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002149 break;
2150 }
2151 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002152 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002153 Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2);
2154 break;
2155 }
2156 if (Node->getOpcode() == ISD::SUB &&
2157 TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2158 TLI.isOperationLegalOrCustom(ISD::XOR, VT)) {
2159 Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2,
2160 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
2161 Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2162 Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002163 break;
2164 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00002165
Dan Gohman82669522007-10-11 23:57:53 +00002166 // Check to see if we have a libcall for this operator.
2167 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2168 bool isSigned = false;
2169 switch (Node->getOpcode()) {
2170 case ISD::UDIV:
2171 case ISD::SDIV:
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002172 isSigned = Node->getOpcode() == ISD::SDIV;
2173 if (VT == MVT::i16)
2174 LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16);
2175 else if (VT == MVT::i32)
2176 LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32);
2177 else if (VT == MVT::i64)
2178 LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64);
2179 else if (VT == MVT::i128)
2180 LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128);
2181 break;
Chris Lattner31d71612008-10-04 21:27:46 +00002182 case ISD::MUL:
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002183 if (VT == MVT::i16)
2184 LC = RTLIB::MUL_I16;
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002185 else if (VT == MVT::i32)
Chris Lattner31d71612008-10-04 21:27:46 +00002186 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00002187 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00002188 LC = RTLIB::MUL_I64;
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002189 else if (VT == MVT::i128)
2190 LC = RTLIB::MUL_I128;
Chris Lattner31d71612008-10-04 21:27:46 +00002191 break;
Dan Gohman82669522007-10-11 23:57:53 +00002192 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002193 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2194 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002195 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002196 case ISD::FDIV:
2197 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
2198 RTLIB::DIV_PPCF128);
2199 break;
Dan Gohman82669522007-10-11 23:57:53 +00002200 default: break;
2201 }
2202 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Eli Friedman47b41f72009-05-27 02:21:29 +00002203 Result = ExpandLibCall(LC, Node, isSigned);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002204 break;
2205 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002206
Eli Friedman74807f22009-05-26 08:55:52 +00002207 assert(0 && "Cannot expand this binary operator!");
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002208 break;
2209 }
Evan Chengcc987612006-04-12 21:20:24 +00002210 case TargetLowering::Promote: {
2211 switch (Node->getOpcode()) {
2212 default: assert(0 && "Do not know how to promote this BinOp!");
2213 case ISD::AND:
2214 case ISD::OR:
2215 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 MVT OVT = Node->getValueType(0);
2217 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2218 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00002219 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002220 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
2221 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
2222 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00002223 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002224 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00002225 break;
2226 }
2227 }
2228 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002229 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002230 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002231 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2232 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00002233 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00002234
Chris Lattnera09f8482006-03-05 05:09:38 +00002235 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002236
Chris Lattnera09f8482006-03-05 05:09:38 +00002237 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2238 default: assert(0 && "Operation not supported");
2239 case TargetLowering::Custom:
2240 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002241 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002242 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002243 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002244 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002245 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002246 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002247 "Ugly special-cased code!");
2248 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002249 SDValue SignBit;
2250 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
2251 if (isTypeLegal(IVT)) {
2252 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
2253 } else {
2254 assert(isTypeLegal(TLI.getPointerTy()) &&
2255 (TLI.getPointerTy() == MVT::i32 ||
2256 TLI.getPointerTy() == MVT::i64) &&
2257 "Legal type for load?!");
2258 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
2259 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
2260 SDValue Ch =
2261 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
2262 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
2263 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
2264 LoadPtr, DAG.getIntPtrConstant(4));
2265 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
2266 Ch, LoadPtr, NULL, 0, MVT::i32);
2267 }
2268 SignBit =
2269 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
2270 SignBit, DAG.getConstant(0, SignBit.getValueType()),
2271 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002272 // Get the absolute value of the result.
2273 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
2274 // Select between the nabs and abs value based on the sign bit of
2275 // the input.
2276 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
2277 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
2278 AbsVal),
2279 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00002280 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002281 break;
2282 }
Evan Cheng912095b2007-01-04 21:56:39 +00002283 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002284 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002285 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002286 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00002287 // TODO: handle the case where the Lo and Hi operands are not of legal type
2288 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2289 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2290 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002291 case TargetLowering::Promote:
2292 case TargetLowering::Custom:
2293 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002294 case TargetLowering::Legal:
2295 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00002296 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002297 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002298 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00002299 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
2300 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
2301 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002302 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00002303 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002304 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002305 break;
2306 }
2307 break;
2308 }
2309
Nate Begemanc105e192005-04-06 00:23:54 +00002310 case ISD::UREM:
2311 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002312 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002313 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2314 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002315
Nate Begemanc105e192005-04-06 00:23:54 +00002316 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002317 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2318 case TargetLowering::Custom:
2319 isCustom = true;
2320 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002321 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002322 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002323 if (isCustom) {
2324 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002326 }
Nate Begemanc105e192005-04-06 00:23:54 +00002327 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002328 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002329 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002330 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002331 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002332
Dan Gohman525178c2007-10-08 18:33:35 +00002333 // See if remainder can be lowered using two-result operations.
2334 SDVTList VTs = DAG.getVTList(VT, VT);
2335 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002336 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002337 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002338 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002339 break;
2340 }
2341 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002342 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002343 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
2344 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002345 break;
2346 }
2347
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002348 if (VT.isInteger() &&
2349 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
2350 // X % Y -> X-X/Y*Y
2351 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
2352 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
2353 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
2354 break;
Nate Begemanc105e192005-04-06 00:23:54 +00002355 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002356
2357 // Check to see if we have a libcall for this operator.
2358 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2359 switch (Node->getOpcode()) {
2360 default: break;
2361 case ISD::UREM:
2362 case ISD::SREM:
2363 if (VT == MVT::i16)
2364 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
2365 else if (VT == MVT::i32)
2366 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
2367 else if (VT == MVT::i64)
2368 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
2369 else if (VT == MVT::i128)
2370 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
2371 break;
2372 case ISD::FREM:
2373 // Floating point mod -> fmod libcall.
2374 LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
2375 RTLIB::REM_F80, RTLIB::REM_PPCF128);
2376 break;
2377 }
2378
2379 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Eli Friedman47b41f72009-05-27 02:21:29 +00002380 Result = ExpandLibCall(LC, Node, isSigned);
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002381 break;
2382 }
2383
Eli Friedman74807f22009-05-26 08:55:52 +00002384 assert(0 && "Cannot expand this binary operator!");
Nate Begemanc105e192005-04-06 00:23:54 +00002385 break;
2386 }
Dan Gohman525178c2007-10-08 18:33:35 +00002387 }
Nate Begemanc105e192005-04-06 00:23:54 +00002388 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002389 case ISD::VAARG: {
2390 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2391 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2392
Duncan Sands83ec4b62008-06-06 12:08:01 +00002393 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002394 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2395 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002396 case TargetLowering::Custom:
2397 isCustom = true;
2398 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002399 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002400 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2401 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002402 Tmp1 = Result.getValue(1);
2403
2404 if (isCustom) {
2405 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002406 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002407 Result = LegalizeOp(Tmp2);
2408 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2409 }
2410 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002411 break;
2412 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00002413 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00002414 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002415 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00002416 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002417 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00002418 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002419 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00002420 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00002421 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002422 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00002423 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002424 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002425 Result = LegalizeOp(Result);
2426 break;
2427 }
2428 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002429 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00002430 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002431 AddLegalizedOperand(SDValue(Node, 0), Result);
2432 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002433 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002434 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002435 // Unary operators
2436 case ISD::FABS:
2437 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002438 case ISD::FSQRT:
2439 case ISD::FSIN:
2440 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002441 case ISD::FLOG:
2442 case ISD::FLOG2:
2443 case ISD::FLOG10:
2444 case ISD::FEXP:
2445 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00002446 case ISD::FTRUNC:
2447 case ISD::FFLOOR:
2448 case ISD::FCEIL:
2449 case ISD::FRINT:
2450 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002451 Tmp1 = LegalizeOp(Node->getOperand(0));
2452 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002453 case TargetLowering::Promote:
2454 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002455 isCustom = true;
2456 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002457 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002458 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002459 if (isCustom) {
2460 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002461 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00002462 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002463 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002464 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002465 switch (Node->getOpcode()) {
2466 default: assert(0 && "Unreachable!");
2467 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002468 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2469 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00002470 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002471 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002472 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002473 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002474 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002475 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00002476 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002477 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00002478 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2479 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002480 break;
2481 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00002482 case ISD::FSQRT:
2483 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00002484 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002485 case ISD::FLOG:
2486 case ISD::FLOG2:
2487 case ISD::FLOG10:
2488 case ISD::FEXP:
2489 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00002490 case ISD::FTRUNC:
2491 case ISD::FFLOOR:
2492 case ISD::FCEIL:
2493 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00002494 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002495 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00002496
Eli Friedman74807f22009-05-26 08:55:52 +00002497 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00002498
Evan Cheng56966222007-01-12 02:11:51 +00002499 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002500 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002501 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00002502 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2503 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002504 break;
2505 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00002506 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
2507 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002508 break;
2509 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00002510 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
2511 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002512 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002513 case ISD::FLOG:
2514 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
2515 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
2516 break;
2517 case ISD::FLOG2:
2518 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2519 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
2520 break;
2521 case ISD::FLOG10:
2522 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2523 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
2524 break;
2525 case ISD::FEXP:
2526 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
2527 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
2528 break;
2529 case ISD::FEXP2:
2530 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2531 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
2532 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00002533 case ISD::FTRUNC:
2534 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2535 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
2536 break;
2537 case ISD::FFLOOR:
2538 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2539 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
2540 break;
2541 case ISD::FCEIL:
2542 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2543 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
2544 break;
2545 case ISD::FRINT:
2546 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
2547 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
2548 break;
2549 case ISD::FNEARBYINT:
2550 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
2551 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
2552 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00002553 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002554 default: assert(0 && "Unreachable!");
2555 }
Eli Friedman47b41f72009-05-27 02:21:29 +00002556 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002557 break;
2558 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002559 }
2560 break;
2561 }
2562 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002563 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002564 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00002565
2566 // Expand unsupported unary vector operators by unrolling them.
Eli Friedman74807f22009-05-26 08:55:52 +00002567 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00002568
2569 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00002570 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
2571 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Eli Friedman47b41f72009-05-27 02:21:29 +00002572 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002573 break;
2574 }
Bill Wendling74c37652008-12-09 22:08:41 +00002575 case ISD::SADDO:
2576 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002577 MVT VT = Node->getValueType(0);
2578 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2579 default: assert(0 && "This action not supported for this op yet!");
2580 case TargetLowering::Custom:
2581 Result = TLI.LowerOperation(Op, DAG);
2582 if (Result.getNode()) break;
2583 // FALLTHROUGH
2584 case TargetLowering::Legal: {
2585 SDValue LHS = LegalizeOp(Node->getOperand(0));
2586 SDValue RHS = LegalizeOp(Node->getOperand(1));
2587
Scott Michelfdc40a02009-02-17 22:15:04 +00002588 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002589 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002590 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002591 MVT OType = Node->getValueType(1);
2592
Bill Wendlinga6af91a2008-11-25 08:19:22 +00002593 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002594
Bill Wendling740464e2008-11-25 19:40:17 +00002595 // LHSSign -> LHS >= 0
2596 // RHSSign -> RHS >= 0
2597 // SumSign -> Sum >= 0
2598 //
Bill Wendling74c37652008-12-09 22:08:41 +00002599 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00002600 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00002601 // Sub:
2602 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00002603 //
Dale Johannesenca57b842009-02-02 23:46:53 +00002604 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2605 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00002606 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2607 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002608 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002609
Dale Johannesenca57b842009-02-02 23:46:53 +00002610 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2611 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002612
Dale Johannesenca57b842009-02-02 23:46:53 +00002613 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002614
2615 MVT ValueVTs[] = { LHS.getValueType(), OType };
2616 SDValue Ops[] = { Sum, Cmp };
2617
Scott Michelfdc40a02009-02-17 22:15:04 +00002618 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002619 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002620 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002621 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002622 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002623 break;
2624 }
2625 }
2626
2627 break;
2628 }
Bill Wendling74c37652008-12-09 22:08:41 +00002629 case ISD::UADDO:
2630 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00002631 MVT VT = Node->getValueType(0);
2632 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2633 default: assert(0 && "This action not supported for this op yet!");
2634 case TargetLowering::Custom:
2635 Result = TLI.LowerOperation(Op, DAG);
2636 if (Result.getNode()) break;
2637 // FALLTHROUGH
2638 case TargetLowering::Legal: {
2639 SDValue LHS = LegalizeOp(Node->getOperand(0));
2640 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002641
Bill Wendling74c37652008-12-09 22:08:41 +00002642 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002643 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002644 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002645 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00002646 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00002647 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002648 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002649
Bill Wendling41ea7e72008-11-24 19:21:46 +00002650 MVT ValueVTs[] = { LHS.getValueType(), OType };
2651 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002652
Scott Michelfdc40a02009-02-17 22:15:04 +00002653 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002654 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002655 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002656 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002657 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002658 break;
2659 }
2660 }
2661
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002662 break;
2663 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002664 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002665
Chris Lattner4ddd2832006-04-08 04:13:17 +00002666 assert(Result.getValueType() == Op.getValueType() &&
2667 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002668
Chris Lattner456a93a2006-01-28 07:39:30 +00002669 // Make sure that the generated code is itself legal.
2670 if (Result != Op)
2671 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002672
Chris Lattner45982da2005-05-12 16:53:42 +00002673 // Note that LegalizeOp may be reentered even from single-use nodes, which
2674 // means that we always must cache transformed nodes.
2675 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002676 return Result;
2677}
2678
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002679SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
2680 SDValue Vec = Op.getOperand(0);
2681 SDValue Idx = Op.getOperand(1);
2682 DebugLoc dl = Op.getDebugLoc();
2683 // Store the value to a temporary stack slot, then LOAD the returned part.
2684 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
2685 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
2686
2687 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00002688 unsigned EltSize =
2689 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002690 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
2691 DAG.getConstant(EltSize, Idx.getValueType()));
2692
2693 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
2694 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
2695 else
2696 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
2697
2698 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
2699
2700 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
2701}
2702
Nate Begeman750ac1b2006-02-01 07:19:44 +00002703/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2704/// with condition CC on the current target. This usually involves legalizing
2705/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2706/// there may be no choice but to create a new SetCC node to represent the
2707/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00002708/// LHS, and the SDValue returned in RHS has a nil SDNode value.
2709void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
2710 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002711 SDValue &CC,
2712 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00002713 LHS = LegalizeOp(LHS);
2714 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002715}
2716
Evan Cheng7f042682008-10-15 02:05:31 +00002717/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
2718/// condition code CC on the current target. This routine assumes LHS and rHS
2719/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
2720/// illegal condition code into AND / OR of multiple SETCC values.
2721void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
2722 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002723 SDValue &CC,
2724 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00002725 MVT OpVT = LHS.getValueType();
2726 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2727 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
2728 default: assert(0 && "Unknown condition code action!");
2729 case TargetLowering::Legal:
2730 // Nothing to do.
2731 break;
2732 case TargetLowering::Expand: {
2733 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
2734 unsigned Opc = 0;
2735 switch (CCCode) {
2736 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00002737 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
2738 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2739 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2740 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2741 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2742 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2743 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2744 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2745 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2746 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2747 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2748 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00002749 // FIXME: Implement more expansions.
2750 }
2751
Dale Johannesenbb5da912009-02-02 20:41:04 +00002752 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
2753 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
2754 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00002755 RHS = SDValue();
2756 CC = SDValue();
2757 break;
2758 }
2759 }
2760}
2761
Chris Lattner1401d152008-01-16 07:45:30 +00002762/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2763/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2764/// a load from the stack slot to DestVT, extending it if needed.
2765/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002766SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2767 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002768 MVT DestVT,
2769 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002770 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002771 unsigned SrcAlign =
2772 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2773 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002774 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002775
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002776 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002777 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002778 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2779
Duncan Sands83ec4b62008-06-06 12:08:01 +00002780 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2781 unsigned SlotSize = SlotVT.getSizeInBits();
2782 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002783 unsigned DestAlign =
2784 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002785
Chris Lattner1401d152008-01-16 07:45:30 +00002786 // Emit a store to the stack slot. Use a truncstore if the input value is
2787 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002788 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002789
Chris Lattner1401d152008-01-16 07:45:30 +00002790 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002791 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002792 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002793 else {
2794 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002795 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002796 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002797 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002798
Chris Lattner35481892005-12-23 00:16:34 +00002799 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002800 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002801 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002802
Chris Lattner1401d152008-01-16 07:45:30 +00002803 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002804 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002805 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002806}
2807
Dan Gohman475871a2008-07-27 21:46:04 +00002808SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002809 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002810 // Create a vector sized/aligned stack slot, store the value to element #0,
2811 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002812 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002813
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002814 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002815 int SPFI = StackPtrFI->getIndex();
2816
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002817 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2818 StackPtr,
2819 PseudoSourceValue::getFixedStack(SPFI), 0,
2820 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002821 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002822 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002823}
2824
2825
Chris Lattnerce872152006-03-19 06:31:19 +00002826/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002827/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002828SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002829 unsigned NumElems = Node->getNumOperands();
2830 SDValue SplatValue = Node->getOperand(0);
2831 DebugLoc dl = Node->getDebugLoc();
2832 MVT VT = Node->getValueType(0);
2833 MVT OpVT = SplatValue.getValueType();
2834 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002835
2836 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002837 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002838 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002839
Dan Gohman475871a2008-07-27 21:46:04 +00002840 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002841 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002842 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002843 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002844 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002845 bool isConstant = true;
2846 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2847 SplatValue.getOpcode() != ISD::UNDEF)
2848 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002849
Evan Cheng033e6812006-03-24 01:17:21 +00002850 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002851 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002852 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002853 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002854 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002855 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002856 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002857
2858 // If this isn't a constant element or an undef, we can't use a constant
2859 // pool load.
2860 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2861 V.getOpcode() != ISD::UNDEF)
2862 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002863 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002864
Chris Lattnerce872152006-03-19 06:31:19 +00002865 if (isOnlyLowElement) {
2866 // If the low element is an undef too, then this whole things is an undef.
2867 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002868 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00002869 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002870 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00002871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002872
Chris Lattner2eb86532006-03-24 07:29:17 +00002873 // If all elements are constants, create a load from the constant pool.
2874 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002875 std::vector<Constant*> CV;
2876 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002877 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002878 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002879 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002880 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002881 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002882 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00002883 } else {
2884 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002885 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00002886 CV.push_back(UndefValue::get(OpNTy));
2887 }
2888 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002889 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002890 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002891 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002892 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002893 PseudoSourceValue::getConstantPool(), 0,
2894 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002895 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002896
Gabor Greifba36cb52008-08-28 21:40:38 +00002897 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00002898 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00002899 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00002900
2901 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00002902 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002903 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00002904 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002905 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002906
Chris Lattner87100e02006-03-20 01:52:29 +00002907 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00002908 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
2909 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00002910 }
2911 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002912
Evan Cheng033e6812006-03-24 01:17:21 +00002913 // If there are only two unique elements, we may be able to turn this into a
2914 // vector shuffle.
2915 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002916 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00002917 SDValue Val1 = Node->getOperand(1);
2918 SDValue Val2;
2919 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002920 if (MI->first != Val1)
2921 Val2 = MI->first;
2922 else
2923 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00002924
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002925 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002926 // vector shuffle has the undef vector on the RHS.
2927 if (Val1.getOpcode() == ISD::UNDEF)
2928 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002929
Evan Cheng033e6812006-03-24 01:17:21 +00002930 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002932
2933 // Set elements of the shuffle mask for Val1.
2934 std::vector<unsigned> &Val1Elts = Values[Val1];
2935 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002936 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002937
2938 // Set elements of the shuffle mask for Val2.
2939 std::vector<unsigned> &Val2Elts = Values[Val2];
2940 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
2941 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00002942 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00002943
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002944 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002945 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002947 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
2948 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00002950 }
2951 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002952
Chris Lattnerce872152006-03-19 06:31:19 +00002953 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
2954 // aligned object on the stack, store each element into it, then load
2955 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00002956 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00002957 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00002958 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
2959 const Value *SV = PseudoSourceValue::getFixedStack(FI);
2960
Chris Lattnerce872152006-03-19 06:31:19 +00002961 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00002962 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002963 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00002964 // Store (in the right endianness) the elements to memory.
2965 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2966 // Ignore undef elements.
2967 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00002968
Chris Lattner841c8822006-03-22 01:46:54 +00002969 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00002970
Dan Gohman475871a2008-07-27 21:46:04 +00002971 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002972 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00002973
Dale Johannesen8a782a22009-02-02 22:12:50 +00002974 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
2975 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00002976 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002977
Dan Gohman475871a2008-07-27 21:46:04 +00002978 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00002979 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00002980 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002981 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00002982 else
2983 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002984
Chris Lattnerce872152006-03-19 06:31:19 +00002985 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00002986 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00002987}
2988
Chris Lattner77e77a62005-01-21 06:05:23 +00002989// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2990// does not fit into a register, return the lo part and set the hi part to the
2991// by-reg argument. If it does fit into a single register, return the result
2992// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002993SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002994 bool isSigned) {
Chris Lattner6831a812006-02-13 09:18:02 +00002995 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002996 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002997 // Legalizing the call will automatically add the previous call to the
2998 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002999 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003000
Chris Lattner77e77a62005-01-21 06:05:23 +00003001 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00003002 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00003003 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003004 MVT ArgVT = Node->getOperand(i).getValueType();
3005 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00003006 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00003007 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00003008 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00003009 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00003010 }
Bill Wendling056292f2008-09-16 21:48:12 +00003011 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00003012 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003013
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003014 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003015 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003016 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00003017 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003018 CallingConv::C, false, Callee, Args, DAG,
3019 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003020
Chris Lattner6831a812006-02-13 09:18:02 +00003021 // Legalize the call sequence, starting with the chain. This will advance
3022 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3023 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3024 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00003025 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003026}
3027
Chris Lattner22cde6a2006-01-28 08:25:58 +00003028/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3029/// INT_TO_FP operation of the specified operand when the target requests that
3030/// we expand it. At this point, we know that the result and operand types are
3031/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00003032SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3033 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00003034 MVT DestVT,
3035 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003036 if (Op0.getValueType() == MVT::i32) {
3037 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00003038
Chris Lattner23594d42008-01-16 07:03:22 +00003039 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00003040 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00003041
Chris Lattner22cde6a2006-01-28 08:25:58 +00003042 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00003043 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00003044 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00003045 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00003046 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003047 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00003048 if (TLI.isLittleEndian())
3049 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00003050
Chris Lattner22cde6a2006-01-28 08:25:58 +00003051 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00003052 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003053 if (isSigned) {
3054 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00003055 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00003056 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003057 } else {
3058 Op0Mapped = Op0;
3059 }
3060 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00003061 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003062 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003063 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00003064 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003065 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00003066 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003067 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00003068 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003069 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00003070 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003071 BitsToDouble(0x4330000080000000ULL) :
3072 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00003073 MVT::f64);
3074 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00003075 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003076 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003078 // handle final rounding
3079 if (DestVT == MVT::f64) {
3080 // do nothing
3081 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003082 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00003083 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00003084 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003085 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00003086 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003087 }
3088 return Result;
3089 }
3090 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00003091 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003092
Dale Johannesenaf435272009-02-02 19:03:57 +00003093 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003094 Op0, DAG.getConstant(0, Op0.getValueType()),
3095 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00003097 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00003098 SignSet, Four, Zero);
3099
3100 // If the sign bit of the integer is set, the large number will be treated
3101 // as a negative number. To counteract this, the dynamic code adds an
3102 // offset depending on the data type.
3103 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003104 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003105 default: assert(0 && "Unsupported integer type!");
3106 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3107 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3108 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3109 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3110 }
3111 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00003112 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003113
Dan Gohman475871a2008-07-27 21:46:04 +00003114 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00003115 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00003116 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00003117 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00003118 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003119 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00003120 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00003121 PseudoSourceValue::getConstantPool(), 0,
3122 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003123 else {
Dan Gohman69de1932008-02-06 22:27:42 +00003124 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00003125 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00003126 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00003127 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00003128 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003129 }
3130
Dale Johannesenaf435272009-02-02 19:03:57 +00003131 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003132}
3133
3134/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3135/// *INT_TO_FP operation of the specified operand when the target requests that
3136/// we promote it. At this point, we know that the result and operand types are
3137/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3138/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00003139SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
3140 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003141 bool isSigned,
3142 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003143 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003144 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003145
3146 unsigned OpToUse = 0;
3147
3148 // Scan for the appropriate larger type to use.
3149 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003150 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
3151 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003152
3153 // If the target supports SINT_TO_FP of this type, use it.
3154 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3155 default: break;
3156 case TargetLowering::Legal:
3157 if (!TLI.isTypeLegal(NewInTy))
3158 break; // Can't use this datatype.
3159 // FALL THROUGH.
3160 case TargetLowering::Custom:
3161 OpToUse = ISD::SINT_TO_FP;
3162 break;
3163 }
3164 if (OpToUse) break;
3165 if (isSigned) continue;
3166
3167 // If the target supports UINT_TO_FP of this type, use it.
3168 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3169 default: break;
3170 case TargetLowering::Legal:
3171 if (!TLI.isTypeLegal(NewInTy))
3172 break; // Can't use this datatype.
3173 // FALL THROUGH.
3174 case TargetLowering::Custom:
3175 OpToUse = ISD::UINT_TO_FP;
3176 break;
3177 }
3178 if (OpToUse) break;
3179
3180 // Otherwise, try a larger type.
3181 }
3182
3183 // Okay, we found the operation and type to use. Zero extend our input to the
3184 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00003185 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00003186 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00003187 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003188}
3189
3190/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3191/// FP_TO_*INT operation of the specified operand when the target requests that
3192/// we promote it. At this point, we know that the result and operand types are
3193/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3194/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00003195SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
3196 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003197 bool isSigned,
3198 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003199 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003200 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003201
3202 unsigned OpToUse = 0;
3203
3204 // Scan for the appropriate larger type to use.
3205 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003206 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
3207 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003208
3209 // If the target supports FP_TO_SINT returning this type, use it.
3210 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3211 default: break;
3212 case TargetLowering::Legal:
3213 if (!TLI.isTypeLegal(NewOutTy))
3214 break; // Can't use this datatype.
3215 // FALL THROUGH.
3216 case TargetLowering::Custom:
3217 OpToUse = ISD::FP_TO_SINT;
3218 break;
3219 }
3220 if (OpToUse) break;
3221
3222 // If the target supports FP_TO_UINT of this type, use it.
3223 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3224 default: break;
3225 case TargetLowering::Legal:
3226 if (!TLI.isTypeLegal(NewOutTy))
3227 break; // Can't use this datatype.
3228 // FALL THROUGH.
3229 case TargetLowering::Custom:
3230 OpToUse = ISD::FP_TO_UINT;
3231 break;
3232 }
3233 if (OpToUse) break;
3234
3235 // Otherwise, try a larger type.
3236 }
3237
Scott Michelfdc40a02009-02-17 22:15:04 +00003238
Chris Lattner27a6c732007-11-24 07:07:01 +00003239 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00003240 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00003241
Chris Lattner27a6c732007-11-24 07:07:01 +00003242 // If the operation produces an invalid type, it must be custom lowered. Use
3243 // the target lowering hooks to expand it. Just keep the low part of the
3244 // expanded operation, we know that we're truncating anyway.
3245 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00003246 SmallVector<SDValue, 2> Results;
3247 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
3248 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
3249 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00003250 }
Duncan Sands126d9072008-07-04 11:47:58 +00003251
Chris Lattner27a6c732007-11-24 07:07:01 +00003252 // Truncate the result of the extended FP_TO_*INT operation to the desired
3253 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00003254 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003255}
3256
3257/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3258///
Dale Johannesen8a782a22009-02-02 22:12:50 +00003259SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003260 MVT VT = Op.getValueType();
3261 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003262 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003263 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003264 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3265 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003266 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3267 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3268 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003269 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003270 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3271 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3272 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3273 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3274 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3275 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3276 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3277 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3278 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003279 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003280 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
3281 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
3282 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3283 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3284 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3285 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3286 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
3287 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
3288 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3289 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3290 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3291 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3292 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3293 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3294 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
3295 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
3296 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3297 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3298 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
3299 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
3300 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003301 }
3302}
3303
3304/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3305///
Scott Michelfdc40a02009-02-17 22:15:04 +00003306SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003307 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003308 switch (Opc) {
3309 default: assert(0 && "Cannot expand this yet!");
3310 case ISD::CTPOP: {
3311 static const uint64_t mask[6] = {
3312 0x5555555555555555ULL, 0x3333333333333333ULL,
3313 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3314 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3315 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00003316 MVT VT = Op.getValueType();
3317 MVT ShVT = TLI.getShiftAmountTy();
3318 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003319 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3320 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003321 unsigned EltSize = VT.isVector() ?
3322 VT.getVectorElementType().getSizeInBits() : len;
3323 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003324 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003325 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00003326 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003327 DAG.getNode(ISD::AND, dl, VT,
3328 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
3329 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003330 }
3331 return Op;
3332 }
3333 case ISD::CTLZ: {
3334 // for now, we do this:
3335 // x = x | (x >> 1);
3336 // x = x | (x >> 2);
3337 // ...
3338 // x = x | (x >>16);
3339 // x = x | (x >>32); // for 64-bit input
3340 // return popcount(~x);
3341 //
3342 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003343 MVT VT = Op.getValueType();
3344 MVT ShVT = TLI.getShiftAmountTy();
3345 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003346 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003347 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003348 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00003349 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003350 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00003351 Op = DAG.getNOT(dl, Op, VT);
3352 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003353 }
3354 case ISD::CTTZ: {
3355 // for now, we use: { return popcount(~x & (x - 1)); }
3356 // unless the target has ctlz but not ctpop, in which case we use:
3357 // { return 32 - nlz(~x & (x-1)); }
3358 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003359 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00003360 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
3361 DAG.getNOT(dl, Op, VT),
3362 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00003363 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003364 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003365 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
3366 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00003367 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003368 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003369 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
3370 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003371 }
3372 }
3373}
Chris Lattnere34b3962005-01-19 04:19:40 +00003374
Eli Friedman8c377c72009-05-27 01:25:56 +00003375void SelectionDAGLegalize::ExpandNode(SDNode *Node,
3376 SmallVectorImpl<SDValue> &Results) {
3377 DebugLoc dl = Node->getDebugLoc();
3378 SDValue Tmp1, Tmp2;
3379 switch (Node->getOpcode()) {
3380 case ISD::CTPOP:
3381 case ISD::CTLZ:
3382 case ISD::CTTZ:
3383 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
3384 Results.push_back(Tmp1);
3385 break;
3386 case ISD::BSWAP:
3387 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
3388 break;
3389 case ISD::FRAMEADDR:
3390 case ISD::RETURNADDR:
3391 case ISD::FRAME_TO_ARGS_OFFSET:
3392 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
3393 break;
3394 case ISD::FLT_ROUNDS_:
3395 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
3396 break;
3397 case ISD::EH_RETURN:
3398 case ISD::DECLARE:
3399 case ISD::DBG_LABEL:
3400 case ISD::EH_LABEL:
3401 case ISD::PREFETCH:
3402 case ISD::MEMBARRIER:
3403 case ISD::VAEND:
3404 Results.push_back(Node->getOperand(0));
3405 break;
3406 case ISD::MERGE_VALUES:
3407 for (unsigned i = 0; i < Node->getNumValues(); i++)
3408 Results.push_back(Node->getOperand(i));
3409 break;
3410 case ISD::UNDEF: {
3411 MVT VT = Node->getValueType(0);
3412 if (VT.isInteger())
3413 Results.push_back(DAG.getConstant(0, VT));
3414 else if (VT.isFloatingPoint())
3415 Results.push_back(DAG.getConstantFP(0, VT));
3416 else
3417 assert(0 && "Unknown value type!");
3418 break;
3419 }
3420 case ISD::TRAP: {
3421 // If this operation is not supported, lower it to 'abort()' call
3422 TargetLowering::ArgListTy Args;
3423 std::pair<SDValue, SDValue> CallResult =
3424 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
3425 false, false, false, false, CallingConv::C, false,
3426 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3427 Args, DAG, dl);
3428 Results.push_back(CallResult.second);
3429 break;
3430 }
3431 case ISD::FP_ROUND:
3432 case ISD::BIT_CONVERT:
3433 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3434 Node->getValueType(0), dl);
3435 Results.push_back(Tmp1);
3436 break;
3437 case ISD::FP_EXTEND:
3438 Tmp1 = EmitStackConvert(Node->getOperand(0),
3439 Node->getOperand(0).getValueType(),
3440 Node->getValueType(0), dl);
3441 Results.push_back(Tmp1);
3442 break;
3443 case ISD::SIGN_EXTEND_INREG: {
3444 // NOTE: we could fall back on load/store here too for targets without
3445 // SAR. However, it is doubtful that any exist.
3446 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3447 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3448 ExtraVT.getSizeInBits();
3449 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3450 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3451 Node->getOperand(0), ShiftCst);
3452 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3453 Results.push_back(Tmp1);
3454 break;
3455 }
3456 case ISD::FP_ROUND_INREG: {
3457 // The only way we can lower this is to turn it into a TRUNCSTORE,
3458 // EXTLOAD pair, targetting a temporary location (a stack slot).
3459
3460 // NOTE: there is a choice here between constantly creating new stack
3461 // slots and always reusing the same one. We currently always create
3462 // new ones, as reuse may inhibit scheduling.
3463 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3464 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3465 Node->getValueType(0), dl);
3466 Results.push_back(Tmp1);
3467 break;
3468 }
3469 case ISD::SINT_TO_FP:
3470 case ISD::UINT_TO_FP:
3471 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3472 Node->getOperand(0), Node->getValueType(0), dl);
3473 Results.push_back(Tmp1);
3474 break;
3475 case ISD::FP_TO_UINT: {
3476 SDValue True, False;
3477 MVT VT = Node->getOperand(0).getValueType();
3478 MVT NVT = Node->getValueType(0);
3479 const uint64_t zero[] = {0, 0};
3480 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3481 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3482 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3483 Tmp1 = DAG.getConstantFP(apf, VT);
3484 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3485 Node->getOperand(0),
3486 Tmp1, ISD::SETLT);
3487 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3488 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3489 DAG.getNode(ISD::FSUB, dl, VT,
3490 Node->getOperand(0), Tmp1));
3491 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3492 DAG.getConstant(x, NVT));
3493 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3494 Results.push_back(Tmp1);
3495 break;
3496 }
3497 case ISD::VACOPY: {
3498 // This defaults to loading a pointer from the input and storing it to the
3499 // output, returning the chain.
3500 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3501 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3502 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
3503 Node->getOperand(2), VS, 0);
3504 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
3505 Results.push_back(Tmp1);
3506 break;
3507 }
3508 case ISD::EXTRACT_VECTOR_ELT:
3509 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3510 // This must be an access of the only element. Return it.
3511 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
3512 Node->getOperand(0));
3513 else
3514 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3515 Results.push_back(Tmp1);
3516 break;
3517 case ISD::EXTRACT_SUBVECTOR:
3518 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3519 break;
3520 case ISD::SCALAR_TO_VECTOR:
3521 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3522 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003523 case ISD::INSERT_VECTOR_ELT:
3524 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3525 Node->getOperand(1),
3526 Node->getOperand(2), dl));
3527 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003528 case ISD::EXTRACT_ELEMENT: {
3529 MVT OpTy = Node->getOperand(0).getValueType();
3530 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3531 // 1 -> Hi
3532 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3533 DAG.getConstant(OpTy.getSizeInBits()/2,
3534 TLI.getShiftAmountTy()));
3535 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3536 } else {
3537 // 0 -> Lo
3538 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3539 Node->getOperand(0));
3540 }
3541 Results.push_back(Tmp1);
3542 break;
3543 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003544 case ISD::STACKSAVE:
3545 // Expand to CopyFromReg if the target set
3546 // StackPointerRegisterToSaveRestore.
3547 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3548 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3549 Node->getValueType(0)));
3550 Results.push_back(Results[0].getValue(1));
3551 } else {
3552 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3553 Results.push_back(Node->getOperand(0));
3554 }
3555 break;
3556 case ISD::STACKRESTORE:
3557 // Expand to CopyToReg if the target set
3558 // StackPointerRegisterToSaveRestore.
3559 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3560 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3561 Node->getOperand(1)));
3562 } else {
3563 Results.push_back(Node->getOperand(0));
3564 }
3565 break;
3566 case ISD::GLOBAL_OFFSET_TABLE:
3567 case ISD::GlobalAddress:
3568 case ISD::GlobalTLSAddress:
3569 case ISD::ExternalSymbol:
3570 case ISD::ConstantPool:
3571 case ISD::JumpTable:
3572 case ISD::INTRINSIC_W_CHAIN:
3573 case ISD::INTRINSIC_WO_CHAIN:
3574 case ISD::INTRINSIC_VOID:
3575 // FIXME: Custom lowering for these operations shouldn't return null!
3576 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3577 Results.push_back(SDValue(Node, i));
3578 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003579 }
3580}
3581void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3582 SmallVectorImpl<SDValue> &Results) {
3583 MVT OVT = Node->getValueType(0);
3584 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3585 Node->getOpcode() == ISD::SINT_TO_FP) {
3586 OVT = Node->getOperand(0).getValueType();
3587 }
3588 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3589 DebugLoc dl = Node->getDebugLoc();
3590 SDValue Tmp1, Tmp2;
3591 switch (Node->getOpcode()) {
3592 case ISD::CTTZ:
3593 case ISD::CTLZ:
3594 case ISD::CTPOP:
3595 // Zero extend the argument.
3596 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3597 // Perform the larger operation.
3598 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3599 if (Node->getOpcode() == ISD::CTTZ) {
3600 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3601 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3602 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3603 ISD::SETEQ);
3604 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3605 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3606 } else if (Node->getOpcode() == ISD::CTLZ) {
3607 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3608 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3609 DAG.getConstant(NVT.getSizeInBits() -
3610 OVT.getSizeInBits(), NVT));
3611 }
3612 Results.push_back(Tmp1);
3613 break;
3614 case ISD::BSWAP: {
3615 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3616 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3617 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3618 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3619 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3620 Results.push_back(Tmp1);
3621 break;
3622 }
3623 case ISD::FP_TO_UINT:
3624 case ISD::FP_TO_SINT:
3625 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3626 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3627 Results.push_back(Tmp1);
3628 break;
3629 case ISD::UINT_TO_FP:
3630 case ISD::SINT_TO_FP:
3631 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3632 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3633 Results.push_back(Tmp1);
3634 break;
3635 }
3636}
3637
Chris Lattner3e928bb2005-01-07 07:47:09 +00003638// SelectionDAG::Legalize - This is the entry point for the file.
3639//
Bill Wendling98a366d2009-04-29 23:29:43 +00003640void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3641 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003642 /// run - This is the main entry point to this class.
3643 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003644 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003645}
3646