blob: dd91d6a3316d97fb101c64fea322f37458911713 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000031#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000032#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000035#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000036#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000037#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000039#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000065 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000066
Chris Lattner6831a812006-02-13 09:18:02 +000067 /// IsLegalizingCall - This member is used *only* for purposes of providing
Scott Michelfdc40a02009-02-17 22:15:04 +000068 /// helpful assertions that a libcall isn't created while another call is
Chris Lattner6831a812006-02-13 09:18:02 +000069 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
Scott Michelfdc40a02009-02-17 22:15:04 +000071
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Scott Michelfdc40a02009-02-17 22:15:04 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Dan Gohman475871a2008-07-27 21:46:04 +000088 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000089 LegalizedNodes.insert(std::make_pair(From, To));
90 // If someone requests legalization of the new node, return itself.
91 if (From != To)
92 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000093 }
94
Chris Lattner3e928bb2005-01-07 07:47:09 +000095public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000096 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000101 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000102 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 }
104
105 /// isTypeLegal - Return true if this type is legal on this target.
106 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000107 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 return getTypeAction(VT) == Legal;
109 }
110
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 void LegalizeDAG();
112
Chris Lattner456a93a2006-01-28 07:39:30 +0000113private:
Dan Gohman7f321562007-06-25 16:23:39 +0000114 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000115 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000116 void HandleOp(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Chris Lattnerc7029802006-03-18 01:44:44 +0000118 /// LegalizeOp - We know that the specified value has a legal type.
119 /// Recursively ensure that the operands have legal types, then return the
120 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Nate Begeman68679912008-04-25 18:07:40 +0000123 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
124 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
125 /// is necessary to spill the vector being inserted into to memory, perform
126 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000128 SDValue Idx, DebugLoc dl);
129 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
130 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000131
Mon P Wangf007a8b2008-11-06 05:31:54 +0000132 /// Useful 16 element vector type that is used to pass operands for widening.
Scott Michelfdc40a02009-02-17 22:15:04 +0000133 typedef SmallVector<SDValue, 16> SDValueVector;
134
Nate Begeman5a5ca152009-04-29 05:20:52 +0000135 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
136 /// performs the same shuffe in terms of order or result bytes, but on a type
137 /// whose vector element type is narrower than the original shuffle type.
138 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
139 SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
140 SDValue N1, SDValue N2,
141 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000142
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000143 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000144 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000145
Dale Johannesenbb5da912009-02-02 20:41:04 +0000146 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
147 DebugLoc dl);
148 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
149 DebugLoc dl);
150 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
151 DebugLoc dl) {
152 LegalizeSetCCOperands(LHS, RHS, CC, dl);
153 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000154 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Eli Friedman47b41f72009-05-27 02:21:29 +0000156 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000157 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
158 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
159 RTLIB::Libcall Call_PPCF128);
160 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, RTLIB::Libcall Call_I16,
161 RTLIB::Libcall Call_I32, RTLIB::Libcall Call_I64,
162 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000163
Dale Johannesen8a782a22009-02-02 22:12:50 +0000164 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue ExpandBUILD_VECTOR(SDNode *Node);
166 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000167 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
168 DebugLoc dl);
169 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
170 DebugLoc dl);
171 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
172 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000173
Dale Johannesen8a782a22009-02-02 22:12:50 +0000174 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
175 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000176
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000177 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman8c377c72009-05-27 01:25:56 +0000178
179 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
180 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000181};
182}
183
Nate Begeman5a5ca152009-04-29 05:20:52 +0000184/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
185/// performs the same shuffe in terms of order or result bytes, but on a type
186/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000187/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000188SDValue
189SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
190 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000191 SmallVectorImpl<int> &Mask) const {
192 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000193 unsigned NumMaskElts = VT.getVectorNumElements();
194 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000196
Nate Begeman9008ca62009-04-27 18:41:29 +0000197 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
198
199 if (NumEltsGrowth == 1)
200 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
201
202 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000203 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000204 int Idx = Mask[i];
205 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
206 if (Idx < 0)
207 NewMask.push_back(-1);
208 else
209 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000210 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000211 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000212 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000213 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
214 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000215}
216
Bill Wendling5aa49772009-02-24 02:35:30 +0000217SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000218 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000219 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000220 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000221 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223}
224
Chris Lattner3e928bb2005-01-07 07:47:09 +0000225void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000226 LastCALLSEQ_END = DAG.getEntryNode();
227 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000228
Chris Lattnerab510a72005-10-02 17:49:46 +0000229 // The legalize process is inherently a bottom-up recursive process (users
230 // legalize their uses before themselves). Given infinite stack space, we
231 // could just start legalizing on the root and traverse the whole graph. In
232 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000233 // blocks. To avoid this problem, compute an ordering of the nodes where each
234 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000235 DAG.AssignTopologicalOrder();
236 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
237 E = prior(DAG.allnodes_end()); I != next(E); ++I)
238 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000239
240 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000241 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000242 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
243 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000244
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245 LegalizedNodes.clear();
246
247 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000248 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000249}
250
Chris Lattner6831a812006-02-13 09:18:02 +0000251
252/// FindCallEndFromCallStart - Given a chained node that is part of a call
253/// sequence, find the CALLSEQ_END node that terminates the call sequence.
254static SDNode *FindCallEndFromCallStart(SDNode *Node) {
255 if (Node->getOpcode() == ISD::CALLSEQ_END)
256 return Node;
257 if (Node->use_empty())
258 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000259
Chris Lattner6831a812006-02-13 09:18:02 +0000260 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000262 if (TheChain.getValueType() != MVT::Other) {
263 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000264 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000265 if (TheChain.getValueType() != MVT::Other) {
266 // Otherwise, hunt for it.
267 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
268 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000269 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000270 break;
271 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
273 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000274 if (TheChain.getValueType() != MVT::Other)
275 return 0;
276 }
277 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Chris Lattner6831a812006-02-13 09:18:02 +0000279 for (SDNode::use_iterator UI = Node->use_begin(),
280 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Chris Lattner6831a812006-02-13 09:18:02 +0000282 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000283 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000284 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
285 if (User->getOperand(i) == TheChain)
286 if (SDNode *Result = FindCallEndFromCallStart(User))
287 return Result;
288 }
289 return 0;
290}
291
Scott Michelfdc40a02009-02-17 22:15:04 +0000292/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000293/// sequence, find the CALLSEQ_START node that initiates the call sequence.
294static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
295 assert(Node && "Didn't find callseq_start for a call??");
296 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000297
Chris Lattner6831a812006-02-13 09:18:02 +0000298 assert(Node->getOperand(0).getValueType() == MVT::Other &&
299 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000300 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000301}
302
303/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000304/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000305/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000306///
307/// Keep track of the nodes we fine that actually do lead to Dest in
308/// NodesLeadingTo. This avoids retraversing them exponential number of times.
309///
310bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000311 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000312 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000313
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000314 // If we've already processed this node and it does lead to Dest, there is no
315 // need to reprocess it.
316 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000317
Chris Lattner6831a812006-02-13 09:18:02 +0000318 // If the first result of this node has been already legalized, then it cannot
319 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000320 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000321
Chris Lattner6831a812006-02-13 09:18:02 +0000322 // Okay, this node has not already been legalized. Check and legalize all
323 // operands. If none lead to Dest, then we can legalize this node.
324 bool OperandsLeadToDest = false;
325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
326 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000327 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000328
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000329 if (OperandsLeadToDest) {
330 NodesLeadingTo.insert(N);
331 return true;
332 }
Chris Lattner6831a812006-02-13 09:18:02 +0000333
334 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000335 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000336 return false;
337}
338
Mon P Wang0c397192008-10-30 08:01:45 +0000339/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000340/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000341void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000342 // Don't touch TargetConstants
343 if (Op.getOpcode() == ISD::TargetConstant)
344 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000345 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000346 // We should never see any illegal result types here.
347 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Eli Friedman74807f22009-05-26 08:55:52 +0000348 (void)LegalizeOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000349}
Chris Lattner6831a812006-02-13 09:18:02 +0000350
Evan Cheng9f877882006-12-13 20:57:08 +0000351/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
352/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000353static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000354 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000355 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000356 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000357
358 // If a FP immediate is precise when represented as a float and if the
359 // target can do an extending load from float to double, we put it into
360 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000361 // double. This shrinks FP constants and canonicalizes them for targets where
362 // an FP extending load is the same cost as a normal load (such as on the x87
363 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000364 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000365 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000366 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000367 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000368 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000369 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000370 }
371
Duncan Sands83ec4b62008-06-06 12:08:01 +0000372 MVT OrigVT = VT;
373 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000374 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000375 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000376 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
377 // Only do this if the target has a native EXTLOAD instruction from
378 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000379 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000380 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000381 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000382 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
383 VT = SVT;
384 Extend = true;
385 }
Evan Cheng00495212006-12-12 21:32:44 +0000386 }
387
Dan Gohman475871a2008-07-27 21:46:04 +0000388 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000389 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000390 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000391 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000392 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000393 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000394 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000395 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000396 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000397}
398
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000399/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
400static
Dan Gohman475871a2008-07-27 21:46:04 +0000401SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000402 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000403 SDValue Chain = ST->getChain();
404 SDValue Ptr = ST->getBasePtr();
405 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000406 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000407 int Alignment = ST->getAlignment();
408 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000409 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000410 if (ST->getMemoryVT().isFloatingPoint() ||
411 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000412 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
413 if (TLI.isTypeLegal(intVT)) {
414 // Expand to a bitconvert of the value to the integer type of the
415 // same size, then a (misaligned) int store.
416 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000417 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
418 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000419 SVOffset, ST->isVolatile(), Alignment);
420 } else {
421 // Do a (aligned) store to a stack slot, then copy from the stack slot
422 // to the final destination using (unaligned) integer loads and stores.
423 MVT StoredVT = ST->getMemoryVT();
424 MVT RegVT =
425 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
426 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
427 unsigned RegBytes = RegVT.getSizeInBits() / 8;
428 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000429
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000430 // Make sure the stack slot is also aligned for the register type.
431 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
432
433 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000434 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000435 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000436 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
437 SmallVector<SDValue, 8> Stores;
438 unsigned Offset = 0;
439
440 // Do all but one copies using the full register width.
441 for (unsigned i = 1; i < NumRegs; i++) {
442 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000443 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000444 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000445 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000446 ST->getSrcValue(), SVOffset + Offset,
447 ST->isVolatile(),
448 MinAlign(ST->getAlignment(), Offset)));
449 // Increment the pointers.
450 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000451 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000452 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000453 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000454 }
455
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000456 // The last store may be partial. Do a truncating store. On big-endian
457 // machines this requires an extending load from the stack slot to ensure
458 // that the bits are in the right place.
459 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000460
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000461 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000462 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000463 NULL, 0, MemVT);
464
Dale Johannesenbb5da912009-02-02 20:41:04 +0000465 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000466 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000467 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000468 MinAlign(ST->getAlignment(), Offset)));
469 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000470 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000471 Stores.size());
472 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000473 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000474 assert(ST->getMemoryVT().isInteger() &&
475 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000476 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000477 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000478 MVT NewStoredVT =
479 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
480 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000481 int IncrementSize = NumBits / 8;
482
483 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
485 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000486 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000487
488 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000489 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000490 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000491 ST->getSrcValue(), SVOffset, NewStoredVT,
492 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000493 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000494 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000495 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000496 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000497 ST->getSrcValue(), SVOffset + IncrementSize,
498 NewStoredVT, ST->isVolatile(), Alignment);
499
Dale Johannesenbb5da912009-02-02 20:41:04 +0000500 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501}
502
503/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
504static
Dan Gohman475871a2008-07-27 21:46:04 +0000505SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000506 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000507 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000508 SDValue Chain = LD->getChain();
509 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000510 MVT VT = LD->getValueType(0);
511 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000513 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000514 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
515 if (TLI.isTypeLegal(intVT)) {
516 // Expand to a (misaligned) integer load of the same size,
517 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000519 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000520 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000522 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000523 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000524
Duncan Sands05e11fa2008-12-12 21:47:02 +0000525 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000526 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000527 } else {
528 // Copy the value to a (aligned) stack slot using (unaligned) integer
529 // loads and stores, then do a (aligned) load from the stack slot.
530 MVT RegVT = TLI.getRegisterType(intVT);
531 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
532 unsigned RegBytes = RegVT.getSizeInBits() / 8;
533 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
534
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000535 // Make sure the stack slot is also aligned for the register type.
536 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
537
Duncan Sands05e11fa2008-12-12 21:47:02 +0000538 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
539 SmallVector<SDValue, 8> Stores;
540 SDValue StackPtr = StackBase;
541 unsigned Offset = 0;
542
543 // Do all but one copies using the full register width.
544 for (unsigned i = 1; i < NumRegs; i++) {
545 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000546 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000547 SVOffset + Offset, LD->isVolatile(),
548 MinAlign(LD->getAlignment(), Offset));
549 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000550 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000551 NULL, 0));
552 // Increment the pointers.
553 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000554 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
555 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000556 Increment);
557 }
558
559 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000560 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000561 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000562 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000563 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000564 MinAlign(LD->getAlignment(), Offset));
565 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000566 // On big-endian machines this requires a truncating store to ensure
567 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000568 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000569 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000570
571 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000572 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000573 Stores.size());
574
575 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000576 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000577 NULL, 0, LoadedVT);
578
579 // Callers expect a MERGE_VALUES node.
580 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000581 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000582 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000583 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000584 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000585 "Unaligned load of unsupported type.");
586
Dale Johannesen8155d642008-02-27 22:36:00 +0000587 // Compute the new VT that is half the size of the old one. This is an
588 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000589 unsigned NumBits = LoadedVT.getSizeInBits();
590 MVT NewLoadedVT;
591 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000592 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000593
Chris Lattnere400af82007-11-19 21:38:03 +0000594 unsigned Alignment = LD->getAlignment();
595 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000596 ISD::LoadExtType HiExtType = LD->getExtensionType();
597
598 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
599 if (HiExtType == ISD::NON_EXTLOAD)
600 HiExtType = ISD::ZEXTLOAD;
601
602 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000603 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000604 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000605 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000606 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000607 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000608 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000609 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000610 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000611 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000612 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000613 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000614 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000615 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000616 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000617 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000619 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000620 }
621
622 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000624 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
625 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000626
Dale Johannesenbb5da912009-02-02 20:41:04 +0000627 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000628 Hi.getValue(1));
629
Dan Gohman475871a2008-07-27 21:46:04 +0000630 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000631 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632}
Evan Cheng912095b2007-01-04 21:56:39 +0000633
Nate Begeman68679912008-04-25 18:07:40 +0000634/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
635/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
636/// is necessary to spill the vector being inserted into to memory, perform
637/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000638SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000639PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
640 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000641 SDValue Tmp1 = Vec;
642 SDValue Tmp2 = Val;
643 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000644
Nate Begeman68679912008-04-25 18:07:40 +0000645 // If the target doesn't support this, we have to spill the input vector
646 // to a temporary stack slot, update the element, then reload it. This is
647 // badness. We could also load the value into a vector register (either
648 // with a "move to register" or "extload into register" instruction, then
649 // permute it into place, if the idx is a constant and if the idx is
650 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000651 MVT VT = Tmp1.getValueType();
652 MVT EltVT = VT.getVectorElementType();
653 MVT IdxVT = Tmp3.getValueType();
654 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000655 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000656
Gabor Greifba36cb52008-08-28 21:40:38 +0000657 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000658
659 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000660 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000661 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000662
663 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000664 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000665 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000666 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000667 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000668 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
669 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000670 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000671 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000672 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000673 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000674 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000675 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000676}
677
Mon P Wange9f10152008-12-09 05:46:39 +0000678
Eli Friedman3f727d62009-05-27 02:16:40 +0000679SDValue SelectionDAGLegalize::
680ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
681 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
682 // SCALAR_TO_VECTOR requires that the type of the value being inserted
683 // match the element type of the vector being created, except for
684 // integers in which case the inserted value can be over width.
685 MVT EltVT = Vec.getValueType().getVectorElementType();
686 if (Val.getValueType() == EltVT ||
687 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
688 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
689 Vec.getValueType(), Val);
690
691 unsigned NumElts = Vec.getValueType().getVectorNumElements();
692 // We generate a shuffle of InVec and ScVec, so the shuffle mask
693 // should be 0,1,2,3,4,5... with the appropriate element replaced with
694 // elt 0 of the RHS.
695 SmallVector<int, 8> ShufOps;
696 for (unsigned i = 0; i != NumElts; ++i)
697 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
698
699 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
700 &ShufOps[0]);
701 }
702 }
703 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
704}
705
Dan Gohmana3466152007-07-13 20:14:11 +0000706/// LegalizeOp - We know that the specified value has a legal type, and
707/// that its operands are legal. Now ensure that the operation itself
708/// is legal, recursively ensuring that the operands' operations remain
709/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000710SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000711 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
712 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000713
Gabor Greifba36cb52008-08-28 21:40:38 +0000714 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000715 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000716
Eli Friedman1fde9c52009-05-24 02:46:31 +0000717 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
718 assert(getTypeAction(Node->getValueType(i)) == Legal &&
719 "Unexpected illegal type!");
720
721 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
722 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
723 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
724 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725
Chris Lattner45982da2005-05-12 16:53:42 +0000726 // Note that LegalizeOp may be reentered even from single-use nodes, which
727 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000728 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000729 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000730
Dan Gohman475871a2008-07-27 21:46:04 +0000731 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
732 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000733 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000734
Eli Friedman8c377c72009-05-27 01:25:56 +0000735 // Figure out the correct action; the way to query this varies by opcode
736 TargetLowering::LegalizeAction Action;
737 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000738 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000739 case ISD::INTRINSIC_W_CHAIN:
740 case ISD::INTRINSIC_WO_CHAIN:
741 case ISD::INTRINSIC_VOID:
742 case ISD::VAARG:
743 case ISD::STACKSAVE:
744 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
745 break;
746 case ISD::SINT_TO_FP:
747 case ISD::UINT_TO_FP:
748 case ISD::EXTRACT_VECTOR_ELT:
749 Action = TLI.getOperationAction(Node->getOpcode(),
750 Node->getOperand(0).getValueType());
751 break;
752 case ISD::FP_ROUND_INREG:
753 case ISD::SIGN_EXTEND_INREG: {
754 MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
755 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
756 break;
757 }
758 case ISD::LOAD:
759 case ISD::STORE:
760 case ISD::BR_CC:
761 case ISD::FORMAL_ARGUMENTS:
762 case ISD::CALL:
763 case ISD::CALLSEQ_START:
764 case ISD::CALLSEQ_END:
765 case ISD::SELECT_CC:
766 case ISD::SETCC:
Eli Friedman8c377c72009-05-27 01:25:56 +0000767 // These instructions have properties that aren't modeled in the
768 // generic codepath
769 SimpleFinishLegalizing = false;
770 break;
771 case ISD::EXTRACT_ELEMENT:
772 case ISD::FLT_ROUNDS_:
773 case ISD::SADDO:
774 case ISD::SSUBO:
775 case ISD::UADDO:
776 case ISD::USUBO:
777 case ISD::SMULO:
778 case ISD::UMULO:
779 case ISD::FPOWI:
780 case ISD::MERGE_VALUES:
781 case ISD::EH_RETURN:
782 case ISD::FRAME_TO_ARGS_OFFSET:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000783 // These operations lie about being legal: when they claim to be legal,
784 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000785 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
786 if (Action == TargetLowering::Legal)
787 Action = TargetLowering::Expand;
788 break;
789 case ISD::TRAMPOLINE:
790 case ISD::FRAMEADDR:
791 case ISD::RETURNADDR:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000792 // These operations lie about being legal: they must always be
793 // custom-lowered.
Eli Friedman8c377c72009-05-27 01:25:56 +0000794 Action = TargetLowering::Custom;
795 break;
796 case ISD::BUILD_VECTOR:
797 // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize
798 // its operands first!
799 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000800 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000801 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000802 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000803 Action = TargetLowering::Legal;
804 } else {
805 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000806 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000807 break;
808 }
809
810 if (SimpleFinishLegalizing) {
811 SmallVector<SDValue, 8> Ops, ResultVals;
812 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
813 Ops.push_back(LegalizeOp(Node->getOperand(i)));
814 switch (Node->getOpcode()) {
815 default: break;
816 case ISD::BR:
817 case ISD::BRIND:
818 case ISD::BR_JT:
819 case ISD::BR_CC:
820 case ISD::BRCOND:
821 case ISD::RET:
822 // Branches tweak the chain to include LastCALLSEQ_END
823 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
824 LastCALLSEQ_END);
825 Ops[0] = LegalizeOp(Ops[0]);
826 LastCALLSEQ_END = DAG.getEntryNode();
827 break;
828 case ISD::SHL:
829 case ISD::SRL:
830 case ISD::SRA:
831 case ISD::ROTL:
832 case ISD::ROTR:
833 // Legalizing shifts/rotates requires adjusting the shift amount
834 // to the appropriate width.
835 if (!Ops[1].getValueType().isVector())
836 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
837 break;
838 }
839
840 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
841 Ops.size());
842 switch (Action) {
843 case TargetLowering::Legal:
844 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
845 ResultVals.push_back(Result.getValue(i));
846 break;
847 case TargetLowering::Custom:
848 // FIXME: The handling for custom lowering with multiple results is
849 // a complete mess.
850 Tmp1 = TLI.LowerOperation(Result, DAG);
851 if (Tmp1.getNode()) {
852 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
853 if (e == 1)
854 ResultVals.push_back(Tmp1);
855 else
856 ResultVals.push_back(Tmp1.getValue(i));
857 }
858 break;
859 }
860
861 // FALL THROUGH
862 case TargetLowering::Expand:
863 ExpandNode(Result.getNode(), ResultVals);
864 break;
865 case TargetLowering::Promote:
866 PromoteNode(Result.getNode(), ResultVals);
867 break;
868 }
869 if (!ResultVals.empty()) {
870 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
871 if (ResultVals[i] != SDValue(Node, i))
872 ResultVals[i] = LegalizeOp(ResultVals[i]);
873 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
874 }
875 return ResultVals[Op.getResNo()];
876 }
877 }
878
879 switch (Node->getOpcode()) {
880 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000881#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000882 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000883#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000884 assert(0 && "Do not know how to legalize this operator!");
885 abort();
Dan Gohman7f460202008-06-30 20:59:49 +0000886 case ISD::DBG_STOPPOINT:
887 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000889
Dan Gohman7f460202008-06-30 20:59:49 +0000890 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000891 case TargetLowering::Promote:
892 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000893 case TargetLowering::Expand: {
894 DwarfWriter *DW = DAG.getDwarfWriter();
895 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
896 MVT::Other);
897 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000898
Bill Wendling92c1e122009-02-13 02:16:35 +0000899 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
900 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
901 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
902 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000903
Bill Wendling92c1e122009-02-13 02:16:35 +0000904 unsigned Line = DSP->getLine();
905 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000906
Bill Wendling98a366d2009-04-29 23:29:43 +0000907 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000908 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
909 // won't hurt anything.
910 if (useDEBUG_LOC) {
911 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +0000912 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000913 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +0000914 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
915 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000916 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +0000917 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
918 }
Bill Wendling92c1e122009-02-13 02:16:35 +0000919 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000920 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +0000921 }
922 } else {
923 Result = Tmp1; // chain
924 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000925 break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000926 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000927 case TargetLowering::Custom:
928 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000929 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000930 break;
Evan Cheng71e86852008-07-08 20:06:39 +0000931 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +0000932 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +0000933 break;
934
Dan Gohman475871a2008-07-27 21:46:04 +0000935 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +0000936 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +0000937 Ops.push_back(Node->getOperand(1)); // line # must be legal.
938 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +0000939 Ops.push_back(Node->getOperand(3)); // filename must be legal.
940 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
941 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000942 break;
943 }
Evan Cheng71e86852008-07-08 20:06:39 +0000944 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000945 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000946 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000947 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000948 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000949 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +0000951 // A call within a calling sequence must be legalized to something
952 // other than the normal CALLSEQ_END. Violating this gets Legalize
953 // into an infinite loop.
954 assert ((!IsLegalizingCall ||
955 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000956 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +0000957 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000958
959 // The number of incoming and outgoing values should match; unless the final
960 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +0000961 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
962 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
963 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000964 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +0000965 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000966
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000967 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000968 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +0000969 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
970 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000971 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +0000972 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000973 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +0000974 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +0000975 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +0000976 }
977 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +0000978 case ISD::BUILD_VECTOR:
979 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000980 default: assert(0 && "This action is not supported yet!");
981 case TargetLowering::Custom:
982 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000983 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000984 Result = Tmp3;
985 break;
986 }
987 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000988 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +0000990 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000991 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000992 break;
Chris Lattner6831a812006-02-13 09:18:02 +0000993 case ISD::CALLSEQ_START: {
994 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +0000995
Chris Lattner6831a812006-02-13 09:18:02 +0000996 // Recursively Legalize all of the inputs of the call end that do not lead
997 // to this call start. This ensures that any libcalls that need be inserted
998 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +0000999 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001000 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001001 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001002 NodesLeadingTo);
1003 }
Chris Lattner6831a812006-02-13 09:18:02 +00001004
1005 // Now that we legalized all of the inputs (which may have inserted
1006 // libcalls) create the new CALLSEQ_START node.
1007 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1008
1009 // Merge in the last call, to ensure that this call start after the last
1010 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001011 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001012 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001013 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001014 Tmp1 = LegalizeOp(Tmp1);
1015 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001016
Chris Lattner6831a812006-02-13 09:18:02 +00001017 // Do not try to legalize the target-specific arguments (#1+).
1018 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001019 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001020 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001021 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001022 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001023
Chris Lattner6831a812006-02-13 09:18:02 +00001024 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001025 AddLegalizedOperand(Op.getValue(0), Result);
1026 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1027 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001028
Chris Lattner6831a812006-02-13 09:18:02 +00001029 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001030 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001031 // process, no libcalls can/will be inserted, guaranteeing that no calls
1032 // can overlap.
1033 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001034 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001035 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001036 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001037
Chris Lattner6831a812006-02-13 09:18:02 +00001038 // Legalize the call, starting from the CALLSEQ_END.
1039 LegalizeOp(LastCALLSEQ_END);
1040 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1041 return Result;
1042 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001043 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001044 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1045 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001046 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001047 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1048 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001049 assert(I != LegalizedNodes.end() &&
1050 "Legalizing the call start should have legalized this node!");
1051 return I->second;
1052 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001053
1054 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001055 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001057 // Do not try to legalize the target-specific arguments (#1+), except for
1058 // an optional flag input.
1059 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1060 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001061 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001062 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001063 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001064 }
1065 } else {
1066 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1067 if (Tmp1 != Node->getOperand(0) ||
1068 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001069 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001070 Ops[0] = Tmp1;
1071 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001072 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001073 }
Chris Lattner6a542892006-01-24 05:48:21 +00001074 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001075 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001076 // This finishes up call legalization.
1077 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Chris Lattner4b653a02006-02-14 00:55:02 +00001079 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001080 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001081 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001082 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001083 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001084 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001086 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1087 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1088 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001089 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001090
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001091 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001092 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001093 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001094 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001095 case TargetLowering::Expand: {
1096 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1097 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1098 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001099 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001100
1101 // Chain the dynamic stack allocation so that it doesn't modify the stack
1102 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001103 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001104
Dan Gohman475871a2008-07-27 21:46:04 +00001105 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001106 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001107 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001108 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001109 unsigned StackAlign =
1110 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1111 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001112 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001113 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001114 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001115 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001116
Dale Johannesenca57b842009-02-02 23:46:53 +00001117 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001118 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001119
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001120 Tmp1 = LegalizeOp(Tmp1);
1121 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001122 break;
1123 }
1124 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001125 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001126 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001127 Tmp1 = LegalizeOp(Tmp3);
1128 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001129 }
Chris Lattner903d2782006-01-15 08:54:32 +00001130 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001131 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001132 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001133 }
Chris Lattner903d2782006-01-15 08:54:32 +00001134 // Since this op produce two values, make sure to remember that we
1135 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001136 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1137 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001138 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001139 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001140 case ISD::BR_JT:
1141 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1142 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001143 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001144 Tmp1 = LegalizeOp(Tmp1);
1145 LastCALLSEQ_END = DAG.getEntryNode();
1146
1147 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1149
Scott Michelfdc40a02009-02-17 22:15:04 +00001150 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001151 default: assert(0 && "This action is not supported yet!");
1152 case TargetLowering::Legal: break;
1153 case TargetLowering::Custom:
1154 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001155 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001156 break;
1157 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001158 SDValue Chain = Result.getOperand(0);
1159 SDValue Table = Result.getOperand(1);
1160 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001161
Duncan Sands83ec4b62008-06-06 12:08:01 +00001162 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001163 MachineFunction &MF = DAG.getMachineFunction();
1164 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001165 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001166 Index, DAG.getConstant(EntrySize, PTy));
1167 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001168
Duncan Sands712f7b32008-12-12 08:13:38 +00001169 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001170 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001171 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001172 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001173 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001174 // For PIC, the sequence is:
1175 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001176 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001177 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001178 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001179 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001180 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001181 }
1182 }
1183 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001184 case ISD::BR_CC:
1185 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001186 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001187 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001188 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001189 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001190 Tmp3 = Node->getOperand(3); // RHS
1191 Tmp4 = Node->getOperand(1); // CC
1192
Scott Michelfdc40a02009-02-17 22:15:04 +00001193 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001194 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001195 LastCALLSEQ_END = DAG.getEntryNode();
1196
Evan Cheng7f042682008-10-15 02:05:31 +00001197 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001198 // the LHS is a legal SETCC itself. In this case, we need to compare
1199 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001200 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001201 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1202 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001203 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001204
1205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001206 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001207
Chris Lattner181b7a32005-12-17 23:46:46 +00001208 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1209 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001210 case TargetLowering::Legal: break;
1211 case TargetLowering::Custom:
1212 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001213 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001214 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001215 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001216 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001217 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001218 LoadSDNode *LD = cast<LoadSDNode>(Node);
1219 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1220 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001221
Evan Cheng466685d2006-10-09 20:57:25 +00001222 ISD::LoadExtType ExtType = LD->getExtensionType();
1223 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001224 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001225 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1226 Tmp3 = Result.getValue(0);
1227 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001228
Evan Cheng466685d2006-10-09 20:57:25 +00001229 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1230 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001231 case TargetLowering::Legal:
1232 // If this is an unaligned load and the target doesn't support it,
1233 // expand it.
1234 if (!TLI.allowsUnalignedMemoryAccesses()) {
1235 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001236 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001237 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001238 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001239 TLI);
1240 Tmp3 = Result.getOperand(0);
1241 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001242 Tmp3 = LegalizeOp(Tmp3);
1243 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001244 }
1245 }
1246 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001247 case TargetLowering::Custom:
1248 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001249 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001250 Tmp3 = LegalizeOp(Tmp1);
1251 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001252 }
Evan Cheng466685d2006-10-09 20:57:25 +00001253 break;
1254 case TargetLowering::Promote: {
1255 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001256 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001257 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001258 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001259
Dale Johannesenca57b842009-02-02 23:46:53 +00001260 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001261 LD->getSrcValueOffset(),
1262 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001263 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001264 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001265 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001266 }
Evan Cheng466685d2006-10-09 20:57:25 +00001267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001268 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001269 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001270 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1271 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001272 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001273 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001274 MVT SrcVT = LD->getMemoryVT();
1275 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001276 int SVOffset = LD->getSrcValueOffset();
1277 unsigned Alignment = LD->getAlignment();
1278 bool isVolatile = LD->isVolatile();
1279
Duncan Sands83ec4b62008-06-06 12:08:01 +00001280 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001281 // Some targets pretend to have an i1 loading operation, and actually
1282 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1283 // bits are guaranteed to be zero; it helps the optimizers understand
1284 // that these bits are zero. It is also useful for EXTLOAD, since it
1285 // tells the optimizers that those bits are undefined. It would be
1286 // nice to have an effective generic way of getting these benefits...
1287 // Until such a way is found, don't insist on promoting i1 here.
1288 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001289 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001290 // Promote to a byte-sized load if not loading an integral number of
1291 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001292 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1293 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001295
1296 // The extra bits are guaranteed to be zero, since we stored them that
1297 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1298
1299 ISD::LoadExtType NewExtType =
1300 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1301
Dale Johannesenca57b842009-02-02 23:46:53 +00001302 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001303 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1304 NVT, isVolatile, Alignment);
1305
1306 Ch = Result.getValue(1); // The chain.
1307
1308 if (ExtType == ISD::SEXTLOAD)
1309 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001310 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001311 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001312 Result, DAG.getValueType(SrcVT));
1313 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1314 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001315 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001316 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001317 DAG.getValueType(SrcVT));
1318
1319 Tmp1 = LegalizeOp(Result);
1320 Tmp2 = LegalizeOp(Ch);
1321 } else if (SrcWidth & (SrcWidth - 1)) {
1322 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001323 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001324 "Unsupported extload!");
1325 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1326 assert(RoundWidth < SrcWidth);
1327 unsigned ExtraWidth = SrcWidth - RoundWidth;
1328 assert(ExtraWidth < RoundWidth);
1329 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1330 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001331 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1332 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001334 unsigned IncrementSize;
1335
1336 if (TLI.isLittleEndian()) {
1337 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1338 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001339 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1340 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001341 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1342 Alignment);
1343
1344 // Load the remaining ExtraWidth bits.
1345 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001346 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001347 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001348 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001349 LD->getSrcValue(), SVOffset + IncrementSize,
1350 ExtraVT, isVolatile,
1351 MinAlign(Alignment, IncrementSize));
1352
1353 // Build a factor node to remember that this load is independent of the
1354 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001355 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001356 Hi.getValue(1));
1357
1358 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001359 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001360 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1361
1362 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001363 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001364 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001365 // Big endian - avoid unaligned loads.
1366 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1367 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001368 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001369 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1370 Alignment);
1371
1372 // Load the remaining ExtraWidth bits.
1373 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001374 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001375 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001376 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001377 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001378 LD->getSrcValue(), SVOffset + IncrementSize,
1379 ExtraVT, isVolatile,
1380 MinAlign(Alignment, IncrementSize));
1381
1382 // Build a factor node to remember that this load is independent of the
1383 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001384 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001385 Hi.getValue(1));
1386
1387 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001388 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001389 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1390
1391 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001392 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001393 }
1394
1395 Tmp1 = LegalizeOp(Result);
1396 Tmp2 = LegalizeOp(Ch);
1397 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001398 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001399 default: assert(0 && "This action is not supported yet!");
1400 case TargetLowering::Custom:
1401 isCustom = true;
1402 // FALLTHROUGH
1403 case TargetLowering::Legal:
1404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1405 Tmp1 = Result.getValue(0);
1406 Tmp2 = Result.getValue(1);
1407
1408 if (isCustom) {
1409 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001410 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001411 Tmp1 = LegalizeOp(Tmp3);
1412 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1413 }
1414 } else {
1415 // If this is an unaligned load and the target doesn't support it,
1416 // expand it.
1417 if (!TLI.allowsUnalignedMemoryAccesses()) {
1418 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001419 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001420 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001421 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001422 TLI);
1423 Tmp1 = Result.getOperand(0);
1424 Tmp2 = Result.getOperand(1);
1425 Tmp1 = LegalizeOp(Tmp1);
1426 Tmp2 = LegalizeOp(Tmp2);
1427 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001428 }
1429 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001430 break;
1431 case TargetLowering::Expand:
1432 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1433 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001434 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001435 LD->getSrcValueOffset(),
1436 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001437 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001438 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001439 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1440 Tmp2 = LegalizeOp(Load.getValue(1));
1441 break;
1442 }
1443 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1444 // Turn the unsupported load into an EXTLOAD followed by an explicit
1445 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001446 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001447 Tmp1, Tmp2, LD->getSrcValue(),
1448 LD->getSrcValueOffset(), SrcVT,
1449 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001451 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001452 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1453 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001454 Result, DAG.getValueType(SrcVT));
1455 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001456 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001457 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1458 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001459 break;
1460 }
Evan Cheng466685d2006-10-09 20:57:25 +00001461 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001462
Evan Cheng466685d2006-10-09 20:57:25 +00001463 // Since loads produce two values, make sure to remember that we legalized
1464 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001465 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1466 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001467 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001468 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001469 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001470 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001471 StoreSDNode *ST = cast<StoreSDNode>(Node);
1472 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1473 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001474 int SVOffset = ST->getSrcValueOffset();
1475 unsigned Alignment = ST->getAlignment();
1476 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001477
Evan Cheng8b2794a2006-10-13 21:14:26 +00001478 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001479 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1480 // FIXME: We shouldn't do this for TargetConstantFP's.
1481 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1482 // to phase ordering between legalized code and the dag combiner. This
1483 // probably means that we need to integrate dag combiner and legalizer
1484 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001485 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001486 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001487 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001488 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001489 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001490 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001491 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001492 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001493 SVOffset, isVolatile, Alignment);
1494 break;
1495 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001496 // If this target supports 64-bit registers, do a single 64-bit store.
1497 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001498 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001499 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001500 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001501 SVOffset, isVolatile, Alignment);
1502 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001503 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001504 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1505 // stores. If the target supports neither 32- nor 64-bits, this
1506 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001507 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1509 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001510 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001511
Dale Johannesenca57b842009-02-02 23:46:53 +00001512 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001513 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001514 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001515 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001516 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001517 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001518
Dale Johannesenca57b842009-02-02 23:46:53 +00001519 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001520 break;
1521 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001522 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001523 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001524
Eli Friedman957bffa2009-05-24 08:42:01 +00001525 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001526 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001528 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001529
Duncan Sands83ec4b62008-06-06 12:08:01 +00001530 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001531 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1532 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001533 case TargetLowering::Legal:
1534 // If this is an unaligned store and the target doesn't support it,
1535 // expand it.
1536 if (!TLI.allowsUnalignedMemoryAccesses()) {
1537 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001539 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001540 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001541 TLI);
1542 }
1543 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001544 case TargetLowering::Custom:
1545 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001547 break;
1548 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001549 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001550 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001551 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001552 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001553 ST->getSrcValue(), SVOffset, isVolatile,
1554 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001555 break;
1556 }
1557 break;
1558 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001559 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001560 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001561
Duncan Sands83ec4b62008-06-06 12:08:01 +00001562 MVT StVT = ST->getMemoryVT();
1563 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001564
Duncan Sands83ec4b62008-06-06 12:08:01 +00001565 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001566 // Promote to a byte-sized store with upper bits zero if not
1567 // storing an integral number of bytes. For example, promote
1568 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001569 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001570 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1571 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001572 SVOffset, NVT, isVolatile, Alignment);
1573 } else if (StWidth & (StWidth - 1)) {
1574 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001575 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001576 "Unsupported truncstore!");
1577 unsigned RoundWidth = 1 << Log2_32(StWidth);
1578 assert(RoundWidth < StWidth);
1579 unsigned ExtraWidth = StWidth - RoundWidth;
1580 assert(ExtraWidth < RoundWidth);
1581 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1582 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001583 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1584 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001585 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001586 unsigned IncrementSize;
1587
1588 if (TLI.isLittleEndian()) {
1589 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1590 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001591 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001592 SVOffset, RoundVT,
1593 isVolatile, Alignment);
1594
1595 // Store the remaining ExtraWidth bits.
1596 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001597 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001598 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001599 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001600 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001601 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001602 SVOffset + IncrementSize, ExtraVT, isVolatile,
1603 MinAlign(Alignment, IncrementSize));
1604 } else {
1605 // Big endian - avoid unaligned stores.
1606 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1607 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001608 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001609 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001610 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1611 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001612
1613 // Store the remaining ExtraWidth bits.
1614 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001615 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001616 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001617 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001618 SVOffset + IncrementSize, ExtraVT, isVolatile,
1619 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001620 }
Duncan Sands7e857202008-01-22 07:17:34 +00001621
1622 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001623 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001624 } else {
1625 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1626 Tmp2 != ST->getBasePtr())
1627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1628 ST->getOffset());
1629
1630 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1631 default: assert(0 && "This action is not supported yet!");
1632 case TargetLowering::Legal:
1633 // If this is an unaligned store and the target doesn't support it,
1634 // expand it.
1635 if (!TLI.allowsUnalignedMemoryAccesses()) {
1636 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001637 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001638 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001639 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001640 TLI);
1641 }
1642 break;
1643 case TargetLowering::Custom:
1644 Result = TLI.LowerOperation(Result, DAG);
1645 break;
1646 case Expand:
1647 // TRUNCSTORE:i16 i32 -> STORE i16
1648 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001649 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1650 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1651 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001652 break;
1653 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001654 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001655 }
1656 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001657 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001658 case ISD::SELECT_CC: {
1659 Tmp1 = Node->getOperand(0); // LHS
1660 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001661 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1662 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00001664
1665 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001666 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
Evan Cheng7f042682008-10-15 02:05:31 +00001668 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001669 // the LHS is a legal SETCC itself. In this case, we need to compare
1670 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001671 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001672 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1673 CC = DAG.getCondCode(ISD::SETNE);
1674 }
1675 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1676
1677 // Everything is legal, see if we should expand this op or something.
1678 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1679 default: assert(0 && "This action is not supported yet!");
1680 case TargetLowering::Legal: break;
1681 case TargetLowering::Custom:
1682 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001683 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001684 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001685 }
1686 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001687 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001688 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001689 Tmp1 = Node->getOperand(0);
1690 Tmp2 = Node->getOperand(1);
1691 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00001692 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
1694 // If we had to Expand the SetCC operands into a SELECT node, then it may
1695 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00001696 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00001697 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001698 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001699 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001700 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001701
Chris Lattner73e142f2006-01-30 22:43:50 +00001702 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001703 default: assert(0 && "Cannot handle this action for SETCC yet!");
1704 case TargetLowering::Custom:
1705 isCustom = true;
1706 // FALLTHROUGH.
1707 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00001708 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001709 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00001710 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001711 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00001712 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001713 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001714 case TargetLowering::Promote: {
1715 // First step, figure out the appropriate operation to use.
1716 // Allow SETCC to not be supported for all legal data types
1717 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00001718 MVT NewInTy = Node->getOperand(0).getValueType();
1719 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00001720
1721 // Scan for the appropriate larger type to use.
1722 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001723 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00001724
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00001726 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001727 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00001728 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00001729
Andrew Lenharthae355752005-11-30 17:12:26 +00001730 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001731 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001732 break;
1733 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001734 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00001735 assert(0 && "Cannot promote Legal Integer SETCC yet");
1736 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00001737 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
1738 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00001739 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001740 Tmp1 = LegalizeOp(Tmp1);
1741 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00001742 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00001743 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001744 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001745 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001746 case TargetLowering::Expand:
1747 // Expand a setcc node into a select_cc of the same condition, lhs, and
1748 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00001749 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001750 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00001751 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00001752 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001753 break;
1754 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001755 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001756
1757 // Binary operators
Chris Lattnera09f8482006-03-05 05:09:38 +00001758 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1759 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00001760 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00001761
Chris Lattnera09f8482006-03-05 05:09:38 +00001762 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Chris Lattnera09f8482006-03-05 05:09:38 +00001764 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1765 default: assert(0 && "Operation not supported");
1766 case TargetLowering::Custom:
1767 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001768 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001769 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001770 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00001771 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00001772 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00001773 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00001774 "Ugly special-cased code!");
1775 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00001776 SDValue SignBit;
1777 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
1778 if (isTypeLegal(IVT)) {
1779 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
1780 } else {
1781 assert(isTypeLegal(TLI.getPointerTy()) &&
1782 (TLI.getPointerTy() == MVT::i32 ||
1783 TLI.getPointerTy() == MVT::i64) &&
1784 "Legal type for load?!");
1785 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
1786 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
1787 SDValue Ch =
1788 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
1789 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
1790 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
1791 LoadPtr, DAG.getIntPtrConstant(4));
1792 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
1793 Ch, LoadPtr, NULL, 0, MVT::i32);
1794 }
1795 SignBit =
1796 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1797 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1798 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00001799 // Get the absolute value of the result.
1800 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1801 // Select between the nabs and abs value based on the sign bit of
1802 // the input.
1803 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1804 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
1805 AbsVal),
1806 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00001807 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00001808 break;
1809 }
Evan Cheng912095b2007-01-04 21:56:39 +00001810 }
Chris Lattnera09f8482006-03-05 05:09:38 +00001811 break;
Bill Wendling74c37652008-12-09 22:08:41 +00001812 case ISD::SADDO:
1813 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001814 MVT VT = Node->getValueType(0);
1815 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1816 default: assert(0 && "This action not supported for this op yet!");
1817 case TargetLowering::Custom:
1818 Result = TLI.LowerOperation(Op, DAG);
1819 if (Result.getNode()) break;
1820 // FALLTHROUGH
1821 case TargetLowering::Legal: {
1822 SDValue LHS = LegalizeOp(Node->getOperand(0));
1823 SDValue RHS = LegalizeOp(Node->getOperand(1));
1824
Scott Michelfdc40a02009-02-17 22:15:04 +00001825 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00001826 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00001827 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001828 MVT OType = Node->getValueType(1);
1829
Bill Wendlinga6af91a2008-11-25 08:19:22 +00001830 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001831
Bill Wendling740464e2008-11-25 19:40:17 +00001832 // LHSSign -> LHS >= 0
1833 // RHSSign -> RHS >= 0
1834 // SumSign -> Sum >= 0
1835 //
Bill Wendling74c37652008-12-09 22:08:41 +00001836 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00001837 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00001838 // Sub:
1839 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00001840 //
Dale Johannesenca57b842009-02-02 23:46:53 +00001841 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
1842 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00001843 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
1844 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00001845 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001846
Dale Johannesenca57b842009-02-02 23:46:53 +00001847 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
1848 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001849
Dale Johannesenca57b842009-02-02 23:46:53 +00001850 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001851
1852 MVT ValueVTs[] = { LHS.getValueType(), OType };
1853 SDValue Ops[] = { Sum, Cmp };
1854
Scott Michelfdc40a02009-02-17 22:15:04 +00001855 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001856 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001857 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001858 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00001859 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00001860 break;
1861 }
1862 }
1863
1864 break;
1865 }
Bill Wendling74c37652008-12-09 22:08:41 +00001866 case ISD::UADDO:
1867 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00001868 MVT VT = Node->getValueType(0);
1869 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1870 default: assert(0 && "This action not supported for this op yet!");
1871 case TargetLowering::Custom:
1872 Result = TLI.LowerOperation(Op, DAG);
1873 if (Result.getNode()) break;
1874 // FALLTHROUGH
1875 case TargetLowering::Legal: {
1876 SDValue LHS = LegalizeOp(Node->getOperand(0));
1877 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00001878
Bill Wendling74c37652008-12-09 22:08:41 +00001879 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00001880 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00001881 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001882 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00001883 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00001884 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00001885 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00001886
Bill Wendling41ea7e72008-11-24 19:21:46 +00001887 MVT ValueVTs[] = { LHS.getValueType(), OType };
1888 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00001889
Scott Michelfdc40a02009-02-17 22:15:04 +00001890 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001891 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001892 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001893 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00001894 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001895 break;
1896 }
1897 }
1898
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00001899 break;
1900 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001901 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001902
Chris Lattner4ddd2832006-04-08 04:13:17 +00001903 assert(Result.getValueType() == Op.getValueType() &&
1904 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001905
Chris Lattner456a93a2006-01-28 07:39:30 +00001906 // Make sure that the generated code is itself legal.
1907 if (Result != Op)
1908 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001909
Chris Lattner45982da2005-05-12 16:53:42 +00001910 // Note that LegalizeOp may be reentered even from single-use nodes, which
1911 // means that we always must cache transformed nodes.
1912 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001913 return Result;
1914}
1915
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001916SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1917 SDValue Vec = Op.getOperand(0);
1918 SDValue Idx = Op.getOperand(1);
1919 DebugLoc dl = Op.getDebugLoc();
1920 // Store the value to a temporary stack slot, then LOAD the returned part.
1921 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1922 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
1923
1924 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00001925 unsigned EltSize =
1926 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001927 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1928 DAG.getConstant(EltSize, Idx.getValueType()));
1929
1930 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1931 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1932 else
1933 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1934
1935 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1936
1937 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
1938}
1939
Nate Begeman750ac1b2006-02-01 07:19:44 +00001940/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
1941/// with condition CC on the current target. This usually involves legalizing
1942/// or promoting the arguments. In the case where LHS and RHS must be expanded,
1943/// there may be no choice but to create a new SetCC node to represent the
1944/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00001945/// LHS, and the SDValue returned in RHS has a nil SDNode value.
1946void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
1947 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001948 SDValue &CC,
1949 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00001950 LHS = LegalizeOp(LHS);
1951 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001952}
1953
Evan Cheng7f042682008-10-15 02:05:31 +00001954/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
1955/// condition code CC on the current target. This routine assumes LHS and rHS
1956/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
1957/// illegal condition code into AND / OR of multiple SETCC values.
1958void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
1959 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001960 SDValue &CC,
1961 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00001962 MVT OpVT = LHS.getValueType();
1963 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1964 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
1965 default: assert(0 && "Unknown condition code action!");
1966 case TargetLowering::Legal:
1967 // Nothing to do.
1968 break;
1969 case TargetLowering::Expand: {
1970 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1971 unsigned Opc = 0;
1972 switch (CCCode) {
1973 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00001974 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1975 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1976 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1977 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1978 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1979 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1980 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1981 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1982 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1983 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1984 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1985 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001986 // FIXME: Implement more expansions.
1987 }
1988
Dale Johannesenbb5da912009-02-02 20:41:04 +00001989 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1990 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1991 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001992 RHS = SDValue();
1993 CC = SDValue();
1994 break;
1995 }
1996 }
1997}
1998
Chris Lattner1401d152008-01-16 07:45:30 +00001999/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2000/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2001/// a load from the stack slot to DestVT, extending it if needed.
2002/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002003SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2004 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002005 MVT DestVT,
2006 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002007 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002008 unsigned SrcAlign =
2009 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2010 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002011 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002012
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002013 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002014 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002015 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2016
Duncan Sands83ec4b62008-06-06 12:08:01 +00002017 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2018 unsigned SlotSize = SlotVT.getSizeInBits();
2019 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002020 unsigned DestAlign =
2021 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002022
Chris Lattner1401d152008-01-16 07:45:30 +00002023 // Emit a store to the stack slot. Use a truncstore if the input value is
2024 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002025 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002026
Chris Lattner1401d152008-01-16 07:45:30 +00002027 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002028 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002029 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002030 else {
2031 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002032 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002033 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002034 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002035
Chris Lattner35481892005-12-23 00:16:34 +00002036 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002037 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002038 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002039
Chris Lattner1401d152008-01-16 07:45:30 +00002040 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002041 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002042 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002043}
2044
Dan Gohman475871a2008-07-27 21:46:04 +00002045SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002046 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002047 // Create a vector sized/aligned stack slot, store the value to element #0,
2048 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002050
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002051 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002052 int SPFI = StackPtrFI->getIndex();
2053
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002054 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2055 StackPtr,
2056 PseudoSourceValue::getFixedStack(SPFI), 0,
2057 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002058 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002059 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002060}
2061
2062
Chris Lattnerce872152006-03-19 06:31:19 +00002063/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002064/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002065SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002066 unsigned NumElems = Node->getNumOperands();
2067 SDValue SplatValue = Node->getOperand(0);
2068 DebugLoc dl = Node->getDebugLoc();
2069 MVT VT = Node->getValueType(0);
2070 MVT OpVT = SplatValue.getValueType();
2071 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002072
2073 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002074 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002075 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Dan Gohman475871a2008-07-27 21:46:04 +00002077 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002078 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002079 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002080 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002081 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002082 bool isConstant = true;
2083 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2084 SplatValue.getOpcode() != ISD::UNDEF)
2085 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002086
Evan Cheng033e6812006-03-24 01:17:21 +00002087 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002088 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002089 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002090 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002091 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002092 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002093 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002094
2095 // If this isn't a constant element or an undef, we can't use a constant
2096 // pool load.
2097 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2098 V.getOpcode() != ISD::UNDEF)
2099 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002100 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002101
Chris Lattnerce872152006-03-19 06:31:19 +00002102 if (isOnlyLowElement) {
2103 // If the low element is an undef too, then this whole things is an undef.
2104 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002105 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00002106 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002107 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00002108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002109
Chris Lattner2eb86532006-03-24 07:29:17 +00002110 // If all elements are constants, create a load from the constant pool.
2111 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002112 std::vector<Constant*> CV;
2113 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002114 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002115 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002116 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002117 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002118 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002119 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00002120 } else {
2121 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002122 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00002123 CV.push_back(UndefValue::get(OpNTy));
2124 }
2125 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002126 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002127 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002128 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002129 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002130 PseudoSourceValue::getConstantPool(), 0,
2131 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Gabor Greifba36cb52008-08-28 21:40:38 +00002134 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00002135 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00002136 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00002137
2138 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00002139 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002140 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00002141 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002142 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Chris Lattner87100e02006-03-20 01:52:29 +00002144 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00002145 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
2146 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00002147 }
2148 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002149
Evan Cheng033e6812006-03-24 01:17:21 +00002150 // If there are only two unique elements, we may be able to turn this into a
2151 // vector shuffle.
2152 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002153 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00002154 SDValue Val1 = Node->getOperand(1);
2155 SDValue Val2;
2156 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002157 if (MI->first != Val1)
2158 Val2 = MI->first;
2159 else
2160 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00002161
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002162 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002163 // vector shuffle has the undef vector on the RHS.
2164 if (Val1.getOpcode() == ISD::UNDEF)
2165 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002166
Evan Cheng033e6812006-03-24 01:17:21 +00002167 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00002168 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002169
2170 // Set elements of the shuffle mask for Val1.
2171 std::vector<unsigned> &Val1Elts = Values[Val1];
2172 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002173 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002174
2175 // Set elements of the shuffle mask for Val2.
2176 std::vector<unsigned> &Val2Elts = Values[Val2];
2177 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
2178 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00002179 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00002180
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002181 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002182 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00002183 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002184 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
2185 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00002186 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00002187 }
2188 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002189
Chris Lattnerce872152006-03-19 06:31:19 +00002190 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
2191 // aligned object on the stack, store each element into it, then load
2192 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00002193 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00002194 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00002195 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
2196 const Value *SV = PseudoSourceValue::getFixedStack(FI);
2197
Chris Lattnerce872152006-03-19 06:31:19 +00002198 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00002199 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002200 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00002201 // Store (in the right endianness) the elements to memory.
2202 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2203 // Ignore undef elements.
2204 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00002205
Chris Lattner841c8822006-03-22 01:46:54 +00002206 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00002207
Dan Gohman475871a2008-07-27 21:46:04 +00002208 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002209 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00002210
Dale Johannesen8a782a22009-02-02 22:12:50 +00002211 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
2212 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00002213 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002214
Dan Gohman475871a2008-07-27 21:46:04 +00002215 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00002216 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00002217 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002218 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00002219 else
2220 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002221
Chris Lattnerce872152006-03-19 06:31:19 +00002222 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00002223 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00002224}
2225
Chris Lattner77e77a62005-01-21 06:05:23 +00002226// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2227// does not fit into a register, return the lo part and set the hi part to the
2228// by-reg argument. If it does fit into a single register, return the result
2229// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002230SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002231 bool isSigned) {
Chris Lattner6831a812006-02-13 09:18:02 +00002232 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002233 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002234 // Legalizing the call will automatically add the previous call to the
2235 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002236 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002237
Chris Lattner77e77a62005-01-21 06:05:23 +00002238 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002239 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002240 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002241 MVT ArgVT = Node->getOperand(i).getValueType();
2242 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00002243 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002244 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002245 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002246 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002247 }
Bill Wendling056292f2008-09-16 21:48:12 +00002248 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002249 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002250
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002251 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002252 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002253 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002254 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002255 CallingConv::C, false, Callee, Args, DAG,
2256 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002257
Chris Lattner6831a812006-02-13 09:18:02 +00002258 // Legalize the call sequence, starting with the chain. This will advance
2259 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2260 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2261 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002262 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002263}
2264
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002265SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2266 RTLIB::Libcall Call_F32,
2267 RTLIB::Libcall Call_F64,
2268 RTLIB::Libcall Call_F80,
2269 RTLIB::Libcall Call_PPCF128) {
2270 RTLIB::Libcall LC;
2271 switch (Node->getValueType(0).getSimpleVT()) {
2272 default: assert(0 && "Unexpected request for libcall!");
2273 case MVT::f32: LC = Call_F32; break;
2274 case MVT::f64: LC = Call_F64; break;
2275 case MVT::f80: LC = Call_F80; break;
2276 case MVT::ppcf128: LC = Call_PPCF128; break;
2277 }
2278 return ExpandLibCall(LC, Node, false);
2279}
2280
2281SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2282 RTLIB::Libcall Call_I16,
2283 RTLIB::Libcall Call_I32,
2284 RTLIB::Libcall Call_I64,
2285 RTLIB::Libcall Call_I128) {
2286 RTLIB::Libcall LC;
2287 switch (Node->getValueType(0).getSimpleVT()) {
2288 default: assert(0 && "Unexpected request for libcall!");
2289 case MVT::i16: LC = Call_I16; break;
2290 case MVT::i32: LC = Call_I32; break;
2291 case MVT::i64: LC = Call_I64; break;
2292 case MVT::i128: LC = Call_I128; break;
2293 }
2294 return ExpandLibCall(LC, Node, isSigned);
2295}
2296
Chris Lattner22cde6a2006-01-28 08:25:58 +00002297/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2298/// INT_TO_FP operation of the specified operand when the target requests that
2299/// we expand it. At this point, we know that the result and operand types are
2300/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002301SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2302 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00002303 MVT DestVT,
2304 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002305 if (Op0.getValueType() == MVT::i32) {
2306 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002307
Chris Lattner23594d42008-01-16 07:03:22 +00002308 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00002309 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002310
Chris Lattner22cde6a2006-01-28 08:25:58 +00002311 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002312 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002313 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002314 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002315 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002316 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002317 if (TLI.isLittleEndian())
2318 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002319
Chris Lattner22cde6a2006-01-28 08:25:58 +00002320 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002321 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002322 if (isSigned) {
2323 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00002324 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00002325 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002326 } else {
2327 Op0Mapped = Op0;
2328 }
2329 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002330 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002331 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002332 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00002333 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002334 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00002335 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002336 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00002337 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002338 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002339 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002340 BitsToDouble(0x4330000080000000ULL) :
2341 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002342 MVT::f64);
2343 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00002344 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002345 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002347 // handle final rounding
2348 if (DestVT == MVT::f64) {
2349 // do nothing
2350 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00002351 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002352 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002353 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002354 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002355 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002356 }
2357 return Result;
2358 }
2359 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00002360 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002361
Dale Johannesenaf435272009-02-02 19:03:57 +00002362 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002363 Op0, DAG.getConstant(0, Op0.getValueType()),
2364 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00002365 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00002366 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002367 SignSet, Four, Zero);
2368
2369 // If the sign bit of the integer is set, the large number will be treated
2370 // as a negative number. To counteract this, the dynamic code adds an
2371 // offset depending on the data type.
2372 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002374 default: assert(0 && "Unsupported integer type!");
2375 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2376 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2377 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2378 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2379 }
2380 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00002381 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002382
Dan Gohman475871a2008-07-27 21:46:04 +00002383 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002384 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00002385 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00002386 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00002387 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002388 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00002389 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002390 PseudoSourceValue::getConstantPool(), 0,
2391 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002392 else {
Dan Gohman69de1932008-02-06 22:27:42 +00002393 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00002394 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00002395 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00002396 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00002397 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002398 }
2399
Dale Johannesenaf435272009-02-02 19:03:57 +00002400 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002401}
2402
2403/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2404/// *INT_TO_FP operation of the specified operand when the target requests that
2405/// we promote it. At this point, we know that the result and operand types are
2406/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2407/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002408SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
2409 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002410 bool isSigned,
2411 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002412 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002413 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002414
2415 unsigned OpToUse = 0;
2416
2417 // Scan for the appropriate larger type to use.
2418 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002419 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
2420 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002421
2422 // If the target supports SINT_TO_FP of this type, use it.
2423 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
2424 default: break;
2425 case TargetLowering::Legal:
2426 if (!TLI.isTypeLegal(NewInTy))
2427 break; // Can't use this datatype.
2428 // FALL THROUGH.
2429 case TargetLowering::Custom:
2430 OpToUse = ISD::SINT_TO_FP;
2431 break;
2432 }
2433 if (OpToUse) break;
2434 if (isSigned) continue;
2435
2436 // If the target supports UINT_TO_FP of this type, use it.
2437 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
2438 default: break;
2439 case TargetLowering::Legal:
2440 if (!TLI.isTypeLegal(NewInTy))
2441 break; // Can't use this datatype.
2442 // FALL THROUGH.
2443 case TargetLowering::Custom:
2444 OpToUse = ISD::UINT_TO_FP;
2445 break;
2446 }
2447 if (OpToUse) break;
2448
2449 // Otherwise, try a larger type.
2450 }
2451
2452 // Okay, we found the operation and type to use. Zero extend our input to the
2453 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002454 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002455 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002456 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002457}
2458
2459/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2460/// FP_TO_*INT operation of the specified operand when the target requests that
2461/// we promote it. At this point, we know that the result and operand types are
2462/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2463/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002464SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
2465 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002466 bool isSigned,
2467 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002468 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002469 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002470
2471 unsigned OpToUse = 0;
2472
2473 // Scan for the appropriate larger type to use.
2474 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002475 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
2476 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002477
2478 // If the target supports FP_TO_SINT returning this type, use it.
2479 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
2480 default: break;
2481 case TargetLowering::Legal:
2482 if (!TLI.isTypeLegal(NewOutTy))
2483 break; // Can't use this datatype.
2484 // FALL THROUGH.
2485 case TargetLowering::Custom:
2486 OpToUse = ISD::FP_TO_SINT;
2487 break;
2488 }
2489 if (OpToUse) break;
2490
2491 // If the target supports FP_TO_UINT of this type, use it.
2492 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
2493 default: break;
2494 case TargetLowering::Legal:
2495 if (!TLI.isTypeLegal(NewOutTy))
2496 break; // Can't use this datatype.
2497 // FALL THROUGH.
2498 case TargetLowering::Custom:
2499 OpToUse = ISD::FP_TO_UINT;
2500 break;
2501 }
2502 if (OpToUse) break;
2503
2504 // Otherwise, try a larger type.
2505 }
2506
Scott Michelfdc40a02009-02-17 22:15:04 +00002507
Chris Lattner27a6c732007-11-24 07:07:01 +00002508 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002509 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002510
Chris Lattner27a6c732007-11-24 07:07:01 +00002511 // If the operation produces an invalid type, it must be custom lowered. Use
2512 // the target lowering hooks to expand it. Just keep the low part of the
2513 // expanded operation, we know that we're truncating anyway.
2514 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00002515 SmallVector<SDValue, 2> Results;
2516 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
2517 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
2518 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00002519 }
Duncan Sands126d9072008-07-04 11:47:58 +00002520
Chris Lattner27a6c732007-11-24 07:07:01 +00002521 // Truncate the result of the extended FP_TO_*INT operation to the desired
2522 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002523 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002524}
2525
2526/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2527///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002528SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 MVT VT = Op.getValueType();
2530 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002531 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002532 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002533 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2534 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002535 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2536 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2537 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002538 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002539 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2540 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2541 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2542 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2543 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2544 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2545 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2546 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2547 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002548 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002549 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2550 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2551 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2552 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2553 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2554 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2555 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2556 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2557 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2558 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2559 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2560 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2561 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2562 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2563 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2564 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2565 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2566 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2567 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2568 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2569 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002570 }
2571}
2572
2573/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2574///
Scott Michelfdc40a02009-02-17 22:15:04 +00002575SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002576 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002577 switch (Opc) {
2578 default: assert(0 && "Cannot expand this yet!");
2579 case ISD::CTPOP: {
2580 static const uint64_t mask[6] = {
2581 0x5555555555555555ULL, 0x3333333333333333ULL,
2582 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2583 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2584 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00002585 MVT VT = Op.getValueType();
2586 MVT ShVT = TLI.getShiftAmountTy();
2587 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002588 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2589 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002590 unsigned EltSize = VT.isVector() ?
2591 VT.getVectorElementType().getSizeInBits() : len;
2592 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002593 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002594 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00002595 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002596 DAG.getNode(ISD::AND, dl, VT,
2597 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
2598 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002599 }
2600 return Op;
2601 }
2602 case ISD::CTLZ: {
2603 // for now, we do this:
2604 // x = x | (x >> 1);
2605 // x = x | (x >> 2);
2606 // ...
2607 // x = x | (x >>16);
2608 // x = x | (x >>32); // for 64-bit input
2609 // return popcount(~x);
2610 //
2611 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612 MVT VT = Op.getValueType();
2613 MVT ShVT = TLI.getShiftAmountTy();
2614 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002615 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002617 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002618 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002619 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002620 Op = DAG.getNOT(dl, Op, VT);
2621 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002622 }
2623 case ISD::CTTZ: {
2624 // for now, we use: { return popcount(~x & (x - 1)); }
2625 // unless the target has ctlz but not ctpop, in which case we use:
2626 // { return 32 - nlz(~x & (x-1)); }
2627 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00002628 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002629 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2630 DAG.getNOT(dl, Op, VT),
2631 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002632 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002633 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002634 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2635 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002636 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002637 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002638 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2639 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002640 }
2641 }
2642}
Chris Lattnere34b3962005-01-19 04:19:40 +00002643
Eli Friedman8c377c72009-05-27 01:25:56 +00002644void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2645 SmallVectorImpl<SDValue> &Results) {
2646 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002647 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00002648 switch (Node->getOpcode()) {
2649 case ISD::CTPOP:
2650 case ISD::CTLZ:
2651 case ISD::CTTZ:
2652 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2653 Results.push_back(Tmp1);
2654 break;
2655 case ISD::BSWAP:
2656 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
2657 break;
2658 case ISD::FRAMEADDR:
2659 case ISD::RETURNADDR:
2660 case ISD::FRAME_TO_ARGS_OFFSET:
2661 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2662 break;
2663 case ISD::FLT_ROUNDS_:
2664 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2665 break;
2666 case ISD::EH_RETURN:
2667 case ISD::DECLARE:
2668 case ISD::DBG_LABEL:
2669 case ISD::EH_LABEL:
2670 case ISD::PREFETCH:
2671 case ISD::MEMBARRIER:
2672 case ISD::VAEND:
2673 Results.push_back(Node->getOperand(0));
2674 break;
2675 case ISD::MERGE_VALUES:
2676 for (unsigned i = 0; i < Node->getNumValues(); i++)
2677 Results.push_back(Node->getOperand(i));
2678 break;
2679 case ISD::UNDEF: {
2680 MVT VT = Node->getValueType(0);
2681 if (VT.isInteger())
2682 Results.push_back(DAG.getConstant(0, VT));
2683 else if (VT.isFloatingPoint())
2684 Results.push_back(DAG.getConstantFP(0, VT));
2685 else
2686 assert(0 && "Unknown value type!");
2687 break;
2688 }
2689 case ISD::TRAP: {
2690 // If this operation is not supported, lower it to 'abort()' call
2691 TargetLowering::ArgListTy Args;
2692 std::pair<SDValue, SDValue> CallResult =
2693 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
2694 false, false, false, false, CallingConv::C, false,
2695 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
2696 Args, DAG, dl);
2697 Results.push_back(CallResult.second);
2698 break;
2699 }
2700 case ISD::FP_ROUND:
2701 case ISD::BIT_CONVERT:
2702 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2703 Node->getValueType(0), dl);
2704 Results.push_back(Tmp1);
2705 break;
2706 case ISD::FP_EXTEND:
2707 Tmp1 = EmitStackConvert(Node->getOperand(0),
2708 Node->getOperand(0).getValueType(),
2709 Node->getValueType(0), dl);
2710 Results.push_back(Tmp1);
2711 break;
2712 case ISD::SIGN_EXTEND_INREG: {
2713 // NOTE: we could fall back on load/store here too for targets without
2714 // SAR. However, it is doubtful that any exist.
2715 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2716 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
2717 ExtraVT.getSizeInBits();
2718 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
2719 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2720 Node->getOperand(0), ShiftCst);
2721 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2722 Results.push_back(Tmp1);
2723 break;
2724 }
2725 case ISD::FP_ROUND_INREG: {
2726 // The only way we can lower this is to turn it into a TRUNCSTORE,
2727 // EXTLOAD pair, targetting a temporary location (a stack slot).
2728
2729 // NOTE: there is a choice here between constantly creating new stack
2730 // slots and always reusing the same one. We currently always create
2731 // new ones, as reuse may inhibit scheduling.
2732 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
2733 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2734 Node->getValueType(0), dl);
2735 Results.push_back(Tmp1);
2736 break;
2737 }
2738 case ISD::SINT_TO_FP:
2739 case ISD::UINT_TO_FP:
2740 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2741 Node->getOperand(0), Node->getValueType(0), dl);
2742 Results.push_back(Tmp1);
2743 break;
2744 case ISD::FP_TO_UINT: {
2745 SDValue True, False;
2746 MVT VT = Node->getOperand(0).getValueType();
2747 MVT NVT = Node->getValueType(0);
2748 const uint64_t zero[] = {0, 0};
2749 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
2750 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2751 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2752 Tmp1 = DAG.getConstantFP(apf, VT);
2753 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2754 Node->getOperand(0),
2755 Tmp1, ISD::SETLT);
2756 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
2757 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2758 DAG.getNode(ISD::FSUB, dl, VT,
2759 Node->getOperand(0), Tmp1));
2760 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2761 DAG.getConstant(x, NVT));
2762 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2763 Results.push_back(Tmp1);
2764 break;
2765 }
Eli Friedman509150f2009-05-27 07:58:35 +00002766 case ISD::VAARG: {
2767 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2768 MVT VT = Node->getValueType(0);
2769 Tmp1 = Node->getOperand(0);
2770 Tmp2 = Node->getOperand(1);
2771 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
2772 // Increment the pointer, VAList, to the next vaarg
2773 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2774 DAG.getConstant(TLI.getTargetData()->
2775 getTypeAllocSize(VT.getTypeForMVT()),
2776 TLI.getPointerTy()));
2777 // Store the incremented VAList to the legalized pointer
2778 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
2779 // Load the actual argument out of the pointer VAList
2780 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0));
2781 Results.push_back(Results[0].getValue(1));
2782 break;
2783 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002784 case ISD::VACOPY: {
2785 // This defaults to loading a pointer from the input and storing it to the
2786 // output, returning the chain.
2787 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2788 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2789 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
2790 Node->getOperand(2), VS, 0);
2791 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
2792 Results.push_back(Tmp1);
2793 break;
2794 }
2795 case ISD::EXTRACT_VECTOR_ELT:
2796 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2797 // This must be an access of the only element. Return it.
2798 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
2799 Node->getOperand(0));
2800 else
2801 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2802 Results.push_back(Tmp1);
2803 break;
2804 case ISD::EXTRACT_SUBVECTOR:
2805 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
2806 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002807 case ISD::CONCAT_VECTORS: {
2808 // Use extract/insert/build vector for now. We might try to be
2809 // more clever later.
2810 SmallVector<SDValue, 8> Ops;
2811 unsigned NumOperands = Node->getNumOperands();
2812 for (unsigned i=0; i < NumOperands; ++i) {
2813 SDValue SubOp = Node->getOperand(i);
2814 MVT VVT = SubOp.getNode()->getValueType(0);
2815 MVT EltVT = VVT.getVectorElementType();
2816 unsigned NumSubElem = VVT.getVectorNumElements();
2817 for (unsigned j=0; j < NumSubElem; ++j) {
2818 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
2819 DAG.getIntPtrConstant(j)));
2820 }
2821 }
2822 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, Node->getValueType(0),
2823 &Ops[0], Ops.size());
2824 Results.push_back(Tmp1);
2825 break;
2826 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002827 case ISD::SCALAR_TO_VECTOR:
2828 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
2829 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002830 case ISD::INSERT_VECTOR_ELT:
2831 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2832 Node->getOperand(1),
2833 Node->getOperand(2), dl));
2834 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002835 case ISD::VECTOR_SHUFFLE: {
2836 SmallVector<int, 8> Mask;
2837 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2838
2839 MVT VT = Node->getValueType(0);
2840 MVT EltVT = VT.getVectorElementType();
2841 unsigned NumElems = VT.getVectorNumElements();
2842 SmallVector<SDValue, 8> Ops;
2843 for (unsigned i = 0; i != NumElems; ++i) {
2844 if (Mask[i] < 0) {
2845 Ops.push_back(DAG.getUNDEF(EltVT));
2846 continue;
2847 }
2848 unsigned Idx = Mask[i];
2849 if (Idx < NumElems)
2850 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2851 Node->getOperand(0),
2852 DAG.getIntPtrConstant(Idx)));
2853 else
2854 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2855 Node->getOperand(1),
2856 DAG.getIntPtrConstant(Idx - NumElems)));
2857 }
2858 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2859 Results.push_back(Tmp1);
2860 break;
2861 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002862 case ISD::EXTRACT_ELEMENT: {
2863 MVT OpTy = Node->getOperand(0).getValueType();
2864 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2865 // 1 -> Hi
2866 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2867 DAG.getConstant(OpTy.getSizeInBits()/2,
2868 TLI.getShiftAmountTy()));
2869 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2870 } else {
2871 // 0 -> Lo
2872 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2873 Node->getOperand(0));
2874 }
2875 Results.push_back(Tmp1);
2876 break;
2877 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002878 case ISD::STACKSAVE:
2879 // Expand to CopyFromReg if the target set
2880 // StackPointerRegisterToSaveRestore.
2881 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2882 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2883 Node->getValueType(0)));
2884 Results.push_back(Results[0].getValue(1));
2885 } else {
2886 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
2887 Results.push_back(Node->getOperand(0));
2888 }
2889 break;
2890 case ISD::STACKRESTORE:
2891 // Expand to CopyToReg if the target set
2892 // StackPointerRegisterToSaveRestore.
2893 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2894 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2895 Node->getOperand(1)));
2896 } else {
2897 Results.push_back(Node->getOperand(0));
2898 }
2899 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002900 case ISD::FNEG:
2901 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2902 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2903 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2904 Node->getOperand(0));
2905 Results.push_back(Tmp1);
2906 break;
2907 case ISD::FABS: {
2908 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2909 MVT VT = Node->getValueType(0);
2910 Tmp1 = Node->getOperand(0);
2911 Tmp2 = DAG.getConstantFP(0.0, VT);
2912 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
2913 Tmp1, Tmp2, ISD::SETUGT);
2914 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2915 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
2916 Results.push_back(Tmp1);
2917 break;
2918 }
2919 case ISD::FSQRT:
2920 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2921 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
2922 break;
2923 case ISD::FSIN:
2924 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2925 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
2926 break;
2927 case ISD::FCOS:
2928 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2929 RTLIB::COS_F80, RTLIB::COS_PPCF128));
2930 break;
2931 case ISD::FLOG:
2932 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2933 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
2934 break;
2935 case ISD::FLOG2:
2936 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2937 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
2938 break;
2939 case ISD::FLOG10:
2940 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2941 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
2942 break;
2943 case ISD::FEXP:
2944 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2945 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
2946 break;
2947 case ISD::FEXP2:
2948 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2949 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
2950 break;
2951 case ISD::FTRUNC:
2952 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2953 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
2954 break;
2955 case ISD::FFLOOR:
2956 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2957 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
2958 break;
2959 case ISD::FCEIL:
2960 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2961 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
2962 break;
2963 case ISD::FRINT:
2964 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2965 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
2966 break;
2967 case ISD::FNEARBYINT:
2968 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2969 RTLIB::NEARBYINT_F64,
2970 RTLIB::NEARBYINT_F80,
2971 RTLIB::NEARBYINT_PPCF128));
2972 break;
2973 case ISD::FPOWI:
2974 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2975 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
2976 break;
2977 case ISD::FPOW:
2978 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2979 RTLIB::POW_F80, RTLIB::POW_PPCF128));
2980 break;
2981 case ISD::FDIV:
2982 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2983 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
2984 break;
2985 case ISD::FREM:
2986 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2987 RTLIB::REM_F80, RTLIB::REM_PPCF128));
2988 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002989 case ISD::ConstantFP: {
2990 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
2991 // Check to see if this FP immediate is already legal.
2992 bool isLegal = false;
2993 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
2994 E = TLI.legal_fpimm_end(); I != E; ++I) {
2995 if (CFP->isExactlyValue(*I)) {
2996 isLegal = true;
2997 break;
2998 }
2999 }
3000 // If this is a legal constant, turn it into a TargetConstantFP node.
3001 if (isLegal)
3002 Results.push_back(SDValue(Node, 0));
3003 else
3004 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
3005 break;
3006 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003007 case ISD::EHSELECTION: {
3008 unsigned Reg = TLI.getExceptionSelectorRegister();
3009 assert(Reg && "Can't expand to unknown register!");
3010 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3011 Node->getValueType(0)));
3012 Results.push_back(Results[0].getValue(1));
3013 break;
3014 }
3015 case ISD::EXCEPTIONADDR: {
3016 unsigned Reg = TLI.getExceptionAddressRegister();
3017 assert(Reg && "Can't expand to unknown register!");
3018 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3019 Node->getValueType(0)));
3020 Results.push_back(Results[0].getValue(1));
3021 break;
3022 }
3023 case ISD::SUB: {
3024 MVT VT = Node->getValueType(0);
3025 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3026 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3027 "Don't know how to expand this subtraction!");
3028 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3029 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
3030 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3031 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3032 break;
3033 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003034 case ISD::UREM:
3035 case ISD::SREM: {
Eli Friedman26ea8f92009-05-27 07:05:37 +00003036 MVT VT = Node->getValueType(0);
3037 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003038 bool isSigned = Node->getOpcode() == ISD::SREM;
3039 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3040 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3041 Tmp2 = Node->getOperand(0);
3042 Tmp3 = Node->getOperand(1);
3043 if (TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
3044 // X % Y -> X-X/Y*Y
3045 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3046 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3047 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
3048 } else if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3049 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3050 } else if (isSigned) {
3051 Tmp1 = ExpandIntLibCall(Node, true, RTLIB::SREM_I16, RTLIB::SREM_I32,
3052 RTLIB::SREM_I64, RTLIB::SREM_I128);
3053 } else {
Eli Friedman26ea8f92009-05-27 07:05:37 +00003054 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::UREM_I16, RTLIB::UREM_I32,
3055 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003056 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003057 Results.push_back(Tmp1);
3058 break;
3059 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003060 case ISD::UDIV:
3061 case ISD::SDIV: {
3062 bool isSigned = Node->getOpcode() == ISD::SDIV;
3063 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003064 MVT VT = Node->getValueType(0);
3065 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003066 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3067 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3068 Node->getOperand(1));
3069 else if (isSigned)
Eli Friedman26ea8f92009-05-27 07:05:37 +00003070 Tmp1 = ExpandIntLibCall(Node, true, RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3071 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003072 else
3073 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3074 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003075 Results.push_back(Tmp1);
3076 break;
3077 }
3078 case ISD::MULHU:
3079 case ISD::MULHS: {
3080 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3081 ISD::SMUL_LOHI;
3082 MVT VT = Node->getValueType(0);
3083 SDVTList VTs = DAG.getVTList(VT, VT);
3084 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3085 "If this wasn't legal, it shouldn't have been created!");
3086 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3087 Node->getOperand(1));
3088 Results.push_back(Tmp1.getValue(1));
3089 break;
3090 }
3091 case ISD::MUL: {
3092 MVT VT = Node->getValueType(0);
3093 SDVTList VTs = DAG.getVTList(VT, VT);
3094 // See if multiply or divide can be lowered using two-result operations.
3095 // We just need the low half of the multiply; try both the signed
3096 // and unsigned forms. If the target supports both SMUL_LOHI and
3097 // UMUL_LOHI, form a preference by checking which forms of plain
3098 // MULH it supports.
3099 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3100 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3101 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3102 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3103 unsigned OpToUse = 0;
3104 if (HasSMUL_LOHI && !HasMULHS) {
3105 OpToUse = ISD::SMUL_LOHI;
3106 } else if (HasUMUL_LOHI && !HasMULHU) {
3107 OpToUse = ISD::UMUL_LOHI;
3108 } else if (HasSMUL_LOHI) {
3109 OpToUse = ISD::SMUL_LOHI;
3110 } else if (HasUMUL_LOHI) {
3111 OpToUse = ISD::UMUL_LOHI;
3112 }
3113 if (OpToUse) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003114 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3115 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003116 break;
3117 }
3118 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::MUL_I16, RTLIB::MUL_I32,
3119 RTLIB::MUL_I64, RTLIB::MUL_I128);
3120 Results.push_back(Tmp1);
3121 break;
3122 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003123 case ISD::BUILD_PAIR: {
3124 MVT PairTy = Node->getValueType(0);
3125 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3126 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
3127 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
3128 DAG.getConstant(PairTy.getSizeInBits()/2,
3129 TLI.getShiftAmountTy()));
3130 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
3131 break;
3132 }
Eli Friedman509150f2009-05-27 07:58:35 +00003133 case ISD::SELECT:
3134 Tmp1 = Node->getOperand(0);
3135 Tmp2 = Node->getOperand(1);
3136 Tmp3 = Node->getOperand(2);
3137 if (Tmp1.getOpcode() == ISD::SETCC) {
3138 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3139 Tmp2, Tmp3,
3140 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
3141 } else {
3142 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3143 DAG.getConstant(0, Tmp1.getValueType()),
3144 Tmp2, Tmp3, ISD::SETNE);
3145 }
3146 Results.push_back(Tmp1);
3147 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003148 case ISD::BRCOND:
3149 // Expand brcond's setcc into its constituent parts and create a BR_CC
3150 // Node.
3151 Tmp1 = Node->getOperand(0);
3152 Tmp2 = Node->getOperand(1);
3153 if (Tmp2.getOpcode() == ISD::SETCC) {
3154 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
3155 Tmp1, Tmp2.getOperand(2),
3156 Tmp2.getOperand(0), Tmp2.getOperand(1),
3157 Node->getOperand(2));
3158 } else {
3159 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
3160 DAG.getCondCode(ISD::SETNE), Tmp2,
3161 DAG.getConstant(0, Tmp2.getValueType()),
3162 Node->getOperand(2));
3163 }
3164 Results.push_back(Tmp1);
3165 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003166 case ISD::GLOBAL_OFFSET_TABLE:
3167 case ISD::GlobalAddress:
3168 case ISD::GlobalTLSAddress:
3169 case ISD::ExternalSymbol:
3170 case ISD::ConstantPool:
3171 case ISD::JumpTable:
3172 case ISD::INTRINSIC_W_CHAIN:
3173 case ISD::INTRINSIC_WO_CHAIN:
3174 case ISD::INTRINSIC_VOID:
3175 // FIXME: Custom lowering for these operations shouldn't return null!
3176 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3177 Results.push_back(SDValue(Node, i));
3178 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003179 }
3180}
3181void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3182 SmallVectorImpl<SDValue> &Results) {
3183 MVT OVT = Node->getValueType(0);
3184 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3185 Node->getOpcode() == ISD::SINT_TO_FP) {
3186 OVT = Node->getOperand(0).getValueType();
3187 }
3188 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3189 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003190 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003191 switch (Node->getOpcode()) {
3192 case ISD::CTTZ:
3193 case ISD::CTLZ:
3194 case ISD::CTPOP:
3195 // Zero extend the argument.
3196 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3197 // Perform the larger operation.
3198 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3199 if (Node->getOpcode() == ISD::CTTZ) {
3200 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3201 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3202 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3203 ISD::SETEQ);
3204 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3205 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3206 } else if (Node->getOpcode() == ISD::CTLZ) {
3207 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3208 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3209 DAG.getConstant(NVT.getSizeInBits() -
3210 OVT.getSizeInBits(), NVT));
3211 }
3212 Results.push_back(Tmp1);
3213 break;
3214 case ISD::BSWAP: {
3215 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3216 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3217 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3218 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3219 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3220 Results.push_back(Tmp1);
3221 break;
3222 }
3223 case ISD::FP_TO_UINT:
3224 case ISD::FP_TO_SINT:
3225 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3226 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3227 Results.push_back(Tmp1);
3228 break;
3229 case ISD::UINT_TO_FP:
3230 case ISD::SINT_TO_FP:
3231 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3232 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3233 Results.push_back(Tmp1);
3234 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003235 case ISD::AND:
3236 case ISD::OR:
3237 case ISD::XOR:
3238 assert(OVT.isVector() && "Don't know how to promote scalar logic ops");
3239 // Bit convert each of the values to the new type.
3240 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
3241 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1));
3242 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3243 // Bit convert the result back the original type.
3244 Results.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1));
3245 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003246 case ISD::SELECT:
3247 unsigned ExtOp, TruncOp;
3248 if (Tmp2.getValueType().isVector()) {
3249 ExtOp = ISD::BIT_CONVERT;
3250 TruncOp = ISD::BIT_CONVERT;
3251 } else if (Tmp2.getValueType().isInteger()) {
3252 ExtOp = ISD::ANY_EXTEND;
3253 TruncOp = ISD::TRUNCATE;
3254 } else {
3255 ExtOp = ISD::FP_EXTEND;
3256 TruncOp = ISD::FP_ROUND;
3257 }
3258 Tmp1 = Node->getOperand(0);
3259 // Promote each of the values to the new type.
3260 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3261 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3262 // Perform the larger operation, then round down.
3263 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3264 if (TruncOp != ISD::FP_ROUND)
3265 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
3266 else
3267 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
3268 DAG.getIntPtrConstant(0));
3269 Results.push_back(Tmp1);
3270 break;
3271 case ISD::VECTOR_SHUFFLE: {
3272 SmallVector<int, 8> Mask;
3273 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3274
3275 // Cast the two input vectors.
3276 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
3277 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1));
3278
3279 // Convert the shuffle mask to the right # elements.
3280 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
3281 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1);
3282 Results.push_back(Tmp1);
3283 break;
3284 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003285 }
3286}
3287
Chris Lattner3e928bb2005-01-07 07:47:09 +00003288// SelectionDAG::Legalize - This is the entry point for the file.
3289//
Bill Wendling98a366d2009-04-29 23:29:43 +00003290void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3291 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003292 /// run - This is the main entry point to this class.
3293 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003294 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003295}
3296