blob: 220825fbcdfff25a6f51e822d702c29b26df0f89 [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,
Dale Johannesenbb5da912009-02-02 20:41:04 +0000128 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000129
Mon P Wangf007a8b2008-11-06 05:31:54 +0000130 /// Useful 16 element vector type that is used to pass operands for widening.
Scott Michelfdc40a02009-02-17 22:15:04 +0000131 typedef SmallVector<SDValue, 16> SDValueVector;
132
Nate Begeman5a5ca152009-04-29 05:20:52 +0000133 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
134 /// performs the same shuffe in terms of order or result bytes, but on a type
135 /// whose vector element type is narrower than the original shuffle type.
136 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
137 SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
138 SDValue N1, SDValue N2,
139 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000140
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000141 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000142 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000143
Dale Johannesenbb5da912009-02-02 20:41:04 +0000144 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
145 DebugLoc dl);
146 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
147 DebugLoc dl);
148 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
149 DebugLoc dl) {
150 LegalizeSetCCOperands(LHS, RHS, CC, dl);
151 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000152 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000153
Dan Gohman475871a2008-07-27 21:46:04 +0000154 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
155 SDValue &Hi);
Chris Lattnercad063f2005-07-16 00:19:57 +0000156
Dale Johannesen8a782a22009-02-02 22:12:50 +0000157 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000158 SDValue ExpandBUILD_VECTOR(SDNode *Node);
159 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000160 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
161 DebugLoc dl);
162 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
163 DebugLoc dl);
164 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
165 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000166
Dale Johannesen8a782a22009-02-02 22:12:50 +0000167 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
168 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000169
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000170 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman8c377c72009-05-27 01:25:56 +0000171
172 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
173 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000174};
175}
176
Nate Begeman5a5ca152009-04-29 05:20:52 +0000177/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
178/// performs the same shuffe in terms of order or result bytes, but on a type
179/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000180/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000181SDValue
182SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
183 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000184 SmallVectorImpl<int> &Mask) const {
185 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000186 unsigned NumMaskElts = VT.getVectorNumElements();
187 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000188 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000189
Nate Begeman9008ca62009-04-27 18:41:29 +0000190 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
191
192 if (NumEltsGrowth == 1)
193 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
194
195 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000196 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000197 int Idx = Mask[i];
198 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
199 if (Idx < 0)
200 NewMask.push_back(-1);
201 else
202 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000203 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000204 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000205 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000206 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
207 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000208}
209
Bill Wendling5aa49772009-02-24 02:35:30 +0000210SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000211 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000212 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000213 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000214 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000215 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000216}
217
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000219 LastCALLSEQ_END = DAG.getEntryNode();
220 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000221
Chris Lattnerab510a72005-10-02 17:49:46 +0000222 // The legalize process is inherently a bottom-up recursive process (users
223 // legalize their uses before themselves). Given infinite stack space, we
224 // could just start legalizing on the root and traverse the whole graph. In
225 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000226 // blocks. To avoid this problem, compute an ordering of the nodes where each
227 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000228 DAG.AssignTopologicalOrder();
229 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
230 E = prior(DAG.allnodes_end()); I != next(E); ++I)
231 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000232
233 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000235 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
236 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000237
Chris Lattner3e928bb2005-01-07 07:47:09 +0000238 LegalizedNodes.clear();
239
240 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000241 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242}
243
Chris Lattner6831a812006-02-13 09:18:02 +0000244
245/// FindCallEndFromCallStart - Given a chained node that is part of a call
246/// sequence, find the CALLSEQ_END node that terminates the call sequence.
247static SDNode *FindCallEndFromCallStart(SDNode *Node) {
248 if (Node->getOpcode() == ISD::CALLSEQ_END)
249 return Node;
250 if (Node->use_empty())
251 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000252
Chris Lattner6831a812006-02-13 09:18:02 +0000253 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000254 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000255 if (TheChain.getValueType() != MVT::Other) {
256 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000257 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000258 if (TheChain.getValueType() != MVT::Other) {
259 // Otherwise, hunt for it.
260 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
261 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000262 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000263 break;
264 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000265
266 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000267 if (TheChain.getValueType() != MVT::Other)
268 return 0;
269 }
270 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000271
Chris Lattner6831a812006-02-13 09:18:02 +0000272 for (SDNode::use_iterator UI = Node->use_begin(),
273 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
Chris Lattner6831a812006-02-13 09:18:02 +0000275 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000276 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000277 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
278 if (User->getOperand(i) == TheChain)
279 if (SDNode *Result = FindCallEndFromCallStart(User))
280 return Result;
281 }
282 return 0;
283}
284
Scott Michelfdc40a02009-02-17 22:15:04 +0000285/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000286/// sequence, find the CALLSEQ_START node that initiates the call sequence.
287static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
288 assert(Node && "Didn't find callseq_start for a call??");
289 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000290
Chris Lattner6831a812006-02-13 09:18:02 +0000291 assert(Node->getOperand(0).getValueType() == MVT::Other &&
292 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000293 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000294}
295
296/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000297/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000298/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000299///
300/// Keep track of the nodes we fine that actually do lead to Dest in
301/// NodesLeadingTo. This avoids retraversing them exponential number of times.
302///
303bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000304 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000305 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000306
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000307 // If we've already processed this node and it does lead to Dest, there is no
308 // need to reprocess it.
309 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000310
Chris Lattner6831a812006-02-13 09:18:02 +0000311 // If the first result of this node has been already legalized, then it cannot
312 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000313 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000314
Chris Lattner6831a812006-02-13 09:18:02 +0000315 // Okay, this node has not already been legalized. Check and legalize all
316 // operands. If none lead to Dest, then we can legalize this node.
317 bool OperandsLeadToDest = false;
318 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
319 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000320 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000321
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000322 if (OperandsLeadToDest) {
323 NodesLeadingTo.insert(N);
324 return true;
325 }
Chris Lattner6831a812006-02-13 09:18:02 +0000326
327 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000328 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000329 return false;
330}
331
Mon P Wang0c397192008-10-30 08:01:45 +0000332/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000333/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000334void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000335 // Don't touch TargetConstants
336 if (Op.getOpcode() == ISD::TargetConstant)
337 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000338 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000339 // We should never see any illegal result types here.
340 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Eli Friedman74807f22009-05-26 08:55:52 +0000341 (void)LegalizeOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000342}
Chris Lattner6831a812006-02-13 09:18:02 +0000343
Evan Cheng9f877882006-12-13 20:57:08 +0000344/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
345/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000346static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000347 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000348 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000349 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000350
351 // If a FP immediate is precise when represented as a float and if the
352 // target can do an extending load from float to double, we put it into
353 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000354 // double. This shrinks FP constants and canonicalizes them for targets where
355 // an FP extending load is the same cost as a normal load (such as on the x87
356 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000357 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000358 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000359 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000360 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000361 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000362 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000363 }
364
Duncan Sands83ec4b62008-06-06 12:08:01 +0000365 MVT OrigVT = VT;
366 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000367 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000368 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000369 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
370 // Only do this if the target has a native EXTLOAD instruction from
371 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000372 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000373 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000374 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000375 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
376 VT = SVT;
377 Extend = true;
378 }
Evan Cheng00495212006-12-12 21:32:44 +0000379 }
380
Dan Gohman475871a2008-07-27 21:46:04 +0000381 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000382 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000383 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000384 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000385 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000386 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000387 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000388 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000389 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000390}
391
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000392/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
393static
Dan Gohman475871a2008-07-27 21:46:04 +0000394SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000395 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000396 SDValue Chain = ST->getChain();
397 SDValue Ptr = ST->getBasePtr();
398 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000399 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000400 int Alignment = ST->getAlignment();
401 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000402 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000403 if (ST->getMemoryVT().isFloatingPoint() ||
404 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000405 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
406 if (TLI.isTypeLegal(intVT)) {
407 // Expand to a bitconvert of the value to the integer type of the
408 // same size, then a (misaligned) int store.
409 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000410 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
411 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000412 SVOffset, ST->isVolatile(), Alignment);
413 } else {
414 // Do a (aligned) store to a stack slot, then copy from the stack slot
415 // to the final destination using (unaligned) integer loads and stores.
416 MVT StoredVT = ST->getMemoryVT();
417 MVT RegVT =
418 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
419 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
420 unsigned RegBytes = RegVT.getSizeInBits() / 8;
421 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000422
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000423 // Make sure the stack slot is also aligned for the register type.
424 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
425
426 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000427 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000428 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000429 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
430 SmallVector<SDValue, 8> Stores;
431 unsigned Offset = 0;
432
433 // Do all but one copies using the full register width.
434 for (unsigned i = 1; i < NumRegs; i++) {
435 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000436 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000437 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000438 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000439 ST->getSrcValue(), SVOffset + Offset,
440 ST->isVolatile(),
441 MinAlign(ST->getAlignment(), Offset)));
442 // Increment the pointers.
443 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000444 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000445 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000446 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000447 }
448
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000449 // The last store may be partial. Do a truncating store. On big-endian
450 // machines this requires an extending load from the stack slot to ensure
451 // that the bits are in the right place.
452 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000453
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000454 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000455 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000456 NULL, 0, MemVT);
457
Dale Johannesenbb5da912009-02-02 20:41:04 +0000458 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000459 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000460 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000461 MinAlign(ST->getAlignment(), Offset)));
462 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000463 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000464 Stores.size());
465 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000466 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000467 assert(ST->getMemoryVT().isInteger() &&
468 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000469 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000470 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000471 MVT NewStoredVT =
472 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
473 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000474 int IncrementSize = NumBits / 8;
475
476 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000477 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
478 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000479 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000480
481 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000483 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000484 ST->getSrcValue(), SVOffset, NewStoredVT,
485 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000486 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000487 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000488 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000489 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000490 ST->getSrcValue(), SVOffset + IncrementSize,
491 NewStoredVT, ST->isVolatile(), Alignment);
492
Dale Johannesenbb5da912009-02-02 20:41:04 +0000493 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000494}
495
496/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
497static
Dan Gohman475871a2008-07-27 21:46:04 +0000498SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000499 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000500 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000501 SDValue Chain = LD->getChain();
502 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000503 MVT VT = LD->getValueType(0);
504 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000505 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000506 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000507 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
508 if (TLI.isTypeLegal(intVT)) {
509 // Expand to a (misaligned) integer load of the same size,
510 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000511 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000512 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000513 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000514 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000515 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000516 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000517
Duncan Sands05e11fa2008-12-12 21:47:02 +0000518 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000519 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000520 } else {
521 // Copy the value to a (aligned) stack slot using (unaligned) integer
522 // loads and stores, then do a (aligned) load from the stack slot.
523 MVT RegVT = TLI.getRegisterType(intVT);
524 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
525 unsigned RegBytes = RegVT.getSizeInBits() / 8;
526 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
527
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000528 // Make sure the stack slot is also aligned for the register type.
529 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
530
Duncan Sands05e11fa2008-12-12 21:47:02 +0000531 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
532 SmallVector<SDValue, 8> Stores;
533 SDValue StackPtr = StackBase;
534 unsigned Offset = 0;
535
536 // Do all but one copies using the full register width.
537 for (unsigned i = 1; i < NumRegs; i++) {
538 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000539 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000540 SVOffset + Offset, LD->isVolatile(),
541 MinAlign(LD->getAlignment(), Offset));
542 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000543 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000544 NULL, 0));
545 // Increment the pointers.
546 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000547 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
548 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000549 Increment);
550 }
551
552 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000553 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000554 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000555 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000556 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000557 MinAlign(LD->getAlignment(), Offset));
558 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000559 // On big-endian machines this requires a truncating store to ensure
560 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000561 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000562 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000563
564 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000565 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000566 Stores.size());
567
568 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000569 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000570 NULL, 0, LoadedVT);
571
572 // Callers expect a MERGE_VALUES node.
573 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000574 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000575 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000576 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000577 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000578 "Unaligned load of unsupported type.");
579
Dale Johannesen8155d642008-02-27 22:36:00 +0000580 // Compute the new VT that is half the size of the old one. This is an
581 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000582 unsigned NumBits = LoadedVT.getSizeInBits();
583 MVT NewLoadedVT;
584 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000585 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000586
Chris Lattnere400af82007-11-19 21:38:03 +0000587 unsigned Alignment = LD->getAlignment();
588 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000589 ISD::LoadExtType HiExtType = LD->getExtensionType();
590
591 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
592 if (HiExtType == ISD::NON_EXTLOAD)
593 HiExtType = ISD::ZEXTLOAD;
594
595 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000596 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000597 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000598 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000599 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000600 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000601 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000602 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000603 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000604 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000605 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000606 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000607 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000608 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000609 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000610 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000611 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000612 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000613 }
614
615 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000616 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000617 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
618 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000619
Dale Johannesenbb5da912009-02-02 20:41:04 +0000620 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000621 Hi.getValue(1));
622
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000624 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000625}
Evan Cheng912095b2007-01-04 21:56:39 +0000626
Duncan Sands007f9842008-01-10 10:28:30 +0000627/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000628static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000629 RTLIB::Libcall Call_F32,
630 RTLIB::Libcall Call_F64,
631 RTLIB::Libcall Call_F80,
632 RTLIB::Libcall Call_PPCF128) {
633 return
634 VT == MVT::f32 ? Call_F32 :
635 VT == MVT::f64 ? Call_F64 :
636 VT == MVT::f80 ? Call_F80 :
637 VT == MVT::ppcf128 ? Call_PPCF128 :
638 RTLIB::UNKNOWN_LIBCALL;
639}
640
Nate Begeman68679912008-04-25 18:07:40 +0000641/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
642/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
643/// is necessary to spill the vector being inserted into to memory, perform
644/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000645SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000646PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
647 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000648 SDValue Tmp1 = Vec;
649 SDValue Tmp2 = Val;
650 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Nate Begeman68679912008-04-25 18:07:40 +0000652 // If the target doesn't support this, we have to spill the input vector
653 // to a temporary stack slot, update the element, then reload it. This is
654 // badness. We could also load the value into a vector register (either
655 // with a "move to register" or "extload into register" instruction, then
656 // permute it into place, if the idx is a constant and if the idx is
657 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000658 MVT VT = Tmp1.getValueType();
659 MVT EltVT = VT.getVectorElementType();
660 MVT IdxVT = Tmp3.getValueType();
661 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000662 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000663
Gabor Greifba36cb52008-08-28 21:40:38 +0000664 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000665
666 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000667 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000668 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000669
670 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000671 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000672 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000673 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000674 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000675 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
676 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000677 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000678 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000679 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000680 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000681 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000682 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000683}
684
Mon P Wange9f10152008-12-09 05:46:39 +0000685
Dan Gohmana3466152007-07-13 20:14:11 +0000686/// LegalizeOp - We know that the specified value has a legal type, and
687/// that its operands are legal. Now ensure that the operation itself
688/// is legal, recursively ensuring that the operands' operations remain
689/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000690SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000691 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
692 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000693
Gabor Greifba36cb52008-08-28 21:40:38 +0000694 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000695 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000696
Eli Friedman1fde9c52009-05-24 02:46:31 +0000697 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
698 assert(getTypeAction(Node->getValueType(i)) == Legal &&
699 "Unexpected illegal type!");
700
701 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
702 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
703 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
704 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000705
Chris Lattner45982da2005-05-12 16:53:42 +0000706 // Note that LegalizeOp may be reentered even from single-use nodes, which
707 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000708 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000709 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000710
Dan Gohman475871a2008-07-27 21:46:04 +0000711 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
712 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000713 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000714
Eli Friedman8c377c72009-05-27 01:25:56 +0000715 // Figure out the correct action; the way to query this varies by opcode
716 TargetLowering::LegalizeAction Action;
717 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000718 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000719 case ISD::INTRINSIC_W_CHAIN:
720 case ISD::INTRINSIC_WO_CHAIN:
721 case ISD::INTRINSIC_VOID:
722 case ISD::VAARG:
723 case ISD::STACKSAVE:
724 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
725 break;
726 case ISD::SINT_TO_FP:
727 case ISD::UINT_TO_FP:
728 case ISD::EXTRACT_VECTOR_ELT:
729 Action = TLI.getOperationAction(Node->getOpcode(),
730 Node->getOperand(0).getValueType());
731 break;
732 case ISD::FP_ROUND_INREG:
733 case ISD::SIGN_EXTEND_INREG: {
734 MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
735 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
736 break;
737 }
738 case ISD::LOAD:
739 case ISD::STORE:
740 case ISD::BR_CC:
741 case ISD::FORMAL_ARGUMENTS:
742 case ISD::CALL:
743 case ISD::CALLSEQ_START:
744 case ISD::CALLSEQ_END:
745 case ISD::SELECT_CC:
746 case ISD::SETCC:
747 case ISD::EXCEPTIONADDR:
748 case ISD::EHSELECTION:
749 // These instructions have properties that aren't modeled in the
750 // generic codepath
751 SimpleFinishLegalizing = false;
752 break;
753 case ISD::EXTRACT_ELEMENT:
754 case ISD::FLT_ROUNDS_:
755 case ISD::SADDO:
756 case ISD::SSUBO:
757 case ISD::UADDO:
758 case ISD::USUBO:
759 case ISD::SMULO:
760 case ISD::UMULO:
761 case ISD::FPOWI:
762 case ISD::MERGE_VALUES:
763 case ISD::EH_RETURN:
764 case ISD::FRAME_TO_ARGS_OFFSET:
765 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
766 if (Action == TargetLowering::Legal)
767 Action = TargetLowering::Expand;
768 break;
769 case ISD::TRAMPOLINE:
770 case ISD::FRAMEADDR:
771 case ISD::RETURNADDR:
772 Action = TargetLowering::Custom;
773 break;
774 case ISD::BUILD_VECTOR:
775 // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize
776 // its operands first!
777 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000778 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000779 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000780 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000781 Action = TargetLowering::Legal;
782 } else {
783 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000784 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000785 break;
786 }
787
788 if (SimpleFinishLegalizing) {
789 SmallVector<SDValue, 8> Ops, ResultVals;
790 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
791 Ops.push_back(LegalizeOp(Node->getOperand(i)));
792 switch (Node->getOpcode()) {
793 default: break;
794 case ISD::BR:
795 case ISD::BRIND:
796 case ISD::BR_JT:
797 case ISD::BR_CC:
798 case ISD::BRCOND:
799 case ISD::RET:
800 // Branches tweak the chain to include LastCALLSEQ_END
801 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
802 LastCALLSEQ_END);
803 Ops[0] = LegalizeOp(Ops[0]);
804 LastCALLSEQ_END = DAG.getEntryNode();
805 break;
806 case ISD::SHL:
807 case ISD::SRL:
808 case ISD::SRA:
809 case ISD::ROTL:
810 case ISD::ROTR:
811 // Legalizing shifts/rotates requires adjusting the shift amount
812 // to the appropriate width.
813 if (!Ops[1].getValueType().isVector())
814 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
815 break;
816 }
817
818 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
819 Ops.size());
820 switch (Action) {
821 case TargetLowering::Legal:
822 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
823 ResultVals.push_back(Result.getValue(i));
824 break;
825 case TargetLowering::Custom:
826 // FIXME: The handling for custom lowering with multiple results is
827 // a complete mess.
828 Tmp1 = TLI.LowerOperation(Result, DAG);
829 if (Tmp1.getNode()) {
830 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
831 if (e == 1)
832 ResultVals.push_back(Tmp1);
833 else
834 ResultVals.push_back(Tmp1.getValue(i));
835 }
836 break;
837 }
838
839 // FALL THROUGH
840 case TargetLowering::Expand:
841 ExpandNode(Result.getNode(), ResultVals);
842 break;
843 case TargetLowering::Promote:
844 PromoteNode(Result.getNode(), ResultVals);
845 break;
846 }
847 if (!ResultVals.empty()) {
848 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
849 if (ResultVals[i] != SDValue(Node, i))
850 ResultVals[i] = LegalizeOp(ResultVals[i]);
851 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
852 }
853 return ResultVals[Op.getResNo()];
854 }
855 }
856
857 switch (Node->getOpcode()) {
858 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000859#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000860 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000861#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000862 assert(0 && "Do not know how to legalize this operator!");
863 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000864 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000865 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000866 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000867 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000868 case ISD::ConstantPool:
869 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000870 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
871 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000872 case TargetLowering::Custom:
873 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000874 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +0000875 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000876 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000877 break;
878 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000879 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000880 case ISD::EXCEPTIONADDR: {
881 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000882 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000883 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
884 default: assert(0 && "This action is not supported yet!");
885 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000886 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000887 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000888 }
889 break;
890 case TargetLowering::Custom:
891 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000892 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000893 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000894 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000895 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000896 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000897 break;
898 }
899 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000900 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000901 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000902
Gabor Greifba36cb52008-08-28 21:40:38 +0000903 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000904 "Cannot return more than two values!");
905
906 // Since we produced two values, make sure to remember that we
907 // legalized both of them.
908 Tmp1 = LegalizeOp(Result);
909 Tmp2 = LegalizeOp(Result.getValue(1));
910 AddLegalizedOperand(Op.getValue(0), Tmp1);
911 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000912 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000913 case ISD::EHSELECTION: {
914 Tmp1 = LegalizeOp(Node->getOperand(0));
915 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +0000917 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
918 default: assert(0 && "This action is not supported yet!");
919 case TargetLowering::Expand: {
920 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000921 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000922 }
923 break;
924 case TargetLowering::Custom:
925 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000926 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +0000927 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000928 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000929 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000930 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +0000931 break;
932 }
933 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000934 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000936
Gabor Greifba36cb52008-08-28 21:40:38 +0000937 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000938 "Cannot return more than two values!");
939
940 // Since we produced two values, make sure to remember that we
941 // legalized both of them.
942 Tmp1 = LegalizeOp(Result);
943 Tmp2 = LegalizeOp(Result.getValue(1));
944 AddLegalizedOperand(Op.getValue(0), Tmp1);
945 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000946 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner48b61a72006-03-28 00:40:33 +0000947 case ISD::INTRINSIC_W_CHAIN:
948 case ISD::INTRINSIC_WO_CHAIN:
949 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000951 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
952 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000953 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +0000954
Chris Lattner10d7fa62006-03-26 09:12:51 +0000955 // Allow the target to custom lower its intrinsics if it wants to.
Scott Michelfdc40a02009-02-17 22:15:04 +0000956 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000957 TargetLowering::Custom) {
958 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000959 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000960 }
961
Gabor Greifba36cb52008-08-28 21:40:38 +0000962 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000963
964 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +0000965 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +0000966 "Cannot return more than two values!");
967
Scott Michelfdc40a02009-02-17 22:15:04 +0000968 // Since loads produce two values, make sure to remember that we
Chris Lattner13fc2f12006-03-27 20:28:29 +0000969 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +0000970 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
971 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000972 return Result.getValue(Op.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +0000973 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000974
Dan Gohman7f460202008-06-30 20:59:49 +0000975 case ISD::DBG_STOPPOINT:
976 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000977 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000978
Dan Gohman7f460202008-06-30 20:59:49 +0000979 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000980 case TargetLowering::Promote:
981 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000982 case TargetLowering::Expand: {
983 DwarfWriter *DW = DAG.getDwarfWriter();
984 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
985 MVT::Other);
986 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000987
Bill Wendling92c1e122009-02-13 02:16:35 +0000988 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
989 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
990 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
991 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000992
Bill Wendling92c1e122009-02-13 02:16:35 +0000993 unsigned Line = DSP->getLine();
994 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000995
Bill Wendling98a366d2009-04-29 23:29:43 +0000996 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000997 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
998 // won't hurt anything.
999 if (useDEBUG_LOC) {
1000 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +00001001 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001002 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +00001003 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
1004 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001005 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +00001006 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
1007 }
Bill Wendling92c1e122009-02-13 02:16:35 +00001008 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +00001009 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +00001010 }
1011 } else {
1012 Result = Tmp1; // chain
1013 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001014 break;
Bill Wendling92c1e122009-02-13 02:16:35 +00001015 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001016 case TargetLowering::Custom:
1017 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001018 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001019 break;
Evan Cheng71e86852008-07-08 20:06:39 +00001020 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +00001021 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001022 break;
1023
Dan Gohman475871a2008-07-27 21:46:04 +00001024 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001025 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +00001026 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1027 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +00001028 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1029 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1030 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001031 break;
1032 }
Evan Cheng71e86852008-07-08 20:06:39 +00001033 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001034 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001035 case ISD::ConstantFP: {
1036 // Spill FP immediates to the constant pool if the target cannot directly
1037 // codegen them. Targets often have some immediate values that can be
1038 // efficiently generated into an FP register without a load. We explicitly
1039 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001040 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1041
Chris Lattner3181a772006-01-29 06:26:56 +00001042 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1043 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001044 case TargetLowering::Legal:
1045 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001046 case TargetLowering::Custom:
1047 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001048 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001049 Result = Tmp3;
1050 break;
1051 }
1052 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001053 case TargetLowering::Expand: {
1054 // Check to see if this FP immediate is already legal.
1055 bool isLegal = false;
1056 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1057 E = TLI.legal_fpimm_end(); I != E; ++I) {
1058 if (CFP->isExactlyValue(*I)) {
1059 isLegal = true;
1060 break;
1061 }
1062 }
1063 // If this is a legal constant, turn it into a TargetConstantFP node.
1064 if (isLegal)
1065 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001066 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001067 }
Nate Begemane1795842008-02-14 08:57:00 +00001068 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001069 break;
1070 }
Chris Lattnerfdfded52006-04-12 16:20:43 +00001071 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001072 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001073 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001074 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001076 // A call within a calling sequence must be legalized to something
1077 // other than the normal CALLSEQ_END. Violating this gets Legalize
1078 // into an infinite loop.
1079 assert ((!IsLegalizingCall ||
1080 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001081 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001082 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001083
1084 // The number of incoming and outgoing values should match; unless the final
1085 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1087 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1088 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001089 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001090 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001091
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001092 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001093 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001094 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1095 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001096 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001097 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001098 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001099 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001100 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001101 }
1102 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001103 case ISD::BUILD_VECTOR:
1104 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001105 default: assert(0 && "This action is not supported yet!");
1106 case TargetLowering::Custom:
1107 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001108 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001109 Result = Tmp3;
1110 break;
1111 }
1112 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001113 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001114 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001115 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001116 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001117 break;
1118 case ISD::INSERT_VECTOR_ELT:
1119 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001120 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001121
1122 // The type of the value to insert may not be legal, even though the vector
1123 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1124 // here.
Eli Friedman957bffa2009-05-24 08:42:01 +00001125 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2332b9f2006-03-19 01:17:20 +00001126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001127
Chris Lattner2332b9f2006-03-19 01:17:20 +00001128 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1129 Node->getValueType(0))) {
1130 default: assert(0 && "This action is not supported yet!");
1131 case TargetLowering::Legal:
1132 break;
1133 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001134 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001135 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001136 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001137 break;
1138 }
1139 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001140 case TargetLowering::Promote:
1141 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001142 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001143 // If the insert index is a constant, codegen this as a scalar_to_vector,
1144 // then a shuffle that inserts it into the right position in the vector.
1145 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001146 // SCALAR_TO_VECTOR requires that the type of the value being inserted
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001147 // match the element type of the vector being created, except for
1148 // integers in which case the inserted value can be over width.
1149 MVT EltVT = Op.getValueType().getVectorElementType();
1150 if (Tmp2.getValueType() == EltVT ||
1151 (EltVT.isInteger() && Tmp2.getValueType().bitsGE(EltVT))) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001152 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001153 Tmp1.getValueType(), Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001154
Duncan Sands83ec4b62008-06-06 12:08:01 +00001155 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
Nate Begeman0325d902008-02-13 06:43:04 +00001156 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1157 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1158 // elt 0 of the RHS.
Nate Begeman9008ca62009-04-27 18:41:29 +00001159 SmallVector<int, 8> ShufOps;
1160 for (unsigned i = 0; i != NumElts; ++i)
1161 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
1162
1163 Result = DAG.getVectorShuffle(Tmp1.getValueType(), dl, Tmp1, ScVec,
1164 &ShufOps[0]);
Nate Begeman0325d902008-02-13 06:43:04 +00001165 Result = LegalizeOp(Result);
1166 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001167 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001168 }
Dale Johannesenbb5da912009-02-02 20:41:04 +00001169 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001170 break;
1171 }
1172 }
1173 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001174 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001175 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1176 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1178 MVT VT = Result.getValueType();
1179
Nate Begeman5a5ca152009-04-29 05:20:52 +00001180 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001181 SmallVector<int, 8> Mask;
1182 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001183
1184 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001185 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001186 default: assert(0 && "Unknown operation action!");
1187 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001188 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001189 "vector shuffle should not be created if not legal!");
1190 break;
1191 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001192 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001193 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001194 Result = Tmp3;
1195 break;
1196 }
1197 // FALLTHROUGH
1198 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001199 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001200 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001201 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001202 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001203 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001204 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001205 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001206 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001207 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001208 if (Idx < NumElems)
1209 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1210 DAG.getIntPtrConstant(Idx)));
1211 else
1212 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1213 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001214 }
Evan Chenga87008d2009-02-25 22:49:59 +00001215 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001216 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001217 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001218 case TargetLowering::Promote: {
1219 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001220 MVT OVT = Node->getValueType(0);
1221 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001222
1223 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001224 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1225 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Chris Lattner4352cc92006-04-04 17:23:26 +00001227 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001228 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001229 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001230 break;
1231 }
Chris Lattner87100e02006-03-20 01:52:29 +00001232 }
1233 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001234 }
Mon P Wang0c397192008-10-30 08:01:45 +00001235 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001236 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001237 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001238 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1239 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1240 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1241
1242 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1243 Node->getValueType(0))) {
1244 default: assert(0 && "Unknown operation action!");
1245 case TargetLowering::Legal:
1246 break;
1247 case TargetLowering::Custom:
1248 Tmp3 = TLI.LowerOperation(Result, DAG);
1249 if (Tmp3.getNode()) {
1250 Result = Tmp3;
1251 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001252 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001253 // FALLTHROUGH
1254 case TargetLowering::Expand: {
1255 // Use extract/insert/build vector for now. We might try to be
1256 // more clever later.
1257 MVT PtrVT = TLI.getPointerTy();
1258 SmallVector<SDValue, 8> Ops;
1259 unsigned NumOperands = Node->getNumOperands();
1260 for (unsigned i=0; i < NumOperands; ++i) {
1261 SDValue SubOp = Node->getOperand(i);
1262 MVT VVT = SubOp.getNode()->getValueType(0);
1263 MVT EltVT = VVT.getVectorElementType();
1264 unsigned NumSubElem = VVT.getVectorNumElements();
1265 for (unsigned j=0; j < NumSubElem; ++j) {
1266 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1267 DAG.getConstant(j, PtrVT)));
1268 }
1269 }
1270 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1271 Node->getValueType(0),
1272 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001273 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001274 }
1275 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001276 }
1277
Chris Lattner6831a812006-02-13 09:18:02 +00001278 case ISD::CALLSEQ_START: {
1279 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Chris Lattner6831a812006-02-13 09:18:02 +00001281 // Recursively Legalize all of the inputs of the call end that do not lead
1282 // to this call start. This ensures that any libcalls that need be inserted
1283 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001284 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001285 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001287 NodesLeadingTo);
1288 }
Chris Lattner6831a812006-02-13 09:18:02 +00001289
1290 // Now that we legalized all of the inputs (which may have inserted
1291 // libcalls) create the new CALLSEQ_START node.
1292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1293
1294 // Merge in the last call, to ensure that this call start after the last
1295 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001296 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001297 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001298 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001299 Tmp1 = LegalizeOp(Tmp1);
1300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001301
Chris Lattner6831a812006-02-13 09:18:02 +00001302 // Do not try to legalize the target-specific arguments (#1+).
1303 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001304 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001305 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001306 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Chris Lattner6831a812006-02-13 09:18:02 +00001309 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001310 AddLegalizedOperand(Op.getValue(0), Result);
1311 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1312 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001313
Chris Lattner6831a812006-02-13 09:18:02 +00001314 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001315 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001316 // process, no libcalls can/will be inserted, guaranteeing that no calls
1317 // can overlap.
1318 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001319 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001320 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001321 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001322
Chris Lattner6831a812006-02-13 09:18:02 +00001323 // Legalize the call, starting from the CALLSEQ_END.
1324 LegalizeOp(LastCALLSEQ_END);
1325 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1326 return Result;
1327 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001328 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001329 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1330 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001331 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001332 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1333 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001334 assert(I != LegalizedNodes.end() &&
1335 "Legalizing the call start should have legalized this node!");
1336 return I->second;
1337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001338
1339 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001340 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001342 // Do not try to legalize the target-specific arguments (#1+), except for
1343 // an optional flag input.
1344 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1345 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001346 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001347 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001348 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001349 }
1350 } else {
1351 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1352 if (Tmp1 != Node->getOperand(0) ||
1353 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001354 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001355 Ops[0] = Tmp1;
1356 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001357 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001358 }
Chris Lattner6a542892006-01-24 05:48:21 +00001359 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001360 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001361 // This finishes up call legalization.
1362 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001363
Chris Lattner4b653a02006-02-14 00:55:02 +00001364 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001365 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001366 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001367 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001368 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001369 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001370 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001371 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1372 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1373 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001375
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001376 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001377 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001378 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001379 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001380 case TargetLowering::Expand: {
1381 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1382 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1383 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001384 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001385
1386 // Chain the dynamic stack allocation so that it doesn't modify the stack
1387 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001388 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001389
Dan Gohman475871a2008-07-27 21:46:04 +00001390 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001391 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001392 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001393 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001394 unsigned StackAlign =
1395 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1396 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001397 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001398 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001399 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001400 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001401
Dale Johannesenca57b842009-02-02 23:46:53 +00001402 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001403 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001404
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001405 Tmp1 = LegalizeOp(Tmp1);
1406 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001407 break;
1408 }
1409 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001410 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001412 Tmp1 = LegalizeOp(Tmp3);
1413 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001414 }
Chris Lattner903d2782006-01-15 08:54:32 +00001415 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001416 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001417 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001418 }
Chris Lattner903d2782006-01-15 08:54:32 +00001419 // Since this op produce two values, make sure to remember that we
1420 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001421 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1422 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001423 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001424 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001425 case ISD::BR_JT:
1426 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1427 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001428 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001429 Tmp1 = LegalizeOp(Tmp1);
1430 LastCALLSEQ_END = DAG.getEntryNode();
1431
1432 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1433 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1434
Scott Michelfdc40a02009-02-17 22:15:04 +00001435 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001436 default: assert(0 && "This action is not supported yet!");
1437 case TargetLowering::Legal: break;
1438 case TargetLowering::Custom:
1439 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001440 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001441 break;
1442 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Chain = Result.getOperand(0);
1444 SDValue Table = Result.getOperand(1);
1445 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001446
Duncan Sands83ec4b62008-06-06 12:08:01 +00001447 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001448 MachineFunction &MF = DAG.getMachineFunction();
1449 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001450 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001451 Index, DAG.getConstant(EntrySize, PTy));
1452 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001453
Duncan Sands712f7b32008-12-12 08:13:38 +00001454 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001455 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001456 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001457 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001458 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001459 // For PIC, the sequence is:
1460 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001461 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001462 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001463 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001464 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001465 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001466 }
1467 }
1468 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001469 case ISD::BRCOND:
1470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001471 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00001472 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001473 Tmp1 = LegalizeOp(Tmp1);
1474 LastCALLSEQ_END = DAG.getEntryNode();
1475
Eli Friedman957bffa2009-05-24 08:42:01 +00001476 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00001477
1478 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001479 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
1481 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001482 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001483 case TargetLowering::Legal: break;
1484 case TargetLowering::Custom:
1485 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001486 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001487 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001488 case TargetLowering::Expand:
1489 // Expand brcond's setcc into its constituent parts and create a BR_CC
1490 // Node.
1491 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001492 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001493 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00001494 Tmp2.getOperand(0), Tmp2.getOperand(1),
1495 Node->getOperand(2));
1496 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001497 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00001498 DAG.getCondCode(ISD::SETNE), Tmp2,
1499 DAG.getConstant(0, Tmp2.getValueType()),
1500 Node->getOperand(2));
1501 }
1502 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001503 }
1504 break;
1505 case ISD::BR_CC:
1506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001507 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001508 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001509 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001510 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001511 Tmp3 = Node->getOperand(3); // RHS
1512 Tmp4 = Node->getOperand(1); // CC
1513
Scott Michelfdc40a02009-02-17 22:15:04 +00001514 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001515 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001516 LastCALLSEQ_END = DAG.getEntryNode();
1517
Evan Cheng7f042682008-10-15 02:05:31 +00001518 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001519 // the LHS is a legal SETCC itself. In this case, we need to compare
1520 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001521 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001522 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1523 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001525
1526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001527 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001528
Chris Lattner181b7a32005-12-17 23:46:46 +00001529 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1530 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001531 case TargetLowering::Legal: break;
1532 case TargetLowering::Custom:
1533 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001534 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001535 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001536 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001537 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001538 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001539 LoadSDNode *LD = cast<LoadSDNode>(Node);
1540 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1541 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001542
Evan Cheng466685d2006-10-09 20:57:25 +00001543 ISD::LoadExtType ExtType = LD->getExtensionType();
1544 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001545 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001546 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1547 Tmp3 = Result.getValue(0);
1548 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Evan Cheng466685d2006-10-09 20:57:25 +00001550 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1551 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001552 case TargetLowering::Legal:
1553 // If this is an unaligned load and the target doesn't support it,
1554 // expand it.
1555 if (!TLI.allowsUnalignedMemoryAccesses()) {
1556 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001557 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001558 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001559 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001560 TLI);
1561 Tmp3 = Result.getOperand(0);
1562 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001563 Tmp3 = LegalizeOp(Tmp3);
1564 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001565 }
1566 }
1567 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001568 case TargetLowering::Custom:
1569 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001570 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001571 Tmp3 = LegalizeOp(Tmp1);
1572 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001573 }
Evan Cheng466685d2006-10-09 20:57:25 +00001574 break;
1575 case TargetLowering::Promote: {
1576 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001577 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001578 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001579 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001580
Dale Johannesenca57b842009-02-02 23:46:53 +00001581 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001582 LD->getSrcValueOffset(),
1583 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001584 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001585 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001586 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001587 }
Evan Cheng466685d2006-10-09 20:57:25 +00001588 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001589 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001590 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001591 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1592 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001593 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001594 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001595 MVT SrcVT = LD->getMemoryVT();
1596 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001597 int SVOffset = LD->getSrcValueOffset();
1598 unsigned Alignment = LD->getAlignment();
1599 bool isVolatile = LD->isVolatile();
1600
Duncan Sands83ec4b62008-06-06 12:08:01 +00001601 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001602 // Some targets pretend to have an i1 loading operation, and actually
1603 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1604 // bits are guaranteed to be zero; it helps the optimizers understand
1605 // that these bits are zero. It is also useful for EXTLOAD, since it
1606 // tells the optimizers that those bits are undefined. It would be
1607 // nice to have an effective generic way of getting these benefits...
1608 // Until such a way is found, don't insist on promoting i1 here.
1609 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001610 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001611 // Promote to a byte-sized load if not loading an integral number of
1612 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001613 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1614 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001615 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001616
1617 // The extra bits are guaranteed to be zero, since we stored them that
1618 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1619
1620 ISD::LoadExtType NewExtType =
1621 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1622
Dale Johannesenca57b842009-02-02 23:46:53 +00001623 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001624 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1625 NVT, isVolatile, Alignment);
1626
1627 Ch = Result.getValue(1); // The chain.
1628
1629 if (ExtType == ISD::SEXTLOAD)
1630 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001631 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001632 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001633 Result, DAG.getValueType(SrcVT));
1634 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1635 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001636 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001637 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001638 DAG.getValueType(SrcVT));
1639
1640 Tmp1 = LegalizeOp(Result);
1641 Tmp2 = LegalizeOp(Ch);
1642 } else if (SrcWidth & (SrcWidth - 1)) {
1643 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001644 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001645 "Unsupported extload!");
1646 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1647 assert(RoundWidth < SrcWidth);
1648 unsigned ExtraWidth = SrcWidth - RoundWidth;
1649 assert(ExtraWidth < RoundWidth);
1650 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1651 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001652 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1653 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001654 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001655 unsigned IncrementSize;
1656
1657 if (TLI.isLittleEndian()) {
1658 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1659 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001660 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1661 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001662 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1663 Alignment);
1664
1665 // Load the remaining ExtraWidth bits.
1666 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001667 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001668 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001669 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001670 LD->getSrcValue(), SVOffset + IncrementSize,
1671 ExtraVT, isVolatile,
1672 MinAlign(Alignment, IncrementSize));
1673
1674 // Build a factor node to remember that this load is independent of the
1675 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001676 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001677 Hi.getValue(1));
1678
1679 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001680 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001681 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1682
1683 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001684 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001685 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001686 // Big endian - avoid unaligned loads.
1687 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1688 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001689 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001690 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1691 Alignment);
1692
1693 // Load the remaining ExtraWidth bits.
1694 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001695 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001696 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001697 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001698 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001699 LD->getSrcValue(), SVOffset + IncrementSize,
1700 ExtraVT, isVolatile,
1701 MinAlign(Alignment, IncrementSize));
1702
1703 // Build a factor node to remember that this load is independent of the
1704 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001705 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001706 Hi.getValue(1));
1707
1708 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001709 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001710 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1711
1712 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001713 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001714 }
1715
1716 Tmp1 = LegalizeOp(Result);
1717 Tmp2 = LegalizeOp(Ch);
1718 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001719 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001720 default: assert(0 && "This action is not supported yet!");
1721 case TargetLowering::Custom:
1722 isCustom = true;
1723 // FALLTHROUGH
1724 case TargetLowering::Legal:
1725 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1726 Tmp1 = Result.getValue(0);
1727 Tmp2 = Result.getValue(1);
1728
1729 if (isCustom) {
1730 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001731 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001732 Tmp1 = LegalizeOp(Tmp3);
1733 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1734 }
1735 } else {
1736 // If this is an unaligned load and the target doesn't support it,
1737 // expand it.
1738 if (!TLI.allowsUnalignedMemoryAccesses()) {
1739 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001740 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001741 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001742 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001743 TLI);
1744 Tmp1 = Result.getOperand(0);
1745 Tmp2 = Result.getOperand(1);
1746 Tmp1 = LegalizeOp(Tmp1);
1747 Tmp2 = LegalizeOp(Tmp2);
1748 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001749 }
1750 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001751 break;
1752 case TargetLowering::Expand:
1753 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1754 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001755 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001756 LD->getSrcValueOffset(),
1757 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001758 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001759 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001760 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1761 Tmp2 = LegalizeOp(Load.getValue(1));
1762 break;
1763 }
1764 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1765 // Turn the unsupported load into an EXTLOAD followed by an explicit
1766 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001767 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001768 Tmp1, Tmp2, LD->getSrcValue(),
1769 LD->getSrcValueOffset(), SrcVT,
1770 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001771 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001772 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001773 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1774 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001775 Result, DAG.getValueType(SrcVT));
1776 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001777 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001778 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1779 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001780 break;
1781 }
Evan Cheng466685d2006-10-09 20:57:25 +00001782 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001783
Evan Cheng466685d2006-10-09 20:57:25 +00001784 // Since loads produce two values, make sure to remember that we legalized
1785 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001786 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1787 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001788 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001789 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001790 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001791 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001792 StoreSDNode *ST = cast<StoreSDNode>(Node);
1793 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1794 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001795 int SVOffset = ST->getSrcValueOffset();
1796 unsigned Alignment = ST->getAlignment();
1797 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001798
Evan Cheng8b2794a2006-10-13 21:14:26 +00001799 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001800 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1801 // FIXME: We shouldn't do this for TargetConstantFP's.
1802 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1803 // to phase ordering between legalized code and the dag combiner. This
1804 // probably means that we need to integrate dag combiner and legalizer
1805 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001806 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001807 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001808 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001809 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001810 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001811 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001812 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001813 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001814 SVOffset, isVolatile, Alignment);
1815 break;
1816 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001817 // If this target supports 64-bit registers, do a single 64-bit store.
1818 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001819 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001820 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001821 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001822 SVOffset, isVolatile, Alignment);
1823 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001824 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001825 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1826 // stores. If the target supports neither 32- nor 64-bits, this
1827 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001828 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001829 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1830 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001831 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001832
Dale Johannesenca57b842009-02-02 23:46:53 +00001833 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001834 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001835 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001836 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001837 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001838 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001839
Dale Johannesenca57b842009-02-02 23:46:53 +00001840 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001841 break;
1842 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001843 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Eli Friedman957bffa2009-05-24 08:42:01 +00001846 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001847 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001848 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001849 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001850
Duncan Sands83ec4b62008-06-06 12:08:01 +00001851 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001852 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1853 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001854 case TargetLowering::Legal:
1855 // If this is an unaligned store and the target doesn't support it,
1856 // expand it.
1857 if (!TLI.allowsUnalignedMemoryAccesses()) {
1858 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001860 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001861 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001862 TLI);
1863 }
1864 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001865 case TargetLowering::Custom:
1866 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001867 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001868 break;
1869 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001870 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001871 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001872 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001873 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001874 ST->getSrcValue(), SVOffset, isVolatile,
1875 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001876 break;
1877 }
1878 break;
1879 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001880 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001881 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001882
Duncan Sands83ec4b62008-06-06 12:08:01 +00001883 MVT StVT = ST->getMemoryVT();
1884 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001885
Duncan Sands83ec4b62008-06-06 12:08:01 +00001886 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001887 // Promote to a byte-sized store with upper bits zero if not
1888 // storing an integral number of bytes. For example, promote
1889 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001890 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001891 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1892 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001893 SVOffset, NVT, isVolatile, Alignment);
1894 } else if (StWidth & (StWidth - 1)) {
1895 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001896 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001897 "Unsupported truncstore!");
1898 unsigned RoundWidth = 1 << Log2_32(StWidth);
1899 assert(RoundWidth < StWidth);
1900 unsigned ExtraWidth = StWidth - RoundWidth;
1901 assert(ExtraWidth < RoundWidth);
1902 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1903 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001904 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1905 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001906 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001907 unsigned IncrementSize;
1908
1909 if (TLI.isLittleEndian()) {
1910 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1911 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001912 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001913 SVOffset, RoundVT,
1914 isVolatile, Alignment);
1915
1916 // Store the remaining ExtraWidth bits.
1917 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001918 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001919 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001920 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001921 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001922 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001923 SVOffset + IncrementSize, ExtraVT, isVolatile,
1924 MinAlign(Alignment, IncrementSize));
1925 } else {
1926 // Big endian - avoid unaligned stores.
1927 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1928 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001929 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001930 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001931 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1932 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001933
1934 // Store the remaining ExtraWidth bits.
1935 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001936 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001937 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001938 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001939 SVOffset + IncrementSize, ExtraVT, isVolatile,
1940 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001941 }
Duncan Sands7e857202008-01-22 07:17:34 +00001942
1943 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001944 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001945 } else {
1946 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1947 Tmp2 != ST->getBasePtr())
1948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1949 ST->getOffset());
1950
1951 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1952 default: assert(0 && "This action is not supported yet!");
1953 case TargetLowering::Legal:
1954 // If this is an unaligned store and the target doesn't support it,
1955 // expand it.
1956 if (!TLI.allowsUnalignedMemoryAccesses()) {
1957 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001958 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001959 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001960 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001961 TLI);
1962 }
1963 break;
1964 case TargetLowering::Custom:
1965 Result = TLI.LowerOperation(Result, DAG);
1966 break;
1967 case Expand:
1968 // TRUNCSTORE:i16 i32 -> STORE i16
1969 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001970 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1971 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1972 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001973 break;
1974 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001975 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001976 }
1977 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001978 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001979 case ISD::STACKSAVE:
1980 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001981 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1982 Tmp1 = Result.getValue(0);
1983 Tmp2 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001984
Chris Lattner140d53c2006-01-13 02:50:02 +00001985 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1986 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001987 case TargetLowering::Legal: break;
1988 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001989 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001990 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001991 Tmp1 = LegalizeOp(Tmp3);
1992 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001993 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001994 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001995 case TargetLowering::Expand:
Scott Michelfdc40a02009-02-17 22:15:04 +00001996 // Expand to CopyFromReg if the target set
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001997 // StackPointerRegisterToSaveRestore.
1998 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00001999 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), dl, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002000 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002001 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002002 } else {
Dale Johannesene8d72302009-02-06 23:05:02 +00002003 Tmp1 = DAG.getUNDEF(Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002004 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002005 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002006 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002007 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002008
2009 // Since stacksave produce two values, make sure to remember that we
2010 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002011 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2012 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002013 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002014
Chris Lattner140d53c2006-01-13 02:50:02 +00002015 case ISD::STACKRESTORE:
2016 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2017 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002018 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002019
Chris Lattner140d53c2006-01-13 02:50:02 +00002020 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2021 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002022 case TargetLowering::Legal: break;
2023 case TargetLowering::Custom:
2024 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002025 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002026 break;
2027 case TargetLowering::Expand:
Scott Michelfdc40a02009-02-17 22:15:04 +00002028 // Expand to CopyToReg if the target set
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002029 // StackPointerRegisterToSaveRestore.
2030 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002031 Result = DAG.getCopyToReg(Tmp1, dl, SP, Tmp2);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002032 } else {
2033 Result = Tmp1;
2034 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002035 break;
2036 }
2037 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00002038 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00002039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00002040 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002041 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002042
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002043 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00002044
Nate Begemanb942a3d2005-08-23 04:29:48 +00002045 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002046 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002047 case TargetLowering::Legal: break;
2048 case TargetLowering::Custom: {
2049 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002050 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002051 break;
2052 }
Nate Begeman9373a812005-08-10 20:51:12 +00002053 case TargetLowering::Expand:
2054 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002055 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00002056 Tmp2, Tmp3,
2057 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2058 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002059 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00002060 DAG.getConstant(0, Tmp1.getValueType()),
2061 Tmp2, Tmp3, ISD::SETNE);
2062 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002063 break;
2064 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002065 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002066 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2067 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002068 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002069 ExtOp = ISD::BIT_CONVERT;
2070 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002071 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002072 ExtOp = ISD::ANY_EXTEND;
2073 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002074 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002075 ExtOp = ISD::FP_EXTEND;
2076 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002077 }
2078 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002079 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
2080 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002081 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002082 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002083 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00002084 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00002085 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002086 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00002087 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002088 break;
2089 }
2090 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002091 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002092 case ISD::SELECT_CC: {
2093 Tmp1 = Node->getOperand(0); // LHS
2094 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002095 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2096 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002097 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00002098
2099 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00002100 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00002101
Evan Cheng7f042682008-10-15 02:05:31 +00002102 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002103 // the LHS is a legal SETCC itself. In this case, we need to compare
2104 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002105 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002106 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2107 CC = DAG.getCondCode(ISD::SETNE);
2108 }
2109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2110
2111 // Everything is legal, see if we should expand this op or something.
2112 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2113 default: assert(0 && "This action is not supported yet!");
2114 case TargetLowering::Legal: break;
2115 case TargetLowering::Custom:
2116 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002117 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002118 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002119 }
2120 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002121 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002122 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002123 Tmp1 = Node->getOperand(0);
2124 Tmp2 = Node->getOperand(1);
2125 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00002126 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00002127
2128 // If we had to Expand the SetCC operands into a SELECT node, then it may
2129 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00002130 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002131 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002132 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002133 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002134 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002135
Chris Lattner73e142f2006-01-30 22:43:50 +00002136 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002137 default: assert(0 && "Cannot handle this action for SETCC yet!");
2138 case TargetLowering::Custom:
2139 isCustom = true;
2140 // FALLTHROUGH.
2141 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002142 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002143 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002144 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002145 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002146 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002147 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002148 case TargetLowering::Promote: {
2149 // First step, figure out the appropriate operation to use.
2150 // Allow SETCC to not be supported for all legal data types
2151 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002152 MVT NewInTy = Node->getOperand(0).getValueType();
2153 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002154
2155 // Scan for the appropriate larger type to use.
2156 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002157 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002158
Duncan Sands83ec4b62008-06-06 12:08:01 +00002159 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002160 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002161 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002162 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00002163
Andrew Lenharthae355752005-11-30 17:12:26 +00002164 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002165 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002166 break;
2167 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002168 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002169 assert(0 && "Cannot promote Legal Integer SETCC yet");
2170 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002171 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
2172 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00002173 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002174 Tmp1 = LegalizeOp(Tmp1);
2175 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002177 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002178 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002179 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002180 case TargetLowering::Expand:
2181 // Expand a setcc node into a select_cc of the same condition, lhs, and
2182 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002183 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002184 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00002185 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002186 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002187 break;
2188 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002189 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002190
2191 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002192 case ISD::ADD:
2193 case ISD::SUB:
2194 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002195 case ISD::MULHS:
2196 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002197 case ISD::UDIV:
2198 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002199 case ISD::AND:
2200 case ISD::OR:
2201 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002202 case ISD::SHL:
2203 case ISD::SRL:
2204 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002205 case ISD::FADD:
2206 case ISD::FSUB:
2207 case ISD::FMUL:
2208 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002209 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002210 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00002211 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002212
2213 if ((Node->getOpcode() == ISD::SHL ||
2214 Node->getOpcode() == ISD::SRL ||
2215 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00002216 !Node->getValueType(0).isVector())
2217 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
2218
Eli Friedman957bffa2009-05-24 08:42:01 +00002219 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002220
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002221 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002222
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002223 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002224 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002225 case TargetLowering::Legal: break;
2226 case TargetLowering::Custom:
2227 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002228 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002229 Result = Tmp1;
2230 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00002231 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002232 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002233 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002234 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002235
Dan Gohman525178c2007-10-08 18:33:35 +00002236 // See if multiply or divide can be lowered using two-result operations.
2237 SDVTList VTs = DAG.getVTList(VT, VT);
2238 if (Node->getOpcode() == ISD::MUL) {
2239 // We just need the low half of the multiply; try both the signed
2240 // and unsigned forms. If the target supports both SMUL_LOHI and
2241 // UMUL_LOHI, form a preference by checking which forms of plain
2242 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002243 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2244 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2245 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2246 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00002247 unsigned OpToUse = 0;
2248 if (HasSMUL_LOHI && !HasMULHS) {
2249 OpToUse = ISD::SMUL_LOHI;
2250 } else if (HasUMUL_LOHI && !HasMULHU) {
2251 OpToUse = ISD::UMUL_LOHI;
2252 } else if (HasSMUL_LOHI) {
2253 OpToUse = ISD::SMUL_LOHI;
2254 } else if (HasUMUL_LOHI) {
2255 OpToUse = ISD::UMUL_LOHI;
2256 }
2257 if (OpToUse) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002258 Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002259 break;
2260 }
2261 }
2262 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002263 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002265 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002266 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002267 break;
2268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002269 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002270 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002271 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
2272 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002273 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002274 break;
2275 }
2276 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002277 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002278 Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002279 break;
2280 }
2281 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002282 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002283 Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2);
2284 break;
2285 }
2286 if (Node->getOpcode() == ISD::SUB &&
2287 TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2288 TLI.isOperationLegalOrCustom(ISD::XOR, VT)) {
2289 Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2,
2290 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
2291 Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2292 Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002293 break;
2294 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00002295
Dan Gohman82669522007-10-11 23:57:53 +00002296 // Check to see if we have a libcall for this operator.
2297 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2298 bool isSigned = false;
2299 switch (Node->getOpcode()) {
2300 case ISD::UDIV:
2301 case ISD::SDIV:
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002302 isSigned = Node->getOpcode() == ISD::SDIV;
2303 if (VT == MVT::i16)
2304 LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16);
2305 else if (VT == MVT::i32)
2306 LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32);
2307 else if (VT == MVT::i64)
2308 LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64);
2309 else if (VT == MVT::i128)
2310 LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128);
2311 break;
Chris Lattner31d71612008-10-04 21:27:46 +00002312 case ISD::MUL:
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002313 if (VT == MVT::i16)
2314 LC = RTLIB::MUL_I16;
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002315 else if (VT == MVT::i32)
Chris Lattner31d71612008-10-04 21:27:46 +00002316 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00002317 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00002318 LC = RTLIB::MUL_I64;
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002319 else if (VT == MVT::i128)
2320 LC = RTLIB::MUL_I128;
Chris Lattner31d71612008-10-04 21:27:46 +00002321 break;
Dan Gohman82669522007-10-11 23:57:53 +00002322 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002323 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2324 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002325 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002326 case ISD::FDIV:
2327 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
2328 RTLIB::DIV_PPCF128);
2329 break;
Dan Gohman82669522007-10-11 23:57:53 +00002330 default: break;
2331 }
2332 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00002333 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002334 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002335 break;
2336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002337
Eli Friedman74807f22009-05-26 08:55:52 +00002338 assert(0 && "Cannot expand this binary operator!");
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002339 break;
2340 }
Evan Chengcc987612006-04-12 21:20:24 +00002341 case TargetLowering::Promote: {
2342 switch (Node->getOpcode()) {
2343 default: assert(0 && "Do not know how to promote this BinOp!");
2344 case ISD::AND:
2345 case ISD::OR:
2346 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002347 MVT OVT = Node->getValueType(0);
2348 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2349 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00002350 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002351 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
2352 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
2353 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00002354 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002355 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00002356 break;
2357 }
2358 }
2359 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002360 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002361 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002362 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2363 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00002364 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00002365
Chris Lattnera09f8482006-03-05 05:09:38 +00002366 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002367
Chris Lattnera09f8482006-03-05 05:09:38 +00002368 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2369 default: assert(0 && "Operation not supported");
2370 case TargetLowering::Custom:
2371 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002372 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002373 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002374 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002375 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002376 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002377 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002378 "Ugly special-cased code!");
2379 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002380 SDValue SignBit;
2381 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
2382 if (isTypeLegal(IVT)) {
2383 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
2384 } else {
2385 assert(isTypeLegal(TLI.getPointerTy()) &&
2386 (TLI.getPointerTy() == MVT::i32 ||
2387 TLI.getPointerTy() == MVT::i64) &&
2388 "Legal type for load?!");
2389 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
2390 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
2391 SDValue Ch =
2392 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
2393 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
2394 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
2395 LoadPtr, DAG.getIntPtrConstant(4));
2396 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
2397 Ch, LoadPtr, NULL, 0, MVT::i32);
2398 }
2399 SignBit =
2400 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
2401 SignBit, DAG.getConstant(0, SignBit.getValueType()),
2402 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002403 // Get the absolute value of the result.
2404 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
2405 // Select between the nabs and abs value based on the sign bit of
2406 // the input.
2407 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
2408 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
2409 AbsVal),
2410 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00002411 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002412 break;
2413 }
Evan Cheng912095b2007-01-04 21:56:39 +00002414 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002415 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002416 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002417 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00002418 // TODO: handle the case where the Lo and Hi operands are not of legal type
2419 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2420 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2421 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002422 case TargetLowering::Promote:
2423 case TargetLowering::Custom:
2424 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002425 case TargetLowering::Legal:
2426 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00002427 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002428 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002429 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00002430 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
2431 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
2432 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002433 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00002434 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002435 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002436 break;
2437 }
2438 break;
2439 }
2440
Nate Begemanc105e192005-04-06 00:23:54 +00002441 case ISD::UREM:
2442 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002443 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002444 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2445 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002446
Nate Begemanc105e192005-04-06 00:23:54 +00002447 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002448 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2449 case TargetLowering::Custom:
2450 isCustom = true;
2451 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002452 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002454 if (isCustom) {
2455 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002456 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002457 }
Nate Begemanc105e192005-04-06 00:23:54 +00002458 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002459 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002460 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002461 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002462 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002463
Dan Gohman525178c2007-10-08 18:33:35 +00002464 // See if remainder can be lowered using two-result operations.
2465 SDVTList VTs = DAG.getVTList(VT, VT);
2466 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002467 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002468 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002469 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002470 break;
2471 }
2472 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002473 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002474 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
2475 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002476 break;
2477 }
2478
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002479 if (VT.isInteger() &&
2480 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
2481 // X % Y -> X-X/Y*Y
2482 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
2483 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
2484 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
2485 break;
Nate Begemanc105e192005-04-06 00:23:54 +00002486 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002487
2488 // Check to see if we have a libcall for this operator.
2489 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2490 switch (Node->getOpcode()) {
2491 default: break;
2492 case ISD::UREM:
2493 case ISD::SREM:
2494 if (VT == MVT::i16)
2495 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
2496 else if (VT == MVT::i32)
2497 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
2498 else if (VT == MVT::i64)
2499 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
2500 else if (VT == MVT::i128)
2501 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
2502 break;
2503 case ISD::FREM:
2504 // Floating point mod -> fmod libcall.
2505 LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
2506 RTLIB::REM_F80, RTLIB::REM_PPCF128);
2507 break;
2508 }
2509
2510 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2511 SDValue Dummy;
2512 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
2513 break;
2514 }
2515
Eli Friedman74807f22009-05-26 08:55:52 +00002516 assert(0 && "Cannot expand this binary operator!");
Nate Begemanc105e192005-04-06 00:23:54 +00002517 break;
2518 }
Dan Gohman525178c2007-10-08 18:33:35 +00002519 }
Nate Begemanc105e192005-04-06 00:23:54 +00002520 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002521 case ISD::VAARG: {
2522 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2523 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2524
Duncan Sands83ec4b62008-06-06 12:08:01 +00002525 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002526 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2527 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002528 case TargetLowering::Custom:
2529 isCustom = true;
2530 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002531 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002532 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2533 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002534 Tmp1 = Result.getValue(1);
2535
2536 if (isCustom) {
2537 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002538 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002539 Result = LegalizeOp(Tmp2);
2540 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2541 }
2542 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002543 break;
2544 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00002545 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00002546 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002547 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00002548 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002549 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00002550 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002551 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00002552 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00002553 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002554 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00002555 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002556 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002557 Result = LegalizeOp(Result);
2558 break;
2559 }
2560 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002561 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00002562 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002563 AddLegalizedOperand(SDValue(Node, 0), Result);
2564 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002565 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002566 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002567 // Unary operators
2568 case ISD::FABS:
2569 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002570 case ISD::FSQRT:
2571 case ISD::FSIN:
2572 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002573 case ISD::FLOG:
2574 case ISD::FLOG2:
2575 case ISD::FLOG10:
2576 case ISD::FEXP:
2577 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00002578 case ISD::FTRUNC:
2579 case ISD::FFLOOR:
2580 case ISD::FCEIL:
2581 case ISD::FRINT:
2582 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002583 Tmp1 = LegalizeOp(Node->getOperand(0));
2584 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002585 case TargetLowering::Promote:
2586 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002587 isCustom = true;
2588 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002589 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002590 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002591 if (isCustom) {
2592 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002593 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00002594 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002595 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002596 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002597 switch (Node->getOpcode()) {
2598 default: assert(0 && "Unreachable!");
2599 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002600 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2601 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00002602 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002603 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002604 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002605 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002607 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00002608 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002609 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00002610 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2611 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002612 break;
2613 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00002614 case ISD::FSQRT:
2615 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00002616 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002617 case ISD::FLOG:
2618 case ISD::FLOG2:
2619 case ISD::FLOG10:
2620 case ISD::FEXP:
2621 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00002622 case ISD::FTRUNC:
2623 case ISD::FFLOOR:
2624 case ISD::FCEIL:
2625 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00002626 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002627 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00002628
Eli Friedman74807f22009-05-26 08:55:52 +00002629 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00002630
Evan Cheng56966222007-01-12 02:11:51 +00002631 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002632 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002633 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00002634 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2635 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002636 break;
2637 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00002638 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
2639 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002640 break;
2641 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00002642 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
2643 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002644 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002645 case ISD::FLOG:
2646 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
2647 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
2648 break;
2649 case ISD::FLOG2:
2650 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2651 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
2652 break;
2653 case ISD::FLOG10:
2654 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2655 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
2656 break;
2657 case ISD::FEXP:
2658 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
2659 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
2660 break;
2661 case ISD::FEXP2:
2662 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2663 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
2664 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00002665 case ISD::FTRUNC:
2666 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2667 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
2668 break;
2669 case ISD::FFLOOR:
2670 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2671 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
2672 break;
2673 case ISD::FCEIL:
2674 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2675 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
2676 break;
2677 case ISD::FRINT:
2678 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
2679 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
2680 break;
2681 case ISD::FNEARBYINT:
2682 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
2683 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
2684 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00002685 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002686 default: assert(0 && "Unreachable!");
2687 }
Dan Gohman475871a2008-07-27 21:46:04 +00002688 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002689 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002690 break;
2691 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002692 }
2693 break;
2694 }
2695 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002696 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002697 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00002698
2699 // Expand unsupported unary vector operators by unrolling them.
Eli Friedman74807f22009-05-26 08:55:52 +00002700 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00002701
2702 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00002703 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
2704 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00002705 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002706 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002707 break;
2708 }
Bill Wendling74c37652008-12-09 22:08:41 +00002709 case ISD::SADDO:
2710 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002711 MVT VT = Node->getValueType(0);
2712 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2713 default: assert(0 && "This action not supported for this op yet!");
2714 case TargetLowering::Custom:
2715 Result = TLI.LowerOperation(Op, DAG);
2716 if (Result.getNode()) break;
2717 // FALLTHROUGH
2718 case TargetLowering::Legal: {
2719 SDValue LHS = LegalizeOp(Node->getOperand(0));
2720 SDValue RHS = LegalizeOp(Node->getOperand(1));
2721
Scott Michelfdc40a02009-02-17 22:15:04 +00002722 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002723 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002724 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002725 MVT OType = Node->getValueType(1);
2726
Bill Wendlinga6af91a2008-11-25 08:19:22 +00002727 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002728
Bill Wendling740464e2008-11-25 19:40:17 +00002729 // LHSSign -> LHS >= 0
2730 // RHSSign -> RHS >= 0
2731 // SumSign -> Sum >= 0
2732 //
Bill Wendling74c37652008-12-09 22:08:41 +00002733 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00002734 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00002735 // Sub:
2736 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00002737 //
Dale Johannesenca57b842009-02-02 23:46:53 +00002738 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2739 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00002740 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2741 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002742 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002743
Dale Johannesenca57b842009-02-02 23:46:53 +00002744 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2745 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002746
Dale Johannesenca57b842009-02-02 23:46:53 +00002747 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002748
2749 MVT ValueVTs[] = { LHS.getValueType(), OType };
2750 SDValue Ops[] = { Sum, Cmp };
2751
Scott Michelfdc40a02009-02-17 22:15:04 +00002752 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002753 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002754 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002755 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002756 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002757 break;
2758 }
2759 }
2760
2761 break;
2762 }
Bill Wendling74c37652008-12-09 22:08:41 +00002763 case ISD::UADDO:
2764 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00002765 MVT VT = Node->getValueType(0);
2766 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2767 default: assert(0 && "This action not supported for this op yet!");
2768 case TargetLowering::Custom:
2769 Result = TLI.LowerOperation(Op, DAG);
2770 if (Result.getNode()) break;
2771 // FALLTHROUGH
2772 case TargetLowering::Legal: {
2773 SDValue LHS = LegalizeOp(Node->getOperand(0));
2774 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002775
Bill Wendling74c37652008-12-09 22:08:41 +00002776 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002777 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002778 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002779 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00002780 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00002781 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002782 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002783
Bill Wendling41ea7e72008-11-24 19:21:46 +00002784 MVT ValueVTs[] = { LHS.getValueType(), OType };
2785 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002786
Scott Michelfdc40a02009-02-17 22:15:04 +00002787 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002788 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002789 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002790 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002791 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002792 break;
2793 }
2794 }
2795
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002796 break;
2797 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002798 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002799
Chris Lattner4ddd2832006-04-08 04:13:17 +00002800 assert(Result.getValueType() == Op.getValueType() &&
2801 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002802
Chris Lattner456a93a2006-01-28 07:39:30 +00002803 // Make sure that the generated code is itself legal.
2804 if (Result != Op)
2805 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002806
Chris Lattner45982da2005-05-12 16:53:42 +00002807 // Note that LegalizeOp may be reentered even from single-use nodes, which
2808 // means that we always must cache transformed nodes.
2809 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002810 return Result;
2811}
2812
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002813SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
2814 SDValue Vec = Op.getOperand(0);
2815 SDValue Idx = Op.getOperand(1);
2816 DebugLoc dl = Op.getDebugLoc();
2817 // Store the value to a temporary stack slot, then LOAD the returned part.
2818 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
2819 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
2820
2821 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00002822 unsigned EltSize =
2823 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002824 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
2825 DAG.getConstant(EltSize, Idx.getValueType()));
2826
2827 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
2828 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
2829 else
2830 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
2831
2832 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
2833
2834 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
2835}
2836
Nate Begeman750ac1b2006-02-01 07:19:44 +00002837/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2838/// with condition CC on the current target. This usually involves legalizing
2839/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2840/// there may be no choice but to create a new SetCC node to represent the
2841/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00002842/// LHS, and the SDValue returned in RHS has a nil SDNode value.
2843void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
2844 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002845 SDValue &CC,
2846 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00002847 LHS = LegalizeOp(LHS);
2848 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002849}
2850
Evan Cheng7f042682008-10-15 02:05:31 +00002851/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
2852/// condition code CC on the current target. This routine assumes LHS and rHS
2853/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
2854/// illegal condition code into AND / OR of multiple SETCC values.
2855void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
2856 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002857 SDValue &CC,
2858 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00002859 MVT OpVT = LHS.getValueType();
2860 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2861 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
2862 default: assert(0 && "Unknown condition code action!");
2863 case TargetLowering::Legal:
2864 // Nothing to do.
2865 break;
2866 case TargetLowering::Expand: {
2867 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
2868 unsigned Opc = 0;
2869 switch (CCCode) {
2870 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00002871 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
2872 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2873 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2874 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2875 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2876 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2877 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2878 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2879 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2880 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2881 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2882 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00002883 // FIXME: Implement more expansions.
2884 }
2885
Dale Johannesenbb5da912009-02-02 20:41:04 +00002886 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
2887 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
2888 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00002889 RHS = SDValue();
2890 CC = SDValue();
2891 break;
2892 }
2893 }
2894}
2895
Chris Lattner1401d152008-01-16 07:45:30 +00002896/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2897/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2898/// a load from the stack slot to DestVT, extending it if needed.
2899/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002900SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2901 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002902 MVT DestVT,
2903 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002904 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002905 unsigned SrcAlign =
2906 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2907 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002909
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002910 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002911 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002912 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2913
Duncan Sands83ec4b62008-06-06 12:08:01 +00002914 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2915 unsigned SlotSize = SlotVT.getSizeInBits();
2916 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002917 unsigned DestAlign =
2918 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002919
Chris Lattner1401d152008-01-16 07:45:30 +00002920 // Emit a store to the stack slot. Use a truncstore if the input value is
2921 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002923
Chris Lattner1401d152008-01-16 07:45:30 +00002924 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002925 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002926 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002927 else {
2928 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002929 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002930 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002931 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002932
Chris Lattner35481892005-12-23 00:16:34 +00002933 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002934 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002935 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002936
Chris Lattner1401d152008-01-16 07:45:30 +00002937 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002938 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002939 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002940}
2941
Dan Gohman475871a2008-07-27 21:46:04 +00002942SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002943 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002944 // Create a vector sized/aligned stack slot, store the value to element #0,
2945 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002946 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002947
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002948 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002949 int SPFI = StackPtrFI->getIndex();
2950
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002951 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2952 StackPtr,
2953 PseudoSourceValue::getFixedStack(SPFI), 0,
2954 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002955 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002956 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002957}
2958
2959
Chris Lattnerce872152006-03-19 06:31:19 +00002960/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002961/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002962SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002963 unsigned NumElems = Node->getNumOperands();
2964 SDValue SplatValue = Node->getOperand(0);
2965 DebugLoc dl = Node->getDebugLoc();
2966 MVT VT = Node->getValueType(0);
2967 MVT OpVT = SplatValue.getValueType();
2968 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002969
2970 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002971 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002972 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002973
Dan Gohman475871a2008-07-27 21:46:04 +00002974 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002975 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002977 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002978 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002979 bool isConstant = true;
2980 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2981 SplatValue.getOpcode() != ISD::UNDEF)
2982 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002983
Evan Cheng033e6812006-03-24 01:17:21 +00002984 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002985 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002986 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002987 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002988 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002989 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002990 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002991
2992 // If this isn't a constant element or an undef, we can't use a constant
2993 // pool load.
2994 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2995 V.getOpcode() != ISD::UNDEF)
2996 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002998
Chris Lattnerce872152006-03-19 06:31:19 +00002999 if (isOnlyLowElement) {
3000 // If the low element is an undef too, then this whole things is an undef.
3001 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003002 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00003003 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003004 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00003005 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003006
Chris Lattner2eb86532006-03-24 07:29:17 +00003007 // If all elements are constants, create a load from the constant pool.
3008 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00003009 std::vector<Constant*> CV;
3010 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003011 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00003012 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00003013 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00003014 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003015 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00003016 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003017 } else {
3018 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003019 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00003020 CV.push_back(UndefValue::get(OpNTy));
3021 }
3022 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00003023 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00003024 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00003025 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00003026 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00003027 PseudoSourceValue::getConstantPool(), 0,
3028 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00003029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003030
Gabor Greifba36cb52008-08-28 21:40:38 +00003031 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00003032 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00003034
3035 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00003037 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00003038 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003039 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00003040
Chris Lattner87100e02006-03-20 01:52:29 +00003041 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
3043 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00003044 }
3045 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003046
Evan Cheng033e6812006-03-24 01:17:21 +00003047 // If there are only two unique elements, we may be able to turn this into a
3048 // vector shuffle.
3049 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003050 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00003051 SDValue Val1 = Node->getOperand(1);
3052 SDValue Val2;
3053 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003054 if (MI->first != Val1)
3055 Val2 = MI->first;
3056 else
3057 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00003058
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003059 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003060 // vector shuffle has the undef vector on the RHS.
3061 if (Val1.getOpcode() == ISD::UNDEF)
3062 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00003063
Evan Cheng033e6812006-03-24 01:17:21 +00003064 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003066
3067 // Set elements of the shuffle mask for Val1.
3068 std::vector<unsigned> &Val1Elts = Values[Val1];
3069 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003070 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003071
3072 // Set elements of the shuffle mask for Val2.
3073 std::vector<unsigned> &Val2Elts = Values[Val2];
3074 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
3075 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00003076 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00003077
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003078 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003079 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003081 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
3082 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00003084 }
3085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003086
Chris Lattnerce872152006-03-19 06:31:19 +00003087 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3088 // aligned object on the stack, store each element into it, then load
3089 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00003090 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00003091 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00003092 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
3093 const Value *SV = PseudoSourceValue::getFixedStack(FI);
3094
Chris Lattnerce872152006-03-19 06:31:19 +00003095 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003097 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00003098 // Store (in the right endianness) the elements to memory.
3099 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3100 // Ignore undef elements.
3101 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00003102
Chris Lattner841c8822006-03-22 01:46:54 +00003103 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00003104
Dan Gohman475871a2008-07-27 21:46:04 +00003105 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00003106 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003107
Dale Johannesen8a782a22009-02-02 22:12:50 +00003108 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
3109 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00003110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003111
Dan Gohman475871a2008-07-27 21:46:04 +00003112 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00003113 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00003114 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003115 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00003116 else
3117 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003118
Chris Lattnerce872152006-03-19 06:31:19 +00003119 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00003120 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00003121}
3122
Chris Lattner77e77a62005-01-21 06:05:23 +00003123// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3124// does not fit into a register, return the lo part and set the hi part to the
3125// by-reg argument. If it does fit into a single register, return the result
3126// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00003127SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
3128 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003129 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003130 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00003131 // Legalizing the call will automatically add the previous call to the
3132 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00003133 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003134
Chris Lattner77e77a62005-01-21 06:05:23 +00003135 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00003136 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00003137 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003138 MVT ArgVT = Node->getOperand(i).getValueType();
3139 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00003140 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00003141 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00003142 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00003143 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00003144 }
Bill Wendling056292f2008-09-16 21:48:12 +00003145 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00003146 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003147
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003148 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003150 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00003151 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003152 CallingConv::C, false, Callee, Args, DAG,
3153 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003154
Chris Lattner6831a812006-02-13 09:18:02 +00003155 // Legalize the call sequence, starting with the chain. This will advance
3156 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3157 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3158 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00003159 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003160}
3161
Chris Lattner22cde6a2006-01-28 08:25:58 +00003162/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3163/// INT_TO_FP operation of the specified operand when the target requests that
3164/// we expand it. At this point, we know that the result and operand types are
3165/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00003166SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3167 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00003168 MVT DestVT,
3169 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003170 if (Op0.getValueType() == MVT::i32) {
3171 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00003172
Chris Lattner23594d42008-01-16 07:03:22 +00003173 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00003174 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00003175
Chris Lattner22cde6a2006-01-28 08:25:58 +00003176 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00003178 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00003179 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00003180 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003181 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00003182 if (TLI.isLittleEndian())
3183 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00003184
Chris Lattner22cde6a2006-01-28 08:25:58 +00003185 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00003186 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003187 if (isSigned) {
3188 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00003189 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00003190 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003191 } else {
3192 Op0Mapped = Op0;
3193 }
3194 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00003195 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003196 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003197 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00003198 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003199 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00003200 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003201 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00003202 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003203 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003205 BitsToDouble(0x4330000080000000ULL) :
3206 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00003207 MVT::f64);
3208 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00003209 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003210 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00003211 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003212 // handle final rounding
3213 if (DestVT == MVT::f64) {
3214 // do nothing
3215 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003216 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00003217 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00003218 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003219 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00003220 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003221 }
3222 return Result;
3223 }
3224 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00003225 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003226
Dale Johannesenaf435272009-02-02 19:03:57 +00003227 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003228 Op0, DAG.getConstant(0, Op0.getValueType()),
3229 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00003230 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00003231 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00003232 SignSet, Four, Zero);
3233
3234 // If the sign bit of the integer is set, the large number will be treated
3235 // as a negative number. To counteract this, the dynamic code adds an
3236 // offset depending on the data type.
3237 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003238 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003239 default: assert(0 && "Unsupported integer type!");
3240 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3241 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3242 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3243 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3244 }
3245 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00003246 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003247
Dan Gohman475871a2008-07-27 21:46:04 +00003248 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00003249 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00003250 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00003251 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00003252 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003253 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00003254 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00003255 PseudoSourceValue::getConstantPool(), 0,
3256 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003257 else {
Dan Gohman69de1932008-02-06 22:27:42 +00003258 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00003259 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00003260 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00003261 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00003262 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003263 }
3264
Dale Johannesenaf435272009-02-02 19:03:57 +00003265 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003266}
3267
3268/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3269/// *INT_TO_FP operation of the specified operand when the target requests that
3270/// we promote it. At this point, we know that the result and operand types are
3271/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3272/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00003273SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
3274 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003275 bool isSigned,
3276 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003277 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003278 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003279
3280 unsigned OpToUse = 0;
3281
3282 // Scan for the appropriate larger type to use.
3283 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003284 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
3285 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003286
3287 // If the target supports SINT_TO_FP of this type, use it.
3288 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3289 default: break;
3290 case TargetLowering::Legal:
3291 if (!TLI.isTypeLegal(NewInTy))
3292 break; // Can't use this datatype.
3293 // FALL THROUGH.
3294 case TargetLowering::Custom:
3295 OpToUse = ISD::SINT_TO_FP;
3296 break;
3297 }
3298 if (OpToUse) break;
3299 if (isSigned) continue;
3300
3301 // If the target supports UINT_TO_FP of this type, use it.
3302 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3303 default: break;
3304 case TargetLowering::Legal:
3305 if (!TLI.isTypeLegal(NewInTy))
3306 break; // Can't use this datatype.
3307 // FALL THROUGH.
3308 case TargetLowering::Custom:
3309 OpToUse = ISD::UINT_TO_FP;
3310 break;
3311 }
3312 if (OpToUse) break;
3313
3314 // Otherwise, try a larger type.
3315 }
3316
3317 // Okay, we found the operation and type to use. Zero extend our input to the
3318 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00003319 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00003320 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00003321 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003322}
3323
3324/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3325/// FP_TO_*INT operation of the specified operand when the target requests that
3326/// we promote it. At this point, we know that the result and operand types are
3327/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3328/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00003329SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
3330 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003331 bool isSigned,
3332 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003333 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003334 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003335
3336 unsigned OpToUse = 0;
3337
3338 // Scan for the appropriate larger type to use.
3339 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003340 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
3341 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003342
3343 // If the target supports FP_TO_SINT returning this type, use it.
3344 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3345 default: break;
3346 case TargetLowering::Legal:
3347 if (!TLI.isTypeLegal(NewOutTy))
3348 break; // Can't use this datatype.
3349 // FALL THROUGH.
3350 case TargetLowering::Custom:
3351 OpToUse = ISD::FP_TO_SINT;
3352 break;
3353 }
3354 if (OpToUse) break;
3355
3356 // If the target supports FP_TO_UINT of this type, use it.
3357 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3358 default: break;
3359 case TargetLowering::Legal:
3360 if (!TLI.isTypeLegal(NewOutTy))
3361 break; // Can't use this datatype.
3362 // FALL THROUGH.
3363 case TargetLowering::Custom:
3364 OpToUse = ISD::FP_TO_UINT;
3365 break;
3366 }
3367 if (OpToUse) break;
3368
3369 // Otherwise, try a larger type.
3370 }
3371
Scott Michelfdc40a02009-02-17 22:15:04 +00003372
Chris Lattner27a6c732007-11-24 07:07:01 +00003373 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00003374 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00003375
Chris Lattner27a6c732007-11-24 07:07:01 +00003376 // If the operation produces an invalid type, it must be custom lowered. Use
3377 // the target lowering hooks to expand it. Just keep the low part of the
3378 // expanded operation, we know that we're truncating anyway.
3379 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00003380 SmallVector<SDValue, 2> Results;
3381 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
3382 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
3383 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00003384 }
Duncan Sands126d9072008-07-04 11:47:58 +00003385
Chris Lattner27a6c732007-11-24 07:07:01 +00003386 // Truncate the result of the extended FP_TO_*INT operation to the desired
3387 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00003388 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003389}
3390
3391/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3392///
Dale Johannesen8a782a22009-02-02 22:12:50 +00003393SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003394 MVT VT = Op.getValueType();
3395 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003396 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003397 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003398 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3399 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003400 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3401 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3402 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003403 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003404 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3405 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3406 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3407 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3408 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3409 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3410 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3411 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3412 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003413 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003414 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
3415 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
3416 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3417 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3418 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3419 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3420 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
3421 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
3422 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3423 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3424 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3425 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3426 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3427 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3428 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
3429 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
3430 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3431 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3432 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
3433 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
3434 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003435 }
3436}
3437
3438/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3439///
Scott Michelfdc40a02009-02-17 22:15:04 +00003440SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003441 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003442 switch (Opc) {
3443 default: assert(0 && "Cannot expand this yet!");
3444 case ISD::CTPOP: {
3445 static const uint64_t mask[6] = {
3446 0x5555555555555555ULL, 0x3333333333333333ULL,
3447 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3448 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3449 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00003450 MVT VT = Op.getValueType();
3451 MVT ShVT = TLI.getShiftAmountTy();
3452 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003453 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3454 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003455 unsigned EltSize = VT.isVector() ?
3456 VT.getVectorElementType().getSizeInBits() : len;
3457 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003458 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003459 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00003460 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003461 DAG.getNode(ISD::AND, dl, VT,
3462 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
3463 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003464 }
3465 return Op;
3466 }
3467 case ISD::CTLZ: {
3468 // for now, we do this:
3469 // x = x | (x >> 1);
3470 // x = x | (x >> 2);
3471 // ...
3472 // x = x | (x >>16);
3473 // x = x | (x >>32); // for 64-bit input
3474 // return popcount(~x);
3475 //
3476 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003477 MVT VT = Op.getValueType();
3478 MVT ShVT = TLI.getShiftAmountTy();
3479 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003480 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003481 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003482 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00003483 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003484 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00003485 Op = DAG.getNOT(dl, Op, VT);
3486 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003487 }
3488 case ISD::CTTZ: {
3489 // for now, we use: { return popcount(~x & (x - 1)); }
3490 // unless the target has ctlz but not ctpop, in which case we use:
3491 // { return 32 - nlz(~x & (x-1)); }
3492 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003493 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00003494 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
3495 DAG.getNOT(dl, Op, VT),
3496 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00003497 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003498 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003499 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
3500 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00003501 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003502 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003503 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
3504 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003505 }
3506 }
3507}
Chris Lattnere34b3962005-01-19 04:19:40 +00003508
Eli Friedman8c377c72009-05-27 01:25:56 +00003509void SelectionDAGLegalize::ExpandNode(SDNode *Node,
3510 SmallVectorImpl<SDValue> &Results) {
3511 DebugLoc dl = Node->getDebugLoc();
3512 SDValue Tmp1, Tmp2;
3513 switch (Node->getOpcode()) {
3514 case ISD::CTPOP:
3515 case ISD::CTLZ:
3516 case ISD::CTTZ:
3517 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
3518 Results.push_back(Tmp1);
3519 break;
3520 case ISD::BSWAP:
3521 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
3522 break;
3523 case ISD::FRAMEADDR:
3524 case ISD::RETURNADDR:
3525 case ISD::FRAME_TO_ARGS_OFFSET:
3526 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
3527 break;
3528 case ISD::FLT_ROUNDS_:
3529 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
3530 break;
3531 case ISD::EH_RETURN:
3532 case ISD::DECLARE:
3533 case ISD::DBG_LABEL:
3534 case ISD::EH_LABEL:
3535 case ISD::PREFETCH:
3536 case ISD::MEMBARRIER:
3537 case ISD::VAEND:
3538 Results.push_back(Node->getOperand(0));
3539 break;
3540 case ISD::MERGE_VALUES:
3541 for (unsigned i = 0; i < Node->getNumValues(); i++)
3542 Results.push_back(Node->getOperand(i));
3543 break;
3544 case ISD::UNDEF: {
3545 MVT VT = Node->getValueType(0);
3546 if (VT.isInteger())
3547 Results.push_back(DAG.getConstant(0, VT));
3548 else if (VT.isFloatingPoint())
3549 Results.push_back(DAG.getConstantFP(0, VT));
3550 else
3551 assert(0 && "Unknown value type!");
3552 break;
3553 }
3554 case ISD::TRAP: {
3555 // If this operation is not supported, lower it to 'abort()' call
3556 TargetLowering::ArgListTy Args;
3557 std::pair<SDValue, SDValue> CallResult =
3558 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
3559 false, false, false, false, CallingConv::C, false,
3560 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3561 Args, DAG, dl);
3562 Results.push_back(CallResult.second);
3563 break;
3564 }
3565 case ISD::FP_ROUND:
3566 case ISD::BIT_CONVERT:
3567 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3568 Node->getValueType(0), dl);
3569 Results.push_back(Tmp1);
3570 break;
3571 case ISD::FP_EXTEND:
3572 Tmp1 = EmitStackConvert(Node->getOperand(0),
3573 Node->getOperand(0).getValueType(),
3574 Node->getValueType(0), dl);
3575 Results.push_back(Tmp1);
3576 break;
3577 case ISD::SIGN_EXTEND_INREG: {
3578 // NOTE: we could fall back on load/store here too for targets without
3579 // SAR. However, it is doubtful that any exist.
3580 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3581 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3582 ExtraVT.getSizeInBits();
3583 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3584 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3585 Node->getOperand(0), ShiftCst);
3586 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3587 Results.push_back(Tmp1);
3588 break;
3589 }
3590 case ISD::FP_ROUND_INREG: {
3591 // The only way we can lower this is to turn it into a TRUNCSTORE,
3592 // EXTLOAD pair, targetting a temporary location (a stack slot).
3593
3594 // NOTE: there is a choice here between constantly creating new stack
3595 // slots and always reusing the same one. We currently always create
3596 // new ones, as reuse may inhibit scheduling.
3597 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3598 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3599 Node->getValueType(0), dl);
3600 Results.push_back(Tmp1);
3601 break;
3602 }
3603 case ISD::SINT_TO_FP:
3604 case ISD::UINT_TO_FP:
3605 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3606 Node->getOperand(0), Node->getValueType(0), dl);
3607 Results.push_back(Tmp1);
3608 break;
3609 case ISD::FP_TO_UINT: {
3610 SDValue True, False;
3611 MVT VT = Node->getOperand(0).getValueType();
3612 MVT NVT = Node->getValueType(0);
3613 const uint64_t zero[] = {0, 0};
3614 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3615 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3616 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3617 Tmp1 = DAG.getConstantFP(apf, VT);
3618 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3619 Node->getOperand(0),
3620 Tmp1, ISD::SETLT);
3621 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3622 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3623 DAG.getNode(ISD::FSUB, dl, VT,
3624 Node->getOperand(0), Tmp1));
3625 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3626 DAG.getConstant(x, NVT));
3627 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3628 Results.push_back(Tmp1);
3629 break;
3630 }
3631 case ISD::VACOPY: {
3632 // This defaults to loading a pointer from the input and storing it to the
3633 // output, returning the chain.
3634 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3635 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3636 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
3637 Node->getOperand(2), VS, 0);
3638 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
3639 Results.push_back(Tmp1);
3640 break;
3641 }
3642 case ISD::EXTRACT_VECTOR_ELT:
3643 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3644 // This must be an access of the only element. Return it.
3645 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
3646 Node->getOperand(0));
3647 else
3648 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3649 Results.push_back(Tmp1);
3650 break;
3651 case ISD::EXTRACT_SUBVECTOR:
3652 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3653 break;
3654 case ISD::SCALAR_TO_VECTOR:
3655 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3656 break;
3657 case ISD::EXTRACT_ELEMENT: {
3658 MVT OpTy = Node->getOperand(0).getValueType();
3659 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3660 // 1 -> Hi
3661 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3662 DAG.getConstant(OpTy.getSizeInBits()/2,
3663 TLI.getShiftAmountTy()));
3664 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3665 } else {
3666 // 0 -> Lo
3667 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3668 Node->getOperand(0));
3669 }
3670 Results.push_back(Tmp1);
3671 break;
3672 }
3673 }
3674}
3675void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3676 SmallVectorImpl<SDValue> &Results) {
3677 MVT OVT = Node->getValueType(0);
3678 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3679 Node->getOpcode() == ISD::SINT_TO_FP) {
3680 OVT = Node->getOperand(0).getValueType();
3681 }
3682 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3683 DebugLoc dl = Node->getDebugLoc();
3684 SDValue Tmp1, Tmp2;
3685 switch (Node->getOpcode()) {
3686 case ISD::CTTZ:
3687 case ISD::CTLZ:
3688 case ISD::CTPOP:
3689 // Zero extend the argument.
3690 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3691 // Perform the larger operation.
3692 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3693 if (Node->getOpcode() == ISD::CTTZ) {
3694 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3695 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3696 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3697 ISD::SETEQ);
3698 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3699 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3700 } else if (Node->getOpcode() == ISD::CTLZ) {
3701 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3702 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3703 DAG.getConstant(NVT.getSizeInBits() -
3704 OVT.getSizeInBits(), NVT));
3705 }
3706 Results.push_back(Tmp1);
3707 break;
3708 case ISD::BSWAP: {
3709 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3710 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3711 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3712 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3713 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3714 Results.push_back(Tmp1);
3715 break;
3716 }
3717 case ISD::FP_TO_UINT:
3718 case ISD::FP_TO_SINT:
3719 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3720 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3721 Results.push_back(Tmp1);
3722 break;
3723 case ISD::UINT_TO_FP:
3724 case ISD::SINT_TO_FP:
3725 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3726 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3727 Results.push_back(Tmp1);
3728 break;
3729 }
3730}
3731
Chris Lattner3e928bb2005-01-07 07:47:09 +00003732// SelectionDAG::Legalize - This is the entry point for the file.
3733//
Bill Wendling98a366d2009-04-29 23:29:43 +00003734void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3735 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003736 /// run - This is the main entry point to this class.
3737 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003738 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003739}
3740