blob: 44bf18bef923a1274d061009380b0c12800b8ac9 [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);
Scott Michelfdc40a02009-02-17 22:15:04 +0000160 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy,
Dale Johannesenaf435272009-02-02 19:03:57 +0000161 SDValue Op, DebugLoc dl);
162 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT,
163 DebugLoc dl);
164 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned,
165 DebugLoc dl);
166 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned,
167 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000168
Dale Johannesen8a782a22009-02-02 22:12:50 +0000169 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
170 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000171
Dan Gohman475871a2008-07-27 21:46:04 +0000172 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000173 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
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
Chris Lattner3e928bb2005-01-07 07:47:09 +0000715 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000716 case ISD::FrameIndex:
717 case ISD::EntryToken:
718 case ISD::Register:
719 case ISD::BasicBlock:
720 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000721 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000722 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000723 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000724 case ISD::TargetConstantPool:
725 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000726 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000727 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +0000728 case ISD::VALUETYPE:
729 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000730 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000731 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000732 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000733 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000734 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000735 "This must be legal!");
736 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000737 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000738 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
739 // If this is a target node, legalize it by legalizing the operands then
740 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +0000741 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000742 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000743 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000744
Jay Foade3e51c02009-05-21 09:52:38 +0000745 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(), Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000746
747 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
748 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000749 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000750 }
751 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000752#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000753 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000754#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000755 assert(0 && "Do not know how to legalize this operator!");
756 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000757 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000758 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000759 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000760 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000761 case ISD::ConstantPool:
762 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000763 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
764 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000765 case TargetLowering::Custom:
766 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000767 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +0000768 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000769 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000770 break;
771 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000772 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000773 case ISD::FRAMEADDR:
774 case ISD::RETURNADDR:
775 // The only option for these nodes is to custom lower them. If the target
776 // does not custom lower them, then return zero.
777 Tmp1 = TLI.LowerOperation(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +0000778 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +0000779 Result = Tmp1;
780 else
781 Result = DAG.getConstant(0, TLI.getPointerTy());
782 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000783 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000784 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000785 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
786 default: assert(0 && "This action is not supported yet!");
787 case TargetLowering::Custom:
788 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000789 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000790 // Fall Thru
791 case TargetLowering::Legal:
792 Result = DAG.getConstant(0, VT);
793 break;
794 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000795 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000796 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000797 case ISD::EXCEPTIONADDR: {
798 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000799 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000800 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
801 default: assert(0 && "This action is not supported yet!");
802 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000803 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000804 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000805 }
806 break;
807 case TargetLowering::Custom:
808 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000809 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000810 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000811 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000812 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000813 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000814 break;
815 }
816 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000817 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000818 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000819
Gabor Greifba36cb52008-08-28 21:40:38 +0000820 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000821 "Cannot return more than two values!");
822
823 // Since we produced two values, make sure to remember that we
824 // legalized both of them.
825 Tmp1 = LegalizeOp(Result);
826 Tmp2 = LegalizeOp(Result.getValue(1));
827 AddLegalizedOperand(Op.getValue(0), Tmp1);
828 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000829 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000830 case ISD::EHSELECTION: {
831 Tmp1 = LegalizeOp(Node->getOperand(0));
832 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000833 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +0000834 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
835 default: assert(0 && "This action is not supported yet!");
836 case TargetLowering::Expand: {
837 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000838 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000839 }
840 break;
841 case TargetLowering::Custom:
842 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000843 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +0000844 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000845 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000847 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +0000848 break;
849 }
850 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000851 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000852 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000853
Gabor Greifba36cb52008-08-28 21:40:38 +0000854 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000855 "Cannot return more than two values!");
856
857 // Since we produced two values, make sure to remember that we
858 // legalized both of them.
859 Tmp1 = LegalizeOp(Result);
860 Tmp2 = LegalizeOp(Result.getValue(1));
861 AddLegalizedOperand(Op.getValue(0), Tmp1);
862 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000863 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000864 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000865 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000866 // The only "good" option for this node is to custom lower it.
867 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
868 default: assert(0 && "This action is not supported at all!");
869 case TargetLowering::Custom:
870 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000871 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000872 // Fall Thru
873 case TargetLowering::Legal:
874 // Target does not know, how to lower this, lower to noop
875 Result = LegalizeOp(Node->getOperand(0));
876 break;
877 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000878 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000879 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000880 case ISD::AssertSext:
881 case ISD::AssertZext:
882 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000884 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000885 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000886 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000887 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +0000888 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000889 case ISD::CopyFromReg:
890 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000891 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000892 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000893 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000894 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000895 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000896 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000897 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
899 } else {
900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
901 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000902 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
903 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000904 // Since CopyFromReg produces two values, make sure to remember that we
905 // legalized both of them.
906 AddLegalizedOperand(Op.getValue(0), Result);
907 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000908 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000909 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000910 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000911 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000912 default: assert(0 && "This action is not supported yet!");
913 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000914 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000915 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916 else if (VT.isFloatingPoint())
917 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +0000918 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000919 else
920 assert(0 && "Unknown value type!");
921 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000922 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000923 break;
924 }
925 break;
926 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000927
Chris Lattner48b61a72006-03-28 00:40:33 +0000928 case ISD::INTRINSIC_W_CHAIN:
929 case ISD::INTRINSIC_WO_CHAIN:
930 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +0000931 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000932 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
933 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000934 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +0000935
Chris Lattner10d7fa62006-03-26 09:12:51 +0000936 // Allow the target to custom lower its intrinsics if it wants to.
Scott Michelfdc40a02009-02-17 22:15:04 +0000937 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000938 TargetLowering::Custom) {
939 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000940 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000941 }
942
Gabor Greifba36cb52008-08-28 21:40:38 +0000943 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000944
945 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +0000946 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +0000947 "Cannot return more than two values!");
948
Scott Michelfdc40a02009-02-17 22:15:04 +0000949 // Since loads produce two values, make sure to remember that we
Chris Lattner13fc2f12006-03-27 20:28:29 +0000950 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +0000951 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
952 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000953 return Result.getValue(Op.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +0000954 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000955
Dan Gohman7f460202008-06-30 20:59:49 +0000956 case ISD::DBG_STOPPOINT:
957 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000959
Dan Gohman7f460202008-06-30 20:59:49 +0000960 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000961 case TargetLowering::Promote:
962 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000963 case TargetLowering::Expand: {
964 DwarfWriter *DW = DAG.getDwarfWriter();
965 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
966 MVT::Other);
967 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000968
Bill Wendling92c1e122009-02-13 02:16:35 +0000969 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
970 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
971 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
972 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000973
Bill Wendling92c1e122009-02-13 02:16:35 +0000974 unsigned Line = DSP->getLine();
975 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000976
Bill Wendling98a366d2009-04-29 23:29:43 +0000977 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000978 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
979 // won't hurt anything.
980 if (useDEBUG_LOC) {
981 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +0000982 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000983 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +0000984 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
985 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000986 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +0000987 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
988 }
Bill Wendling92c1e122009-02-13 02:16:35 +0000989 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000990 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +0000991 }
992 } else {
993 Result = Tmp1; // chain
994 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000995 break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000996 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000997 case TargetLowering::Custom:
998 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000999 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001000 break;
Evan Cheng71e86852008-07-08 20:06:39 +00001001 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +00001002 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001003 break;
1004
Dan Gohman475871a2008-07-27 21:46:04 +00001005 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001006 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +00001007 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1008 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +00001009 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1010 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1011 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001012 break;
1013 }
Evan Cheng71e86852008-07-08 20:06:39 +00001014 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001015 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001016
1017 case ISD::DECLARE:
1018 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1019 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1020 default: assert(0 && "This action is not supported yet!");
1021 case TargetLowering::Legal:
1022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1023 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1024 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1025 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1026 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001027 case TargetLowering::Expand:
1028 Result = LegalizeOp(Node->getOperand(0));
1029 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001031 break;
1032
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001033 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001034 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001035 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001036 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001037 case TargetLowering::Legal: {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Eli Friedman957bffa2009-05-24 08:42:01 +00001039 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001040 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00001041 Tmp2 = Node->getOperand(1);
1042 Tmp3 = Node->getOperand(2);
1043 Tmp4 = Node->getOperand(3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001045 break;
1046 }
Evan Cheng71e86852008-07-08 20:06:39 +00001047 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001048 break;
Jim Laskeyabf6d172006-01-05 01:25:28 +00001049
Dan Gohman44066042008-07-01 00:05:16 +00001050 case ISD::DBG_LABEL:
1051 case ISD::EH_LABEL:
1052 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1053 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001054 default: assert(0 && "This action is not supported yet!");
1055 case TargetLowering::Legal:
1056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001057 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001058 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001059 case TargetLowering::Expand:
1060 Result = LegalizeOp(Node->getOperand(0));
1061 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001062 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001063 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001064
Evan Cheng27b7db52008-03-08 00:58:38 +00001065 case ISD::PREFETCH:
1066 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1067 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1068 default: assert(0 && "This action is not supported yet!");
1069 case TargetLowering::Legal:
1070 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1071 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1072 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1073 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1074 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1075 break;
1076 case TargetLowering::Expand:
1077 // It's a noop.
1078 Result = LegalizeOp(Node->getOperand(0));
1079 break;
1080 }
1081 break;
1082
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001083 case ISD::MEMBARRIER: {
1084 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001085 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1086 default: assert(0 && "This action is not supported yet!");
1087 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001088 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001089 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001090 for (int x = 1; x < 6; ++x) {
1091 Ops[x] = Node->getOperand(x);
Duncan Sandse90a6152008-02-27 08:53:44 +00001092 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001093 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1094 break;
1095 }
1096 case TargetLowering::Expand:
1097 //There is no libgcc call for this op
1098 Result = Node->getOperand(0); // Noop
1099 break;
1100 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001101 break;
1102 }
1103
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001104 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001105 unsigned int num_operands = 4;
1106 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001107 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001108 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001109 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001110 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Scott Michelfdc40a02009-02-17 22:15:04 +00001111
Mon P Wang63307c32008-05-05 19:05:59 +00001112 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1113 default: assert(0 && "This action is not supported yet!");
1114 case TargetLowering::Custom:
1115 Result = TLI.LowerOperation(Result, DAG);
1116 break;
1117 case TargetLowering::Legal:
1118 break;
1119 }
Dan Gohman475871a2008-07-27 21:46:04 +00001120 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1121 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001122 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001123 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001124 case ISD::ATOMIC_LOAD_ADD:
1125 case ISD::ATOMIC_LOAD_SUB:
1126 case ISD::ATOMIC_LOAD_AND:
1127 case ISD::ATOMIC_LOAD_OR:
1128 case ISD::ATOMIC_LOAD_XOR:
1129 case ISD::ATOMIC_LOAD_NAND:
1130 case ISD::ATOMIC_LOAD_MIN:
1131 case ISD::ATOMIC_LOAD_MAX:
1132 case ISD::ATOMIC_LOAD_UMIN:
1133 case ISD::ATOMIC_LOAD_UMAX:
1134 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001135 unsigned int num_operands = 3;
1136 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001137 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001138 for (unsigned int x = 0; x < num_operands; ++x)
1139 Ops[x] = LegalizeOp(Node->getOperand(x));
1140 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001141
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001142 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001143 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001144 case TargetLowering::Custom:
1145 Result = TLI.LowerOperation(Result, DAG);
1146 break;
1147 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001148 break;
1149 }
Dan Gohman475871a2008-07-27 21:46:04 +00001150 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1151 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001152 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001153 }
Scott Michelc1513d22007-08-08 23:23:31 +00001154 case ISD::Constant: {
1155 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1156 unsigned opAction =
1157 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1158
Chris Lattner3e928bb2005-01-07 07:47:09 +00001159 // We know we don't need to expand constants here, constants only have one
1160 // value and we check that it is fine above.
1161
Scott Michelc1513d22007-08-08 23:23:31 +00001162 if (opAction == TargetLowering::Custom) {
1163 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001164 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001165 Result = Tmp1;
1166 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001167 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001168 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001169 case ISD::ConstantFP: {
1170 // Spill FP immediates to the constant pool if the target cannot directly
1171 // codegen them. Targets often have some immediate values that can be
1172 // efficiently generated into an FP register without a load. We explicitly
1173 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001174 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1175
Chris Lattner3181a772006-01-29 06:26:56 +00001176 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1177 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001178 case TargetLowering::Legal:
1179 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001180 case TargetLowering::Custom:
1181 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001182 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001183 Result = Tmp3;
1184 break;
1185 }
1186 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001187 case TargetLowering::Expand: {
1188 // Check to see if this FP immediate is already legal.
1189 bool isLegal = false;
1190 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1191 E = TLI.legal_fpimm_end(); I != E; ++I) {
1192 if (CFP->isExactlyValue(*I)) {
1193 isLegal = true;
1194 break;
1195 }
1196 }
1197 // If this is a legal constant, turn it into a TargetConstantFP node.
1198 if (isLegal)
1199 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001200 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001201 }
Nate Begemane1795842008-02-14 08:57:00 +00001202 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001203 break;
1204 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001205 case ISD::TokenFactor:
1206 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001207 Tmp1 = LegalizeOp(Node->getOperand(0));
1208 Tmp2 = LegalizeOp(Node->getOperand(1));
1209 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1210 } else if (Node->getNumOperands() == 3) {
1211 Tmp1 = LegalizeOp(Node->getOperand(0));
1212 Tmp2 = LegalizeOp(Node->getOperand(1));
1213 Tmp3 = LegalizeOp(Node->getOperand(2));
1214 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001215 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001216 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001217 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001218 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1219 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001220 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001221 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001222 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001223
Chris Lattnerfdfded52006-04-12 16:20:43 +00001224 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001225 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001226 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001227 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001228 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001229 // A call within a calling sequence must be legalized to something
1230 // other than the normal CALLSEQ_END. Violating this gets Legalize
1231 // into an infinite loop.
1232 assert ((!IsLegalizingCall ||
1233 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001234 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001235 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001236
1237 // The number of incoming and outgoing values should match; unless the final
1238 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001239 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1240 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1241 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001242 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001243 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001244
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001245 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001246 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001247 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1248 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001249 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001250 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001251 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001252 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001253 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001254 }
1255 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001256 case ISD::BUILD_VECTOR:
1257 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001258 default: assert(0 && "This action is not supported yet!");
1259 case TargetLowering::Custom:
1260 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001262 Result = Tmp3;
1263 break;
1264 }
1265 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001266 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001267 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001268 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001269 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001270 break;
1271 case ISD::INSERT_VECTOR_ELT:
1272 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001273 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001274
1275 // The type of the value to insert may not be legal, even though the vector
1276 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1277 // here.
Eli Friedman957bffa2009-05-24 08:42:01 +00001278 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2332b9f2006-03-19 01:17:20 +00001279 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Chris Lattner2332b9f2006-03-19 01:17:20 +00001281 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1282 Node->getValueType(0))) {
1283 default: assert(0 && "This action is not supported yet!");
1284 case TargetLowering::Legal:
1285 break;
1286 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001287 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001289 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001290 break;
1291 }
1292 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001293 case TargetLowering::Promote:
1294 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001295 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001296 // If the insert index is a constant, codegen this as a scalar_to_vector,
1297 // then a shuffle that inserts it into the right position in the vector.
1298 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001299 // SCALAR_TO_VECTOR requires that the type of the value being inserted
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001300 // match the element type of the vector being created, except for
1301 // integers in which case the inserted value can be over width.
1302 MVT EltVT = Op.getValueType().getVectorElementType();
1303 if (Tmp2.getValueType() == EltVT ||
1304 (EltVT.isInteger() && Tmp2.getValueType().bitsGE(EltVT))) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001305 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001306 Tmp1.getValueType(), Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001307
Duncan Sands83ec4b62008-06-06 12:08:01 +00001308 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
Nate Begeman0325d902008-02-13 06:43:04 +00001309 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1310 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1311 // elt 0 of the RHS.
Nate Begeman9008ca62009-04-27 18:41:29 +00001312 SmallVector<int, 8> ShufOps;
1313 for (unsigned i = 0; i != NumElts; ++i)
1314 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
1315
1316 Result = DAG.getVectorShuffle(Tmp1.getValueType(), dl, Tmp1, ScVec,
1317 &ShufOps[0]);
Nate Begeman0325d902008-02-13 06:43:04 +00001318 Result = LegalizeOp(Result);
1319 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001320 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001321 }
Dale Johannesenbb5da912009-02-02 20:41:04 +00001322 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3, dl);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001323 break;
1324 }
1325 }
1326 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001327 case ISD::SCALAR_TO_VECTOR:
1328 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1329 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1330 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1331 Node->getValueType(0))) {
1332 default: assert(0 && "This action is not supported yet!");
1333 case TargetLowering::Legal:
1334 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001335 case TargetLowering::Custom:
1336 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001337 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001338 Result = Tmp3;
1339 break;
1340 }
1341 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001342 case TargetLowering::Expand:
1343 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001344 break;
1345 }
Chris Lattnerce872152006-03-19 06:31:19 +00001346 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001347 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1349 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1351 MVT VT = Result.getValueType();
1352
Nate Begeman5a5ca152009-04-29 05:20:52 +00001353 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001354 SmallVector<int, 8> Mask;
1355 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001356
1357 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001358 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001359 default: assert(0 && "Unknown operation action!");
1360 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001361 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001362 "vector shuffle should not be created if not legal!");
1363 break;
1364 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001365 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001366 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001367 Result = Tmp3;
1368 break;
1369 }
1370 // FALLTHROUGH
1371 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001372 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001373 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001374 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001375 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001376 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001377 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001378 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001379 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001380 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001381 if (Idx < NumElems)
1382 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1383 DAG.getIntPtrConstant(Idx)));
1384 else
1385 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1386 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001387 }
Evan Chenga87008d2009-02-25 22:49:59 +00001388 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001389 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001390 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001391 case TargetLowering::Promote: {
1392 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001393 MVT OVT = Node->getValueType(0);
1394 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001395
1396 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001397 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1398 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001399
Chris Lattner4352cc92006-04-04 17:23:26 +00001400 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001401 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001402 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001403 break;
1404 }
Chris Lattner87100e02006-03-20 01:52:29 +00001405 }
1406 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001407 }
Chris Lattner384504c2006-03-21 20:44:12 +00001408 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001409 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001410 Tmp2 = LegalizeOp(Node->getOperand(1));
1411 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001412 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001413 break;
1414
Scott Michelfdc40a02009-02-17 22:15:04 +00001415 case ISD::EXTRACT_SUBVECTOR:
Eli Friedman1fde9c52009-05-24 02:46:31 +00001416 Tmp1 = LegalizeOp(Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00001417 Tmp2 = LegalizeOp(Node->getOperand(1));
1418 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Eli Friedman1fde9c52009-05-24 02:46:31 +00001419
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001420 switch (TLI.getOperationAction(ISD::EXTRACT_SUBVECTOR,
1421 Node->getValueType(0))) {
1422 default: assert(0 && "Unknown operation action!");
1423 case TargetLowering::Legal:
1424 break;
1425 case TargetLowering::Custom:
1426 Tmp3 = TLI.LowerOperation(Result, DAG);
1427 if (Tmp3.getNode()) {
1428 Result = Tmp3;
1429 break;
1430 }
1431 // FALLTHROUGH
1432 case TargetLowering::Expand: {
1433 Result = ExpandExtractFromVectorThroughStack(Result);
1434 break;
1435 }
1436 }
Dan Gohman65956352007-06-13 15:12:02 +00001437 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001438
Mon P Wang0c397192008-10-30 08:01:45 +00001439 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001440 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001441 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001442 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1443 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1444 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1445
1446 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1447 Node->getValueType(0))) {
1448 default: assert(0 && "Unknown operation action!");
1449 case TargetLowering::Legal:
1450 break;
1451 case TargetLowering::Custom:
1452 Tmp3 = TLI.LowerOperation(Result, DAG);
1453 if (Tmp3.getNode()) {
1454 Result = Tmp3;
1455 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001456 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001457 // FALLTHROUGH
1458 case TargetLowering::Expand: {
1459 // Use extract/insert/build vector for now. We might try to be
1460 // more clever later.
1461 MVT PtrVT = TLI.getPointerTy();
1462 SmallVector<SDValue, 8> Ops;
1463 unsigned NumOperands = Node->getNumOperands();
1464 for (unsigned i=0; i < NumOperands; ++i) {
1465 SDValue SubOp = Node->getOperand(i);
1466 MVT VVT = SubOp.getNode()->getValueType(0);
1467 MVT EltVT = VVT.getVectorElementType();
1468 unsigned NumSubElem = VVT.getVectorNumElements();
1469 for (unsigned j=0; j < NumSubElem; ++j) {
1470 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1471 DAG.getConstant(j, PtrVT)));
1472 }
1473 }
1474 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1475 Node->getValueType(0),
1476 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001477 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001478 }
1479 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001480 }
1481
Chris Lattner6831a812006-02-13 09:18:02 +00001482 case ISD::CALLSEQ_START: {
1483 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner6831a812006-02-13 09:18:02 +00001485 // Recursively Legalize all of the inputs of the call end that do not lead
1486 // to this call start. This ensures that any libcalls that need be inserted
1487 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001488 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001489 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001490 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001491 NodesLeadingTo);
1492 }
Chris Lattner6831a812006-02-13 09:18:02 +00001493
1494 // Now that we legalized all of the inputs (which may have inserted
1495 // libcalls) create the new CALLSEQ_START node.
1496 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1497
1498 // Merge in the last call, to ensure that this call start after the last
1499 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001500 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001501 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001502 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001503 Tmp1 = LegalizeOp(Tmp1);
1504 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001505
Chris Lattner6831a812006-02-13 09:18:02 +00001506 // Do not try to legalize the target-specific arguments (#1+).
1507 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001509 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001510 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001511 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001512
Chris Lattner6831a812006-02-13 09:18:02 +00001513 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001514 AddLegalizedOperand(Op.getValue(0), Result);
1515 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1516 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001517
Chris Lattner6831a812006-02-13 09:18:02 +00001518 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001519 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001520 // process, no libcalls can/will be inserted, guaranteeing that no calls
1521 // can overlap.
1522 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001523 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001524 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001525 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001526
Chris Lattner6831a812006-02-13 09:18:02 +00001527 // Legalize the call, starting from the CALLSEQ_END.
1528 LegalizeOp(LastCALLSEQ_END);
1529 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1530 return Result;
1531 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001532 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001533 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1534 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001535 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001536 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1537 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001538 assert(I != LegalizedNodes.end() &&
1539 "Legalizing the call start should have legalized this node!");
1540 return I->second;
1541 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001542
1543 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001544 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001545 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001546 // Do not try to legalize the target-specific arguments (#1+), except for
1547 // an optional flag input.
1548 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1549 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001550 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001551 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001552 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001553 }
1554 } else {
1555 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1556 if (Tmp1 != Node->getOperand(0) ||
1557 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001558 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001559 Ops[0] = Tmp1;
1560 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001561 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001562 }
Chris Lattner6a542892006-01-24 05:48:21 +00001563 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001564 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001565 // This finishes up call legalization.
1566 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001567
Chris Lattner4b653a02006-02-14 00:55:02 +00001568 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001569 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001570 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001571 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001572 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001573 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001574 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001575 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1576 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1577 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001578 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001579
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001580 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001581 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001582 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001583 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001584 case TargetLowering::Expand: {
1585 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1586 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1587 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001588 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001589
1590 // Chain the dynamic stack allocation so that it doesn't modify the stack
1591 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001592 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001593
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001595 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001596 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001597 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001598 unsigned StackAlign =
1599 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1600 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001601 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001602 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001603 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001604 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001605
Dale Johannesenca57b842009-02-02 23:46:53 +00001606 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001607 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001608
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001609 Tmp1 = LegalizeOp(Tmp1);
1610 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001611 break;
1612 }
1613 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001614 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001615 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001616 Tmp1 = LegalizeOp(Tmp3);
1617 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001618 }
Chris Lattner903d2782006-01-15 08:54:32 +00001619 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001620 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001621 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001622 }
Chris Lattner903d2782006-01-15 08:54:32 +00001623 // Since this op produce two values, make sure to remember that we
1624 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001625 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1626 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001627 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001628 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001629 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001631 bool Changed = false;
1632 // Legalize all of the operands of the inline asm, in case they are nodes
1633 // that need to be expanded or something. Note we skip the asm string and
1634 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001635 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001636 Changed = Op != Ops[0];
1637 Ops[0] = Op;
1638
1639 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1640 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00001641 unsigned NumVals = InlineAsm::
1642 getNumOperandRegisters(cast<ConstantSDNode>(Ops[i])->getZExtValue());
Chris Lattner25a022c2006-07-11 01:40:09 +00001643 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001644 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001645 if (Op != Ops[i]) {
1646 Changed = true;
1647 Ops[i] = Op;
1648 }
1649 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001650 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001651
1652 if (HasInFlag) {
1653 Op = LegalizeOp(Ops.back());
1654 Changed |= Op != Ops.back();
1655 Ops.back() = Op;
1656 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001657
Chris Lattner25a022c2006-07-11 01:40:09 +00001658 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001659 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Chris Lattnerce7518c2006-01-26 22:24:51 +00001661 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001662 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1663 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001664 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001665 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001666 case ISD::BR:
1667 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001668 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001669 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001670 Tmp1 = LegalizeOp(Tmp1);
1671 LastCALLSEQ_END = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001672
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001673 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001674 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001675 case ISD::BRIND:
1676 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1677 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001678 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Nate Begeman37efe672006-04-22 18:53:45 +00001679 Tmp1 = LegalizeOp(Tmp1);
1680 LastCALLSEQ_END = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001681
Eli Friedman957bffa2009-05-24 08:42:01 +00001682 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Nate Begeman37efe672006-04-22 18:53:45 +00001683 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1684 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001685 case ISD::BR_JT:
1686 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1687 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001688 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001689 Tmp1 = LegalizeOp(Tmp1);
1690 LastCALLSEQ_END = DAG.getEntryNode();
1691
1692 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1693 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1694
Scott Michelfdc40a02009-02-17 22:15:04 +00001695 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001696 default: assert(0 && "This action is not supported yet!");
1697 case TargetLowering::Legal: break;
1698 case TargetLowering::Custom:
1699 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001700 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001701 break;
1702 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SDValue Chain = Result.getOperand(0);
1704 SDValue Table = Result.getOperand(1);
1705 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001706
Duncan Sands83ec4b62008-06-06 12:08:01 +00001707 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001708 MachineFunction &MF = DAG.getMachineFunction();
1709 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001710 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001711 Index, DAG.getConstant(EntrySize, PTy));
1712 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001713
Duncan Sands712f7b32008-12-12 08:13:38 +00001714 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001715 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001716 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001717 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001718 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001719 // For PIC, the sequence is:
1720 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001721 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001722 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001723 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001724 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001725 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001726 }
1727 }
1728 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001729 case ISD::BRCOND:
1730 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001731 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00001732 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001733 Tmp1 = LegalizeOp(Tmp1);
1734 LastCALLSEQ_END = DAG.getEntryNode();
1735
Eli Friedman957bffa2009-05-24 08:42:01 +00001736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00001737
1738 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001739 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
1741 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001742 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001743 case TargetLowering::Legal: break;
1744 case TargetLowering::Custom:
1745 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001746 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001747 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001748 case TargetLowering::Expand:
1749 // Expand brcond's setcc into its constituent parts and create a BR_CC
1750 // Node.
1751 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001752 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001753 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00001754 Tmp2.getOperand(0), Tmp2.getOperand(1),
1755 Node->getOperand(2));
1756 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001757 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00001758 DAG.getCondCode(ISD::SETNE), Tmp2,
1759 DAG.getConstant(0, Tmp2.getValueType()),
1760 Node->getOperand(2));
1761 }
1762 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001763 }
1764 break;
1765 case ISD::BR_CC:
1766 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001767 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001768 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001769 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001770 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001771 Tmp3 = Node->getOperand(3); // RHS
1772 Tmp4 = Node->getOperand(1); // CC
1773
Scott Michelfdc40a02009-02-17 22:15:04 +00001774 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001775 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001776 LastCALLSEQ_END = DAG.getEntryNode();
1777
Evan Cheng7f042682008-10-15 02:05:31 +00001778 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001779 // the LHS is a legal SETCC itself. In this case, we need to compare
1780 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001781 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001782 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1783 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001784 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001785
1786 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001787 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001788
Chris Lattner181b7a32005-12-17 23:46:46 +00001789 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1790 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001791 case TargetLowering::Legal: break;
1792 case TargetLowering::Custom:
1793 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001794 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001795 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001796 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001797 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001798 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001799 LoadSDNode *LD = cast<LoadSDNode>(Node);
1800 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1801 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001802
Evan Cheng466685d2006-10-09 20:57:25 +00001803 ISD::LoadExtType ExtType = LD->getExtensionType();
1804 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001805 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001806 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1807 Tmp3 = Result.getValue(0);
1808 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001809
Evan Cheng466685d2006-10-09 20:57:25 +00001810 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1811 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001812 case TargetLowering::Legal:
1813 // If this is an unaligned load and the target doesn't support it,
1814 // expand it.
1815 if (!TLI.allowsUnalignedMemoryAccesses()) {
1816 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001817 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001818 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001819 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001820 TLI);
1821 Tmp3 = Result.getOperand(0);
1822 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001823 Tmp3 = LegalizeOp(Tmp3);
1824 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001825 }
1826 }
1827 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001828 case TargetLowering::Custom:
1829 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001830 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001831 Tmp3 = LegalizeOp(Tmp1);
1832 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001833 }
Evan Cheng466685d2006-10-09 20:57:25 +00001834 break;
1835 case TargetLowering::Promote: {
1836 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001838 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001839 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001840
Dale Johannesenca57b842009-02-02 23:46:53 +00001841 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001842 LD->getSrcValueOffset(),
1843 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001844 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001845 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001846 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001847 }
Evan Cheng466685d2006-10-09 20:57:25 +00001848 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001849 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001850 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001851 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1852 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001853 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001854 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001855 MVT SrcVT = LD->getMemoryVT();
1856 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001857 int SVOffset = LD->getSrcValueOffset();
1858 unsigned Alignment = LD->getAlignment();
1859 bool isVolatile = LD->isVolatile();
1860
Duncan Sands83ec4b62008-06-06 12:08:01 +00001861 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001862 // Some targets pretend to have an i1 loading operation, and actually
1863 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1864 // bits are guaranteed to be zero; it helps the optimizers understand
1865 // that these bits are zero. It is also useful for EXTLOAD, since it
1866 // tells the optimizers that those bits are undefined. It would be
1867 // nice to have an effective generic way of getting these benefits...
1868 // Until such a way is found, don't insist on promoting i1 here.
1869 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001870 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001871 // Promote to a byte-sized load if not loading an integral number of
1872 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001873 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1874 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001876
1877 // The extra bits are guaranteed to be zero, since we stored them that
1878 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1879
1880 ISD::LoadExtType NewExtType =
1881 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1882
Dale Johannesenca57b842009-02-02 23:46:53 +00001883 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001884 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1885 NVT, isVolatile, Alignment);
1886
1887 Ch = Result.getValue(1); // The chain.
1888
1889 if (ExtType == ISD::SEXTLOAD)
1890 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001891 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001892 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001893 Result, DAG.getValueType(SrcVT));
1894 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1895 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001896 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001897 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001898 DAG.getValueType(SrcVT));
1899
1900 Tmp1 = LegalizeOp(Result);
1901 Tmp2 = LegalizeOp(Ch);
1902 } else if (SrcWidth & (SrcWidth - 1)) {
1903 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001904 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001905 "Unsupported extload!");
1906 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1907 assert(RoundWidth < SrcWidth);
1908 unsigned ExtraWidth = SrcWidth - RoundWidth;
1909 assert(ExtraWidth < RoundWidth);
1910 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1911 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001912 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1913 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001915 unsigned IncrementSize;
1916
1917 if (TLI.isLittleEndian()) {
1918 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1919 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001920 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1921 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001922 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1923 Alignment);
1924
1925 // Load the remaining ExtraWidth bits.
1926 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001927 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001928 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001929 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001930 LD->getSrcValue(), SVOffset + IncrementSize,
1931 ExtraVT, isVolatile,
1932 MinAlign(Alignment, IncrementSize));
1933
1934 // Build a factor node to remember that this load is independent of the
1935 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001936 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001937 Hi.getValue(1));
1938
1939 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001940 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001941 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1942
1943 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001944 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001945 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001946 // Big endian - avoid unaligned loads.
1947 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1948 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001949 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001950 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1951 Alignment);
1952
1953 // Load the remaining ExtraWidth bits.
1954 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001955 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001956 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001957 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001958 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001959 LD->getSrcValue(), SVOffset + IncrementSize,
1960 ExtraVT, isVolatile,
1961 MinAlign(Alignment, IncrementSize));
1962
1963 // Build a factor node to remember that this load is independent of the
1964 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001965 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001966 Hi.getValue(1));
1967
1968 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001969 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001970 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1971
1972 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001973 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001974 }
1975
1976 Tmp1 = LegalizeOp(Result);
1977 Tmp2 = LegalizeOp(Ch);
1978 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001979 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001980 default: assert(0 && "This action is not supported yet!");
1981 case TargetLowering::Custom:
1982 isCustom = true;
1983 // FALLTHROUGH
1984 case TargetLowering::Legal:
1985 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1986 Tmp1 = Result.getValue(0);
1987 Tmp2 = Result.getValue(1);
1988
1989 if (isCustom) {
1990 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001991 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001992 Tmp1 = LegalizeOp(Tmp3);
1993 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1994 }
1995 } else {
1996 // If this is an unaligned load and the target doesn't support it,
1997 // expand it.
1998 if (!TLI.allowsUnalignedMemoryAccesses()) {
1999 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002000 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002001 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002002 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002003 TLI);
2004 Tmp1 = Result.getOperand(0);
2005 Tmp2 = Result.getOperand(1);
2006 Tmp1 = LegalizeOp(Tmp1);
2007 Tmp2 = LegalizeOp(Tmp2);
2008 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002009 }
2010 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002011 break;
2012 case TargetLowering::Expand:
2013 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2014 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002015 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002016 LD->getSrcValueOffset(),
2017 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00002018 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002019 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002020 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2021 Tmp2 = LegalizeOp(Load.getValue(1));
2022 break;
2023 }
2024 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2025 // Turn the unsupported load into an EXTLOAD followed by an explicit
2026 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00002027 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002028 Tmp1, Tmp2, LD->getSrcValue(),
2029 LD->getSrcValueOffset(), SrcVT,
2030 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002032 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00002033 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
2034 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002035 Result, DAG.getValueType(SrcVT));
2036 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002037 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002038 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2039 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002040 break;
2041 }
Evan Cheng466685d2006-10-09 20:57:25 +00002042 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002043
Evan Cheng466685d2006-10-09 20:57:25 +00002044 // Since loads produce two values, make sure to remember that we legalized
2045 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002046 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2047 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002048 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002049 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002050 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002051 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002052 MVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman957bffa2009-05-24 08:42:01 +00002053 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2054 // 1 -> Hi
2055 Result = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2056 DAG.getConstant(OpTy.getSizeInBits()/2,
2057 TLI.getShiftAmountTy()));
2058 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2059 } else {
2060 // 0 -> Lo
2061 Result = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2062 Node->getOperand(0));
Nate Begeman5dc897b2005-10-19 00:06:56 +00002063 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002064 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002065 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002066
2067 case ISD::CopyToReg:
2068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002069
Chris Lattner7310fb12005-12-18 15:27:43 +00002070 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002071 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002072 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002074 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002075 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002076 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002077 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2079 Tmp3);
2080 } else {
2081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002083
Chris Lattner7310fb12005-12-18 15:27:43 +00002084 // Since this produces two values, make sure to remember that we legalized
2085 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002086 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2087 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002088 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002089 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002090 break;
2091
2092 case ISD::RET:
2093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002094
2095 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00002096 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00002097 Tmp1 = LegalizeOp(Tmp1);
2098 LastCALLSEQ_END = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002099
Chris Lattner3e928bb2005-01-07 07:47:09 +00002100 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002101 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002102 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002103 Tmp3 = Node->getOperand(2); // Signness
Eli Friedman957bffa2009-05-24 08:42:01 +00002104 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002105 break;
2106 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002107 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002108 break;
2109 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002110 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002111 NewValues.push_back(Tmp1);
Eli Friedman957bffa2009-05-24 08:42:01 +00002112 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2113 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
2114 NewValues.push_back(Node->getOperand(i+1));
2115 }
2116 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002117 break;
2118 }
2119 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002120
Eli Friedman957bffa2009-05-24 08:42:01 +00002121 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2122 default: assert(0 && "This action is not supported yet!");
2123 case TargetLowering::Legal: break;
2124 case TargetLowering::Custom:
2125 Tmp1 = TLI.LowerOperation(Result, DAG);
2126 if (Tmp1.getNode()) Result = Tmp1;
2127 break;
Evan Cheng17c428e2006-01-06 00:41:43 +00002128 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002129 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002130 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002131 StoreSDNode *ST = cast<StoreSDNode>(Node);
2132 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2133 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002134 int SVOffset = ST->getSrcValueOffset();
2135 unsigned Alignment = ST->getAlignment();
2136 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002137
Evan Cheng8b2794a2006-10-13 21:14:26 +00002138 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002139 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2140 // FIXME: We shouldn't do this for TargetConstantFP's.
2141 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2142 // to phase ordering between legalized code and the dag combiner. This
2143 // probably means that we need to integrate dag combiner and legalizer
2144 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002145 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002146 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002147 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00002148 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002149 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002150 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002151 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00002152 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002153 SVOffset, isVolatile, Alignment);
2154 break;
2155 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002156 // If this target supports 64-bit registers, do a single 64-bit store.
2157 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002158 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002159 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00002160 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002161 SVOffset, isVolatile, Alignment);
2162 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002163 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002164 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2165 // stores. If the target supports neither 32- nor 64-bits, this
2166 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002167 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2169 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002170 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002171
Dale Johannesenca57b842009-02-02 23:46:53 +00002172 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00002173 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00002174 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002175 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00002176 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002177 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002178
Dale Johannesenca57b842009-02-02 23:46:53 +00002179 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002180 break;
2181 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002182 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002183 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002184
Eli Friedman957bffa2009-05-24 08:42:01 +00002185 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002186 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00002187 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002188 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002189
Duncan Sands83ec4b62008-06-06 12:08:01 +00002190 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002191 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2192 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002193 case TargetLowering::Legal:
2194 // If this is an unaligned store and the target doesn't support it,
2195 // expand it.
2196 if (!TLI.allowsUnalignedMemoryAccesses()) {
2197 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002198 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002199 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002200 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002201 TLI);
2202 }
2203 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002204 case TargetLowering::Custom:
2205 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002206 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002207 break;
2208 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002210 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002211 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00002212 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002213 ST->getSrcValue(), SVOffset, isVolatile,
2214 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002215 break;
2216 }
2217 break;
2218 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002219 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00002220 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002221
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 MVT StVT = ST->getMemoryVT();
2223 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002224
Duncan Sands83ec4b62008-06-06 12:08:01 +00002225 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002226 // Promote to a byte-sized store with upper bits zero if not
2227 // storing an integral number of bytes. For example, promote
2228 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002229 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00002230 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
2231 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002232 SVOffset, NVT, isVolatile, Alignment);
2233 } else if (StWidth & (StWidth - 1)) {
2234 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002235 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002236 "Unsupported truncstore!");
2237 unsigned RoundWidth = 1 << Log2_32(StWidth);
2238 assert(RoundWidth < StWidth);
2239 unsigned ExtraWidth = StWidth - RoundWidth;
2240 assert(ExtraWidth < RoundWidth);
2241 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2242 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002243 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2244 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002245 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002246 unsigned IncrementSize;
2247
2248 if (TLI.isLittleEndian()) {
2249 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2250 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002251 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002252 SVOffset, RoundVT,
2253 isVolatile, Alignment);
2254
2255 // Store the remaining ExtraWidth bits.
2256 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002257 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002258 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002259 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002260 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002261 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002262 SVOffset + IncrementSize, ExtraVT, isVolatile,
2263 MinAlign(Alignment, IncrementSize));
2264 } else {
2265 // Big endian - avoid unaligned stores.
2266 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2267 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00002268 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00002269 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002270 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
2271 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002272
2273 // Store the remaining ExtraWidth bits.
2274 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00002275 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00002276 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00002277 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00002278 SVOffset + IncrementSize, ExtraVT, isVolatile,
2279 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002280 }
Duncan Sands7e857202008-01-22 07:17:34 +00002281
2282 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00002283 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00002284 } else {
2285 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2286 Tmp2 != ST->getBasePtr())
2287 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2288 ST->getOffset());
2289
2290 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2291 default: assert(0 && "This action is not supported yet!");
2292 case TargetLowering::Legal:
2293 // If this is an unaligned store and the target doesn't support it,
2294 // expand it.
2295 if (!TLI.allowsUnalignedMemoryAccesses()) {
2296 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002297 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002298 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002299 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002300 TLI);
2301 }
2302 break;
2303 case TargetLowering::Custom:
2304 Result = TLI.LowerOperation(Result, DAG);
2305 break;
2306 case Expand:
2307 // TRUNCSTORE:i16 i32 -> STORE i16
2308 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00002309 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
2310 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
2311 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00002312 break;
2313 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002314 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002315 }
2316 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002317 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002318 case ISD::PCMARKER:
2319 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002321 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002322 case ISD::STACKSAVE:
2323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002324 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2325 Tmp1 = Result.getValue(0);
2326 Tmp2 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002327
Chris Lattner140d53c2006-01-13 02:50:02 +00002328 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2329 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002330 case TargetLowering::Legal: break;
2331 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002332 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002333 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002334 Tmp1 = LegalizeOp(Tmp3);
2335 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002336 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002337 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002338 case TargetLowering::Expand:
Scott Michelfdc40a02009-02-17 22:15:04 +00002339 // Expand to CopyFromReg if the target set
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002340 // StackPointerRegisterToSaveRestore.
2341 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002342 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), dl, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002343 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002344 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002345 } else {
Dale Johannesene8d72302009-02-06 23:05:02 +00002346 Tmp1 = DAG.getUNDEF(Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002347 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002348 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002349 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002350 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002351
2352 // Since stacksave produce two values, make sure to remember that we
2353 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002354 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2355 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002356 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002357
Chris Lattner140d53c2006-01-13 02:50:02 +00002358 case ISD::STACKRESTORE:
2359 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2360 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002362
Chris Lattner140d53c2006-01-13 02:50:02 +00002363 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2364 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002365 case TargetLowering::Legal: break;
2366 case TargetLowering::Custom:
2367 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002368 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002369 break;
2370 case TargetLowering::Expand:
Scott Michelfdc40a02009-02-17 22:15:04 +00002371 // Expand to CopyToReg if the target set
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002372 // StackPointerRegisterToSaveRestore.
2373 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Dale Johannesenc460ae92009-02-04 00:13:36 +00002374 Result = DAG.getCopyToReg(Tmp1, dl, SP, Tmp2);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002375 } else {
2376 Result = Tmp1;
2377 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002378 break;
2379 }
2380 break;
2381
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002382 case ISD::READCYCLECOUNTER:
2383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002384 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002385 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2386 Node->getValueType(0))) {
2387 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002388 case TargetLowering::Legal:
2389 Tmp1 = Result.getValue(0);
2390 Tmp2 = Result.getValue(1);
2391 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002392 case TargetLowering::Custom:
2393 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002394 Tmp1 = LegalizeOp(Result.getValue(0));
2395 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002396 break;
2397 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002398
2399 // Since rdcc produce two values, make sure to remember that we legalized
2400 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002401 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2402 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002403 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002404
Chris Lattner2ee743f2005-01-14 22:08:15 +00002405 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00002406 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00002407 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002408 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002409
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002410 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00002411
Nate Begemanb942a3d2005-08-23 04:29:48 +00002412 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002413 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002414 case TargetLowering::Legal: break;
2415 case TargetLowering::Custom: {
2416 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002417 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002418 break;
2419 }
Nate Begeman9373a812005-08-10 20:51:12 +00002420 case TargetLowering::Expand:
2421 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002422 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00002423 Tmp2, Tmp3,
2424 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2425 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002426 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00002427 DAG.getConstant(0, Tmp1.getValueType()),
2428 Tmp2, Tmp3, ISD::SETNE);
2429 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002430 break;
2431 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002432 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002433 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2434 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002435 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002436 ExtOp = ISD::BIT_CONVERT;
2437 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002439 ExtOp = ISD::ANY_EXTEND;
2440 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002441 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002442 ExtOp = ISD::FP_EXTEND;
2443 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002444 }
2445 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002446 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
2447 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002448 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002449 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002450 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00002451 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00002452 else
Dale Johannesenca57b842009-02-02 23:46:53 +00002453 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00002454 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002455 break;
2456 }
2457 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002458 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002459 case ISD::SELECT_CC: {
2460 Tmp1 = Node->getOperand(0); // LHS
2461 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002462 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2463 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002464 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00002465
2466 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00002467 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00002468
Evan Cheng7f042682008-10-15 02:05:31 +00002469 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002470 // the LHS is a legal SETCC itself. In this case, we need to compare
2471 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002472 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002473 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2474 CC = DAG.getCondCode(ISD::SETNE);
2475 }
2476 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2477
2478 // Everything is legal, see if we should expand this op or something.
2479 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2480 default: assert(0 && "This action is not supported yet!");
2481 case TargetLowering::Legal: break;
2482 case TargetLowering::Custom:
2483 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002484 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002485 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002486 }
2487 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002488 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002489 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002490 Tmp1 = Node->getOperand(0);
2491 Tmp2 = Node->getOperand(1);
2492 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00002493 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00002494
2495 // If we had to Expand the SetCC operands into a SELECT node, then it may
2496 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00002497 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002498 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002499 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002500 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002501 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002502
Chris Lattner73e142f2006-01-30 22:43:50 +00002503 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002504 default: assert(0 && "Cannot handle this action for SETCC yet!");
2505 case TargetLowering::Custom:
2506 isCustom = true;
2507 // FALLTHROUGH.
2508 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002510 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002511 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002512 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002513 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002514 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002515 case TargetLowering::Promote: {
2516 // First step, figure out the appropriate operation to use.
2517 // Allow SETCC to not be supported for all legal data types
2518 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002519 MVT NewInTy = Node->getOperand(0).getValueType();
2520 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002521
2522 // Scan for the appropriate larger type to use.
2523 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002524 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002525
Duncan Sands83ec4b62008-06-06 12:08:01 +00002526 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002527 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002528 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002529 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00002530
Andrew Lenharthae355752005-11-30 17:12:26 +00002531 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002532 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002533 break;
2534 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002535 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002536 assert(0 && "Cannot promote Legal Integer SETCC yet");
2537 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002538 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
2539 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00002540 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002541 Tmp1 = LegalizeOp(Tmp1);
2542 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002544 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002545 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002546 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002547 case TargetLowering::Expand:
2548 // Expand a setcc node into a select_cc of the same condition, lhs, and
2549 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002550 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002551 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00002552 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002553 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002554 break;
2555 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002556 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00002557 case ISD::VSETCC: {
2558 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2559 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00002560 SDValue CC = Node->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002561
Nate Begemanb43e9c12008-05-12 19:40:03 +00002562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
2563
2564 // Everything is legal, see if we should expand this op or something.
2565 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
2566 default: assert(0 && "This action is not supported yet!");
2567 case TargetLowering::Legal: break;
2568 case TargetLowering::Custom:
2569 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002570 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00002571 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002572 case TargetLowering::Expand: {
2573 // Unroll into a nasty set of scalar code for now.
2574 MVT VT = Node->getValueType(0);
2575 unsigned NumElems = VT.getVectorNumElements();
2576 MVT EltVT = VT.getVectorElementType();
2577 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
2578 SmallVector<SDValue, 8> Ops(NumElems);
2579 for (unsigned i = 0; i < NumElems; ++i) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002580 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, TmpEltVT,
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002581 Tmp1, DAG.getIntPtrConstant(i));
Dale Johannesenca57b842009-02-02 23:46:53 +00002582 Ops[i] = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(TmpEltVT),
2583 In1, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2584 TmpEltVT, Tmp2,
2585 DAG.getIntPtrConstant(i)),
Mon P Wang84aff842008-12-17 08:49:47 +00002586 CC);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002587 Ops[i] = DAG.getNode(ISD::SELECT, dl, EltVT, Ops[i],
2588 DAG.getConstant(APInt::getAllOnesValue
2589 (EltVT.getSizeInBits()), EltVT),
2590 DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002591 }
Evan Chenga87008d2009-02-25 22:49:59 +00002592 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], NumElems);
Mon P Wange1a0b2e2008-12-13 08:15:14 +00002593 break;
2594 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00002595 }
2596 break;
2597 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002598
Chris Lattner5b359c62005-04-02 04:00:59 +00002599 case ISD::SHL_PARTS:
2600 case ISD::SRA_PARTS:
2601 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00002602 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002603 bool Changed = false;
Duncan Sands92abc622009-01-31 15:50:11 +00002604 unsigned N = Node->getNumOperands();
2605 for (unsigned i = 0; i + 1 < N; ++i) {
Chris Lattner84f67882005-01-20 18:52:28 +00002606 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2607 Changed |= Ops.back() != Node->getOperand(i);
2608 }
Duncan Sands92abc622009-01-31 15:50:11 +00002609 Ops.push_back(LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(N-1))));
2610 Changed |= Ops.back() != Node->getOperand(N-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002611 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002612 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002613
Evan Cheng05a2d562006-01-09 18:31:59 +00002614 switch (TLI.getOperationAction(Node->getOpcode(),
2615 Node->getValueType(0))) {
2616 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002617 case TargetLowering::Legal: break;
2618 case TargetLowering::Custom:
2619 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002620 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002621 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002622 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002623 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00002624 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002625 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00002626 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002627 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002628 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002629 return RetVal;
2630 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002631 break;
2632 }
2633
Chris Lattner2c8086f2005-04-02 05:00:07 +00002634 // Since these produce multiple values, make sure to remember that we
2635 // legalized all of them.
2636 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00002637 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00002638 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00002639 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002640
2641 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002642 case ISD::ADD:
2643 case ISD::SUB:
2644 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002645 case ISD::MULHS:
2646 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002647 case ISD::UDIV:
2648 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002649 case ISD::AND:
2650 case ISD::OR:
2651 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002652 case ISD::SHL:
2653 case ISD::SRL:
2654 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002655 case ISD::FADD:
2656 case ISD::FSUB:
2657 case ISD::FMUL:
2658 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002659 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002660 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00002661 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002662
2663 if ((Node->getOpcode() == ISD::SHL ||
2664 Node->getOpcode() == ISD::SRL ||
2665 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00002666 !Node->getValueType(0).isVector())
2667 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
2668
Eli Friedman957bffa2009-05-24 08:42:01 +00002669 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002670
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002671 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002672
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002673 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002674 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002675 case TargetLowering::Legal: break;
2676 case TargetLowering::Custom:
2677 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002678 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002679 Result = Tmp1;
2680 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00002681 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002682 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002683 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002685
Dan Gohman525178c2007-10-08 18:33:35 +00002686 // See if multiply or divide can be lowered using two-result operations.
2687 SDVTList VTs = DAG.getVTList(VT, VT);
2688 if (Node->getOpcode() == ISD::MUL) {
2689 // We just need the low half of the multiply; try both the signed
2690 // and unsigned forms. If the target supports both SMUL_LOHI and
2691 // UMUL_LOHI, form a preference by checking which forms of plain
2692 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002693 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2694 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2695 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2696 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00002697 unsigned OpToUse = 0;
2698 if (HasSMUL_LOHI && !HasMULHS) {
2699 OpToUse = ISD::SMUL_LOHI;
2700 } else if (HasUMUL_LOHI && !HasMULHU) {
2701 OpToUse = ISD::UMUL_LOHI;
2702 } else if (HasSMUL_LOHI) {
2703 OpToUse = ISD::SMUL_LOHI;
2704 } else if (HasUMUL_LOHI) {
2705 OpToUse = ISD::UMUL_LOHI;
2706 }
2707 if (OpToUse) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002708 Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002709 break;
2710 }
2711 }
2712 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002713 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002714 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002715 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002716 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002717 break;
2718 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002719 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002720 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002721 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
2722 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002723 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002724 break;
2725 }
2726 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002727 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002728 Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002729 break;
2730 }
2731 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002732 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002733 Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2);
2734 break;
2735 }
2736 if (Node->getOpcode() == ISD::SUB &&
2737 TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2738 TLI.isOperationLegalOrCustom(ISD::XOR, VT)) {
2739 Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2,
2740 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
2741 Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2742 Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002743 break;
2744 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00002745
Dan Gohman82669522007-10-11 23:57:53 +00002746 // Check to see if we have a libcall for this operator.
2747 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2748 bool isSigned = false;
2749 switch (Node->getOpcode()) {
2750 case ISD::UDIV:
2751 case ISD::SDIV:
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002752 isSigned = Node->getOpcode() == ISD::SDIV;
2753 if (VT == MVT::i16)
2754 LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16);
2755 else if (VT == MVT::i32)
2756 LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32);
2757 else if (VT == MVT::i64)
2758 LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64);
2759 else if (VT == MVT::i128)
2760 LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128);
2761 break;
Chris Lattner31d71612008-10-04 21:27:46 +00002762 case ISD::MUL:
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002763 if (VT == MVT::i16)
2764 LC = RTLIB::MUL_I16;
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002765 else if (VT == MVT::i32)
Chris Lattner31d71612008-10-04 21:27:46 +00002766 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00002767 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00002768 LC = RTLIB::MUL_I64;
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002769 else if (VT == MVT::i128)
2770 LC = RTLIB::MUL_I128;
Chris Lattner31d71612008-10-04 21:27:46 +00002771 break;
Dan Gohman82669522007-10-11 23:57:53 +00002772 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002773 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2774 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002775 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002776 case ISD::FDIV:
2777 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
2778 RTLIB::DIV_PPCF128);
2779 break;
Dan Gohman82669522007-10-11 23:57:53 +00002780 default: break;
2781 }
2782 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00002783 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002784 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002785 break;
2786 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002787
Eli Friedman74807f22009-05-26 08:55:52 +00002788 assert(0 && "Cannot expand this binary operator!");
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002789 break;
2790 }
Evan Chengcc987612006-04-12 21:20:24 +00002791 case TargetLowering::Promote: {
2792 switch (Node->getOpcode()) {
2793 default: assert(0 && "Do not know how to promote this BinOp!");
2794 case ISD::AND:
2795 case ISD::OR:
2796 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002797 MVT OVT = Node->getValueType(0);
2798 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2799 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00002800 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002801 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
2802 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
2803 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00002804 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002805 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00002806 break;
2807 }
2808 }
2809 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002810 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002811 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002812
Dan Gohmane14ea862007-10-05 14:17:22 +00002813 case ISD::SMUL_LOHI:
2814 case ISD::UMUL_LOHI:
2815 case ISD::SDIVREM:
2816 case ISD::UDIVREM:
2817 // These nodes will only be produced by target-specific lowering, so
2818 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002819 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002820 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002821
2822 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2823 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2824 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002825 break;
2826
Chris Lattnera09f8482006-03-05 05:09:38 +00002827 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2828 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00002829 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00002830
Chris Lattnera09f8482006-03-05 05:09:38 +00002831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002832
Chris Lattnera09f8482006-03-05 05:09:38 +00002833 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2834 default: assert(0 && "Operation not supported");
2835 case TargetLowering::Custom:
2836 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002837 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002838 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002839 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002840 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002841 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002842 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002843 "Ugly special-cased code!");
2844 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002845 SDValue SignBit;
2846 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
2847 if (isTypeLegal(IVT)) {
2848 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
2849 } else {
2850 assert(isTypeLegal(TLI.getPointerTy()) &&
2851 (TLI.getPointerTy() == MVT::i32 ||
2852 TLI.getPointerTy() == MVT::i64) &&
2853 "Legal type for load?!");
2854 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
2855 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
2856 SDValue Ch =
2857 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
2858 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
2859 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
2860 LoadPtr, DAG.getIntPtrConstant(4));
2861 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
2862 Ch, LoadPtr, NULL, 0, MVT::i32);
2863 }
2864 SignBit =
2865 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
2866 SignBit, DAG.getConstant(0, SignBit.getValueType()),
2867 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002868 // Get the absolute value of the result.
2869 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
2870 // Select between the nabs and abs value based on the sign bit of
2871 // the input.
2872 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
2873 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
2874 AbsVal),
2875 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00002876 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002877 break;
2878 }
Evan Cheng912095b2007-01-04 21:56:39 +00002879 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002880 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002881
Nate Begeman551bf3f2006-02-17 05:43:56 +00002882 case ISD::ADDC:
2883 case ISD::SUBC:
2884 Tmp1 = LegalizeOp(Node->getOperand(0));
2885 Tmp2 = LegalizeOp(Node->getOperand(1));
2886 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00002887 Tmp3 = Result.getValue(0);
2888 Tmp4 = Result.getValue(1);
2889
2890 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2891 default: assert(0 && "This action is not supported yet!");
2892 case TargetLowering::Legal:
2893 break;
2894 case TargetLowering::Custom:
2895 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
2896 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00002897 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00002898 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2899 }
2900 break;
2901 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00002902 // Since this produces two values, make sure to remember that we legalized
2903 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00002904 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2905 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
2906 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002907
Nate Begeman551bf3f2006-02-17 05:43:56 +00002908 case ISD::ADDE:
2909 case ISD::SUBE:
2910 Tmp1 = LegalizeOp(Node->getOperand(0));
2911 Tmp2 = LegalizeOp(Node->getOperand(1));
2912 Tmp3 = LegalizeOp(Node->getOperand(2));
2913 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00002914 Tmp3 = Result.getValue(0);
2915 Tmp4 = Result.getValue(1);
2916
2917 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2918 default: assert(0 && "This action is not supported yet!");
2919 case TargetLowering::Legal:
2920 break;
2921 case TargetLowering::Custom:
2922 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
2923 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00002924 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00002925 Tmp4 = LegalizeOp(Tmp1.getValue(1));
2926 }
2927 break;
2928 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00002929 // Since this produces two values, make sure to remember that we legalized
2930 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00002931 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2932 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
2933 return Op.getResNo() ? Tmp4 : Tmp3;
Scott Michelfdc40a02009-02-17 22:15:04 +00002934
Nate Begeman419f8b62005-10-18 00:27:41 +00002935 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002936 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00002937 // TODO: handle the case where the Lo and Hi operands are not of legal type
2938 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2939 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2940 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002941 case TargetLowering::Promote:
2942 case TargetLowering::Custom:
2943 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002944 case TargetLowering::Legal:
2945 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00002946 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002947 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002948 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00002949 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
2950 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
2951 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002952 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00002953 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002954 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002955 break;
2956 }
2957 break;
2958 }
2959
Nate Begemanc105e192005-04-06 00:23:54 +00002960 case ISD::UREM:
2961 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002962 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002963 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2964 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002965
Nate Begemanc105e192005-04-06 00:23:54 +00002966 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002967 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2968 case TargetLowering::Custom:
2969 isCustom = true;
2970 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002971 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002973 if (isCustom) {
2974 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002975 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002976 }
Nate Begemanc105e192005-04-06 00:23:54 +00002977 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002978 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002979 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002980 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002981 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002982
Dan Gohman525178c2007-10-08 18:33:35 +00002983 // See if remainder can be lowered using two-result operations.
2984 SDVTList VTs = DAG.getVTList(VT, VT);
2985 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002986 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002987 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002988 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002989 break;
2990 }
2991 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002992 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002993 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
2994 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002995 break;
2996 }
2997
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002998 if (VT.isInteger() &&
2999 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
3000 // X % Y -> X-X/Y*Y
3001 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
3002 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
3003 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
3004 break;
Nate Begemanc105e192005-04-06 00:23:54 +00003005 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00003006
3007 // Check to see if we have a libcall for this operator.
3008 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3009 switch (Node->getOpcode()) {
3010 default: break;
3011 case ISD::UREM:
3012 case ISD::SREM:
3013 if (VT == MVT::i16)
3014 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
3015 else if (VT == MVT::i32)
3016 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
3017 else if (VT == MVT::i64)
3018 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
3019 else if (VT == MVT::i128)
3020 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
3021 break;
3022 case ISD::FREM:
3023 // Floating point mod -> fmod libcall.
3024 LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3025 RTLIB::REM_F80, RTLIB::REM_PPCF128);
3026 break;
3027 }
3028
3029 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3030 SDValue Dummy;
3031 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
3032 break;
3033 }
3034
Eli Friedman74807f22009-05-26 08:55:52 +00003035 assert(0 && "Cannot expand this binary operator!");
Nate Begemanc105e192005-04-06 00:23:54 +00003036 break;
3037 }
Dan Gohman525178c2007-10-08 18:33:35 +00003038 }
Nate Begemanc105e192005-04-06 00:23:54 +00003039 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003040 case ISD::VAARG: {
3041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3042 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3043
Duncan Sands83ec4b62008-06-06 12:08:01 +00003044 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003045 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3046 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003047 case TargetLowering::Custom:
3048 isCustom = true;
3049 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003050 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003051 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3052 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003053 Tmp1 = Result.getValue(1);
3054
3055 if (isCustom) {
3056 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003057 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003058 Result = LegalizeOp(Tmp2);
3059 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3060 }
3061 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003062 break;
3063 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003064 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003065 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003066 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00003067 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003068 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00003069 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00003070 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003071 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00003072 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003073 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00003074 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003075 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003076 Result = LegalizeOp(Result);
3077 break;
3078 }
3079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003080 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00003081 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003082 AddLegalizedOperand(SDValue(Node, 0), Result);
3083 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003084 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003086
3087 case ISD::VACOPY:
Nate Begemanacc398c2006-01-25 18:21:52 +00003088 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3089 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3090 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3091
3092 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3093 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003094 case TargetLowering::Custom:
3095 isCustom = true;
3096 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003097 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3099 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003100 if (isCustom) {
3101 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003102 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003103 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003104 break;
3105 case TargetLowering::Expand:
3106 // This defaults to loading a pointer from the input and storing it to the
3107 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003108 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3109 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00003110 Tmp4 = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp3, VS, 0);
3111 Result = DAG.getStore(Tmp4.getValue(1), dl, Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003112 break;
3113 }
3114 break;
3115
Scott Michelfdc40a02009-02-17 22:15:04 +00003116 case ISD::VAEND:
Nate Begemanacc398c2006-01-25 18:21:52 +00003117 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3118 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3119
3120 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3121 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003122 case TargetLowering::Custom:
3123 isCustom = true;
3124 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003125 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003127 if (isCustom) {
3128 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003129 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003130 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003131 break;
3132 case TargetLowering::Expand:
3133 Result = Tmp1; // Default to a no-op, return the chain
3134 break;
3135 }
3136 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003137
3138 case ISD::VASTART:
Nate Begemanacc398c2006-01-25 18:21:52 +00003139 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3140 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3141
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003142 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00003143
Nate Begemanacc398c2006-01-25 18:21:52 +00003144 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3145 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003146 case TargetLowering::Legal: break;
3147 case TargetLowering::Custom:
3148 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003150 break;
3151 }
3152 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003153
Nate Begeman35ef9132006-01-11 21:21:00 +00003154 case ISD::ROTL:
3155 case ISD::ROTR:
3156 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00003157 Tmp2 = LegalizeOp(DAG.getShiftAmountOperand(Node->getOperand(1))); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003159 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3160 default:
3161 assert(0 && "ROTL/ROTR legalize operation not supported");
3162 break;
3163 case TargetLowering::Legal:
3164 break;
3165 case TargetLowering::Custom:
3166 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003167 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003168 break;
3169 case TargetLowering::Promote:
3170 assert(0 && "Do not know how to promote ROTL/ROTR");
3171 break;
3172 case TargetLowering::Expand:
3173 assert(0 && "Do not know how to expand ROTL/ROTR");
3174 break;
3175 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003176 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003177
Nate Begemand88fc032006-01-14 03:14:10 +00003178 case ISD::BSWAP:
3179 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3180 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003181 case TargetLowering::Custom:
3182 assert(0 && "Cannot custom legalize this yet!");
3183 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003184 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003185 break;
3186 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003187 MVT OVT = Tmp1.getValueType();
3188 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3189 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003190
Dale Johannesenca57b842009-02-02 23:46:53 +00003191 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3192 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3193 Result = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Chris Lattner456a93a2006-01-28 07:39:30 +00003194 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3195 break;
3196 }
3197 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003198 Result = ExpandBSWAP(Tmp1, dl);
Chris Lattner456a93a2006-01-28 07:39:30 +00003199 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003200 }
3201 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003202
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003203 case ISD::CTPOP:
3204 case ISD::CTTZ:
3205 case ISD::CTLZ:
3206 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3207 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003208 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003209 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003210 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003211 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003212 TargetLowering::Custom) {
3213 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003215 Result = Tmp1;
3216 }
Scott Michel910b66d2007-07-30 21:00:31 +00003217 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003218 break;
3219 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003220 MVT OVT = Tmp1.getValueType();
3221 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003222
3223 // Zero extend the argument.
Dale Johannesenca57b842009-02-02 23:46:53 +00003224 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003225 // Perform the larger operation, then subtract if needed.
Dale Johannesenca57b842009-02-02 23:46:53 +00003226 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003227 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003228 case ISD::CTPOP:
3229 Result = Tmp1;
3230 break;
3231 case ISD::CTTZ:
3232 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Dale Johannesenca57b842009-02-02 23:46:53 +00003233 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3234 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003235 ISD::SETEQ);
Dale Johannesenca57b842009-02-02 23:46:53 +00003236 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003237 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003238 break;
3239 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003240 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Dale Johannesenca57b842009-02-02 23:46:53 +00003241 Result = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003242 DAG.getConstant(NVT.getSizeInBits() -
3243 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003244 break;
3245 }
3246 break;
3247 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003248 case TargetLowering::Expand:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003249 Result = ExpandBitCount(Node->getOpcode(), Tmp1, dl);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003250 break;
3251 }
3252 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003253
Chris Lattner2c8086f2005-04-02 05:00:07 +00003254 // Unary operators
3255 case ISD::FABS:
3256 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003257 case ISD::FSQRT:
3258 case ISD::FSIN:
3259 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003260 case ISD::FLOG:
3261 case ISD::FLOG2:
3262 case ISD::FLOG10:
3263 case ISD::FEXP:
3264 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003265 case ISD::FTRUNC:
3266 case ISD::FFLOOR:
3267 case ISD::FCEIL:
3268 case ISD::FRINT:
3269 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003270 Tmp1 = LegalizeOp(Node->getOperand(0));
3271 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003272 case TargetLowering::Promote:
3273 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003274 isCustom = true;
3275 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003276 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003277 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003278 if (isCustom) {
3279 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003280 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003281 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003282 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003283 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003284 switch (Node->getOpcode()) {
3285 default: assert(0 && "Unreachable!");
3286 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003287 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3288 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00003289 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003290 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003291 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003292 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003293 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003294 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003295 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003296 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00003297 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3298 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003299 break;
3300 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003301 case ISD::FSQRT:
3302 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00003303 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003304 case ISD::FLOG:
3305 case ISD::FLOG2:
3306 case ISD::FLOG10:
3307 case ISD::FEXP:
3308 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003309 case ISD::FTRUNC:
3310 case ISD::FFLOOR:
3311 case ISD::FCEIL:
3312 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003313 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003314 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003315
Eli Friedman74807f22009-05-26 08:55:52 +00003316 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00003317
Evan Cheng56966222007-01-12 02:11:51 +00003318 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003319 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003320 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003321 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3322 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003323 break;
3324 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003325 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3326 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003327 break;
3328 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003329 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3330 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003331 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003332 case ISD::FLOG:
3333 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3334 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3335 break;
3336 case ISD::FLOG2:
3337 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3338 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3339 break;
3340 case ISD::FLOG10:
3341 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3342 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3343 break;
3344 case ISD::FEXP:
3345 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3346 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3347 break;
3348 case ISD::FEXP2:
3349 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3350 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3351 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003352 case ISD::FTRUNC:
3353 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3354 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3355 break;
3356 case ISD::FFLOOR:
3357 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3358 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3359 break;
3360 case ISD::FCEIL:
3361 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3362 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3363 break;
3364 case ISD::FRINT:
3365 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3366 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3367 break;
3368 case ISD::FNEARBYINT:
3369 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3370 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3371 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003372 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003373 default: assert(0 && "Unreachable!");
3374 }
Dan Gohman475871a2008-07-27 21:46:04 +00003375 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003376 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003377 break;
3378 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003379 }
3380 break;
3381 }
3382 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003383 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003384 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003385
3386 // Expand unsupported unary vector operators by unrolling them.
Eli Friedman74807f22009-05-26 08:55:52 +00003387 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00003388
3389 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003390 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3391 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003392 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003393 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003394 break;
3395 }
Chris Lattner35481892005-12-23 00:16:34 +00003396 case ISD::BIT_CONVERT:
Eli Friedman1fde9c52009-05-24 02:46:31 +00003397 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3398 Node->getOperand(0).getValueType())) {
3399 default: assert(0 && "Unknown operation action!");
3400 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003401 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003402 Node->getValueType(0), dl);
Eli Friedman1fde9c52009-05-24 02:46:31 +00003403 break;
3404 case TargetLowering::Legal:
3405 Tmp1 = LegalizeOp(Node->getOperand(0));
3406 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3407 break;
Chris Lattner35481892005-12-23 00:16:34 +00003408 }
3409 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003410 case ISD::CONVERT_RNDSAT: {
3411 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3412 switch (CvtCode) {
3413 default: assert(0 && "Unknown cvt code!");
3414 case ISD::CVT_SF:
3415 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003416 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003417 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003418 case ISD::CVT_FS:
3419 case ISD::CVT_FU:
3420 case ISD::CVT_SS:
3421 case ISD::CVT_SU:
3422 case ISD::CVT_US:
3423 case ISD::CVT_UU: {
3424 SDValue DTyOp = Node->getOperand(1);
3425 SDValue STyOp = Node->getOperand(2);
3426 SDValue RndOp = Node->getOperand(3);
3427 SDValue SatOp = Node->getOperand(4);
Eli Friedman957bffa2009-05-24 08:42:01 +00003428 Tmp1 = LegalizeOp(Node->getOperand(0));
3429 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3430 RndOp, SatOp);
3431 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3432 TargetLowering::Custom) {
3433 Tmp1 = TLI.LowerOperation(Result, DAG);
3434 if (Tmp1.getNode()) Result = Tmp1;
Mon P Wang77cdf302008-11-10 20:54:11 +00003435 }
3436 break;
3437 }
3438 } // end switch CvtCode
3439 break;
3440 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003441 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003442 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003443 case ISD::UINT_TO_FP: {
3444 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003445 Result = LegalizeINT_TO_FP(Result, isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +00003446 Node->getValueType(0), Node->getOperand(0), dl);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003447 break;
3448 }
3449 case ISD::TRUNCATE:
Eli Friedman957bffa2009-05-24 08:42:01 +00003450 Tmp1 = LegalizeOp(Node->getOperand(0));
3451 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3452 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3453 case TargetLowering::Custom:
3454 isCustom = true;
3455 // FALLTHROUGH
3456 case TargetLowering::Legal:
3457 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3458 if (isCustom) {
3459 Tmp1 = TLI.LowerOperation(Result, DAG);
3460 if (Tmp1.getNode()) Result = Tmp1;
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00003461 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003462 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003463 }
3464 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003465
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003466 case ISD::FP_TO_SINT:
3467 case ISD::FP_TO_UINT:
Eli Friedman957bffa2009-05-24 08:42:01 +00003468 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003469
Eli Friedman957bffa2009-05-24 08:42:01 +00003470 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3471 default: assert(0 && "Unknown operation action!");
3472 case TargetLowering::Custom:
3473 isCustom = true;
3474 // FALLTHROUGH
3475 case TargetLowering::Legal:
3476 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3477 if (isCustom) {
3478 Tmp1 = TLI.LowerOperation(Result, DAG);
3479 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner07dffd62005-08-26 00:14:16 +00003480 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003481 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003482 case TargetLowering::Promote:
3483 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3484 Node->getOpcode() == ISD::FP_TO_SINT,
3485 dl);
Evan Cheng6af00d52006-12-13 01:57:55 +00003486 break;
Eli Friedman957bffa2009-05-24 08:42:01 +00003487 case TargetLowering::Expand:
3488 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3489 SDValue True, False;
3490 MVT VT = Node->getOperand(0).getValueType();
3491 MVT NVT = Node->getValueType(0);
3492 const uint64_t zero[] = {0, 0};
3493 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3494 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3495 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3496 Tmp2 = DAG.getConstantFP(apf, VT);
3497 Tmp3 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3498 Node->getOperand(0),
3499 Tmp2, ISD::SETLT);
3500 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3501 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3502 DAG.getNode(ISD::FSUB, dl, VT,
3503 Node->getOperand(0), Tmp2));
3504 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3505 DAG.getConstant(x, NVT));
3506 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp3, True, False);
3507 } else {
3508 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3509 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003510 break;
3511 }
3512 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003513
Chris Lattnerf2670a82008-01-16 06:57:07 +00003514 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003515 MVT DstVT = Op.getValueType();
3516 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003517 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3518 // The only other way we can lower this is to turn it into a STORE,
3519 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00003520 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00003521 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003522 }
Eli Friedman957bffa2009-05-24 08:42:01 +00003523 Tmp1 = LegalizeOp(Node->getOperand(0));
3524 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003525 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003526 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003527 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003528 MVT DstVT = Op.getValueType();
3529 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003530 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3531 if (SrcVT == MVT::ppcf128) {
Eli Friedman1fde9c52009-05-24 02:46:31 +00003532 // FIXME: Figure out how to extract the double without
3533 // help from type legalization
Dale Johannesen5411a392007-08-09 01:04:01 +00003534 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003535 // The only other way we can lower this is to turn it into a STORE,
3536 // LOAD pair, targetting a temporary location (a stack slot).
Dale Johannesen8a782a22009-02-02 22:12:50 +00003537 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT, dl);
Chris Lattner0bd48932008-01-17 07:00:52 +00003538 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003539 }
Eli Friedman957bffa2009-05-24 08:42:01 +00003540 Tmp1 = LegalizeOp(Node->getOperand(0));
3541 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003542 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003543 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003544 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003545 case ISD::ZERO_EXTEND:
3546 case ISD::SIGN_EXTEND:
Eli Friedman957bffa2009-05-24 08:42:01 +00003547 Tmp1 = LegalizeOp(Node->getOperand(0));
3548 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3549 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3550 TargetLowering::Custom) {
3551 Tmp1 = TLI.LowerOperation(Result, DAG);
3552 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003553 }
3554 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003555 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003556 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003557 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003558 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003559
3560 // If this operation is not supported, convert it to a shl/shr or load/store
3561 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003562 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3563 default: assert(0 && "This action not supported for this op yet!");
3564 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003566 break;
3567 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003568 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003569 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003570 // NOTE: we could fall back on load/store here too for targets without
3571 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3573 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003574 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Dale Johannesenca57b842009-02-02 23:46:53 +00003575 Result = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00003576 Node->getOperand(0), ShiftCst);
Dale Johannesenca57b842009-02-02 23:46:53 +00003577 Result = DAG.getNode(ISD::SRA, dl, Node->getValueType(0),
Chris Lattner45b8caf2005-01-15 07:15:18 +00003578 Result, ShiftCst);
3579 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003580 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003581 // EXTLOAD pair, targetting a temporary location (a stack slot).
3582
3583 // NOTE: there is a choice here between constantly creating new stack
3584 // slots and always reusing the same one. We currently always create
3585 // new ones, as reuse may inhibit scheduling.
Scott Michelfdc40a02009-02-17 22:15:04 +00003586 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003587 Node->getValueType(0), dl);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003588 } else {
3589 assert(0 && "Unknown op");
3590 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003591 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003592 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003593 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003594 }
Duncan Sands36397f52007-07-27 12:58:54 +00003595 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00003596 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00003597 for (unsigned i = 0; i != 6; ++i)
3598 Ops[i] = LegalizeOp(Node->getOperand(i));
3599 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3600 // The only option for this node is to custom lower it.
3601 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003602 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003603
3604 // Since trampoline produces two values, make sure to remember that we
3605 // legalized both of them.
3606 Tmp1 = LegalizeOp(Result.getValue(1));
3607 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00003608 AddLegalizedOperand(SDValue(Node, 0), Result);
3609 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003610 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003611 }
Dan Gohman9c78a392008-05-14 00:43:10 +00003612 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003613 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003614 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3615 default: assert(0 && "This action not supported for this op yet!");
3616 case TargetLowering::Custom:
3617 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003618 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003619 // Fall Thru
3620 case TargetLowering::Legal:
3621 // If this operation is not supported, lower it to constant 1
3622 Result = DAG.getConstant(1, VT);
3623 break;
3624 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00003625 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003626 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003627 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003628 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003629 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3630 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003631 case TargetLowering::Legal:
3632 Tmp1 = LegalizeOp(Node->getOperand(0));
3633 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3634 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003635 case TargetLowering::Custom:
3636 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003637 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003638 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003639 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003640 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003641 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003642 TargetLowering::ArgListTy Args;
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003643 std::pair<SDValue, SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00003644 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003645 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003646 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003647 Args, DAG, dl);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003648 Result = CallResult.second;
3649 break;
3650 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003651 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003652 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003653
Bill Wendling74c37652008-12-09 22:08:41 +00003654 case ISD::SADDO:
3655 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003656 MVT VT = Node->getValueType(0);
3657 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3658 default: assert(0 && "This action not supported for this op yet!");
3659 case TargetLowering::Custom:
3660 Result = TLI.LowerOperation(Op, DAG);
3661 if (Result.getNode()) break;
3662 // FALLTHROUGH
3663 case TargetLowering::Legal: {
3664 SDValue LHS = LegalizeOp(Node->getOperand(0));
3665 SDValue RHS = LegalizeOp(Node->getOperand(1));
3666
Scott Michelfdc40a02009-02-17 22:15:04 +00003667 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00003668 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00003669 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003670 MVT OType = Node->getValueType(1);
3671
Bill Wendlinga6af91a2008-11-25 08:19:22 +00003672 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003673
Bill Wendling740464e2008-11-25 19:40:17 +00003674 // LHSSign -> LHS >= 0
3675 // RHSSign -> RHS >= 0
3676 // SumSign -> Sum >= 0
3677 //
Bill Wendling74c37652008-12-09 22:08:41 +00003678 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00003679 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00003680 // Sub:
3681 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00003682 //
Dale Johannesenca57b842009-02-02 23:46:53 +00003683 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3684 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00003685 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3686 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00003687 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003688
Dale Johannesenca57b842009-02-02 23:46:53 +00003689 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3690 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003691
Dale Johannesenca57b842009-02-02 23:46:53 +00003692 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003693
3694 MVT ValueVTs[] = { LHS.getValueType(), OType };
3695 SDValue Ops[] = { Sum, Cmp };
3696
Scott Michelfdc40a02009-02-17 22:15:04 +00003697 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00003698 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00003699 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003700 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00003701 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00003702 break;
3703 }
3704 }
3705
3706 break;
3707 }
Bill Wendling74c37652008-12-09 22:08:41 +00003708 case ISD::UADDO:
3709 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00003710 MVT VT = Node->getValueType(0);
3711 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3712 default: assert(0 && "This action not supported for this op yet!");
3713 case TargetLowering::Custom:
3714 Result = TLI.LowerOperation(Op, DAG);
3715 if (Result.getNode()) break;
3716 // FALLTHROUGH
3717 case TargetLowering::Legal: {
3718 SDValue LHS = LegalizeOp(Node->getOperand(0));
3719 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003720
Bill Wendling74c37652008-12-09 22:08:41 +00003721 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00003722 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00003723 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00003724 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00003725 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00003726 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00003727 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003728
Bill Wendling41ea7e72008-11-24 19:21:46 +00003729 MVT ValueVTs[] = { LHS.getValueType(), OType };
3730 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003731
Scott Michelfdc40a02009-02-17 22:15:04 +00003732 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00003733 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00003734 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00003735 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00003736 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00003737 break;
3738 }
3739 }
3740
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00003741 break;
3742 }
Bill Wendling74c37652008-12-09 22:08:41 +00003743 case ISD::SMULO:
3744 case ISD::UMULO: {
3745 MVT VT = Node->getValueType(0);
3746 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3747 default: assert(0 && "This action is not supported at all!");
3748 case TargetLowering::Custom:
3749 Result = TLI.LowerOperation(Op, DAG);
3750 if (Result.getNode()) break;
3751 // Fall Thru
3752 case TargetLowering::Legal:
3753 // FIXME: According to Hacker's Delight, this can be implemented in
3754 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00003755 // requires a division + a branch.
Scott Michelfdc40a02009-02-17 22:15:04 +00003756 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
Bill Wendling74c37652008-12-09 22:08:41 +00003757 break;
3758 }
3759 break;
3760 }
3761
Chris Lattner45b8caf2005-01-15 07:15:18 +00003762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003763
Chris Lattner4ddd2832006-04-08 04:13:17 +00003764 assert(Result.getValueType() == Op.getValueType() &&
3765 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003766
Chris Lattner456a93a2006-01-28 07:39:30 +00003767 // Make sure that the generated code is itself legal.
3768 if (Result != Op)
3769 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003770
Chris Lattner45982da2005-05-12 16:53:42 +00003771 // Note that LegalizeOp may be reentered even from single-use nodes, which
3772 // means that we always must cache transformed nodes.
3773 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003774 return Result;
3775}
3776
Dan Gohman7f321562007-06-25 16:23:39 +00003777/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3778/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3779/// based on the vector type. The return type of this matches the element type
3780/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00003781SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003782 // We know that operand #0 is the Vec vector. If the index is a constant
3783 // or if the invec is a supported hardware type, we can use it. Otherwise,
3784 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00003785 SDValue Vec = Op.getOperand(0);
3786 SDValue Idx = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003787 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003788
Duncan Sands83ec4b62008-06-06 12:08:01 +00003789 MVT TVT = Vec.getValueType();
3790 unsigned NumElems = TVT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00003791
Dan Gohman7f321562007-06-25 16:23:39 +00003792 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3793 default: assert(0 && "This action is not supported yet!");
3794 case TargetLowering::Custom: {
3795 Vec = LegalizeOp(Vec);
3796 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00003797 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003798 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00003799 return Tmp3;
3800 break;
3801 }
3802 case TargetLowering::Legal:
3803 if (isTypeLegal(TVT)) {
3804 Vec = LegalizeOp(Vec);
3805 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003806 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003807 }
3808 break;
Mon P Wang0c397192008-10-30 08:01:45 +00003809 case TargetLowering::Promote:
3810 assert(TVT.isVector() && "not vector type");
3811 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00003812 case TargetLowering::Expand:
3813 break;
3814 }
3815
Eli Friedman1fde9c52009-05-24 02:46:31 +00003816 if (NumElems == 1)
Chris Lattner15972212006-03-31 17:55:51 +00003817 // This must be an access of the only element. Return it.
Eli Friedman1fde9c52009-05-24 02:46:31 +00003818 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Vec);
3819 else
3820 return ExpandExtractFromVectorThroughStack(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003821}
3822
Eli Friedman3d43b3f2009-05-23 22:37:25 +00003823SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
3824 SDValue Vec = Op.getOperand(0);
3825 SDValue Idx = Op.getOperand(1);
3826 DebugLoc dl = Op.getDebugLoc();
3827 // Store the value to a temporary stack slot, then LOAD the returned part.
3828 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
3829 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
3830
3831 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00003832 unsigned EltSize =
3833 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00003834 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
3835 DAG.getConstant(EltSize, Idx.getValueType()));
3836
3837 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
3838 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
3839 else
3840 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
3841
3842 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
3843
3844 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
3845}
3846
Nate Begeman750ac1b2006-02-01 07:19:44 +00003847/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3848/// with condition CC on the current target. This usually involves legalizing
3849/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3850/// there may be no choice but to create a new SetCC node to represent the
3851/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00003852/// LHS, and the SDValue returned in RHS has a nil SDNode value.
3853void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
3854 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00003855 SDValue &CC,
3856 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00003857 LHS = LegalizeOp(LHS);
3858 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003859}
3860
Evan Cheng7f042682008-10-15 02:05:31 +00003861/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
3862/// condition code CC on the current target. This routine assumes LHS and rHS
3863/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
3864/// illegal condition code into AND / OR of multiple SETCC values.
3865void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
3866 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00003867 SDValue &CC,
3868 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00003869 MVT OpVT = LHS.getValueType();
3870 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3871 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
3872 default: assert(0 && "Unknown condition code action!");
3873 case TargetLowering::Legal:
3874 // Nothing to do.
3875 break;
3876 case TargetLowering::Expand: {
3877 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
3878 unsigned Opc = 0;
3879 switch (CCCode) {
3880 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00003881 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
3882 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
3883 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
3884 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
3885 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
3886 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
3887 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
3888 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
3889 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
3890 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
3891 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
3892 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00003893 // FIXME: Implement more expansions.
3894 }
3895
Dale Johannesenbb5da912009-02-02 20:41:04 +00003896 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
3897 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
3898 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00003899 RHS = SDValue();
3900 CC = SDValue();
3901 break;
3902 }
3903 }
3904}
3905
Chris Lattner1401d152008-01-16 07:45:30 +00003906/// EmitStackConvert - Emit a store/load combination to the stack. This stores
3907/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
3908/// a load from the stack slot to DestVT, extending it if needed.
3909/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00003910SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
3911 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003912 MVT DestVT,
3913 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00003914 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003915 unsigned SrcAlign =
3916 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
3917 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00003918 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00003919
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00003920 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00003921 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00003922 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
3923
Duncan Sands83ec4b62008-06-06 12:08:01 +00003924 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
3925 unsigned SlotSize = SlotVT.getSizeInBits();
3926 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003927 unsigned DestAlign =
3928 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Chris Lattner1401d152008-01-16 07:45:30 +00003930 // Emit a store to the stack slot. Use a truncstore if the input value is
3931 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00003932 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00003933
Chris Lattner1401d152008-01-16 07:45:30 +00003934 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00003935 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00003936 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00003937 else {
3938 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00003939 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00003940 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00003941 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003942
Chris Lattner35481892005-12-23 00:16:34 +00003943 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00003944 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00003945 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00003946
Chris Lattner1401d152008-01-16 07:45:30 +00003947 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00003948 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00003949 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00003950}
3951
Dan Gohman475871a2008-07-27 21:46:04 +00003952SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00003953 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00003954 // Create a vector sized/aligned stack slot, store the value to element #0,
3955 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00003957
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00003958 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00003959 int SPFI = StackPtrFI->getIndex();
3960
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00003961 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
3962 StackPtr,
3963 PseudoSourceValue::getFixedStack(SPFI), 0,
3964 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00003965 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00003966 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003967}
3968
3969
Chris Lattnerce872152006-03-19 06:31:19 +00003970/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00003971/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00003972SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00003973 unsigned NumElems = Node->getNumOperands();
3974 SDValue SplatValue = Node->getOperand(0);
3975 DebugLoc dl = Node->getDebugLoc();
3976 MVT VT = Node->getValueType(0);
3977 MVT OpVT = SplatValue.getValueType();
3978 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003979
3980 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003981 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00003982 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00003983
Dan Gohman475871a2008-07-27 21:46:04 +00003984 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00003985 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00003986 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00003987 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00003988 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003989 bool isConstant = true;
3990 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3991 SplatValue.getOpcode() != ISD::UNDEF)
3992 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00003993
Evan Cheng033e6812006-03-24 01:17:21 +00003994 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003995 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003996 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003997 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003998 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003999 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004000 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004001
4002 // If this isn't a constant element or an undef, we can't use a constant
4003 // pool load.
4004 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4005 V.getOpcode() != ISD::UNDEF)
4006 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004007 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004008
Chris Lattnerce872152006-03-19 06:31:19 +00004009 if (isOnlyLowElement) {
4010 // If the low element is an undef too, then this whole things is an undef.
4011 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004012 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004013 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004014 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00004015 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004016
Chris Lattner2eb86532006-03-24 07:29:17 +00004017 // If all elements are constants, create a load from the constant pool.
4018 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00004019 std::vector<Constant*> CV;
4020 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004021 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00004022 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004023 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00004024 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004025 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004026 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004027 } else {
4028 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004029 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00004030 CV.push_back(UndefValue::get(OpNTy));
4031 }
4032 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004033 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004034 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00004035 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004036 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00004037 PseudoSourceValue::getConstantPool(), 0,
4038 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00004039 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004040
Gabor Greifba36cb52008-08-28 21:40:38 +00004041 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00004042 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00004043 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00004044
4045 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00004046 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00004047 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00004048 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004049 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00004050
Chris Lattner87100e02006-03-20 01:52:29 +00004051 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
4053 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00004054 }
4055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004056
Evan Cheng033e6812006-03-24 01:17:21 +00004057 // If there are only two unique elements, we may be able to turn this into a
4058 // vector shuffle.
4059 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004060 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00004061 SDValue Val1 = Node->getOperand(1);
4062 SDValue Val2;
4063 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004064 if (MI->first != Val1)
4065 Val2 = MI->first;
4066 else
4067 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00004068
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004069 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004070 // vector shuffle has the undef vector on the RHS.
4071 if (Val1.getOpcode() == ISD::UNDEF)
4072 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004073
Evan Cheng033e6812006-03-24 01:17:21 +00004074 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00004075 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004076
4077 // Set elements of the shuffle mask for Val1.
4078 std::vector<unsigned> &Val1Elts = Values[Val1];
4079 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004080 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004081
4082 // Set elements of the shuffle mask for Val2.
4083 std::vector<unsigned> &Val2Elts = Values[Val2];
4084 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4085 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00004086 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00004087
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004088 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004089 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00004090 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004091 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
4092 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004093 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00004094 }
4095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004096
Chris Lattnerce872152006-03-19 06:31:19 +00004097 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4098 // aligned object on the stack, store each element into it, then load
4099 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00004100 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00004101 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00004102 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
4103 const Value *SV = PseudoSourceValue::getFixedStack(FI);
4104
Chris Lattnerce872152006-03-19 06:31:19 +00004105 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004106 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00004107 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00004108 // Store (in the right endianness) the elements to memory.
4109 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4110 // Ignore undef elements.
4111 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004112
Chris Lattner841c8822006-03-22 01:46:54 +00004113 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00004114
Dan Gohman475871a2008-07-27 21:46:04 +00004115 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00004116 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004117
Dale Johannesen8a782a22009-02-02 22:12:50 +00004118 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
4119 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00004120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004121
Dan Gohman475871a2008-07-27 21:46:04 +00004122 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00004123 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00004124 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004125 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004126 else
4127 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004128
Chris Lattnerce872152006-03-19 06:31:19 +00004129 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00004130 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004131}
4132
Chris Lattner77e77a62005-01-21 06:05:23 +00004133// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4134// does not fit into a register, return the lo part and set the hi part to the
4135// by-reg argument. If it does fit into a single register, return the result
4136// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00004137SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
4138 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004139 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004140 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00004141 // Legalizing the call will automatically add the previous call to the
4142 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00004143 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004144
Chris Lattner77e77a62005-01-21 06:05:23 +00004145 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004146 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004147 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004148 MVT ArgVT = Node->getOperand(i).getValueType();
4149 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00004150 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004151 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00004152 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004153 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004154 }
Bill Wendling056292f2008-09-16 21:48:12 +00004155 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00004156 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004157
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004158 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004159 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004160 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00004161 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00004162 CallingConv::C, false, Callee, Args, DAG,
4163 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004164
Chris Lattner6831a812006-02-13 09:18:02 +00004165 // Legalize the call sequence, starting with the chain. This will advance
4166 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4167 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4168 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00004169 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00004170}
4171
Dan Gohman7f8613e2008-08-14 20:04:46 +00004172/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
4173///
4174SDValue SelectionDAGLegalize::
Dale Johannesenaf435272009-02-02 19:03:57 +00004175LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op,
4176 DebugLoc dl) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00004177 bool isCustom = false;
4178 SDValue Tmp1;
Eli Friedman74807f22009-05-26 08:55:52 +00004179 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
4180 Op.getValueType())) {
4181 default: assert(0 && "Unknown operation action!");
4182 case TargetLowering::Custom:
4183 isCustom = true;
4184 // FALLTHROUGH
4185 case TargetLowering::Legal:
4186 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00004187 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00004188 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4189 else
Dale Johannesenaf435272009-02-02 19:03:57 +00004190 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP, dl,
Dan Gohman7f8613e2008-08-14 20:04:46 +00004191 DestTy, Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +00004192 if (isCustom) {
4193 Tmp1 = TLI.LowerOperation(Result, DAG);
4194 if (Tmp1.getNode()) Result = Tmp1;
4195 }
4196 break;
4197 case TargetLowering::Expand:
4198 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy, dl);
4199 break;
4200 case TargetLowering::Promote:
4201 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned, dl);
Dan Gohman7f8613e2008-08-14 20:04:46 +00004202 break;
4203 }
4204 return Result;
4205}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004206
Chris Lattner22cde6a2006-01-28 08:25:58 +00004207/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4208/// INT_TO_FP operation of the specified operand when the target requests that
4209/// we expand it. At this point, we know that the result and operand types are
4210/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004211SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4212 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00004213 MVT DestVT,
4214 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00004215 if (Op0.getValueType() == MVT::i32) {
4216 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00004217
Chris Lattner23594d42008-01-16 07:03:22 +00004218 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00004219 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00004220
Chris Lattner22cde6a2006-01-28 08:25:58 +00004221 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00004222 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00004223 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00004224 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00004225 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004226 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00004227 if (TLI.isLittleEndian())
4228 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00004229
Chris Lattner22cde6a2006-01-28 08:25:58 +00004230 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00004231 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00004232 if (isSigned) {
4233 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00004234 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00004235 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004236 } else {
4237 Op0Mapped = Op0;
4238 }
4239 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00004240 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004241 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004242 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00004243 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004244 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00004245 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004246 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00004247 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004248 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00004249 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00004250 BitsToDouble(0x4330000080000000ULL) :
4251 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004252 MVT::f64);
4253 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00004254 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004255 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00004256 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00004257 // handle final rounding
4258 if (DestVT == MVT::f64) {
4259 // do nothing
4260 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004261 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00004262 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00004263 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004264 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00004265 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004266 }
4267 return Result;
4268 }
4269 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00004270 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004271
Dale Johannesenaf435272009-02-02 19:03:57 +00004272 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00004273 Op0, DAG.getConstant(0, Op0.getValueType()),
4274 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00004275 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00004276 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004277 SignSet, Four, Zero);
4278
4279 // If the sign bit of the integer is set, the large number will be treated
4280 // as a negative number. To counteract this, the dynamic code adds an
4281 // offset depending on the data type.
4282 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004283 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00004284 default: assert(0 && "Unsupported integer type!");
4285 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4286 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4287 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4288 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4289 }
4290 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00004291 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004292
Dan Gohman475871a2008-07-27 21:46:04 +00004293 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00004294 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00004295 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00004296 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00004297 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00004298 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00004299 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00004300 PseudoSourceValue::getConstantPool(), 0,
4301 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004302 else {
Dan Gohman69de1932008-02-06 22:27:42 +00004303 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00004304 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00004305 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004306 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00004307 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004308 }
4309
Dale Johannesenaf435272009-02-02 19:03:57 +00004310 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004311}
4312
4313/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4314/// *INT_TO_FP operation of the specified operand when the target requests that
4315/// we promote it. At this point, we know that the result and operand types are
4316/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4317/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00004318SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
4319 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00004320 bool isSigned,
4321 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00004322 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004323 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00004324
4325 unsigned OpToUse = 0;
4326
4327 // Scan for the appropriate larger type to use.
4328 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004329 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
4330 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00004331
4332 // If the target supports SINT_TO_FP of this type, use it.
4333 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4334 default: break;
4335 case TargetLowering::Legal:
4336 if (!TLI.isTypeLegal(NewInTy))
4337 break; // Can't use this datatype.
4338 // FALL THROUGH.
4339 case TargetLowering::Custom:
4340 OpToUse = ISD::SINT_TO_FP;
4341 break;
4342 }
4343 if (OpToUse) break;
4344 if (isSigned) continue;
4345
4346 // If the target supports UINT_TO_FP of this type, use it.
4347 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4348 default: break;
4349 case TargetLowering::Legal:
4350 if (!TLI.isTypeLegal(NewInTy))
4351 break; // Can't use this datatype.
4352 // FALL THROUGH.
4353 case TargetLowering::Custom:
4354 OpToUse = ISD::UINT_TO_FP;
4355 break;
4356 }
4357 if (OpToUse) break;
4358
4359 // Otherwise, try a larger type.
4360 }
4361
4362 // Okay, we found the operation and type to use. Zero extend our input to the
4363 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00004364 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00004365 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00004366 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004367}
4368
4369/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4370/// FP_TO_*INT operation of the specified operand when the target requests that
4371/// we promote it. At this point, we know that the result and operand types are
4372/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4373/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00004374SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
4375 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00004376 bool isSigned,
4377 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00004378 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004379 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00004380
4381 unsigned OpToUse = 0;
4382
4383 // Scan for the appropriate larger type to use.
4384 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004385 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
4386 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00004387
4388 // If the target supports FP_TO_SINT returning this type, use it.
4389 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4390 default: break;
4391 case TargetLowering::Legal:
4392 if (!TLI.isTypeLegal(NewOutTy))
4393 break; // Can't use this datatype.
4394 // FALL THROUGH.
4395 case TargetLowering::Custom:
4396 OpToUse = ISD::FP_TO_SINT;
4397 break;
4398 }
4399 if (OpToUse) break;
4400
4401 // If the target supports FP_TO_UINT of this type, use it.
4402 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4403 default: break;
4404 case TargetLowering::Legal:
4405 if (!TLI.isTypeLegal(NewOutTy))
4406 break; // Can't use this datatype.
4407 // FALL THROUGH.
4408 case TargetLowering::Custom:
4409 OpToUse = ISD::FP_TO_UINT;
4410 break;
4411 }
4412 if (OpToUse) break;
4413
4414 // Otherwise, try a larger type.
4415 }
4416
Scott Michelfdc40a02009-02-17 22:15:04 +00004417
Chris Lattner27a6c732007-11-24 07:07:01 +00004418 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00004419 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00004420
Chris Lattner27a6c732007-11-24 07:07:01 +00004421 // If the operation produces an invalid type, it must be custom lowered. Use
4422 // the target lowering hooks to expand it. Just keep the low part of the
4423 // expanded operation, we know that we're truncating anyway.
4424 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00004425 SmallVector<SDValue, 2> Results;
4426 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
4427 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
4428 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00004429 }
Duncan Sands126d9072008-07-04 11:47:58 +00004430
Chris Lattner27a6c732007-11-24 07:07:01 +00004431 // Truncate the result of the extended FP_TO_*INT operation to the desired
4432 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00004433 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004434}
4435
4436/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4437///
Dale Johannesen8a782a22009-02-02 22:12:50 +00004438SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004439 MVT VT = Op.getValueType();
4440 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004441 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004442 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00004443 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4444 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004445 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
4446 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
4447 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004448 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004449 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
4450 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
4451 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
4452 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
4453 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4454 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4455 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
4456 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
4457 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004458 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00004459 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
4460 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
4461 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
4462 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
4463 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
4464 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
4465 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
4466 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
4467 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4468 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4469 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4470 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4471 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4472 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4473 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
4474 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
4475 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
4476 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
4477 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
4478 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
4479 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004480 }
4481}
4482
4483/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4484///
Scott Michelfdc40a02009-02-17 22:15:04 +00004485SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00004486 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00004487 switch (Opc) {
4488 default: assert(0 && "Cannot expand this yet!");
4489 case ISD::CTPOP: {
4490 static const uint64_t mask[6] = {
4491 0x5555555555555555ULL, 0x3333333333333333ULL,
4492 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4493 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4494 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00004495 MVT VT = Op.getValueType();
4496 MVT ShVT = TLI.getShiftAmountTy();
4497 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00004498 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4499 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004500 unsigned EltSize = VT.isVector() ?
4501 VT.getVectorElementType().getSizeInBits() : len;
4502 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004503 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004504 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00004505 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00004506 DAG.getNode(ISD::AND, dl, VT,
4507 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
4508 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004509 }
4510 return Op;
4511 }
4512 case ISD::CTLZ: {
4513 // for now, we do this:
4514 // x = x | (x >> 1);
4515 // x = x | (x >> 2);
4516 // ...
4517 // x = x | (x >>16);
4518 // x = x | (x >>32); // for 64-bit input
4519 // return popcount(~x);
4520 //
4521 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00004522 MVT VT = Op.getValueType();
4523 MVT ShVT = TLI.getShiftAmountTy();
4524 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00004525 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004526 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004527 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00004528 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004529 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00004530 Op = DAG.getNOT(dl, Op, VT);
4531 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004532 }
4533 case ISD::CTTZ: {
4534 // for now, we use: { return popcount(~x & (x - 1)); }
4535 // unless the target has ctlz but not ctpop, in which case we use:
4536 // { return 32 - nlz(~x & (x-1)); }
4537 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00004538 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00004539 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
4540 DAG.getNOT(dl, Op, VT),
4541 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00004542 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004543 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00004544 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
4545 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00004546 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004547 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00004548 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
4549 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004550 }
4551 }
4552}
Chris Lattnere34b3962005-01-19 04:19:40 +00004553
Chris Lattner3e928bb2005-01-07 07:47:09 +00004554// SelectionDAG::Legalize - This is the entry point for the file.
4555//
Bill Wendling98a366d2009-04-29 23:29:43 +00004556void SelectionDAG::Legalize(bool TypesNeedLegalizing,
4557 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004558 /// run - This is the main entry point to this class.
4559 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00004560 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004561}
4562