blob: 77277a8b8637ecbf35c7ba4719f814cd96101f3c [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 Lattner3e928bb2005-01-07 07:47:09 +0000946 case ISD::ConstantFP: {
947 // Spill FP immediates to the constant pool if the target cannot directly
948 // codegen them. Targets often have some immediate values that can be
949 // efficiently generated into an FP register without a load. We explicitly
950 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000951 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
952
Chris Lattner3181a772006-01-29 06:26:56 +0000953 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
954 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +0000955 case TargetLowering::Legal:
956 break;
Chris Lattner3181a772006-01-29 06:26:56 +0000957 case TargetLowering::Custom:
958 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000959 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +0000960 Result = Tmp3;
961 break;
962 }
963 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +0000964 case TargetLowering::Expand: {
965 // Check to see if this FP immediate is already legal.
966 bool isLegal = false;
967 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
968 E = TLI.legal_fpimm_end(); I != E; ++I) {
969 if (CFP->isExactlyValue(*I)) {
970 isLegal = true;
971 break;
972 }
973 }
974 // If this is a legal constant, turn it into a TargetConstantFP node.
975 if (isLegal)
976 break;
Evan Cheng279101e2006-12-12 22:19:28 +0000977 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000978 }
Nate Begemane1795842008-02-14 08:57:00 +0000979 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000980 break;
981 }
Chris Lattnerfdfded52006-04-12 16:20:43 +0000982 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000983 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000984 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000985 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000986 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +0000987 // A call within a calling sequence must be legalized to something
988 // other than the normal CALLSEQ_END. Violating this gets Legalize
989 // into an infinite loop.
990 assert ((!IsLegalizingCall ||
991 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000992 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +0000993 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000994
995 // The number of incoming and outgoing values should match; unless the final
996 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +0000997 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
998 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
999 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001000 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001001 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001002
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001003 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001004 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001005 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1006 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001007 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001008 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001009 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001010 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001011 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001012 }
1013 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001014 case ISD::BUILD_VECTOR:
1015 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001016 default: assert(0 && "This action is not supported yet!");
1017 case TargetLowering::Custom:
1018 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001019 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001020 Result = Tmp3;
1021 break;
1022 }
1023 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001024 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001025 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001026 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001027 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001028 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001029 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001030 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1031 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1033 MVT VT = Result.getValueType();
1034
Nate Begeman5a5ca152009-04-29 05:20:52 +00001035 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001036 SmallVector<int, 8> Mask;
1037 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001038
1039 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001040 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001041 default: assert(0 && "Unknown operation action!");
1042 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001043 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001044 "vector shuffle should not be created if not legal!");
1045 break;
1046 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001047 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001048 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001049 Result = Tmp3;
1050 break;
1051 }
1052 // FALLTHROUGH
1053 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001054 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001055 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001056 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001057 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001058 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001059 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001060 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001061 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001062 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001063 if (Idx < NumElems)
1064 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1065 DAG.getIntPtrConstant(Idx)));
1066 else
1067 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1068 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001069 }
Evan Chenga87008d2009-02-25 22:49:59 +00001070 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001071 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001072 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001073 case TargetLowering::Promote: {
1074 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001075 MVT OVT = Node->getValueType(0);
1076 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001077
1078 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001079 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1080 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001081
Chris Lattner4352cc92006-04-04 17:23:26 +00001082 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001083 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001084 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001085 break;
1086 }
Chris Lattner87100e02006-03-20 01:52:29 +00001087 }
1088 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001089 }
Mon P Wang0c397192008-10-30 08:01:45 +00001090 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001091 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001092 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001093 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1094 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1095 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1096
1097 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1098 Node->getValueType(0))) {
1099 default: assert(0 && "Unknown operation action!");
1100 case TargetLowering::Legal:
1101 break;
1102 case TargetLowering::Custom:
1103 Tmp3 = TLI.LowerOperation(Result, DAG);
1104 if (Tmp3.getNode()) {
1105 Result = Tmp3;
1106 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001107 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001108 // FALLTHROUGH
1109 case TargetLowering::Expand: {
1110 // Use extract/insert/build vector for now. We might try to be
1111 // more clever later.
1112 MVT PtrVT = TLI.getPointerTy();
1113 SmallVector<SDValue, 8> Ops;
1114 unsigned NumOperands = Node->getNumOperands();
1115 for (unsigned i=0; i < NumOperands; ++i) {
1116 SDValue SubOp = Node->getOperand(i);
1117 MVT VVT = SubOp.getNode()->getValueType(0);
1118 MVT EltVT = VVT.getVectorElementType();
1119 unsigned NumSubElem = VVT.getVectorNumElements();
1120 for (unsigned j=0; j < NumSubElem; ++j) {
1121 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1122 DAG.getConstant(j, PtrVT)));
1123 }
1124 }
1125 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1126 Node->getValueType(0),
1127 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001128 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001129 }
1130 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001131 }
1132
Chris Lattner6831a812006-02-13 09:18:02 +00001133 case ISD::CALLSEQ_START: {
1134 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001135
Chris Lattner6831a812006-02-13 09:18:02 +00001136 // Recursively Legalize all of the inputs of the call end that do not lead
1137 // to this call start. This ensures that any libcalls that need be inserted
1138 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001139 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001140 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001141 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001142 NodesLeadingTo);
1143 }
Chris Lattner6831a812006-02-13 09:18:02 +00001144
1145 // Now that we legalized all of the inputs (which may have inserted
1146 // libcalls) create the new CALLSEQ_START node.
1147 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1148
1149 // Merge in the last call, to ensure that this call start after the last
1150 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001151 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001152 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001153 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001154 Tmp1 = LegalizeOp(Tmp1);
1155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001156
Chris Lattner6831a812006-02-13 09:18:02 +00001157 // Do not try to legalize the target-specific arguments (#1+).
1158 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001159 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001160 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001161 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001163
Chris Lattner6831a812006-02-13 09:18:02 +00001164 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001165 AddLegalizedOperand(Op.getValue(0), Result);
1166 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1167 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001168
Chris Lattner6831a812006-02-13 09:18:02 +00001169 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001170 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001171 // process, no libcalls can/will be inserted, guaranteeing that no calls
1172 // can overlap.
1173 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001174 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001175 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001176 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001177
Chris Lattner6831a812006-02-13 09:18:02 +00001178 // Legalize the call, starting from the CALLSEQ_END.
1179 LegalizeOp(LastCALLSEQ_END);
1180 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1181 return Result;
1182 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001183 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001184 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1185 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001187 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1188 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001189 assert(I != LegalizedNodes.end() &&
1190 "Legalizing the call start should have legalized this node!");
1191 return I->second;
1192 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001193
1194 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001195 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001197 // Do not try to legalize the target-specific arguments (#1+), except for
1198 // an optional flag input.
1199 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1200 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001201 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001202 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001203 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001204 }
1205 } else {
1206 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1207 if (Tmp1 != Node->getOperand(0) ||
1208 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001209 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001210 Ops[0] = Tmp1;
1211 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001212 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001213 }
Chris Lattner6a542892006-01-24 05:48:21 +00001214 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001215 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001216 // This finishes up call legalization.
1217 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001218
Chris Lattner4b653a02006-02-14 00:55:02 +00001219 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001220 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001221 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001222 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001223 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001224 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001225 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001226 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1227 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1228 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001229 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001230
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001231 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001232 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001233 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001234 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001235 case TargetLowering::Expand: {
1236 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1237 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1238 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001239 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001240
1241 // Chain the dynamic stack allocation so that it doesn't modify the stack
1242 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001243 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001244
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001246 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001247 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001248 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001249 unsigned StackAlign =
1250 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1251 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001252 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001253 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001254 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001255 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001256
Dale Johannesenca57b842009-02-02 23:46:53 +00001257 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001258 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001259
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001260 Tmp1 = LegalizeOp(Tmp1);
1261 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001262 break;
1263 }
1264 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001265 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001266 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001267 Tmp1 = LegalizeOp(Tmp3);
1268 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001269 }
Chris Lattner903d2782006-01-15 08:54:32 +00001270 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001271 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001272 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001273 }
Chris Lattner903d2782006-01-15 08:54:32 +00001274 // Since this op produce two values, make sure to remember that we
1275 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001276 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1277 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001278 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001279 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001280 case ISD::BR_JT:
1281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1282 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001283 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001284 Tmp1 = LegalizeOp(Tmp1);
1285 LastCALLSEQ_END = DAG.getEntryNode();
1286
1287 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1288 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1289
Scott Michelfdc40a02009-02-17 22:15:04 +00001290 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001291 default: assert(0 && "This action is not supported yet!");
1292 case TargetLowering::Legal: break;
1293 case TargetLowering::Custom:
1294 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001295 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001296 break;
1297 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001298 SDValue Chain = Result.getOperand(0);
1299 SDValue Table = Result.getOperand(1);
1300 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001301
Duncan Sands83ec4b62008-06-06 12:08:01 +00001302 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001303 MachineFunction &MF = DAG.getMachineFunction();
1304 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001305 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001306 Index, DAG.getConstant(EntrySize, PTy));
1307 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001308
Duncan Sands712f7b32008-12-12 08:13:38 +00001309 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001310 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001311 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001312 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001313 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001314 // For PIC, the sequence is:
1315 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001316 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001317 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001318 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001319 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001320 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001321 }
1322 }
1323 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001324 case ISD::BRCOND:
1325 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001326 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00001327 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001328 Tmp1 = LegalizeOp(Tmp1);
1329 LastCALLSEQ_END = DAG.getEntryNode();
1330
Eli Friedman957bffa2009-05-24 08:42:01 +00001331 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00001332
1333 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001334 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00001335
1336 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001337 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001338 case TargetLowering::Legal: break;
1339 case TargetLowering::Custom:
1340 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001341 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001342 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001343 case TargetLowering::Expand:
1344 // Expand brcond's setcc into its constituent parts and create a BR_CC
1345 // Node.
1346 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001347 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001348 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00001349 Tmp2.getOperand(0), Tmp2.getOperand(1),
1350 Node->getOperand(2));
1351 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001352 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00001353 DAG.getCondCode(ISD::SETNE), Tmp2,
1354 DAG.getConstant(0, Tmp2.getValueType()),
1355 Node->getOperand(2));
1356 }
1357 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001358 }
1359 break;
1360 case ISD::BR_CC:
1361 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001362 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001363 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001364 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001365 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001366 Tmp3 = Node->getOperand(3); // RHS
1367 Tmp4 = Node->getOperand(1); // CC
1368
Scott Michelfdc40a02009-02-17 22:15:04 +00001369 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001370 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001371 LastCALLSEQ_END = DAG.getEntryNode();
1372
Evan Cheng7f042682008-10-15 02:05:31 +00001373 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001374 // the LHS is a legal SETCC itself. In this case, we need to compare
1375 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001376 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001377 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1378 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001379 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001380
1381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001382 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001383
Chris Lattner181b7a32005-12-17 23:46:46 +00001384 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1385 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001386 case TargetLowering::Legal: break;
1387 case TargetLowering::Custom:
1388 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001390 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001391 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001392 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001393 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001394 LoadSDNode *LD = cast<LoadSDNode>(Node);
1395 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1396 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001397
Evan Cheng466685d2006-10-09 20:57:25 +00001398 ISD::LoadExtType ExtType = LD->getExtensionType();
1399 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001400 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001401 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1402 Tmp3 = Result.getValue(0);
1403 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001404
Evan Cheng466685d2006-10-09 20:57:25 +00001405 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1406 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001407 case TargetLowering::Legal:
1408 // If this is an unaligned load and the target doesn't support it,
1409 // expand it.
1410 if (!TLI.allowsUnalignedMemoryAccesses()) {
1411 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001412 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001413 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001414 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001415 TLI);
1416 Tmp3 = Result.getOperand(0);
1417 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001418 Tmp3 = LegalizeOp(Tmp3);
1419 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001420 }
1421 }
1422 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001423 case TargetLowering::Custom:
1424 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001425 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001426 Tmp3 = LegalizeOp(Tmp1);
1427 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001428 }
Evan Cheng466685d2006-10-09 20:57:25 +00001429 break;
1430 case TargetLowering::Promote: {
1431 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001432 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001433 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001434 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001435
Dale Johannesenca57b842009-02-02 23:46:53 +00001436 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001437 LD->getSrcValueOffset(),
1438 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001439 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001440 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001441 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001442 }
Evan Cheng466685d2006-10-09 20:57:25 +00001443 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001444 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001445 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001446 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1447 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001448 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001449 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001450 MVT SrcVT = LD->getMemoryVT();
1451 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001452 int SVOffset = LD->getSrcValueOffset();
1453 unsigned Alignment = LD->getAlignment();
1454 bool isVolatile = LD->isVolatile();
1455
Duncan Sands83ec4b62008-06-06 12:08:01 +00001456 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001457 // Some targets pretend to have an i1 loading operation, and actually
1458 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1459 // bits are guaranteed to be zero; it helps the optimizers understand
1460 // that these bits are zero. It is also useful for EXTLOAD, since it
1461 // tells the optimizers that those bits are undefined. It would be
1462 // nice to have an effective generic way of getting these benefits...
1463 // Until such a way is found, don't insist on promoting i1 here.
1464 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001465 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001466 // Promote to a byte-sized load if not loading an integral number of
1467 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001468 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1469 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001470 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001471
1472 // The extra bits are guaranteed to be zero, since we stored them that
1473 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1474
1475 ISD::LoadExtType NewExtType =
1476 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1477
Dale Johannesenca57b842009-02-02 23:46:53 +00001478 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001479 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1480 NVT, isVolatile, Alignment);
1481
1482 Ch = Result.getValue(1); // The chain.
1483
1484 if (ExtType == ISD::SEXTLOAD)
1485 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001486 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001487 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001488 Result, DAG.getValueType(SrcVT));
1489 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1490 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001491 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001492 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001493 DAG.getValueType(SrcVT));
1494
1495 Tmp1 = LegalizeOp(Result);
1496 Tmp2 = LegalizeOp(Ch);
1497 } else if (SrcWidth & (SrcWidth - 1)) {
1498 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001499 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001500 "Unsupported extload!");
1501 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1502 assert(RoundWidth < SrcWidth);
1503 unsigned ExtraWidth = SrcWidth - RoundWidth;
1504 assert(ExtraWidth < RoundWidth);
1505 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1506 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001507 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1508 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001509 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001510 unsigned IncrementSize;
1511
1512 if (TLI.isLittleEndian()) {
1513 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1514 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001515 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1516 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001517 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1518 Alignment);
1519
1520 // Load the remaining ExtraWidth bits.
1521 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001522 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001523 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001524 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001525 LD->getSrcValue(), SVOffset + IncrementSize,
1526 ExtraVT, isVolatile,
1527 MinAlign(Alignment, IncrementSize));
1528
1529 // Build a factor node to remember that this load is independent of the
1530 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001531 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001532 Hi.getValue(1));
1533
1534 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001535 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001536 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1537
1538 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001539 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001540 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001541 // Big endian - avoid unaligned loads.
1542 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1543 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001544 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001545 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1546 Alignment);
1547
1548 // Load the remaining ExtraWidth bits.
1549 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001550 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001551 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001552 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001553 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001554 LD->getSrcValue(), SVOffset + IncrementSize,
1555 ExtraVT, isVolatile,
1556 MinAlign(Alignment, IncrementSize));
1557
1558 // Build a factor node to remember that this load is independent of the
1559 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001560 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001561 Hi.getValue(1));
1562
1563 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001564 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001565 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1566
1567 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001568 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001569 }
1570
1571 Tmp1 = LegalizeOp(Result);
1572 Tmp2 = LegalizeOp(Ch);
1573 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001574 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001575 default: assert(0 && "This action is not supported yet!");
1576 case TargetLowering::Custom:
1577 isCustom = true;
1578 // FALLTHROUGH
1579 case TargetLowering::Legal:
1580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1581 Tmp1 = Result.getValue(0);
1582 Tmp2 = Result.getValue(1);
1583
1584 if (isCustom) {
1585 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001586 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001587 Tmp1 = LegalizeOp(Tmp3);
1588 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1589 }
1590 } else {
1591 // If this is an unaligned load and the target doesn't support it,
1592 // expand it.
1593 if (!TLI.allowsUnalignedMemoryAccesses()) {
1594 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001595 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001596 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001597 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001598 TLI);
1599 Tmp1 = Result.getOperand(0);
1600 Tmp2 = Result.getOperand(1);
1601 Tmp1 = LegalizeOp(Tmp1);
1602 Tmp2 = LegalizeOp(Tmp2);
1603 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001604 }
1605 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001606 break;
1607 case TargetLowering::Expand:
1608 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1609 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001610 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001611 LD->getSrcValueOffset(),
1612 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001613 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001614 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001615 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1616 Tmp2 = LegalizeOp(Load.getValue(1));
1617 break;
1618 }
1619 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1620 // Turn the unsupported load into an EXTLOAD followed by an explicit
1621 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001622 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001623 Tmp1, Tmp2, LD->getSrcValue(),
1624 LD->getSrcValueOffset(), SrcVT,
1625 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001626 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001627 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001628 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1629 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001630 Result, DAG.getValueType(SrcVT));
1631 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001632 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001633 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1634 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001635 break;
1636 }
Evan Cheng466685d2006-10-09 20:57:25 +00001637 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001638
Evan Cheng466685d2006-10-09 20:57:25 +00001639 // Since loads produce two values, make sure to remember that we legalized
1640 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001641 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1642 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001643 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001644 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001645 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001646 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001647 StoreSDNode *ST = cast<StoreSDNode>(Node);
1648 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1649 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001650 int SVOffset = ST->getSrcValueOffset();
1651 unsigned Alignment = ST->getAlignment();
1652 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001653
Evan Cheng8b2794a2006-10-13 21:14:26 +00001654 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001655 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1656 // FIXME: We shouldn't do this for TargetConstantFP's.
1657 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1658 // to phase ordering between legalized code and the dag combiner. This
1659 // probably means that we need to integrate dag combiner and legalizer
1660 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001661 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001662 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001663 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001664 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001665 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001666 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001667 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001668 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001669 SVOffset, isVolatile, Alignment);
1670 break;
1671 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001672 // If this target supports 64-bit registers, do a single 64-bit store.
1673 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001674 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001675 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001676 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001677 SVOffset, isVolatile, Alignment);
1678 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001679 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001680 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1681 // stores. If the target supports neither 32- nor 64-bits, this
1682 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001683 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1685 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001686 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001687
Dale Johannesenca57b842009-02-02 23:46:53 +00001688 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001689 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001690 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001691 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001692 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001693 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001694
Dale Johannesenca57b842009-02-02 23:46:53 +00001695 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001696 break;
1697 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001698 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Eli Friedman957bffa2009-05-24 08:42:01 +00001701 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001702 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001703 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001704 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001705
Duncan Sands83ec4b62008-06-06 12:08:01 +00001706 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001707 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1708 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001709 case TargetLowering::Legal:
1710 // If this is an unaligned store and the target doesn't support it,
1711 // expand it.
1712 if (!TLI.allowsUnalignedMemoryAccesses()) {
1713 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001714 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001715 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001716 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001717 TLI);
1718 }
1719 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001720 case TargetLowering::Custom:
1721 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001722 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001723 break;
1724 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001725 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001726 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001727 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001728 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001729 ST->getSrcValue(), SVOffset, isVolatile,
1730 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001731 break;
1732 }
1733 break;
1734 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001735 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001736 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001737
Duncan Sands83ec4b62008-06-06 12:08:01 +00001738 MVT StVT = ST->getMemoryVT();
1739 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001740
Duncan Sands83ec4b62008-06-06 12:08:01 +00001741 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001742 // Promote to a byte-sized store with upper bits zero if not
1743 // storing an integral number of bytes. For example, promote
1744 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001745 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001746 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1747 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001748 SVOffset, NVT, isVolatile, Alignment);
1749 } else if (StWidth & (StWidth - 1)) {
1750 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001751 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001752 "Unsupported truncstore!");
1753 unsigned RoundWidth = 1 << Log2_32(StWidth);
1754 assert(RoundWidth < StWidth);
1755 unsigned ExtraWidth = StWidth - RoundWidth;
1756 assert(ExtraWidth < RoundWidth);
1757 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1758 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001759 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1760 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001761 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001762 unsigned IncrementSize;
1763
1764 if (TLI.isLittleEndian()) {
1765 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1766 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001767 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001768 SVOffset, RoundVT,
1769 isVolatile, Alignment);
1770
1771 // Store the remaining ExtraWidth bits.
1772 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001773 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001774 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001775 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001776 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001777 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001778 SVOffset + IncrementSize, ExtraVT, isVolatile,
1779 MinAlign(Alignment, IncrementSize));
1780 } else {
1781 // Big endian - avoid unaligned stores.
1782 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1783 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001784 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001785 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001786 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1787 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001788
1789 // Store the remaining ExtraWidth bits.
1790 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001791 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001792 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001793 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001794 SVOffset + IncrementSize, ExtraVT, isVolatile,
1795 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001796 }
Duncan Sands7e857202008-01-22 07:17:34 +00001797
1798 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001799 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001800 } else {
1801 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1802 Tmp2 != ST->getBasePtr())
1803 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1804 ST->getOffset());
1805
1806 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1807 default: assert(0 && "This action is not supported yet!");
1808 case TargetLowering::Legal:
1809 // If this is an unaligned store and the target doesn't support it,
1810 // expand it.
1811 if (!TLI.allowsUnalignedMemoryAccesses()) {
1812 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001814 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001815 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001816 TLI);
1817 }
1818 break;
1819 case TargetLowering::Custom:
1820 Result = TLI.LowerOperation(Result, DAG);
1821 break;
1822 case Expand:
1823 // TRUNCSTORE:i16 i32 -> STORE i16
1824 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001825 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1826 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1827 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001828 break;
1829 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001830 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001831 }
1832 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001833 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001834 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00001835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001836 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001837 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001838
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Nate Begemanb942a3d2005-08-23 04:29:48 +00001841 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001842 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001843 case TargetLowering::Legal: break;
1844 case TargetLowering::Custom: {
1845 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001846 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001847 break;
1848 }
Nate Begeman9373a812005-08-10 20:51:12 +00001849 case TargetLowering::Expand:
1850 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001851 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00001852 Tmp2, Tmp3,
1853 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1854 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001855 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00001856 DAG.getConstant(0, Tmp1.getValueType()),
1857 Tmp2, Tmp3, ISD::SETNE);
1858 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001859 break;
1860 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001861 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001862 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1863 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001864 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001865 ExtOp = ISD::BIT_CONVERT;
1866 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001867 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001868 ExtOp = ISD::ANY_EXTEND;
1869 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001870 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001871 ExtOp = ISD::FP_EXTEND;
1872 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001873 }
1874 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00001875 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
1876 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001877 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001878 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00001879 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00001880 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00001881 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001882 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00001883 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001884 break;
1885 }
1886 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001887 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001888 case ISD::SELECT_CC: {
1889 Tmp1 = Node->getOperand(0); // LHS
1890 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001891 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1892 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00001894
1895 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001896 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001897
Evan Cheng7f042682008-10-15 02:05:31 +00001898 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001899 // the LHS is a legal SETCC itself. In this case, we need to compare
1900 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001901 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001902 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1903 CC = DAG.getCondCode(ISD::SETNE);
1904 }
1905 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1906
1907 // Everything is legal, see if we should expand this op or something.
1908 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1909 default: assert(0 && "This action is not supported yet!");
1910 case TargetLowering::Legal: break;
1911 case TargetLowering::Custom:
1912 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001913 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001914 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001915 }
1916 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001917 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001918 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001919 Tmp1 = Node->getOperand(0);
1920 Tmp2 = Node->getOperand(1);
1921 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00001922 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
1924 // If we had to Expand the SetCC operands into a SELECT node, then it may
1925 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00001926 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001928 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001929 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001931
Chris Lattner73e142f2006-01-30 22:43:50 +00001932 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001933 default: assert(0 && "Cannot handle this action for SETCC yet!");
1934 case TargetLowering::Custom:
1935 isCustom = true;
1936 // FALLTHROUGH.
1937 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00001938 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001939 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00001940 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00001942 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001943 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001944 case TargetLowering::Promote: {
1945 // First step, figure out the appropriate operation to use.
1946 // Allow SETCC to not be supported for all legal data types
1947 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00001948 MVT NewInTy = Node->getOperand(0).getValueType();
1949 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00001950
1951 // Scan for the appropriate larger type to use.
1952 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001953 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00001954
Duncan Sands83ec4b62008-06-06 12:08:01 +00001955 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00001956 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001957 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00001958 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00001959
Andrew Lenharthae355752005-11-30 17:12:26 +00001960 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001961 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001962 break;
1963 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001964 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00001965 assert(0 && "Cannot promote Legal Integer SETCC yet");
1966 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00001967 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
1968 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00001969 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001970 Tmp1 = LegalizeOp(Tmp1);
1971 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00001972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00001973 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001974 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001975 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001976 case TargetLowering::Expand:
1977 // Expand a setcc node into a select_cc of the same condition, lhs, and
1978 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00001979 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001980 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00001981 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00001982 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001983 break;
1984 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001985 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001986
1987 // Binary operators
Chris Lattnera09f8482006-03-05 05:09:38 +00001988 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1989 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00001990 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Chris Lattnera09f8482006-03-05 05:09:38 +00001992 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001993
Chris Lattnera09f8482006-03-05 05:09:38 +00001994 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1995 default: assert(0 && "Operation not supported");
1996 case TargetLowering::Custom:
1997 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001998 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001999 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002000 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002001 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002002 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002003 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002004 "Ugly special-cased code!");
2005 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002006 SDValue SignBit;
2007 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
2008 if (isTypeLegal(IVT)) {
2009 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
2010 } else {
2011 assert(isTypeLegal(TLI.getPointerTy()) &&
2012 (TLI.getPointerTy() == MVT::i32 ||
2013 TLI.getPointerTy() == MVT::i64) &&
2014 "Legal type for load?!");
2015 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
2016 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
2017 SDValue Ch =
2018 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
2019 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
2020 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
2021 LoadPtr, DAG.getIntPtrConstant(4));
2022 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
2023 Ch, LoadPtr, NULL, 0, MVT::i32);
2024 }
2025 SignBit =
2026 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
2027 SignBit, DAG.getConstant(0, SignBit.getValueType()),
2028 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002029 // Get the absolute value of the result.
2030 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
2031 // Select between the nabs and abs value based on the sign bit of
2032 // the input.
2033 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
2034 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
2035 AbsVal),
2036 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00002037 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002038 break;
2039 }
Evan Cheng912095b2007-01-04 21:56:39 +00002040 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002041 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002042 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002043 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00002044 // TODO: handle the case where the Lo and Hi operands are not of legal type
2045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2047 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002048 case TargetLowering::Promote:
2049 case TargetLowering::Custom:
2050 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002051 case TargetLowering::Legal:
2052 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00002053 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002054 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002055 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00002056 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
2057 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
2058 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002059 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00002060 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002061 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002062 break;
2063 }
2064 break;
2065 }
2066
Nate Begemanc105e192005-04-06 00:23:54 +00002067 case ISD::UREM:
2068 case ISD::SREM:
2069 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2070 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002071
Nate Begemanc105e192005-04-06 00:23:54 +00002072 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002073 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2074 case TargetLowering::Custom:
2075 isCustom = true;
2076 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002077 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002079 if (isCustom) {
2080 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002081 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002082 }
Nate Begemanc105e192005-04-06 00:23:54 +00002083 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002084 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002085 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002086 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002087 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Dan Gohman525178c2007-10-08 18:33:35 +00002089 // See if remainder can be lowered using two-result operations.
2090 SDVTList VTs = DAG.getVTList(VT, VT);
2091 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002092 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002093 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002094 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002095 break;
2096 }
2097 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002098 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002099 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
2100 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002101 break;
2102 }
2103
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002104 if (VT.isInteger() &&
2105 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
2106 // X % Y -> X-X/Y*Y
2107 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
2108 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
2109 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
2110 break;
Nate Begemanc105e192005-04-06 00:23:54 +00002111 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002112
2113 // Check to see if we have a libcall for this operator.
2114 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2115 switch (Node->getOpcode()) {
2116 default: break;
2117 case ISD::UREM:
2118 case ISD::SREM:
2119 if (VT == MVT::i16)
2120 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
2121 else if (VT == MVT::i32)
2122 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
2123 else if (VT == MVT::i64)
2124 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
2125 else if (VT == MVT::i128)
2126 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
2127 break;
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002128 }
2129
2130 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Eli Friedman47b41f72009-05-27 02:21:29 +00002131 Result = ExpandLibCall(LC, Node, isSigned);
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002132 break;
2133 }
2134
Eli Friedman74807f22009-05-26 08:55:52 +00002135 assert(0 && "Cannot expand this binary operator!");
Nate Begemanc105e192005-04-06 00:23:54 +00002136 break;
2137 }
Dan Gohman525178c2007-10-08 18:33:35 +00002138 }
Nate Begemanc105e192005-04-06 00:23:54 +00002139 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002140 case ISD::VAARG: {
2141 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2142 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2143
Duncan Sands83ec4b62008-06-06 12:08:01 +00002144 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002145 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2146 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002147 case TargetLowering::Custom:
2148 isCustom = true;
2149 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002150 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002151 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2152 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002153 Tmp1 = Result.getValue(1);
2154
2155 if (isCustom) {
2156 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002157 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002158 Result = LegalizeOp(Tmp2);
2159 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2160 }
2161 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002162 break;
2163 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00002164 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00002165 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002166 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00002167 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002168 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00002169 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002170 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00002171 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00002172 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002173 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00002174 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002175 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002176 Result = LegalizeOp(Result);
2177 break;
2178 }
2179 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002180 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00002181 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002182 AddLegalizedOperand(SDValue(Node, 0), Result);
2183 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002184 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002185 }
Bill Wendling74c37652008-12-09 22:08:41 +00002186 case ISD::SADDO:
2187 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002188 MVT VT = Node->getValueType(0);
2189 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2190 default: assert(0 && "This action not supported for this op yet!");
2191 case TargetLowering::Custom:
2192 Result = TLI.LowerOperation(Op, DAG);
2193 if (Result.getNode()) break;
2194 // FALLTHROUGH
2195 case TargetLowering::Legal: {
2196 SDValue LHS = LegalizeOp(Node->getOperand(0));
2197 SDValue RHS = LegalizeOp(Node->getOperand(1));
2198
Scott Michelfdc40a02009-02-17 22:15:04 +00002199 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002200 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002201 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002202 MVT OType = Node->getValueType(1);
2203
Bill Wendlinga6af91a2008-11-25 08:19:22 +00002204 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002205
Bill Wendling740464e2008-11-25 19:40:17 +00002206 // LHSSign -> LHS >= 0
2207 // RHSSign -> RHS >= 0
2208 // SumSign -> Sum >= 0
2209 //
Bill Wendling74c37652008-12-09 22:08:41 +00002210 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00002211 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00002212 // Sub:
2213 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00002214 //
Dale Johannesenca57b842009-02-02 23:46:53 +00002215 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2216 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00002217 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2218 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002219 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002220
Dale Johannesenca57b842009-02-02 23:46:53 +00002221 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2222 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002223
Dale Johannesenca57b842009-02-02 23:46:53 +00002224 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002225
2226 MVT ValueVTs[] = { LHS.getValueType(), OType };
2227 SDValue Ops[] = { Sum, Cmp };
2228
Scott Michelfdc40a02009-02-17 22:15:04 +00002229 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002230 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002231 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002232 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002233 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002234 break;
2235 }
2236 }
2237
2238 break;
2239 }
Bill Wendling74c37652008-12-09 22:08:41 +00002240 case ISD::UADDO:
2241 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00002242 MVT VT = Node->getValueType(0);
2243 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2244 default: assert(0 && "This action not supported for this op yet!");
2245 case TargetLowering::Custom:
2246 Result = TLI.LowerOperation(Op, DAG);
2247 if (Result.getNode()) break;
2248 // FALLTHROUGH
2249 case TargetLowering::Legal: {
2250 SDValue LHS = LegalizeOp(Node->getOperand(0));
2251 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002252
Bill Wendling74c37652008-12-09 22:08:41 +00002253 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002254 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002255 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002256 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00002257 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002259 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002260
Bill Wendling41ea7e72008-11-24 19:21:46 +00002261 MVT ValueVTs[] = { LHS.getValueType(), OType };
2262 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002263
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002265 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002266 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002267 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002268 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002269 break;
2270 }
2271 }
2272
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002273 break;
2274 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002275 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
Chris Lattner4ddd2832006-04-08 04:13:17 +00002277 assert(Result.getValueType() == Op.getValueType() &&
2278 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002279
Chris Lattner456a93a2006-01-28 07:39:30 +00002280 // Make sure that the generated code is itself legal.
2281 if (Result != Op)
2282 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002283
Chris Lattner45982da2005-05-12 16:53:42 +00002284 // Note that LegalizeOp may be reentered even from single-use nodes, which
2285 // means that we always must cache transformed nodes.
2286 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002287 return Result;
2288}
2289
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002290SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
2291 SDValue Vec = Op.getOperand(0);
2292 SDValue Idx = Op.getOperand(1);
2293 DebugLoc dl = Op.getDebugLoc();
2294 // Store the value to a temporary stack slot, then LOAD the returned part.
2295 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
2296 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
2297
2298 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00002299 unsigned EltSize =
2300 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002301 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
2302 DAG.getConstant(EltSize, Idx.getValueType()));
2303
2304 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
2305 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
2306 else
2307 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
2308
2309 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
2310
2311 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
2312}
2313
Nate Begeman750ac1b2006-02-01 07:19:44 +00002314/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2315/// with condition CC on the current target. This usually involves legalizing
2316/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2317/// there may be no choice but to create a new SetCC node to represent the
2318/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00002319/// LHS, and the SDValue returned in RHS has a nil SDNode value.
2320void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
2321 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002322 SDValue &CC,
2323 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00002324 LHS = LegalizeOp(LHS);
2325 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002326}
2327
Evan Cheng7f042682008-10-15 02:05:31 +00002328/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
2329/// condition code CC on the current target. This routine assumes LHS and rHS
2330/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
2331/// illegal condition code into AND / OR of multiple SETCC values.
2332void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
2333 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002334 SDValue &CC,
2335 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00002336 MVT OpVT = LHS.getValueType();
2337 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2338 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
2339 default: assert(0 && "Unknown condition code action!");
2340 case TargetLowering::Legal:
2341 // Nothing to do.
2342 break;
2343 case TargetLowering::Expand: {
2344 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
2345 unsigned Opc = 0;
2346 switch (CCCode) {
2347 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00002348 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
2349 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2350 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2351 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2352 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2353 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2354 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2355 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2356 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2357 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2358 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2359 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00002360 // FIXME: Implement more expansions.
2361 }
2362
Dale Johannesenbb5da912009-02-02 20:41:04 +00002363 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
2364 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
2365 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00002366 RHS = SDValue();
2367 CC = SDValue();
2368 break;
2369 }
2370 }
2371}
2372
Chris Lattner1401d152008-01-16 07:45:30 +00002373/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2374/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2375/// a load from the stack slot to DestVT, extending it if needed.
2376/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002377SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2378 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002379 MVT DestVT,
2380 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002381 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002382 unsigned SrcAlign =
2383 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2384 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002385 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002386
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002387 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002388 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002389 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2390
Duncan Sands83ec4b62008-06-06 12:08:01 +00002391 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2392 unsigned SlotSize = SlotVT.getSizeInBits();
2393 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002394 unsigned DestAlign =
2395 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002396
Chris Lattner1401d152008-01-16 07:45:30 +00002397 // Emit a store to the stack slot. Use a truncstore if the input value is
2398 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002399 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Chris Lattner1401d152008-01-16 07:45:30 +00002401 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002402 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002403 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002404 else {
2405 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002406 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002407 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002409
Chris Lattner35481892005-12-23 00:16:34 +00002410 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002411 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002412 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002413
Chris Lattner1401d152008-01-16 07:45:30 +00002414 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002415 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002416 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002417}
2418
Dan Gohman475871a2008-07-27 21:46:04 +00002419SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002420 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002421 // Create a vector sized/aligned stack slot, store the value to element #0,
2422 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002423 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002424
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002425 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002426 int SPFI = StackPtrFI->getIndex();
2427
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002428 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2429 StackPtr,
2430 PseudoSourceValue::getFixedStack(SPFI), 0,
2431 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002432 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002433 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002434}
2435
2436
Chris Lattnerce872152006-03-19 06:31:19 +00002437/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002438/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002439SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002440 unsigned NumElems = Node->getNumOperands();
2441 SDValue SplatValue = Node->getOperand(0);
2442 DebugLoc dl = Node->getDebugLoc();
2443 MVT VT = Node->getValueType(0);
2444 MVT OpVT = SplatValue.getValueType();
2445 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002446
2447 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002448 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002449 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002450
Dan Gohman475871a2008-07-27 21:46:04 +00002451 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002452 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002453 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002454 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002455 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002456 bool isConstant = true;
2457 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2458 SplatValue.getOpcode() != ISD::UNDEF)
2459 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002460
Evan Cheng033e6812006-03-24 01:17:21 +00002461 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002462 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002463 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002464 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002465 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002466 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002467 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002468
2469 // If this isn't a constant element or an undef, we can't use a constant
2470 // pool load.
2471 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2472 V.getOpcode() != ISD::UNDEF)
2473 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002474 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002475
Chris Lattnerce872152006-03-19 06:31:19 +00002476 if (isOnlyLowElement) {
2477 // If the low element is an undef too, then this whole things is an undef.
2478 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002479 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00002480 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002481 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00002482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002483
Chris Lattner2eb86532006-03-24 07:29:17 +00002484 // If all elements are constants, create a load from the constant pool.
2485 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002486 std::vector<Constant*> CV;
2487 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002488 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002489 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002490 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002491 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002492 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002493 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00002494 } else {
2495 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002496 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00002497 CV.push_back(UndefValue::get(OpNTy));
2498 }
2499 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002500 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002501 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002502 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002503 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002504 PseudoSourceValue::getConstantPool(), 0,
2505 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002506 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002507
Gabor Greifba36cb52008-08-28 21:40:38 +00002508 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00002509 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00002510 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00002511
2512 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00002513 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002514 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00002515 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002516 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002517
Chris Lattner87100e02006-03-20 01:52:29 +00002518 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
2520 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00002521 }
2522 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002523
Evan Cheng033e6812006-03-24 01:17:21 +00002524 // If there are only two unique elements, we may be able to turn this into a
2525 // vector shuffle.
2526 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002527 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00002528 SDValue Val1 = Node->getOperand(1);
2529 SDValue Val2;
2530 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002531 if (MI->first != Val1)
2532 Val2 = MI->first;
2533 else
2534 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00002535
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002536 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002537 // vector shuffle has the undef vector on the RHS.
2538 if (Val1.getOpcode() == ISD::UNDEF)
2539 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002540
Evan Cheng033e6812006-03-24 01:17:21 +00002541 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00002542 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002543
2544 // Set elements of the shuffle mask for Val1.
2545 std::vector<unsigned> &Val1Elts = Values[Val1];
2546 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002547 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002548
2549 // Set elements of the shuffle mask for Val2.
2550 std::vector<unsigned> &Val2Elts = Values[Val2];
2551 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
2552 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00002553 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00002554
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002555 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002556 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00002557 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002558 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
2559 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00002560 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00002561 }
2562 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002563
Chris Lattnerce872152006-03-19 06:31:19 +00002564 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
2565 // aligned object on the stack, store each element into it, then load
2566 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00002567 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00002568 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00002569 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
2570 const Value *SV = PseudoSourceValue::getFixedStack(FI);
2571
Chris Lattnerce872152006-03-19 06:31:19 +00002572 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00002573 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002574 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00002575 // Store (in the right endianness) the elements to memory.
2576 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2577 // Ignore undef elements.
2578 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00002579
Chris Lattner841c8822006-03-22 01:46:54 +00002580 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00002581
Dan Gohman475871a2008-07-27 21:46:04 +00002582 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002583 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00002584
Dale Johannesen8a782a22009-02-02 22:12:50 +00002585 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
2586 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00002587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002588
Dan Gohman475871a2008-07-27 21:46:04 +00002589 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00002590 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00002591 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002592 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00002593 else
2594 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002595
Chris Lattnerce872152006-03-19 06:31:19 +00002596 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00002597 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00002598}
2599
Chris Lattner77e77a62005-01-21 06:05:23 +00002600// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2601// does not fit into a register, return the lo part and set the hi part to the
2602// by-reg argument. If it does fit into a single register, return the result
2603// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002604SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002605 bool isSigned) {
Chris Lattner6831a812006-02-13 09:18:02 +00002606 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002607 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002608 // Legalizing the call will automatically add the previous call to the
2609 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002610 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002611
Chris Lattner77e77a62005-01-21 06:05:23 +00002612 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002613 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002614 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 MVT ArgVT = Node->getOperand(i).getValueType();
2616 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00002617 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002618 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002619 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002620 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002621 }
Bill Wendling056292f2008-09-16 21:48:12 +00002622 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002623 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002624
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002625 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002626 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002627 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002628 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002629 CallingConv::C, false, Callee, Args, DAG,
2630 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002631
Chris Lattner6831a812006-02-13 09:18:02 +00002632 // Legalize the call sequence, starting with the chain. This will advance
2633 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2634 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2635 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002636 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002637}
2638
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002639SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2640 RTLIB::Libcall Call_F32,
2641 RTLIB::Libcall Call_F64,
2642 RTLIB::Libcall Call_F80,
2643 RTLIB::Libcall Call_PPCF128) {
2644 RTLIB::Libcall LC;
2645 switch (Node->getValueType(0).getSimpleVT()) {
2646 default: assert(0 && "Unexpected request for libcall!");
2647 case MVT::f32: LC = Call_F32; break;
2648 case MVT::f64: LC = Call_F64; break;
2649 case MVT::f80: LC = Call_F80; break;
2650 case MVT::ppcf128: LC = Call_PPCF128; break;
2651 }
2652 return ExpandLibCall(LC, Node, false);
2653}
2654
2655SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2656 RTLIB::Libcall Call_I16,
2657 RTLIB::Libcall Call_I32,
2658 RTLIB::Libcall Call_I64,
2659 RTLIB::Libcall Call_I128) {
2660 RTLIB::Libcall LC;
2661 switch (Node->getValueType(0).getSimpleVT()) {
2662 default: assert(0 && "Unexpected request for libcall!");
2663 case MVT::i16: LC = Call_I16; break;
2664 case MVT::i32: LC = Call_I32; break;
2665 case MVT::i64: LC = Call_I64; break;
2666 case MVT::i128: LC = Call_I128; break;
2667 }
2668 return ExpandLibCall(LC, Node, isSigned);
2669}
2670
Chris Lattner22cde6a2006-01-28 08:25:58 +00002671/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2672/// INT_TO_FP operation of the specified operand when the target requests that
2673/// we expand it. At this point, we know that the result and operand types are
2674/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002675SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2676 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00002677 MVT DestVT,
2678 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002679 if (Op0.getValueType() == MVT::i32) {
2680 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002681
Chris Lattner23594d42008-01-16 07:03:22 +00002682 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00002683 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002684
Chris Lattner22cde6a2006-01-28 08:25:58 +00002685 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002686 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002687 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002688 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002689 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002690 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002691 if (TLI.isLittleEndian())
2692 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002693
Chris Lattner22cde6a2006-01-28 08:25:58 +00002694 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002695 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002696 if (isSigned) {
2697 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00002698 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00002699 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002700 } else {
2701 Op0Mapped = Op0;
2702 }
2703 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002704 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002705 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002706 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00002707 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002708 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00002709 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002710 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00002711 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002712 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002713 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002714 BitsToDouble(0x4330000080000000ULL) :
2715 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002716 MVT::f64);
2717 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00002718 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002719 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002720 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002721 // handle final rounding
2722 if (DestVT == MVT::f64) {
2723 // do nothing
2724 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00002725 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002726 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002727 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002728 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002729 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002730 }
2731 return Result;
2732 }
2733 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00002734 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002735
Dale Johannesenaf435272009-02-02 19:03:57 +00002736 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002737 Op0, DAG.getConstant(0, Op0.getValueType()),
2738 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00002739 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00002740 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00002741 SignSet, Four, Zero);
2742
2743 // If the sign bit of the integer is set, the large number will be treated
2744 // as a negative number. To counteract this, the dynamic code adds an
2745 // offset depending on the data type.
2746 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002747 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002748 default: assert(0 && "Unsupported integer type!");
2749 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2750 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2751 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2752 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2753 }
2754 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00002755 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002756
Dan Gohman475871a2008-07-27 21:46:04 +00002757 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002758 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00002759 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00002760 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00002761 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002762 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00002763 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002764 PseudoSourceValue::getConstantPool(), 0,
2765 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002766 else {
Dan Gohman69de1932008-02-06 22:27:42 +00002767 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00002768 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00002769 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00002770 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00002771 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002772 }
2773
Dale Johannesenaf435272009-02-02 19:03:57 +00002774 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002775}
2776
2777/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2778/// *INT_TO_FP operation of the specified operand when the target requests that
2779/// we promote it. At this point, we know that the result and operand types are
2780/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2781/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002782SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
2783 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002784 bool isSigned,
2785 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002786 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002788
2789 unsigned OpToUse = 0;
2790
2791 // Scan for the appropriate larger type to use.
2792 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002793 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
2794 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002795
2796 // If the target supports SINT_TO_FP of this type, use it.
2797 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
2798 default: break;
2799 case TargetLowering::Legal:
2800 if (!TLI.isTypeLegal(NewInTy))
2801 break; // Can't use this datatype.
2802 // FALL THROUGH.
2803 case TargetLowering::Custom:
2804 OpToUse = ISD::SINT_TO_FP;
2805 break;
2806 }
2807 if (OpToUse) break;
2808 if (isSigned) continue;
2809
2810 // If the target supports UINT_TO_FP of this type, use it.
2811 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
2812 default: break;
2813 case TargetLowering::Legal:
2814 if (!TLI.isTypeLegal(NewInTy))
2815 break; // Can't use this datatype.
2816 // FALL THROUGH.
2817 case TargetLowering::Custom:
2818 OpToUse = ISD::UINT_TO_FP;
2819 break;
2820 }
2821 if (OpToUse) break;
2822
2823 // Otherwise, try a larger type.
2824 }
2825
2826 // Okay, we found the operation and type to use. Zero extend our input to the
2827 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002828 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002829 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002830 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002831}
2832
2833/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2834/// FP_TO_*INT operation of the specified operand when the target requests that
2835/// we promote it. At this point, we know that the result and operand types are
2836/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2837/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002838SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
2839 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002840 bool isSigned,
2841 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002842 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002843 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002844
2845 unsigned OpToUse = 0;
2846
2847 // Scan for the appropriate larger type to use.
2848 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002849 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
2850 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002851
2852 // If the target supports FP_TO_SINT returning this type, use it.
2853 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
2854 default: break;
2855 case TargetLowering::Legal:
2856 if (!TLI.isTypeLegal(NewOutTy))
2857 break; // Can't use this datatype.
2858 // FALL THROUGH.
2859 case TargetLowering::Custom:
2860 OpToUse = ISD::FP_TO_SINT;
2861 break;
2862 }
2863 if (OpToUse) break;
2864
2865 // If the target supports FP_TO_UINT of this type, use it.
2866 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
2867 default: break;
2868 case TargetLowering::Legal:
2869 if (!TLI.isTypeLegal(NewOutTy))
2870 break; // Can't use this datatype.
2871 // FALL THROUGH.
2872 case TargetLowering::Custom:
2873 OpToUse = ISD::FP_TO_UINT;
2874 break;
2875 }
2876 if (OpToUse) break;
2877
2878 // Otherwise, try a larger type.
2879 }
2880
Scott Michelfdc40a02009-02-17 22:15:04 +00002881
Chris Lattner27a6c732007-11-24 07:07:01 +00002882 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002883 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002884
Chris Lattner27a6c732007-11-24 07:07:01 +00002885 // If the operation produces an invalid type, it must be custom lowered. Use
2886 // the target lowering hooks to expand it. Just keep the low part of the
2887 // expanded operation, we know that we're truncating anyway.
2888 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00002889 SmallVector<SDValue, 2> Results;
2890 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
2891 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
2892 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00002893 }
Duncan Sands126d9072008-07-04 11:47:58 +00002894
Chris Lattner27a6c732007-11-24 07:07:01 +00002895 // Truncate the result of the extended FP_TO_*INT operation to the desired
2896 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002897 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002898}
2899
2900/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2901///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002902SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002903 MVT VT = Op.getValueType();
2904 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002906 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002907 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2908 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002909 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2910 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2911 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002912 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002913 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2914 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2915 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2916 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2917 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2918 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2919 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2920 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2921 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002922 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002923 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2924 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2925 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2926 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2927 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2928 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2929 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2930 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2931 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2932 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2933 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2934 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2935 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2936 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2937 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2938 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2939 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2940 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2941 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2942 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2943 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002944 }
2945}
2946
2947/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2948///
Scott Michelfdc40a02009-02-17 22:15:04 +00002949SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002950 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002951 switch (Opc) {
2952 default: assert(0 && "Cannot expand this yet!");
2953 case ISD::CTPOP: {
2954 static const uint64_t mask[6] = {
2955 0x5555555555555555ULL, 0x3333333333333333ULL,
2956 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2957 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2958 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00002959 MVT VT = Op.getValueType();
2960 MVT ShVT = TLI.getShiftAmountTy();
2961 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002962 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2963 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002964 unsigned EltSize = VT.isVector() ?
2965 VT.getVectorElementType().getSizeInBits() : len;
2966 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002967 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002968 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00002969 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002970 DAG.getNode(ISD::AND, dl, VT,
2971 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
2972 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002973 }
2974 return Op;
2975 }
2976 case ISD::CTLZ: {
2977 // for now, we do this:
2978 // x = x | (x >> 1);
2979 // x = x | (x >> 2);
2980 // ...
2981 // x = x | (x >>16);
2982 // x = x | (x >>32); // for 64-bit input
2983 // return popcount(~x);
2984 //
2985 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00002986 MVT VT = Op.getValueType();
2987 MVT ShVT = TLI.getShiftAmountTy();
2988 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002989 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002991 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002992 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002993 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002994 Op = DAG.getNOT(dl, Op, VT);
2995 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002996 }
2997 case ISD::CTTZ: {
2998 // for now, we use: { return popcount(~x & (x - 1)); }
2999 // unless the target has ctlz but not ctpop, in which case we use:
3000 // { return 32 - nlz(~x & (x-1)); }
3001 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003002 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00003003 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
3004 DAG.getNOT(dl, Op, VT),
3005 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00003006 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003007 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003008 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
3009 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00003010 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003011 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003012 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
3013 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003014 }
3015 }
3016}
Chris Lattnere34b3962005-01-19 04:19:40 +00003017
Eli Friedman8c377c72009-05-27 01:25:56 +00003018void SelectionDAGLegalize::ExpandNode(SDNode *Node,
3019 SmallVectorImpl<SDValue> &Results) {
3020 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003021 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003022 switch (Node->getOpcode()) {
3023 case ISD::CTPOP:
3024 case ISD::CTLZ:
3025 case ISD::CTTZ:
3026 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
3027 Results.push_back(Tmp1);
3028 break;
3029 case ISD::BSWAP:
3030 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
3031 break;
3032 case ISD::FRAMEADDR:
3033 case ISD::RETURNADDR:
3034 case ISD::FRAME_TO_ARGS_OFFSET:
3035 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
3036 break;
3037 case ISD::FLT_ROUNDS_:
3038 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
3039 break;
3040 case ISD::EH_RETURN:
3041 case ISD::DECLARE:
3042 case ISD::DBG_LABEL:
3043 case ISD::EH_LABEL:
3044 case ISD::PREFETCH:
3045 case ISD::MEMBARRIER:
3046 case ISD::VAEND:
3047 Results.push_back(Node->getOperand(0));
3048 break;
3049 case ISD::MERGE_VALUES:
3050 for (unsigned i = 0; i < Node->getNumValues(); i++)
3051 Results.push_back(Node->getOperand(i));
3052 break;
3053 case ISD::UNDEF: {
3054 MVT VT = Node->getValueType(0);
3055 if (VT.isInteger())
3056 Results.push_back(DAG.getConstant(0, VT));
3057 else if (VT.isFloatingPoint())
3058 Results.push_back(DAG.getConstantFP(0, VT));
3059 else
3060 assert(0 && "Unknown value type!");
3061 break;
3062 }
3063 case ISD::TRAP: {
3064 // If this operation is not supported, lower it to 'abort()' call
3065 TargetLowering::ArgListTy Args;
3066 std::pair<SDValue, SDValue> CallResult =
3067 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
3068 false, false, false, false, CallingConv::C, false,
3069 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3070 Args, DAG, dl);
3071 Results.push_back(CallResult.second);
3072 break;
3073 }
3074 case ISD::FP_ROUND:
3075 case ISD::BIT_CONVERT:
3076 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3077 Node->getValueType(0), dl);
3078 Results.push_back(Tmp1);
3079 break;
3080 case ISD::FP_EXTEND:
3081 Tmp1 = EmitStackConvert(Node->getOperand(0),
3082 Node->getOperand(0).getValueType(),
3083 Node->getValueType(0), dl);
3084 Results.push_back(Tmp1);
3085 break;
3086 case ISD::SIGN_EXTEND_INREG: {
3087 // NOTE: we could fall back on load/store here too for targets without
3088 // SAR. However, it is doubtful that any exist.
3089 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3090 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3091 ExtraVT.getSizeInBits();
3092 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3093 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3094 Node->getOperand(0), ShiftCst);
3095 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3096 Results.push_back(Tmp1);
3097 break;
3098 }
3099 case ISD::FP_ROUND_INREG: {
3100 // The only way we can lower this is to turn it into a TRUNCSTORE,
3101 // EXTLOAD pair, targetting a temporary location (a stack slot).
3102
3103 // NOTE: there is a choice here between constantly creating new stack
3104 // slots and always reusing the same one. We currently always create
3105 // new ones, as reuse may inhibit scheduling.
3106 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3107 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3108 Node->getValueType(0), dl);
3109 Results.push_back(Tmp1);
3110 break;
3111 }
3112 case ISD::SINT_TO_FP:
3113 case ISD::UINT_TO_FP:
3114 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3115 Node->getOperand(0), Node->getValueType(0), dl);
3116 Results.push_back(Tmp1);
3117 break;
3118 case ISD::FP_TO_UINT: {
3119 SDValue True, False;
3120 MVT VT = Node->getOperand(0).getValueType();
3121 MVT NVT = Node->getValueType(0);
3122 const uint64_t zero[] = {0, 0};
3123 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3124 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3125 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3126 Tmp1 = DAG.getConstantFP(apf, VT);
3127 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3128 Node->getOperand(0),
3129 Tmp1, ISD::SETLT);
3130 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3131 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3132 DAG.getNode(ISD::FSUB, dl, VT,
3133 Node->getOperand(0), Tmp1));
3134 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3135 DAG.getConstant(x, NVT));
3136 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3137 Results.push_back(Tmp1);
3138 break;
3139 }
3140 case ISD::VACOPY: {
3141 // This defaults to loading a pointer from the input and storing it to the
3142 // output, returning the chain.
3143 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3144 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3145 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
3146 Node->getOperand(2), VS, 0);
3147 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
3148 Results.push_back(Tmp1);
3149 break;
3150 }
3151 case ISD::EXTRACT_VECTOR_ELT:
3152 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3153 // This must be an access of the only element. Return it.
3154 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
3155 Node->getOperand(0));
3156 else
3157 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3158 Results.push_back(Tmp1);
3159 break;
3160 case ISD::EXTRACT_SUBVECTOR:
3161 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3162 break;
3163 case ISD::SCALAR_TO_VECTOR:
3164 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3165 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003166 case ISD::INSERT_VECTOR_ELT:
3167 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3168 Node->getOperand(1),
3169 Node->getOperand(2), dl));
3170 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003171 case ISD::EXTRACT_ELEMENT: {
3172 MVT OpTy = Node->getOperand(0).getValueType();
3173 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3174 // 1 -> Hi
3175 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3176 DAG.getConstant(OpTy.getSizeInBits()/2,
3177 TLI.getShiftAmountTy()));
3178 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3179 } else {
3180 // 0 -> Lo
3181 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3182 Node->getOperand(0));
3183 }
3184 Results.push_back(Tmp1);
3185 break;
3186 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003187 case ISD::STACKSAVE:
3188 // Expand to CopyFromReg if the target set
3189 // StackPointerRegisterToSaveRestore.
3190 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3191 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3192 Node->getValueType(0)));
3193 Results.push_back(Results[0].getValue(1));
3194 } else {
3195 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3196 Results.push_back(Node->getOperand(0));
3197 }
3198 break;
3199 case ISD::STACKRESTORE:
3200 // Expand to CopyToReg if the target set
3201 // StackPointerRegisterToSaveRestore.
3202 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3203 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3204 Node->getOperand(1)));
3205 } else {
3206 Results.push_back(Node->getOperand(0));
3207 }
3208 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003209 case ISD::FNEG:
3210 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3211 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3212 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3213 Node->getOperand(0));
3214 Results.push_back(Tmp1);
3215 break;
3216 case ISD::FABS: {
3217 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3218 MVT VT = Node->getValueType(0);
3219 Tmp1 = Node->getOperand(0);
3220 Tmp2 = DAG.getConstantFP(0.0, VT);
3221 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3222 Tmp1, Tmp2, ISD::SETUGT);
3223 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3224 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
3225 Results.push_back(Tmp1);
3226 break;
3227 }
3228 case ISD::FSQRT:
3229 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3230 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
3231 break;
3232 case ISD::FSIN:
3233 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3234 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
3235 break;
3236 case ISD::FCOS:
3237 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3238 RTLIB::COS_F80, RTLIB::COS_PPCF128));
3239 break;
3240 case ISD::FLOG:
3241 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3242 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
3243 break;
3244 case ISD::FLOG2:
3245 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3246 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
3247 break;
3248 case ISD::FLOG10:
3249 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3250 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
3251 break;
3252 case ISD::FEXP:
3253 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3254 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
3255 break;
3256 case ISD::FEXP2:
3257 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3258 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
3259 break;
3260 case ISD::FTRUNC:
3261 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3262 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
3263 break;
3264 case ISD::FFLOOR:
3265 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3266 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
3267 break;
3268 case ISD::FCEIL:
3269 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3270 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
3271 break;
3272 case ISD::FRINT:
3273 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3274 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
3275 break;
3276 case ISD::FNEARBYINT:
3277 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3278 RTLIB::NEARBYINT_F64,
3279 RTLIB::NEARBYINT_F80,
3280 RTLIB::NEARBYINT_PPCF128));
3281 break;
3282 case ISD::FPOWI:
3283 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3284 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
3285 break;
3286 case ISD::FPOW:
3287 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3288 RTLIB::POW_F80, RTLIB::POW_PPCF128));
3289 break;
3290 case ISD::FDIV:
3291 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3292 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
3293 break;
3294 case ISD::FREM:
3295 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3296 RTLIB::REM_F80, RTLIB::REM_PPCF128));
3297 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003298 case ISD::EHSELECTION: {
3299 unsigned Reg = TLI.getExceptionSelectorRegister();
3300 assert(Reg && "Can't expand to unknown register!");
3301 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3302 Node->getValueType(0)));
3303 Results.push_back(Results[0].getValue(1));
3304 break;
3305 }
3306 case ISD::EXCEPTIONADDR: {
3307 unsigned Reg = TLI.getExceptionAddressRegister();
3308 assert(Reg && "Can't expand to unknown register!");
3309 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3310 Node->getValueType(0)));
3311 Results.push_back(Results[0].getValue(1));
3312 break;
3313 }
3314 case ISD::SUB: {
3315 MVT VT = Node->getValueType(0);
3316 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3317 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3318 "Don't know how to expand this subtraction!");
3319 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3320 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
3321 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3322 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3323 break;
3324 }
3325 case ISD::UDIV:
3326 case ISD::UREM: {
3327 bool isRem = Node->getOpcode() == ISD::UREM;
3328 MVT VT = Node->getValueType(0);
3329 SDVTList VTs = DAG.getVTList(VT, VT);
3330 if (TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT))
3331 Tmp1 = DAG.getNode(ISD::UDIVREM, dl, VTs, Node->getOperand(0),
3332 Node->getOperand(1)).getValue(isRem);
3333 else if (isRem)
3334 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::UREM_I16, RTLIB::UREM_I32,
3335 RTLIB::UREM_I64, RTLIB::UREM_I128);
3336 else
3337 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3338 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
3339 Results.push_back(Tmp1);
3340 break;
3341 }
3342 case ISD::SDIV:
3343 case ISD::SREM: {
3344 bool isRem = Node->getOpcode() == ISD::SREM;
3345 MVT VT = Node->getValueType(0);
3346 SDVTList VTs = DAG.getVTList(VT, VT);
3347 if (TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT))
3348 Tmp1 = DAG.getNode(ISD::SDIVREM, dl, VTs, Node->getOperand(0),
3349 Node->getOperand(1)).getValue(isRem);
3350 else if (isRem)
3351 Tmp1 = ExpandIntLibCall(Node, true, RTLIB::SREM_I16, RTLIB::SREM_I32,
3352 RTLIB::SREM_I64, RTLIB::SREM_I128);
3353 else
3354 Tmp1 = ExpandIntLibCall(Node, true, RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3355 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3356 Results.push_back(Tmp1);
3357 break;
3358 }
3359 case ISD::MULHU:
3360 case ISD::MULHS: {
3361 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3362 ISD::SMUL_LOHI;
3363 MVT VT = Node->getValueType(0);
3364 SDVTList VTs = DAG.getVTList(VT, VT);
3365 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3366 "If this wasn't legal, it shouldn't have been created!");
3367 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3368 Node->getOperand(1));
3369 Results.push_back(Tmp1.getValue(1));
3370 break;
3371 }
3372 case ISD::MUL: {
3373 MVT VT = Node->getValueType(0);
3374 SDVTList VTs = DAG.getVTList(VT, VT);
3375 // See if multiply or divide can be lowered using two-result operations.
3376 // We just need the low half of the multiply; try both the signed
3377 // and unsigned forms. If the target supports both SMUL_LOHI and
3378 // UMUL_LOHI, form a preference by checking which forms of plain
3379 // MULH it supports.
3380 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3381 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3382 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3383 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3384 unsigned OpToUse = 0;
3385 if (HasSMUL_LOHI && !HasMULHS) {
3386 OpToUse = ISD::SMUL_LOHI;
3387 } else if (HasUMUL_LOHI && !HasMULHU) {
3388 OpToUse = ISD::UMUL_LOHI;
3389 } else if (HasSMUL_LOHI) {
3390 OpToUse = ISD::SMUL_LOHI;
3391 } else if (HasUMUL_LOHI) {
3392 OpToUse = ISD::UMUL_LOHI;
3393 }
3394 if (OpToUse) {
3395 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2));
3396 break;
3397 }
3398 Tmp1 = ExpandIntLibCall(Node, false, RTLIB::MUL_I16, RTLIB::MUL_I32,
3399 RTLIB::MUL_I64, RTLIB::MUL_I128);
3400 Results.push_back(Tmp1);
3401 break;
3402 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003403 case ISD::GLOBAL_OFFSET_TABLE:
3404 case ISD::GlobalAddress:
3405 case ISD::GlobalTLSAddress:
3406 case ISD::ExternalSymbol:
3407 case ISD::ConstantPool:
3408 case ISD::JumpTable:
3409 case ISD::INTRINSIC_W_CHAIN:
3410 case ISD::INTRINSIC_WO_CHAIN:
3411 case ISD::INTRINSIC_VOID:
3412 // FIXME: Custom lowering for these operations shouldn't return null!
3413 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3414 Results.push_back(SDValue(Node, i));
3415 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003416 }
3417}
3418void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3419 SmallVectorImpl<SDValue> &Results) {
3420 MVT OVT = Node->getValueType(0);
3421 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3422 Node->getOpcode() == ISD::SINT_TO_FP) {
3423 OVT = Node->getOperand(0).getValueType();
3424 }
3425 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3426 DebugLoc dl = Node->getDebugLoc();
3427 SDValue Tmp1, Tmp2;
3428 switch (Node->getOpcode()) {
3429 case ISD::CTTZ:
3430 case ISD::CTLZ:
3431 case ISD::CTPOP:
3432 // Zero extend the argument.
3433 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3434 // Perform the larger operation.
3435 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3436 if (Node->getOpcode() == ISD::CTTZ) {
3437 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3438 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3439 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3440 ISD::SETEQ);
3441 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3442 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3443 } else if (Node->getOpcode() == ISD::CTLZ) {
3444 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3445 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3446 DAG.getConstant(NVT.getSizeInBits() -
3447 OVT.getSizeInBits(), NVT));
3448 }
3449 Results.push_back(Tmp1);
3450 break;
3451 case ISD::BSWAP: {
3452 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3453 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3454 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3455 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3456 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3457 Results.push_back(Tmp1);
3458 break;
3459 }
3460 case ISD::FP_TO_UINT:
3461 case ISD::FP_TO_SINT:
3462 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3463 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3464 Results.push_back(Tmp1);
3465 break;
3466 case ISD::UINT_TO_FP:
3467 case ISD::SINT_TO_FP:
3468 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3469 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3470 Results.push_back(Tmp1);
3471 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003472 case ISD::AND:
3473 case ISD::OR:
3474 case ISD::XOR:
3475 assert(OVT.isVector() && "Don't know how to promote scalar logic ops");
3476 // Bit convert each of the values to the new type.
3477 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(0));
3478 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Node->getOperand(1));
3479 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3480 // Bit convert the result back the original type.
3481 Results.push_back(DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Tmp1));
3482 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003483 }
3484}
3485
Chris Lattner3e928bb2005-01-07 07:47:09 +00003486// SelectionDAG::Legalize - This is the entry point for the file.
3487//
Bill Wendling98a366d2009-04-29 23:29:43 +00003488void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3489 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003490 /// run - This is the main entry point to this class.
3491 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003492 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003493}
3494