blob: f3a48dd53ec74c971ac5c28de2dc886004c329e7 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000019#include "llvm/CodeGen/DwarfWriter.h"
20#include "llvm/Analysis/DebugInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000022#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000027#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000028#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000029#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000030#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000031#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000032#include "llvm/GlobalVariable.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000035#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000036#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000037#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000038#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000039#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000040using namespace llvm;
41
42//===----------------------------------------------------------------------===//
43/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
44/// hacks on it until the target machine can handle it. This involves
45/// eliminating value sizes the machine cannot handle (promoting small sizes to
46/// large sizes or splitting up large values into small values) as well as
47/// eliminating operations the machine cannot handle.
48///
49/// This code also does a small amount of optimization and recognition of idioms
50/// as part of its processing. For example, if a target does not support a
51/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
52/// will attempt merge setcc and brc instructions into brcc's.
53///
54namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000055class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 TargetLowering &TLI;
57 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000065 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000066
Chris Lattner6831a812006-02-13 09:18:02 +000067 /// IsLegalizingCall - This member is used *only* for purposes of providing
Scott Michelfdc40a02009-02-17 22:15:04 +000068 /// helpful assertions that a libcall isn't created while another call is
Chris Lattner6831a812006-02-13 09:18:02 +000069 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
Scott Michelfdc40a02009-02-17 22:15:04 +000071
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Scott Michelfdc40a02009-02-17 22:15:04 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Dan Gohman475871a2008-07-27 21:46:04 +000088 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000089 LegalizedNodes.insert(std::make_pair(From, To));
90 // If someone requests legalization of the new node, return itself.
91 if (From != To)
92 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000093 }
94
Chris Lattner3e928bb2005-01-07 07:47:09 +000095public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000096 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000101 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000102 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 }
104
105 /// isTypeLegal - Return true if this type is legal on this target.
106 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000107 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 return getTypeAction(VT) == Legal;
109 }
110
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 void LegalizeDAG();
112
Chris Lattner456a93a2006-01-28 07:39:30 +0000113private:
Dan Gohman7f321562007-06-25 16:23:39 +0000114 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000115 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000116 void HandleOp(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Chris Lattnerc7029802006-03-18 01:44:44 +0000118 /// LegalizeOp - We know that the specified value has a legal type.
119 /// Recursively ensure that the operands have legal types, then return the
120 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Nate Begeman68679912008-04-25 18:07:40 +0000123 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
124 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
125 /// is necessary to spill the vector being inserted into to memory, perform
126 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000128 SDValue Idx, DebugLoc dl);
129 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
130 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000131
Mon P Wangf007a8b2008-11-06 05:31:54 +0000132 /// Useful 16 element vector type that is used to pass operands for widening.
Scott Michelfdc40a02009-02-17 22:15:04 +0000133 typedef SmallVector<SDValue, 16> SDValueVector;
134
Nate Begeman5a5ca152009-04-29 05:20:52 +0000135 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
136 /// performs the same shuffe in terms of order or result bytes, but on a type
137 /// whose vector element type is narrower than the original shuffle type.
138 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
139 SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
140 SDValue N1, SDValue N2,
141 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000142
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000143 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000144 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000145
Dale Johannesenbb5da912009-02-02 20:41:04 +0000146 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC,
147 DebugLoc dl);
148 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
149 DebugLoc dl);
150 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
151 DebugLoc dl) {
152 LegalizeSetCCOperands(LHS, RHS, CC, dl);
153 LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl);
Evan Cheng7f042682008-10-15 02:05:31 +0000154 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Dan Gohman475871a2008-07-27 21:46:04 +0000156 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
157 SDValue &Hi);
Chris Lattnercad063f2005-07-16 00:19:57 +0000158
Dale Johannesen8a782a22009-02-02 22:12:50 +0000159 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000160 SDValue ExpandBUILD_VECTOR(SDNode *Node);
161 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dale Johannesenaf435272009-02-02 19:03:57 +0000162 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
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000172 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman8c377c72009-05-27 01:25:56 +0000173
174 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
175 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000176};
177}
178
Nate Begeman5a5ca152009-04-29 05:20:52 +0000179/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
180/// performs the same shuffe in terms of order or result bytes, but on a type
181/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000182/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +0000183SDValue
184SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl,
185 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000186 SmallVectorImpl<int> &Mask) const {
187 MVT EltVT = NVT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +0000188 unsigned NumMaskElts = VT.getVectorNumElements();
189 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000190 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000191
Nate Begeman9008ca62009-04-27 18:41:29 +0000192 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
193
194 if (NumEltsGrowth == 1)
195 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
196
197 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000198 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000199 int Idx = Mask[i];
200 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
201 if (Idx < 0)
202 NewMask.push_back(-1);
203 else
204 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000205 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000206 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000207 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000208 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
209 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000210}
211
Bill Wendling5aa49772009-02-24 02:35:30 +0000212SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000213 CodeGenOpt::Level ol)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000214 : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000215 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000216 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000217 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218}
219
Chris Lattner3e928bb2005-01-07 07:47:09 +0000220void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000221 LastCALLSEQ_END = DAG.getEntryNode();
222 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000223
Chris Lattnerab510a72005-10-02 17:49:46 +0000224 // The legalize process is inherently a bottom-up recursive process (users
225 // legalize their uses before themselves). Given infinite stack space, we
226 // could just start legalizing on the root and traverse the whole graph. In
227 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000228 // blocks. To avoid this problem, compute an ordering of the nodes where each
229 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000230 DAG.AssignTopologicalOrder();
231 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
232 E = prior(DAG.allnodes_end()); I != next(E); ++I)
233 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000234
235 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000236 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000237 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
238 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000239
Chris Lattner3e928bb2005-01-07 07:47:09 +0000240 LegalizedNodes.clear();
241
242 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000243 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000244}
245
Chris Lattner6831a812006-02-13 09:18:02 +0000246
247/// FindCallEndFromCallStart - Given a chained node that is part of a call
248/// sequence, find the CALLSEQ_END node that terminates the call sequence.
249static SDNode *FindCallEndFromCallStart(SDNode *Node) {
250 if (Node->getOpcode() == ISD::CALLSEQ_END)
251 return Node;
252 if (Node->use_empty())
253 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000254
Chris Lattner6831a812006-02-13 09:18:02 +0000255 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000256 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000257 if (TheChain.getValueType() != MVT::Other) {
258 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000259 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000260 if (TheChain.getValueType() != MVT::Other) {
261 // Otherwise, hunt for it.
262 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
263 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000264 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000265 break;
266 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000267
268 // Otherwise, we walked into a node without a chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000269 if (TheChain.getValueType() != MVT::Other)
270 return 0;
271 }
272 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000273
Chris Lattner6831a812006-02-13 09:18:02 +0000274 for (SDNode::use_iterator UI = Node->use_begin(),
275 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000276
Chris Lattner6831a812006-02-13 09:18:02 +0000277 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000278 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000279 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
280 if (User->getOperand(i) == TheChain)
281 if (SDNode *Result = FindCallEndFromCallStart(User))
282 return Result;
283 }
284 return 0;
285}
286
Scott Michelfdc40a02009-02-17 22:15:04 +0000287/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000288/// sequence, find the CALLSEQ_START node that initiates the call sequence.
289static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
290 assert(Node && "Didn't find callseq_start for a call??");
291 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Scott Michelfdc40a02009-02-17 22:15:04 +0000292
Chris Lattner6831a812006-02-13 09:18:02 +0000293 assert(Node->getOperand(0).getValueType() == MVT::Other &&
294 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000295 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000296}
297
298/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000299/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000300/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000301///
302/// Keep track of the nodes we fine that actually do lead to Dest in
303/// NodesLeadingTo. This avoids retraversing them exponential number of times.
304///
305bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000306 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000307 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000308
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000309 // If we've already processed this node and it does lead to Dest, there is no
310 // need to reprocess it.
311 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000312
Chris Lattner6831a812006-02-13 09:18:02 +0000313 // If the first result of this node has been already legalized, then it cannot
314 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000315 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000316
Chris Lattner6831a812006-02-13 09:18:02 +0000317 // Okay, this node has not already been legalized. Check and legalize all
318 // operands. If none lead to Dest, then we can legalize this node.
319 bool OperandsLeadToDest = false;
320 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
321 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000322 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000323
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000324 if (OperandsLeadToDest) {
325 NodesLeadingTo.insert(N);
326 return true;
327 }
Chris Lattner6831a812006-02-13 09:18:02 +0000328
329 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000330 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000331 return false;
332}
333
Mon P Wang0c397192008-10-30 08:01:45 +0000334/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000335/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000336void SelectionDAGLegalize::HandleOp(SDValue Op) {
Eli Friedman1fde9c52009-05-24 02:46:31 +0000337 // Don't touch TargetConstants
338 if (Op.getOpcode() == ISD::TargetConstant)
339 return;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000340 MVT VT = Op.getValueType();
Eli Friedman1fde9c52009-05-24 02:46:31 +0000341 // We should never see any illegal result types here.
342 assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?");
Eli Friedman74807f22009-05-26 08:55:52 +0000343 (void)LegalizeOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000344}
Chris Lattner6831a812006-02-13 09:18:02 +0000345
Evan Cheng9f877882006-12-13 20:57:08 +0000346/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
347/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000348static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000349 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000350 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000351 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000352
353 // If a FP immediate is precise when represented as a float and if the
354 // target can do an extending load from float to double, we put it into
355 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000356 // double. This shrinks FP constants and canonicalizes them for targets where
357 // an FP extending load is the same cost as a normal load (such as on the x87
358 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000359 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000360 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000361 if (!UseCP) {
Chris Lattnerd2e936a2009-03-08 01:47:41 +0000362 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000363 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000364 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000365 }
366
Duncan Sands83ec4b62008-06-06 12:08:01 +0000367 MVT OrigVT = VT;
368 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000369 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000370 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000371 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
372 // Only do this if the target has a native EXTLOAD instruction from
373 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000374 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000375 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000376 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000377 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
378 VT = SVT;
379 Extend = true;
380 }
Evan Cheng00495212006-12-12 21:32:44 +0000381 }
382
Dan Gohman475871a2008-07-27 21:46:04 +0000383 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000384 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000385 if (Extend)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000386 return DAG.getExtLoad(ISD::EXTLOAD, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +0000387 OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000388 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000389 0, VT, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000390 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000391 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000392}
393
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000394/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
395static
Dan Gohman475871a2008-07-27 21:46:04 +0000396SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000397 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000398 SDValue Chain = ST->getChain();
399 SDValue Ptr = ST->getBasePtr();
400 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000401 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000402 int Alignment = ST->getAlignment();
403 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000404 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000405 if (ST->getMemoryVT().isFloatingPoint() ||
406 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000407 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
408 if (TLI.isTypeLegal(intVT)) {
409 // Expand to a bitconvert of the value to the integer type of the
410 // same size, then a (misaligned) int store.
411 // FIXME: Does not handle truncating floating point stores!
Dale Johannesenbb5da912009-02-02 20:41:04 +0000412 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val);
413 return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000414 SVOffset, ST->isVolatile(), Alignment);
415 } else {
416 // Do a (aligned) store to a stack slot, then copy from the stack slot
417 // to the final destination using (unaligned) integer loads and stores.
418 MVT StoredVT = ST->getMemoryVT();
419 MVT RegVT =
420 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
421 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
422 unsigned RegBytes = RegVT.getSizeInBits() / 8;
423 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000424
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000425 // Make sure the stack slot is also aligned for the register type.
426 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
427
428 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 SDValue Store = DAG.getTruncStore(Chain, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000430 Val, StackPtr, NULL, 0, StoredVT);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000431 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
432 SmallVector<SDValue, 8> Stores;
433 unsigned Offset = 0;
434
435 // Do all but one copies using the full register width.
436 for (unsigned i = 1; i < NumRegs; i++) {
437 // Load one integer register's worth from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000438 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000439 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000440 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000441 ST->getSrcValue(), SVOffset + Offset,
442 ST->isVolatile(),
443 MinAlign(ST->getAlignment(), Offset)));
444 // Increment the pointers.
445 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000446 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000447 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000448 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000449 }
450
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000451 // The last store may be partial. Do a truncating store. On big-endian
452 // machines this requires an extending load from the stack slot to ensure
453 // that the bits are in the right place.
454 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000455
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000456 // Load from the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000457 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000458 NULL, 0, MemVT);
459
Dale Johannesenbb5da912009-02-02 20:41:04 +0000460 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000461 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000462 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000463 MinAlign(ST->getAlignment(), Offset)));
464 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000465 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000466 Stores.size());
467 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000468 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000469 assert(ST->getMemoryVT().isInteger() &&
470 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000471 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000472 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000473 MVT NewStoredVT =
474 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
475 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000476 int IncrementSize = NumBits / 8;
477
478 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000479 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
480 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000481 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000482
483 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000484 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000485 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000486 ST->getSrcValue(), SVOffset, NewStoredVT,
487 ST->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000488 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000489 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000490 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000491 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000492 ST->getSrcValue(), SVOffset + IncrementSize,
493 NewStoredVT, ST->isVolatile(), Alignment);
494
Dale Johannesenbb5da912009-02-02 20:41:04 +0000495 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000496}
497
498/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
499static
Dan Gohman475871a2008-07-27 21:46:04 +0000500SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000501 const TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000502 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000503 SDValue Chain = LD->getChain();
504 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000505 MVT VT = LD->getValueType(0);
506 MVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000507 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000508 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000509 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
510 if (TLI.isTypeLegal(intVT)) {
511 // Expand to a (misaligned) integer load of the same size,
512 // then bitconvert to floating point or vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000513 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000514 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000515 LD->getAlignment());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000516 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000517 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000519
Duncan Sands05e11fa2008-12-12 21:47:02 +0000520 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000522 } else {
523 // Copy the value to a (aligned) stack slot using (unaligned) integer
524 // loads and stores, then do a (aligned) load from the stack slot.
525 MVT RegVT = TLI.getRegisterType(intVT);
526 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
527 unsigned RegBytes = RegVT.getSizeInBits() / 8;
528 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
529
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000530 // Make sure the stack slot is also aligned for the register type.
531 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
532
Duncan Sands05e11fa2008-12-12 21:47:02 +0000533 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
534 SmallVector<SDValue, 8> Stores;
535 SDValue StackPtr = StackBase;
536 unsigned Offset = 0;
537
538 // Do all but one copies using the full register width.
539 for (unsigned i = 1; i < NumRegs; i++) {
540 // Load one integer register's worth from the original location.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000541 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000542 SVOffset + Offset, LD->isVolatile(),
543 MinAlign(LD->getAlignment(), Offset));
544 // Follow the load with a store to the stack slot. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000545 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000546 NULL, 0));
547 // Increment the pointers.
548 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000549 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
550 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000551 Increment);
552 }
553
554 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000555 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000556 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000557 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000558 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000559 MinAlign(LD->getAlignment(), Offset));
560 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000561 // On big-endian machines this requires a truncating store to ensure
562 // that the bits end up in the right place.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000563 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000564 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000565
566 // The order of the stores doesn't matter - say it with a TokenFactor.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000567 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000568 Stores.size());
569
570 // Finally, perform the original load only redirected to the stack slot.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000571 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000572 NULL, 0, LoadedVT);
573
574 // Callers expect a MERGE_VALUES node.
575 SDValue Ops[] = { Load, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000576 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000577 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000578 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000579 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000580 "Unaligned load of unsupported type.");
581
Dale Johannesen8155d642008-02-27 22:36:00 +0000582 // Compute the new VT that is half the size of the old one. This is an
583 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000584 unsigned NumBits = LoadedVT.getSizeInBits();
585 MVT NewLoadedVT;
586 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000587 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000588
Chris Lattnere400af82007-11-19 21:38:03 +0000589 unsigned Alignment = LD->getAlignment();
590 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000591 ISD::LoadExtType HiExtType = LD->getExtensionType();
592
593 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
594 if (HiExtType == ISD::NON_EXTLOAD)
595 HiExtType = ISD::ZEXTLOAD;
596
597 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000598 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000599 if (TLI.isLittleEndian()) {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000600 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000601 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000602 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000603 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000604 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000605 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000606 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000607 } else {
Dale Johannesenbb5da912009-02-02 20:41:04 +0000608 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(),
Bob Wilsonec15bbf2009-04-10 18:48:47 +0000609 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000610 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000611 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000612 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000613 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000614 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000615 }
616
617 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000618 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000619 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
620 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000621
Dale Johannesenbb5da912009-02-02 20:41:04 +0000622 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000623 Hi.getValue(1));
624
Dan Gohman475871a2008-07-27 21:46:04 +0000625 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000626 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000627}
Evan Cheng912095b2007-01-04 21:56:39 +0000628
Duncan Sands007f9842008-01-10 10:28:30 +0000629/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000630static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000631 RTLIB::Libcall Call_F32,
632 RTLIB::Libcall Call_F64,
633 RTLIB::Libcall Call_F80,
634 RTLIB::Libcall Call_PPCF128) {
635 return
636 VT == MVT::f32 ? Call_F32 :
637 VT == MVT::f64 ? Call_F64 :
638 VT == MVT::f80 ? Call_F80 :
639 VT == MVT::ppcf128 ? Call_PPCF128 :
640 RTLIB::UNKNOWN_LIBCALL;
641}
642
Nate Begeman68679912008-04-25 18:07:40 +0000643/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
644/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
645/// is necessary to spill the vector being inserted into to memory, perform
646/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000647SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000648PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
649 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000650 SDValue Tmp1 = Vec;
651 SDValue Tmp2 = Val;
652 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000653
Nate Begeman68679912008-04-25 18:07:40 +0000654 // If the target doesn't support this, we have to spill the input vector
655 // to a temporary stack slot, update the element, then reload it. This is
656 // badness. We could also load the value into a vector register (either
657 // with a "move to register" or "extload into register" instruction, then
658 // permute it into place, if the idx is a constant and if the idx is
659 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000660 MVT VT = Tmp1.getValueType();
661 MVT EltVT = VT.getVectorElementType();
662 MVT IdxVT = Tmp3.getValueType();
663 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000664 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000665
Gabor Greifba36cb52008-08-28 21:40:38 +0000666 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000667
668 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000669 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000670 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000671
672 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000673 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000674 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000675 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000676 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000677 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
678 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000679 // Store the scalar value.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000680 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000681 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000682 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000683 return DAG.getLoad(VT, dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000684 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000685}
686
Mon P Wange9f10152008-12-09 05:46:39 +0000687
Eli Friedman3f727d62009-05-27 02:16:40 +0000688SDValue SelectionDAGLegalize::
689ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
690 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
691 // SCALAR_TO_VECTOR requires that the type of the value being inserted
692 // match the element type of the vector being created, except for
693 // integers in which case the inserted value can be over width.
694 MVT EltVT = Vec.getValueType().getVectorElementType();
695 if (Val.getValueType() == EltVT ||
696 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
697 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
698 Vec.getValueType(), Val);
699
700 unsigned NumElts = Vec.getValueType().getVectorNumElements();
701 // We generate a shuffle of InVec and ScVec, so the shuffle mask
702 // should be 0,1,2,3,4,5... with the appropriate element replaced with
703 // elt 0 of the RHS.
704 SmallVector<int, 8> ShufOps;
705 for (unsigned i = 0; i != NumElts; ++i)
706 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
707
708 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
709 &ShufOps[0]);
710 }
711 }
712 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
713}
714
Dan Gohmana3466152007-07-13 20:14:11 +0000715/// LegalizeOp - We know that the specified value has a legal type, and
716/// that its operands are legal. Now ensure that the operation itself
717/// is legal, recursively ensuring that the operands' operations remain
718/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000719SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000720 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
721 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000722
Gabor Greifba36cb52008-08-28 21:40:38 +0000723 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000724 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000725
Eli Friedman1fde9c52009-05-24 02:46:31 +0000726 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
727 assert(getTypeAction(Node->getValueType(i)) == Legal &&
728 "Unexpected illegal type!");
729
730 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
731 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
732 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
733 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000734
Chris Lattner45982da2005-05-12 16:53:42 +0000735 // Note that LegalizeOp may be reentered even from single-use nodes, which
736 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000737 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000738 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000739
Dan Gohman475871a2008-07-27 21:46:04 +0000740 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
741 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000742 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000743
Eli Friedman8c377c72009-05-27 01:25:56 +0000744 // Figure out the correct action; the way to query this varies by opcode
745 TargetLowering::LegalizeAction Action;
746 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000747 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000748 case ISD::INTRINSIC_W_CHAIN:
749 case ISD::INTRINSIC_WO_CHAIN:
750 case ISD::INTRINSIC_VOID:
751 case ISD::VAARG:
752 case ISD::STACKSAVE:
753 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
754 break;
755 case ISD::SINT_TO_FP:
756 case ISD::UINT_TO_FP:
757 case ISD::EXTRACT_VECTOR_ELT:
758 Action = TLI.getOperationAction(Node->getOpcode(),
759 Node->getOperand(0).getValueType());
760 break;
761 case ISD::FP_ROUND_INREG:
762 case ISD::SIGN_EXTEND_INREG: {
763 MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
764 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
765 break;
766 }
767 case ISD::LOAD:
768 case ISD::STORE:
769 case ISD::BR_CC:
770 case ISD::FORMAL_ARGUMENTS:
771 case ISD::CALL:
772 case ISD::CALLSEQ_START:
773 case ISD::CALLSEQ_END:
774 case ISD::SELECT_CC:
775 case ISD::SETCC:
776 case ISD::EXCEPTIONADDR:
777 case ISD::EHSELECTION:
778 // These instructions have properties that aren't modeled in the
779 // generic codepath
780 SimpleFinishLegalizing = false;
781 break;
782 case ISD::EXTRACT_ELEMENT:
783 case ISD::FLT_ROUNDS_:
784 case ISD::SADDO:
785 case ISD::SSUBO:
786 case ISD::UADDO:
787 case ISD::USUBO:
788 case ISD::SMULO:
789 case ISD::UMULO:
790 case ISD::FPOWI:
791 case ISD::MERGE_VALUES:
792 case ISD::EH_RETURN:
793 case ISD::FRAME_TO_ARGS_OFFSET:
794 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
795 if (Action == TargetLowering::Legal)
796 Action = TargetLowering::Expand;
797 break;
798 case ISD::TRAMPOLINE:
799 case ISD::FRAMEADDR:
800 case ISD::RETURNADDR:
801 Action = TargetLowering::Custom;
802 break;
803 case ISD::BUILD_VECTOR:
804 // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize
805 // its operands first!
806 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000807 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000808 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000809 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000810 Action = TargetLowering::Legal;
811 } else {
812 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000813 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000814 break;
815 }
816
817 if (SimpleFinishLegalizing) {
818 SmallVector<SDValue, 8> Ops, ResultVals;
819 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
820 Ops.push_back(LegalizeOp(Node->getOperand(i)));
821 switch (Node->getOpcode()) {
822 default: break;
823 case ISD::BR:
824 case ISD::BRIND:
825 case ISD::BR_JT:
826 case ISD::BR_CC:
827 case ISD::BRCOND:
828 case ISD::RET:
829 // Branches tweak the chain to include LastCALLSEQ_END
830 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
831 LastCALLSEQ_END);
832 Ops[0] = LegalizeOp(Ops[0]);
833 LastCALLSEQ_END = DAG.getEntryNode();
834 break;
835 case ISD::SHL:
836 case ISD::SRL:
837 case ISD::SRA:
838 case ISD::ROTL:
839 case ISD::ROTR:
840 // Legalizing shifts/rotates requires adjusting the shift amount
841 // to the appropriate width.
842 if (!Ops[1].getValueType().isVector())
843 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
844 break;
845 }
846
847 Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(),
848 Ops.size());
849 switch (Action) {
850 case TargetLowering::Legal:
851 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
852 ResultVals.push_back(Result.getValue(i));
853 break;
854 case TargetLowering::Custom:
855 // FIXME: The handling for custom lowering with multiple results is
856 // a complete mess.
857 Tmp1 = TLI.LowerOperation(Result, DAG);
858 if (Tmp1.getNode()) {
859 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
860 if (e == 1)
861 ResultVals.push_back(Tmp1);
862 else
863 ResultVals.push_back(Tmp1.getValue(i));
864 }
865 break;
866 }
867
868 // FALL THROUGH
869 case TargetLowering::Expand:
870 ExpandNode(Result.getNode(), ResultVals);
871 break;
872 case TargetLowering::Promote:
873 PromoteNode(Result.getNode(), ResultVals);
874 break;
875 }
876 if (!ResultVals.empty()) {
877 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
878 if (ResultVals[i] != SDValue(Node, i))
879 ResultVals[i] = LegalizeOp(ResultVals[i]);
880 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
881 }
882 return ResultVals[Op.getResNo()];
883 }
884 }
885
886 switch (Node->getOpcode()) {
887 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000888#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000889 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000890#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000891 assert(0 && "Do not know how to legalize this operator!");
892 abort();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000893 case ISD::EXCEPTIONADDR: {
894 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000896 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
897 default: assert(0 && "This action is not supported yet!");
898 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000899 unsigned Reg = TLI.getExceptionAddressRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000900 Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000901 }
902 break;
903 case TargetLowering::Custom:
904 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000905 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000906 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000907 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000908 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000909 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000910 break;
911 }
912 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000913 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000914 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000915
Gabor Greifba36cb52008-08-28 21:40:38 +0000916 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000917 "Cannot return more than two values!");
918
919 // Since we produced two values, make sure to remember that we
920 // legalized both of them.
921 Tmp1 = LegalizeOp(Result);
922 Tmp2 = LegalizeOp(Result.getValue(1));
923 AddLegalizedOperand(Op.getValue(0), Tmp1);
924 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000925 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000926 case ISD::EHSELECTION: {
927 Tmp1 = LegalizeOp(Node->getOperand(0));
928 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000929 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +0000930 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
931 default: assert(0 && "This action is not supported yet!");
932 case TargetLowering::Expand: {
933 unsigned Reg = TLI.getExceptionSelectorRegister();
Dale Johannesenc460ae92009-02-04 00:13:36 +0000934 Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000935 }
936 break;
937 case TargetLowering::Custom:
938 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000939 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +0000940 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000941 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Dale Johannesenca57b842009-02-02 23:46:53 +0000943 Result = DAG.getMergeValues(Ops, 2, dl);
Jim Laskey8782d482007-02-28 20:43:58 +0000944 break;
945 }
946 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000947 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000948 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000949
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000951 "Cannot return more than two values!");
952
953 // Since we produced two values, make sure to remember that we
954 // legalized both of them.
955 Tmp1 = LegalizeOp(Result);
956 Tmp2 = LegalizeOp(Result.getValue(1));
957 AddLegalizedOperand(Op.getValue(0), Tmp1);
958 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000959 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner36ce6912005-11-29 06:21:05 +0000960
Dan Gohman7f460202008-06-30 20:59:49 +0000961 case ISD::DBG_STOPPOINT:
962 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +0000963 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
Dan Gohman7f460202008-06-30 20:59:49 +0000965 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000966 case TargetLowering::Promote:
967 default: assert(0 && "This action is not supported yet!");
Bill Wendling92c1e122009-02-13 02:16:35 +0000968 case TargetLowering::Expand: {
969 DwarfWriter *DW = DAG.getDwarfWriter();
970 bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC,
971 MVT::Other);
972 bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +0000973
Bill Wendling92c1e122009-02-13 02:16:35 +0000974 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
975 GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit());
976 if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) {
977 DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit()));
Scott Michelfdc40a02009-02-17 22:15:04 +0000978
Bill Wendling92c1e122009-02-13 02:16:35 +0000979 unsigned Line = DSP->getLine();
980 unsigned Col = DSP->getColumn();
Bill Wendling86e6cb92009-02-17 01:04:54 +0000981
Bill Wendling98a366d2009-04-29 23:29:43 +0000982 if (OptLevel == CodeGenOpt::None) {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000983 // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it
984 // won't hurt anything.
985 if (useDEBUG_LOC) {
986 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Bill Wendling92c1e122009-02-13 02:16:35 +0000987 DAG.getConstant(Col, MVT::i32),
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000988 DAG.getSrcValue(CU.getGV()) };
Bill Wendling86e6cb92009-02-17 01:04:54 +0000989 Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4);
990 } else {
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000991 unsigned ID = DW->RecordSourceLine(Line, Col, CU);
Bill Wendling86e6cb92009-02-17 01:04:54 +0000992 Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID);
993 }
Bill Wendling92c1e122009-02-13 02:16:35 +0000994 } else {
Bill Wendling86e6cb92009-02-17 01:04:54 +0000995 Result = Tmp1; // chain
Bill Wendling92c1e122009-02-13 02:16:35 +0000996 }
997 } else {
998 Result = Tmp1; // chain
999 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001000 break;
Bill Wendling92c1e122009-02-13 02:16:35 +00001001 }
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001002 case TargetLowering::Custom:
1003 Result = TLI.LowerOperation(Op, DAG);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001004 if (Result.getNode())
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +00001005 break;
Evan Cheng71e86852008-07-08 20:06:39 +00001006 case TargetLowering::Legal: {
Eli Friedman74807f22009-05-26 08:55:52 +00001007 if (Tmp1 == Node->getOperand(0))
Evan Cheng71e86852008-07-08 20:06:39 +00001008 break;
1009
Dan Gohman475871a2008-07-27 21:46:04 +00001010 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001011 Ops.push_back(Tmp1);
Eli Friedman74807f22009-05-26 08:55:52 +00001012 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1013 Ops.push_back(Node->getOperand(2)); // col # must be legal.
Evan Cheng71e86852008-07-08 20:06:39 +00001014 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1015 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1016 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001017 break;
1018 }
Evan Cheng71e86852008-07-08 20:06:39 +00001019 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001020 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001021 case ISD::ConstantFP: {
1022 // Spill FP immediates to the constant pool if the target cannot directly
1023 // codegen them. Targets often have some immediate values that can be
1024 // efficiently generated into an FP register without a load. We explicitly
1025 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001026 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1027
Chris Lattner3181a772006-01-29 06:26:56 +00001028 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1029 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001030 case TargetLowering::Legal:
1031 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001032 case TargetLowering::Custom:
1033 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001034 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001035 Result = Tmp3;
1036 break;
1037 }
1038 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001039 case TargetLowering::Expand: {
1040 // Check to see if this FP immediate is already legal.
1041 bool isLegal = false;
1042 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1043 E = TLI.legal_fpimm_end(); I != E; ++I) {
1044 if (CFP->isExactlyValue(*I)) {
1045 isLegal = true;
1046 break;
1047 }
1048 }
1049 // If this is a legal constant, turn it into a TargetConstantFP node.
1050 if (isLegal)
1051 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001052 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001053 }
Nate Begemane1795842008-02-14 08:57:00 +00001054 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001055 break;
1056 }
Chris Lattnerfdfded52006-04-12 16:20:43 +00001057 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001058 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001059 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001060 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001061 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001062 // A call within a calling sequence must be legalized to something
1063 // other than the normal CALLSEQ_END. Violating this gets Legalize
1064 // into an infinite loop.
1065 assert ((!IsLegalizingCall ||
1066 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001067 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001068 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001069
1070 // The number of incoming and outgoing values should match; unless the final
1071 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001072 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1073 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1074 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001075 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001076 "Lowering call/formal_arguments produced unexpected # results!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001077
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001078 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001079 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1081 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001082 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001083 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001084 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001085 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001086 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001087 }
1088 return Tmp2;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001089 case ISD::BUILD_VECTOR:
1090 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001091 default: assert(0 && "This action is not supported yet!");
1092 case TargetLowering::Custom:
1093 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001094 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001095 Result = Tmp3;
1096 break;
1097 }
1098 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001099 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001100 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001101 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001102 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001103 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001104 case ISD::VECTOR_SHUFFLE: {
Chris Lattner87100e02006-03-20 01:52:29 +00001105 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1106 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00001107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1108 MVT VT = Result.getValueType();
1109
Nate Begeman5a5ca152009-04-29 05:20:52 +00001110 // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal.
Nate Begeman9008ca62009-04-27 18:41:29 +00001111 SmallVector<int, 8> Mask;
1112 cast<ShuffleVectorSDNode>(Result)->getMask(Mask);
Chris Lattner87100e02006-03-20 01:52:29 +00001113
1114 // Allow targets to custom lower the SHUFFLEs they support.
Nate Begeman9008ca62009-04-27 18:41:29 +00001115 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
Chris Lattner4352cc92006-04-04 17:23:26 +00001116 default: assert(0 && "Unknown operation action!");
1117 case TargetLowering::Legal:
Nate Begeman9008ca62009-04-27 18:41:29 +00001118 assert(TLI.isShuffleMaskLegal(Mask, VT) &&
Chris Lattner4352cc92006-04-04 17:23:26 +00001119 "vector shuffle should not be created if not legal!");
1120 break;
1121 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001122 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001123 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001124 Result = Tmp3;
1125 break;
1126 }
1127 // FALLTHROUGH
1128 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001129 MVT EltVT = VT.getVectorElementType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00001130 unsigned NumElems = VT.getVectorNumElements();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001131 SmallVector<SDValue, 8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001132 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001133 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001134 Ops.push_back(DAG.getUNDEF(EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00001135 continue;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001136 }
Nate Begeman5a5ca152009-04-29 05:20:52 +00001137 unsigned Idx = Mask[i];
Nate Begeman9008ca62009-04-27 18:41:29 +00001138 if (Idx < NumElems)
1139 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1,
1140 DAG.getIntPtrConstant(Idx)));
1141 else
1142 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2,
1143 DAG.getIntPtrConstant(Idx - NumElems)));
Evan Cheng18dd6d02006-04-05 06:07:11 +00001144 }
Evan Chenga87008d2009-02-25 22:49:59 +00001145 Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001146 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001147 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001148 case TargetLowering::Promote: {
1149 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001150 MVT OVT = Node->getValueType(0);
1151 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001152
1153 // Cast the two input vectors.
Dale Johannesenca57b842009-02-02 23:46:53 +00001154 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
1155 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00001156
Chris Lattner4352cc92006-04-04 17:23:26 +00001157 // Convert the shuffle mask to the right # elements.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001158 Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Dale Johannesenca57b842009-02-02 23:46:53 +00001159 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Chris Lattner4352cc92006-04-04 17:23:26 +00001160 break;
1161 }
Chris Lattner87100e02006-03-20 01:52:29 +00001162 }
1163 break;
Nate Begeman9008ca62009-04-27 18:41:29 +00001164 }
Mon P Wang0c397192008-10-30 08:01:45 +00001165 case ISD::CONCAT_VECTORS: {
Bob Wilson5ee24e52009-05-01 17:55:32 +00001166 // Legalize the operands.
Mon P Wang0c397192008-10-30 08:01:45 +00001167 SmallVector<SDValue, 8> Ops;
Bob Wilson5ee24e52009-05-01 17:55:32 +00001168 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1169 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1170 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
1171
1172 switch (TLI.getOperationAction(ISD::CONCAT_VECTORS,
1173 Node->getValueType(0))) {
1174 default: assert(0 && "Unknown operation action!");
1175 case TargetLowering::Legal:
1176 break;
1177 case TargetLowering::Custom:
1178 Tmp3 = TLI.LowerOperation(Result, DAG);
1179 if (Tmp3.getNode()) {
1180 Result = Tmp3;
1181 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001182 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001183 // FALLTHROUGH
1184 case TargetLowering::Expand: {
1185 // Use extract/insert/build vector for now. We might try to be
1186 // more clever later.
1187 MVT PtrVT = TLI.getPointerTy();
1188 SmallVector<SDValue, 8> Ops;
1189 unsigned NumOperands = Node->getNumOperands();
1190 for (unsigned i=0; i < NumOperands; ++i) {
1191 SDValue SubOp = Node->getOperand(i);
1192 MVT VVT = SubOp.getNode()->getValueType(0);
1193 MVT EltVT = VVT.getVectorElementType();
1194 unsigned NumSubElem = VVT.getVectorNumElements();
1195 for (unsigned j=0; j < NumSubElem; ++j) {
1196 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp,
1197 DAG.getConstant(j, PtrVT)));
1198 }
1199 }
1200 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl,
1201 Node->getValueType(0),
1202 &Ops[0], Ops.size()));
Mon P Wang0c397192008-10-30 08:01:45 +00001203 }
Bob Wilson5ee24e52009-05-01 17:55:32 +00001204 }
1205 break;
Mon P Wang0c397192008-10-30 08:01:45 +00001206 }
1207
Chris Lattner6831a812006-02-13 09:18:02 +00001208 case ISD::CALLSEQ_START: {
1209 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001210
Chris Lattner6831a812006-02-13 09:18:02 +00001211 // Recursively Legalize all of the inputs of the call end that do not lead
1212 // to this call start. This ensures that any libcalls that need be inserted
1213 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001214 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001215 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001216 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001217 NodesLeadingTo);
1218 }
Chris Lattner6831a812006-02-13 09:18:02 +00001219
1220 // Now that we legalized all of the inputs (which may have inserted
1221 // libcalls) create the new CALLSEQ_START node.
1222 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1223
1224 // Merge in the last call, to ensure that this call start after the last
1225 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001226 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001227 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001228 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001229 Tmp1 = LegalizeOp(Tmp1);
1230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001231
Chris Lattner6831a812006-02-13 09:18:02 +00001232 // Do not try to legalize the target-specific arguments (#1+).
1233 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001234 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001235 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001236 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001238
Chris Lattner6831a812006-02-13 09:18:02 +00001239 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001240 AddLegalizedOperand(Op.getValue(0), Result);
1241 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1242 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001243
Chris Lattner6831a812006-02-13 09:18:02 +00001244 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001245 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001246 // process, no libcalls can/will be inserted, guaranteeing that no calls
1247 // can overlap.
1248 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001249 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001250 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001251 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Chris Lattner6831a812006-02-13 09:18:02 +00001253 // Legalize the call, starting from the CALLSEQ_END.
1254 LegalizeOp(LastCALLSEQ_END);
1255 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1256 return Result;
1257 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001258 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001259 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1260 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001262 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1263 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001264 assert(I != LegalizedNodes.end() &&
1265 "Legalizing the call start should have legalized this node!");
1266 return I->second;
1267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001268
1269 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001270 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001271 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001272 // Do not try to legalize the target-specific arguments (#1+), except for
1273 // an optional flag input.
1274 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1275 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001276 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001277 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001278 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001279 }
1280 } else {
1281 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1282 if (Tmp1 != Node->getOperand(0) ||
1283 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001284 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001285 Ops[0] = Tmp1;
1286 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001287 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001288 }
Chris Lattner6a542892006-01-24 05:48:21 +00001289 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001290 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001291 // This finishes up call legalization.
1292 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Chris Lattner4b653a02006-02-14 00:55:02 +00001294 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001295 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001296 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001297 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001298 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001299 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001300 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001301 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1302 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1303 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001305
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001306 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001307 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001308 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001309 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001310 case TargetLowering::Expand: {
1311 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1312 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1313 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001314 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001315
1316 // Chain the dynamic stack allocation so that it doesn't modify the stack
1317 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001318 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001319
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue Size = Tmp2.getOperand(1);
Dale Johannesenc460ae92009-02-04 00:13:36 +00001321 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001322 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001323 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001324 unsigned StackAlign =
1325 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1326 if (Align > StackAlign)
Dale Johannesenca57b842009-02-02 23:46:53 +00001327 SP = DAG.getNode(ISD::AND, dl, VT, SP,
Evan Cheng3e20bba2007-08-17 18:02:22 +00001328 DAG.getConstant(-(uint64_t)Align, VT));
Dale Johannesenca57b842009-02-02 23:46:53 +00001329 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenc460ae92009-02-04 00:13:36 +00001330 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001331
Dale Johannesenca57b842009-02-02 23:46:53 +00001332 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
Chris Lattnere563bbc2008-10-11 22:08:30 +00001333 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001334
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001335 Tmp1 = LegalizeOp(Tmp1);
1336 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001337 break;
1338 }
1339 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001340 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001341 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001342 Tmp1 = LegalizeOp(Tmp3);
1343 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001344 }
Chris Lattner903d2782006-01-15 08:54:32 +00001345 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001346 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001347 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001348 }
Chris Lattner903d2782006-01-15 08:54:32 +00001349 // Since this op produce two values, make sure to remember that we
1350 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001351 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1352 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001353 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001354 }
Evan Cheng3d4ce112006-10-30 08:00:44 +00001355 case ISD::BR_JT:
1356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1357 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001358 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001359 Tmp1 = LegalizeOp(Tmp1);
1360 LastCALLSEQ_END = DAG.getEntryNode();
1361
1362 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1364
Scott Michelfdc40a02009-02-17 22:15:04 +00001365 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001366 default: assert(0 && "This action is not supported yet!");
1367 case TargetLowering::Legal: break;
1368 case TargetLowering::Custom:
1369 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001370 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001371 break;
1372 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue Chain = Result.getOperand(0);
1374 SDValue Table = Result.getOperand(1);
1375 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001376
Duncan Sands83ec4b62008-06-06 12:08:01 +00001377 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001378 MachineFunction &MF = DAG.getMachineFunction();
1379 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001380 Index= DAG.getNode(ISD::MUL, dl, PTy,
Dale Johannesenca57b842009-02-02 23:46:53 +00001381 Index, DAG.getConstant(EntrySize, PTy));
1382 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001383
Duncan Sands712f7b32008-12-12 08:13:38 +00001384 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
Dale Johannesenca57b842009-02-02 23:46:53 +00001385 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Duncan Sands712f7b32008-12-12 08:13:38 +00001386 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00001387 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001388 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001389 // For PIC, the sequence is:
1390 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001391 // RelocBase can be JumpTable, GOT or some sort of global base.
Dale Johannesenca57b842009-02-02 23:46:53 +00001392 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
Evan Chengcc415862007-11-09 01:32:10 +00001393 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001394 }
Dale Johannesenca57b842009-02-02 23:46:53 +00001395 Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001396 }
1397 }
1398 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001399 case ISD::BRCOND:
1400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001401 // Ensure that libcalls are emitted before a return.
Dale Johannesenca57b842009-02-02 23:46:53 +00001402 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001403 Tmp1 = LegalizeOp(Tmp1);
1404 LastCALLSEQ_END = DAG.getEntryNode();
1405
Eli Friedman957bffa2009-05-24 08:42:01 +00001406 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
Chris Lattner456a93a2006-01-28 07:39:30 +00001407
1408 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00001410
1411 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001412 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001413 case TargetLowering::Legal: break;
1414 case TargetLowering::Custom:
1415 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001416 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001417 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001418 case TargetLowering::Expand:
1419 // Expand brcond's setcc into its constituent parts and create a BR_CC
1420 // Node.
1421 if (Tmp2.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001422 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001423 Tmp1, Tmp2.getOperand(2),
Nate Begeman7cbd5252005-08-16 19:49:35 +00001424 Tmp2.getOperand(0), Tmp2.getOperand(1),
1425 Node->getOperand(2));
1426 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001427 Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Nate Begeman7cbd5252005-08-16 19:49:35 +00001428 DAG.getCondCode(ISD::SETNE), Tmp2,
1429 DAG.getConstant(0, Tmp2.getValueType()),
1430 Node->getOperand(2));
1431 }
1432 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001433 }
1434 break;
1435 case ISD::BR_CC:
1436 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001437 // Ensure that libcalls are emitted before a branch.
Dale Johannesenca57b842009-02-02 23:46:53 +00001438 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END);
Chris Lattner6831a812006-02-13 09:18:02 +00001439 Tmp1 = LegalizeOp(Tmp1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001440 Tmp2 = Node->getOperand(2); // LHS
Nate Begeman750ac1b2006-02-01 07:19:44 +00001441 Tmp3 = Node->getOperand(3); // RHS
1442 Tmp4 = Node->getOperand(1); // CC
1443
Scott Michelfdc40a02009-02-17 22:15:04 +00001444 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001445 Tmp2, Tmp3, Tmp4, dl);
Evan Cheng722cb362006-12-18 22:55:34 +00001446 LastCALLSEQ_END = DAG.getEntryNode();
1447
Evan Cheng7f042682008-10-15 02:05:31 +00001448 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001449 // the LHS is a legal SETCC itself. In this case, we need to compare
1450 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001451 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001452 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1453 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001454 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001455
1456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001457 Node->getOperand(4));
Scott Michelfdc40a02009-02-17 22:15:04 +00001458
Chris Lattner181b7a32005-12-17 23:46:46 +00001459 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1460 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001461 case TargetLowering::Legal: break;
1462 case TargetLowering::Custom:
1463 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001464 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001465 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001466 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001467 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001468 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001469 LoadSDNode *LD = cast<LoadSDNode>(Node);
1470 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1471 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001472
Evan Cheng466685d2006-10-09 20:57:25 +00001473 ISD::LoadExtType ExtType = LD->getExtensionType();
1474 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001475 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001476 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1477 Tmp3 = Result.getValue(0);
1478 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001479
Evan Cheng466685d2006-10-09 20:57:25 +00001480 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1481 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001482 case TargetLowering::Legal:
1483 // If this is an unaligned load and the target doesn't support it,
1484 // expand it.
1485 if (!TLI.allowsUnalignedMemoryAccesses()) {
1486 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001487 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001488 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001490 TLI);
1491 Tmp3 = Result.getOperand(0);
1492 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001493 Tmp3 = LegalizeOp(Tmp3);
1494 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001495 }
1496 }
1497 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001498 case TargetLowering::Custom:
1499 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001500 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001501 Tmp3 = LegalizeOp(Tmp1);
1502 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001503 }
Evan Cheng466685d2006-10-09 20:57:25 +00001504 break;
1505 case TargetLowering::Promote: {
1506 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001507 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001508 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001509 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001510
Dale Johannesenca57b842009-02-02 23:46:53 +00001511 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001512 LD->getSrcValueOffset(),
1513 LD->isVolatile(), LD->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001514 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001515 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001516 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001517 }
Evan Cheng466685d2006-10-09 20:57:25 +00001518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001519 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001520 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001521 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1522 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001523 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00001524 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001525 MVT SrcVT = LD->getMemoryVT();
1526 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001527 int SVOffset = LD->getSrcValueOffset();
1528 unsigned Alignment = LD->getAlignment();
1529 bool isVolatile = LD->isVolatile();
1530
Duncan Sands83ec4b62008-06-06 12:08:01 +00001531 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001532 // Some targets pretend to have an i1 loading operation, and actually
1533 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1534 // bits are guaranteed to be zero; it helps the optimizers understand
1535 // that these bits are zero. It is also useful for EXTLOAD, since it
1536 // tells the optimizers that those bits are undefined. It would be
1537 // nice to have an effective generic way of getting these benefits...
1538 // Until such a way is found, don't insist on promoting i1 here.
1539 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00001540 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001541 // Promote to a byte-sized load if not loading an integral number of
1542 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001543 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1544 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001545 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001546
1547 // The extra bits are guaranteed to be zero, since we stored them that
1548 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1549
1550 ISD::LoadExtType NewExtType =
1551 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1552
Dale Johannesenca57b842009-02-02 23:46:53 +00001553 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001554 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1555 NVT, isVolatile, Alignment);
1556
1557 Ch = Result.getValue(1); // The chain.
1558
1559 if (ExtType == ISD::SEXTLOAD)
1560 // Having the top bits zero doesn't help when sign extending.
Scott Michelfdc40a02009-02-17 22:15:04 +00001561 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001562 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001563 Result, DAG.getValueType(SrcVT));
1564 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1565 // All the top bits are guaranteed to be zero - inform the optimizers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001566 Result = DAG.getNode(ISD::AssertZext, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001567 Result.getValueType(), Result,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001568 DAG.getValueType(SrcVT));
1569
1570 Tmp1 = LegalizeOp(Result);
1571 Tmp2 = LegalizeOp(Ch);
1572 } else if (SrcWidth & (SrcWidth - 1)) {
1573 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001574 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001575 "Unsupported extload!");
1576 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1577 assert(RoundWidth < SrcWidth);
1578 unsigned ExtraWidth = SrcWidth - RoundWidth;
1579 assert(ExtraWidth < RoundWidth);
1580 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1581 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001582 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1583 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001584 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001585 unsigned IncrementSize;
1586
1587 if (TLI.isLittleEndian()) {
1588 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1589 // Load the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001590 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
1591 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001592 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1593 Alignment);
1594
1595 // Load the remaining ExtraWidth bits.
1596 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001597 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001598 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001599 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001600 LD->getSrcValue(), SVOffset + IncrementSize,
1601 ExtraVT, isVolatile,
1602 MinAlign(Alignment, IncrementSize));
1603
1604 // Build a factor node to remember that this load is independent of the
1605 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001606 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001607 Hi.getValue(1));
1608
1609 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001610 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001611 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1612
1613 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001614 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001615 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001616 // Big endian - avoid unaligned loads.
1617 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1618 // Load the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001619 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001620 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1621 Alignment);
1622
1623 // Load the remaining ExtraWidth bits.
1624 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001625 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001626 DAG.getIntPtrConstant(IncrementSize));
Scott Michelfdc40a02009-02-17 22:15:04 +00001627 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001628 Node->getValueType(0), Tmp1, Tmp2,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001629 LD->getSrcValue(), SVOffset + IncrementSize,
1630 ExtraVT, isVolatile,
1631 MinAlign(Alignment, IncrementSize));
1632
1633 // Build a factor node to remember that this load is independent of the
1634 // other one.
Dale Johannesenca57b842009-02-02 23:46:53 +00001635 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001636 Hi.getValue(1));
1637
1638 // Move the top bits to the right place.
Dale Johannesenca57b842009-02-02 23:46:53 +00001639 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001640 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1641
1642 // Join the hi and lo parts.
Dale Johannesenca57b842009-02-02 23:46:53 +00001643 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001644 }
1645
1646 Tmp1 = LegalizeOp(Result);
1647 Tmp2 = LegalizeOp(Ch);
1648 } else {
Evan Cheng03294662008-10-14 21:26:46 +00001649 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001650 default: assert(0 && "This action is not supported yet!");
1651 case TargetLowering::Custom:
1652 isCustom = true;
1653 // FALLTHROUGH
1654 case TargetLowering::Legal:
1655 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1656 Tmp1 = Result.getValue(0);
1657 Tmp2 = Result.getValue(1);
1658
1659 if (isCustom) {
1660 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001661 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001662 Tmp1 = LegalizeOp(Tmp3);
1663 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1664 }
1665 } else {
1666 // If this is an unaligned load and the target doesn't support it,
1667 // expand it.
1668 if (!TLI.allowsUnalignedMemoryAccesses()) {
1669 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001670 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001671 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001673 TLI);
1674 Tmp1 = Result.getOperand(0);
1675 Tmp2 = Result.getOperand(1);
1676 Tmp1 = LegalizeOp(Tmp1);
1677 Tmp2 = LegalizeOp(Tmp2);
1678 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001679 }
1680 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001681 break;
1682 case TargetLowering::Expand:
1683 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1684 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dale Johannesenca57b842009-02-02 23:46:53 +00001685 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001686 LD->getSrcValueOffset(),
1687 LD->isVolatile(), LD->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00001688 Result = DAG.getNode(ISD::FP_EXTEND, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00001689 Node->getValueType(0), Load);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001690 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1691 Tmp2 = LegalizeOp(Load.getValue(1));
1692 break;
1693 }
1694 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1695 // Turn the unsupported load into an EXTLOAD followed by an explicit
1696 // zero/sign extend inreg.
Dale Johannesenca57b842009-02-02 23:46:53 +00001697 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001698 Tmp1, Tmp2, LD->getSrcValue(),
1699 LD->getSrcValueOffset(), SrcVT,
1700 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00001701 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001702 if (ExtType == ISD::SEXTLOAD)
Dale Johannesenca57b842009-02-02 23:46:53 +00001703 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1704 Result.getValueType(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001705 Result, DAG.getValueType(SrcVT));
1706 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001707 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001708 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1709 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00001710 break;
1711 }
Evan Cheng466685d2006-10-09 20:57:25 +00001712 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001713
Evan Cheng466685d2006-10-09 20:57:25 +00001714 // Since loads produce two values, make sure to remember that we legalized
1715 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001716 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1717 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001718 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001719 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001720 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001721 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001722 StoreSDNode *ST = cast<StoreSDNode>(Node);
1723 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1724 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001725 int SVOffset = ST->getSrcValueOffset();
1726 unsigned Alignment = ST->getAlignment();
1727 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001728
Evan Cheng8b2794a2006-10-13 21:14:26 +00001729 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001730 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1731 // FIXME: We shouldn't do this for TargetConstantFP's.
1732 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1733 // to phase ordering between legalized code and the dag combiner. This
1734 // probably means that we need to integrate dag combiner and legalizer
1735 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001736 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00001737 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001738 if (CFP->getValueType(0) == MVT::f32 &&
Chris Lattner3cb93512007-10-15 05:46:06 +00001739 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001740 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00001741 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001742 MVT::i32);
Dale Johannesenca57b842009-02-02 23:46:53 +00001743 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001744 SVOffset, isVolatile, Alignment);
1745 break;
1746 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001747 // If this target supports 64-bit registers, do a single 64-bit store.
1748 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00001749 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00001750 zextOrTrunc(64), MVT::i64);
Dale Johannesenca57b842009-02-02 23:46:53 +00001751 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001752 SVOffset, isVolatile, Alignment);
1753 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001754 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00001755 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
1756 // stores. If the target supports neither 32- nor 64-bits, this
1757 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00001758 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
1760 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00001761 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001762
Dale Johannesenca57b842009-02-02 23:46:53 +00001763 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(),
Chris Lattner3cb93512007-10-15 05:46:06 +00001764 SVOffset, isVolatile, Alignment);
Dale Johannesenca57b842009-02-02 23:46:53 +00001765 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00001766 DAG.getIntPtrConstant(4));
Dale Johannesenca57b842009-02-02 23:46:53 +00001767 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00001768 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00001769
Dale Johannesenca57b842009-02-02 23:46:53 +00001770 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00001771 break;
1772 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001773 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001774 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001775
Eli Friedman957bffa2009-05-24 08:42:01 +00001776 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001777 Tmp3 = LegalizeOp(ST->getValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001778 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001779 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001780
Duncan Sands83ec4b62008-06-06 12:08:01 +00001781 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001782 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1783 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001784 case TargetLowering::Legal:
1785 // If this is an unaligned store and the target doesn't support it,
1786 // expand it.
1787 if (!TLI.allowsUnalignedMemoryAccesses()) {
1788 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001789 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001790 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001791 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001792 TLI);
1793 }
1794 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001795 case TargetLowering::Custom:
1796 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001797 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001798 break;
1799 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001800 assert(VT.isVector() && "Unknown legal promote case!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001801 Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001802 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001803 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001804 ST->getSrcValue(), SVOffset, isVolatile,
1805 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001806 break;
1807 }
1808 break;
1809 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001810 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001811 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001812
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 MVT StVT = ST->getMemoryVT();
1814 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001815
Duncan Sands83ec4b62008-06-06 12:08:01 +00001816 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001817 // Promote to a byte-sized store with upper bits zero if not
1818 // storing an integral number of bytes. For example, promote
1819 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001820 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001821 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
1822 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001823 SVOffset, NVT, isVolatile, Alignment);
1824 } else if (StWidth & (StWidth - 1)) {
1825 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00001827 "Unsupported truncstore!");
1828 unsigned RoundWidth = 1 << Log2_32(StWidth);
1829 assert(RoundWidth < StWidth);
1830 unsigned ExtraWidth = StWidth - RoundWidth;
1831 assert(ExtraWidth < RoundWidth);
1832 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1833 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00001834 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
1835 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001837 unsigned IncrementSize;
1838
1839 if (TLI.isLittleEndian()) {
1840 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1841 // Store the bottom RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001842 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001843 SVOffset, RoundVT,
1844 isVolatile, Alignment);
1845
1846 // Store the remaining ExtraWidth bits.
1847 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001848 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001849 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001850 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001851 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001852 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001853 SVOffset + IncrementSize, ExtraVT, isVolatile,
1854 MinAlign(Alignment, IncrementSize));
1855 } else {
1856 // Big endian - avoid unaligned stores.
1857 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1858 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001859 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001860 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00001861 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(),
1862 SVOffset, RoundVT, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001863
1864 // Store the remaining ExtraWidth bits.
1865 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001866 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001867 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001868 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
Duncan Sands7e857202008-01-22 07:17:34 +00001869 SVOffset + IncrementSize, ExtraVT, isVolatile,
1870 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001871 }
Duncan Sands7e857202008-01-22 07:17:34 +00001872
1873 // The order of the stores doesn't matter.
Dale Johannesenca57b842009-02-02 23:46:53 +00001874 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001875 } else {
1876 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1877 Tmp2 != ST->getBasePtr())
1878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1879 ST->getOffset());
1880
1881 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
1882 default: assert(0 && "This action is not supported yet!");
1883 case TargetLowering::Legal:
1884 // If this is an unaligned store and the target doesn't support it,
1885 // expand it.
1886 if (!TLI.allowsUnalignedMemoryAccesses()) {
1887 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001888 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00001889 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00001890 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00001891 TLI);
1892 }
1893 break;
1894 case TargetLowering::Custom:
1895 Result = TLI.LowerOperation(Result, DAG);
1896 break;
1897 case Expand:
1898 // TRUNCSTORE:i16 i32 -> STORE i16
1899 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001900 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
1901 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(),
1902 SVOffset, isVolatile, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001903 break;
1904 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001905 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001906 }
1907 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001908 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001909 case ISD::SELECT:
Eli Friedman957bffa2009-05-24 08:42:01 +00001910 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001911 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001912 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001913
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Scott Michelfdc40a02009-02-17 22:15:04 +00001915
Nate Begemanb942a3d2005-08-23 04:29:48 +00001916 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001917 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001918 case TargetLowering::Legal: break;
1919 case TargetLowering::Custom: {
1920 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001921 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001922 break;
1923 }
Nate Begeman9373a812005-08-10 20:51:12 +00001924 case TargetLowering::Expand:
1925 if (Tmp1.getOpcode() == ISD::SETCC) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001926 Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
Nate Begeman9373a812005-08-10 20:51:12 +00001927 Tmp2, Tmp3,
1928 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1929 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00001930 Result = DAG.getSelectCC(dl, Tmp1,
Nate Begeman9373a812005-08-10 20:51:12 +00001931 DAG.getConstant(0, Tmp1.getValueType()),
1932 Tmp2, Tmp3, ISD::SETNE);
1933 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001934 break;
1935 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001936 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001937 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1938 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001939 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001940 ExtOp = ISD::BIT_CONVERT;
1941 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001942 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001943 ExtOp = ISD::ANY_EXTEND;
1944 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001945 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001946 ExtOp = ISD::FP_EXTEND;
1947 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001948 }
1949 // Promote each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00001950 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2);
1951 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001952 // Perform the larger operation, then round down.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001953 Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00001954 if (TruncOp != ISD::FP_ROUND)
Dale Johannesenca57b842009-02-02 23:46:53 +00001955 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00001956 else
Dale Johannesenca57b842009-02-02 23:46:53 +00001957 Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result,
Chris Lattner0bd48932008-01-17 07:00:52 +00001958 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001959 break;
1960 }
1961 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001962 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001963 case ISD::SELECT_CC: {
1964 Tmp1 = Node->getOperand(0); // LHS
1965 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001966 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1967 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue CC = Node->getOperand(4);
Scott Michelfdc40a02009-02-17 22:15:04 +00001969
1970 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
Dale Johannesenbb5da912009-02-02 20:41:04 +00001971 Tmp1, Tmp2, CC, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972
Evan Cheng7f042682008-10-15 02:05:31 +00001973 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00001974 // the LHS is a legal SETCC itself. In this case, we need to compare
1975 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00001976 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00001977 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1978 CC = DAG.getCondCode(ISD::SETNE);
1979 }
1980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1981
1982 // Everything is legal, see if we should expand this op or something.
1983 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1984 default: assert(0 && "This action is not supported yet!");
1985 case TargetLowering::Legal: break;
1986 case TargetLowering::Custom:
1987 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001988 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001989 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001990 }
1991 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001992 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001993 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001994 Tmp1 = Node->getOperand(0);
1995 Tmp2 = Node->getOperand(1);
1996 Tmp3 = Node->getOperand(2);
Dale Johannesenbb5da912009-02-02 20:41:04 +00001997 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00001998
1999 // If we had to Expand the SetCC operands into a SELECT node, then it may
2000 // not always be possible to return a true LHS & RHS. In this case, just
Nate Begeman750ac1b2006-02-01 07:19:44 +00002001 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002002 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002003 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002004 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002005 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002006
Chris Lattner73e142f2006-01-30 22:43:50 +00002007 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002008 default: assert(0 && "Cannot handle this action for SETCC yet!");
2009 case TargetLowering::Custom:
2010 isCustom = true;
2011 // FALLTHROUGH.
2012 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002014 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002015 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002016 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002017 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002018 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002019 case TargetLowering::Promote: {
2020 // First step, figure out the appropriate operation to use.
2021 // Allow SETCC to not be supported for all legal data types
2022 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002023 MVT NewInTy = Node->getOperand(0).getValueType();
2024 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002025
2026 // Scan for the appropriate larger type to use.
2027 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002028 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002029
Duncan Sands83ec4b62008-06-06 12:08:01 +00002030 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002031 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002032 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002033 "Fell off of the edge of the floating point world");
Scott Michelfdc40a02009-02-17 22:15:04 +00002034
Andrew Lenharthae355752005-11-30 17:12:26 +00002035 // If the target supports SETCC of this type, use it.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002036 if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002037 break;
2038 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002039 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002040 assert(0 && "Cannot promote Legal Integer SETCC yet");
2041 else {
Dale Johannesenca57b842009-02-02 23:46:53 +00002042 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1);
2043 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2);
Andrew Lenharthae355752005-11-30 17:12:26 +00002044 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002045 Tmp1 = LegalizeOp(Tmp1);
2046 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002048 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002049 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002050 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002051 case TargetLowering::Expand:
2052 // Expand a setcc node into a select_cc of the same condition, lhs, and
2053 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002054 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002055 Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
Nate Begemanb942a3d2005-08-23 04:29:48 +00002056 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002057 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002058 break;
2059 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002060 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002061
2062 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002063 case ISD::ADD:
2064 case ISD::SUB:
2065 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002066 case ISD::MULHS:
2067 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002068 case ISD::UDIV:
2069 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002070 case ISD::AND:
2071 case ISD::OR:
2072 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002073 case ISD::SHL:
2074 case ISD::SRL:
2075 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002076 case ISD::FADD:
2077 case ISD::FSUB:
2078 case ISD::FMUL:
2079 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002080 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002081 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Duncan Sands92abc622009-01-31 15:50:11 +00002082 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002083
2084 if ((Node->getOpcode() == ISD::SHL ||
2085 Node->getOpcode() == ISD::SRL ||
2086 Node->getOpcode() == ISD::SRA) &&
Duncan Sands92abc622009-01-31 15:50:11 +00002087 !Node->getValueType(0).isVector())
2088 Tmp2 = DAG.getShiftAmountOperand(Tmp2);
2089
Eli Friedman957bffa2009-05-24 08:42:01 +00002090 Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS.
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002091
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002093
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002094 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002095 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002096 case TargetLowering::Legal: break;
2097 case TargetLowering::Custom:
2098 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002099 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002100 Result = Tmp1;
2101 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00002102 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00002103 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002104 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002105 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002106
Dan Gohman525178c2007-10-08 18:33:35 +00002107 // See if multiply or divide can be lowered using two-result operations.
2108 SDVTList VTs = DAG.getVTList(VT, VT);
2109 if (Node->getOpcode() == ISD::MUL) {
2110 // We just need the low half of the multiply; try both the signed
2111 // and unsigned forms. If the target supports both SMUL_LOHI and
2112 // UMUL_LOHI, form a preference by checking which forms of plain
2113 // MULH it supports.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002114 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
2115 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
2116 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
2117 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
Dan Gohman525178c2007-10-08 18:33:35 +00002118 unsigned OpToUse = 0;
2119 if (HasSMUL_LOHI && !HasMULHS) {
2120 OpToUse = ISD::SMUL_LOHI;
2121 } else if (HasUMUL_LOHI && !HasMULHU) {
2122 OpToUse = ISD::UMUL_LOHI;
2123 } else if (HasSMUL_LOHI) {
2124 OpToUse = ISD::SMUL_LOHI;
2125 } else if (HasUMUL_LOHI) {
2126 OpToUse = ISD::UMUL_LOHI;
2127 }
2128 if (OpToUse) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002129 Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002130 break;
2131 }
2132 }
2133 if (Node->getOpcode() == ISD::MULHS &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002134 TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002135 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002136 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002137 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002138 break;
2139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002140 if (Node->getOpcode() == ISD::MULHU &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002141 TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002142 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl,
2143 VTs, Tmp1, Tmp2).getNode(),
Chris Lattner31d71612008-10-04 21:27:46 +00002144 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002145 break;
2146 }
2147 if (Node->getOpcode() == ISD::SDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002148 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002149 Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002150 break;
2151 }
2152 if (Node->getOpcode() == ISD::UDIV &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002153 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Duncan Sandsa9cad0e2009-05-06 11:29:50 +00002154 Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2);
2155 break;
2156 }
2157 if (Node->getOpcode() == ISD::SUB &&
2158 TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
2159 TLI.isOperationLegalOrCustom(ISD::XOR, VT)) {
2160 Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2,
2161 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
2162 Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
2163 Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2);
Dan Gohman525178c2007-10-08 18:33:35 +00002164 break;
2165 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00002166
Dan Gohman82669522007-10-11 23:57:53 +00002167 // Check to see if we have a libcall for this operator.
2168 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2169 bool isSigned = false;
2170 switch (Node->getOpcode()) {
2171 case ISD::UDIV:
2172 case ISD::SDIV:
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002173 isSigned = Node->getOpcode() == ISD::SDIV;
2174 if (VT == MVT::i16)
2175 LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16);
2176 else if (VT == MVT::i32)
2177 LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32);
2178 else if (VT == MVT::i64)
2179 LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64);
2180 else if (VT == MVT::i128)
2181 LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128);
2182 break;
Chris Lattner31d71612008-10-04 21:27:46 +00002183 case ISD::MUL:
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002184 if (VT == MVT::i16)
2185 LC = RTLIB::MUL_I16;
Anton Korobeynikov813090c2009-05-03 13:18:16 +00002186 else if (VT == MVT::i32)
Chris Lattner31d71612008-10-04 21:27:46 +00002187 LC = RTLIB::MUL_I32;
Nate Begeman9b994852009-01-24 22:12:48 +00002188 else if (VT == MVT::i64)
Scott Michel845145f2008-12-29 03:21:37 +00002189 LC = RTLIB::MUL_I64;
Anton Korobeynikov2f25c2c2009-05-03 13:13:51 +00002190 else if (VT == MVT::i128)
2191 LC = RTLIB::MUL_I128;
Chris Lattner31d71612008-10-04 21:27:46 +00002192 break;
Dan Gohman82669522007-10-11 23:57:53 +00002193 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002194 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2195 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002196 break;
Scott Micheld1e8d9c2009-01-21 04:58:48 +00002197 case ISD::FDIV:
2198 LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80,
2199 RTLIB::DIV_PPCF128);
2200 break;
Dan Gohman82669522007-10-11 23:57:53 +00002201 default: break;
2202 }
2203 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00002204 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002205 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002206 break;
2207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002208
Eli Friedman74807f22009-05-26 08:55:52 +00002209 assert(0 && "Cannot expand this binary operator!");
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002210 break;
2211 }
Evan Chengcc987612006-04-12 21:20:24 +00002212 case TargetLowering::Promote: {
2213 switch (Node->getOpcode()) {
2214 default: assert(0 && "Do not know how to promote this BinOp!");
2215 case ISD::AND:
2216 case ISD::OR:
2217 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002218 MVT OVT = Node->getValueType(0);
2219 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2220 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00002221 // Bit convert each of the values to the new type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002222 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1);
2223 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2);
2224 Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
Evan Chengcc987612006-04-12 21:20:24 +00002225 // Bit convert the result back the original type.
Dale Johannesenca57b842009-02-02 23:46:53 +00002226 Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result);
Evan Chengcc987612006-04-12 21:20:24 +00002227 break;
2228 }
2229 }
2230 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002231 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002232 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002233 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2234 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Eli Friedman957bffa2009-05-24 08:42:01 +00002235 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
Scott Michelfdc40a02009-02-17 22:15:04 +00002236
Chris Lattnera09f8482006-03-05 05:09:38 +00002237 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002238
Chris Lattnera09f8482006-03-05 05:09:38 +00002239 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2240 default: assert(0 && "Operation not supported");
2241 case TargetLowering::Custom:
2242 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002243 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002244 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002245 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002246 case TargetLowering::Expand: {
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002247 assert((Tmp2.getValueType() == MVT::f32 ||
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002248 Tmp2.getValueType() == MVT::f64) &&
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002249 "Ugly special-cased code!");
2250 // Get the sign bit of the RHS.
Eli Friedman57f1a4b2009-05-24 20:29:11 +00002251 SDValue SignBit;
2252 MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32;
2253 if (isTypeLegal(IVT)) {
2254 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2);
2255 } else {
2256 assert(isTypeLegal(TLI.getPointerTy()) &&
2257 (TLI.getPointerTy() == MVT::i32 ||
2258 TLI.getPointerTy() == MVT::i64) &&
2259 "Legal type for load?!");
2260 SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType());
2261 SDValue StorePtr = StackPtr, LoadPtr = StackPtr;
2262 SDValue Ch =
2263 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0);
2264 if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian())
2265 LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(),
2266 LoadPtr, DAG.getIntPtrConstant(4));
2267 SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(),
2268 Ch, LoadPtr, NULL, 0, MVT::i32);
2269 }
2270 SignBit =
2271 DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
2272 SignBit, DAG.getConstant(0, SignBit.getValueType()),
2273 ISD::SETLT);
Eli Friedmaned2f8c52009-05-24 10:21:20 +00002274 // Get the absolute value of the result.
2275 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
2276 // Select between the nabs and abs value based on the sign bit of
2277 // the input.
2278 Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
2279 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(),
2280 AbsVal),
2281 AbsVal);
Evan Cheng912095b2007-01-04 21:56:39 +00002282 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002283 break;
2284 }
Evan Cheng912095b2007-01-04 21:56:39 +00002285 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002286 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002287 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002288 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00002289 // TODO: handle the case where the Lo and Hi operands are not of legal type
2290 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2291 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2292 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002293 case TargetLowering::Promote:
2294 case TargetLowering::Custom:
2295 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002296 case TargetLowering::Legal:
2297 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Dale Johannesenca57b842009-02-02 23:46:53 +00002298 Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002299 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002300 case TargetLowering::Expand:
Dale Johannesenca57b842009-02-02 23:46:53 +00002301 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1);
2302 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2);
2303 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002304 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00002305 TLI.getShiftAmountTy()));
Dale Johannesenca57b842009-02-02 23:46:53 +00002306 Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002307 break;
2308 }
2309 break;
2310 }
2311
Nate Begemanc105e192005-04-06 00:23:54 +00002312 case ISD::UREM:
2313 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002314 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002315 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2316 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002317
Nate Begemanc105e192005-04-06 00:23:54 +00002318 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002319 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2320 case TargetLowering::Custom:
2321 isCustom = true;
2322 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002323 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002324 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002325 if (isCustom) {
2326 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002327 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002328 }
Nate Begemanc105e192005-04-06 00:23:54 +00002329 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002330 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002331 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002332 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002333 MVT VT = Node->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002334
Dan Gohman525178c2007-10-08 18:33:35 +00002335 // See if remainder can be lowered using two-result operations.
2336 SDVTList VTs = DAG.getVTList(VT, VT);
2337 if (Node->getOpcode() == ISD::SREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002338 TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002339 Result = SDValue(DAG.getNode(ISD::SDIVREM, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002340 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002341 break;
2342 }
2343 if (Node->getOpcode() == ISD::UREM &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002344 TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) {
Dale Johannesenca57b842009-02-02 23:46:53 +00002345 Result = SDValue(DAG.getNode(ISD::UDIVREM, dl,
2346 VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00002347 break;
2348 }
2349
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002350 if (VT.isInteger() &&
2351 TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) {
2352 // X % Y -> X-X/Y*Y
2353 Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2);
2354 Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2);
2355 Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result);
2356 break;
Nate Begemanc105e192005-04-06 00:23:54 +00002357 }
Anton Korobeynikov58c04e12009-05-08 18:51:08 +00002358
2359 // Check to see if we have a libcall for this operator.
2360 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2361 switch (Node->getOpcode()) {
2362 default: break;
2363 case ISD::UREM:
2364 case ISD::SREM:
2365 if (VT == MVT::i16)
2366 LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16);
2367 else if (VT == MVT::i32)
2368 LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32);
2369 else if (VT == MVT::i64)
2370 LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64);
2371 else if (VT == MVT::i128)
2372 LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128);
2373 break;
2374 case ISD::FREM:
2375 // Floating point mod -> fmod libcall.
2376 LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
2377 RTLIB::REM_F80, RTLIB::REM_PPCF128);
2378 break;
2379 }
2380
2381 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2382 SDValue Dummy;
2383 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
2384 break;
2385 }
2386
Eli Friedman74807f22009-05-26 08:55:52 +00002387 assert(0 && "Cannot expand this binary operator!");
Nate Begemanc105e192005-04-06 00:23:54 +00002388 break;
2389 }
Dan Gohman525178c2007-10-08 18:33:35 +00002390 }
Nate Begemanc105e192005-04-06 00:23:54 +00002391 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002392 case ISD::VAARG: {
2393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2395
Duncan Sands83ec4b62008-06-06 12:08:01 +00002396 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002397 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2398 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002399 case TargetLowering::Custom:
2400 isCustom = true;
2401 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002402 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2404 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002405 Tmp1 = Result.getValue(1);
2406
2407 if (isCustom) {
2408 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002409 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002410 Result = LegalizeOp(Tmp2);
2411 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2412 }
2413 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002414 break;
2415 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00002416 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesenca57b842009-02-02 23:46:53 +00002417 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002418 // Increment the pointer, VAList, to the next vaarg
Dale Johannesenca57b842009-02-02 23:46:53 +00002419 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002420 DAG.getConstant(TLI.getTargetData()->
Duncan Sands777d2302009-05-09 07:06:46 +00002421 getTypeAllocSize(VT.getTypeForMVT()),
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002422 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00002423 // Store the incremented VAList to the legalized pointer
Dale Johannesenca57b842009-02-02 23:46:53 +00002424 Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002425 // Load the actual argument out of the pointer VAList
Dale Johannesenca57b842009-02-02 23:46:53 +00002426 Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002427 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002428 Result = LegalizeOp(Result);
2429 break;
2430 }
2431 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002432 // Since VAARG produces two values, make sure to remember that we
Nate Begemanacc398c2006-01-25 18:21:52 +00002433 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002434 AddLegalizedOperand(SDValue(Node, 0), Result);
2435 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002436 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002437 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002438 // Unary operators
2439 case ISD::FABS:
2440 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002441 case ISD::FSQRT:
2442 case ISD::FSIN:
2443 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002444 case ISD::FLOG:
2445 case ISD::FLOG2:
2446 case ISD::FLOG10:
2447 case ISD::FEXP:
2448 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00002449 case ISD::FTRUNC:
2450 case ISD::FFLOOR:
2451 case ISD::FCEIL:
2452 case ISD::FRINT:
2453 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002454 Tmp1 = LegalizeOp(Node->getOperand(0));
2455 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002456 case TargetLowering::Promote:
2457 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002458 isCustom = true;
2459 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002460 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002461 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002462 if (isCustom) {
2463 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002464 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00002465 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002466 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002467 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002468 switch (Node->getOpcode()) {
2469 default: assert(0 && "Unreachable!");
2470 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002471 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2472 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Dale Johannesenca57b842009-02-02 23:46:53 +00002473 Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002474 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002475 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002476 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002477 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002478 Tmp2 = DAG.getConstantFP(0.0, VT);
Dale Johannesenca57b842009-02-02 23:46:53 +00002479 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00002480 Tmp1, Tmp2, ISD::SETUGT);
Dale Johannesenca57b842009-02-02 23:46:53 +00002481 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2482 Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002483 break;
2484 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00002485 case ISD::FSQRT:
2486 case ISD::FSIN:
Scott Michelfdc40a02009-02-17 22:15:04 +00002487 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002488 case ISD::FLOG:
2489 case ISD::FLOG2:
2490 case ISD::FLOG10:
2491 case ISD::FEXP:
2492 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00002493 case ISD::FTRUNC:
2494 case ISD::FFLOOR:
2495 case ISD::FCEIL:
2496 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00002497 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002498 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00002499
Eli Friedman74807f22009-05-26 08:55:52 +00002500 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00002501
Evan Cheng56966222007-01-12 02:11:51 +00002502 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002503 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002504 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00002505 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2506 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002507 break;
2508 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00002509 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
2510 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002511 break;
2512 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00002513 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
2514 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00002515 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00002516 case ISD::FLOG:
2517 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
2518 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
2519 break;
2520 case ISD::FLOG2:
2521 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2522 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
2523 break;
2524 case ISD::FLOG10:
2525 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2526 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
2527 break;
2528 case ISD::FEXP:
2529 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
2530 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
2531 break;
2532 case ISD::FEXP2:
2533 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2534 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
2535 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00002536 case ISD::FTRUNC:
2537 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2538 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
2539 break;
2540 case ISD::FFLOOR:
2541 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2542 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
2543 break;
2544 case ISD::FCEIL:
2545 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2546 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
2547 break;
2548 case ISD::FRINT:
2549 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
2550 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
2551 break;
2552 case ISD::FNEARBYINT:
2553 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
2554 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
2555 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00002556 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002557 default: assert(0 && "Unreachable!");
2558 }
Dan Gohman475871a2008-07-27 21:46:04 +00002559 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002560 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002561 break;
2562 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002563 }
2564 break;
2565 }
2566 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002567 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00002569
2570 // Expand unsupported unary vector operators by unrolling them.
Eli Friedman74807f22009-05-26 08:55:52 +00002571 assert(!VT.isVector() && "Vector shouldn't get here!");
Dan Gohman82669522007-10-11 23:57:53 +00002572
2573 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00002574 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
2575 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00002576 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00002577 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002578 break;
2579 }
Bill Wendling74c37652008-12-09 22:08:41 +00002580 case ISD::SADDO:
2581 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002582 MVT VT = Node->getValueType(0);
2583 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2584 default: assert(0 && "This action not supported for this op yet!");
2585 case TargetLowering::Custom:
2586 Result = TLI.LowerOperation(Op, DAG);
2587 if (Result.getNode()) break;
2588 // FALLTHROUGH
2589 case TargetLowering::Legal: {
2590 SDValue LHS = LegalizeOp(Node->getOperand(0));
2591 SDValue RHS = LegalizeOp(Node->getOperand(1));
2592
Scott Michelfdc40a02009-02-17 22:15:04 +00002593 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002594 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002595 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002596 MVT OType = Node->getValueType(1);
2597
Bill Wendlinga6af91a2008-11-25 08:19:22 +00002598 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002599
Bill Wendling740464e2008-11-25 19:40:17 +00002600 // LHSSign -> LHS >= 0
2601 // RHSSign -> RHS >= 0
2602 // SumSign -> Sum >= 0
2603 //
Bill Wendling74c37652008-12-09 22:08:41 +00002604 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00002605 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00002606 // Sub:
2607 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00002608 //
Dale Johannesenca57b842009-02-02 23:46:53 +00002609 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
2610 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
Scott Michelfdc40a02009-02-17 22:15:04 +00002611 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
2612 Node->getOpcode() == ISD::SADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002613 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002614
Dale Johannesenca57b842009-02-02 23:46:53 +00002615 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
2616 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002617
Dale Johannesenca57b842009-02-02 23:46:53 +00002618 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002619
2620 MVT ValueVTs[] = { LHS.getValueType(), OType };
2621 SDValue Ops[] = { Sum, Cmp };
2622
Scott Michelfdc40a02009-02-17 22:15:04 +00002623 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002624 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002625 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002626 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002627 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00002628 break;
2629 }
2630 }
2631
2632 break;
2633 }
Bill Wendling74c37652008-12-09 22:08:41 +00002634 case ISD::UADDO:
2635 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00002636 MVT VT = Node->getValueType(0);
2637 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2638 default: assert(0 && "This action not supported for this op yet!");
2639 case TargetLowering::Custom:
2640 Result = TLI.LowerOperation(Op, DAG);
2641 if (Result.getNode()) break;
2642 // FALLTHROUGH
2643 case TargetLowering::Legal: {
2644 SDValue LHS = LegalizeOp(Node->getOperand(0));
2645 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002646
Bill Wendling74c37652008-12-09 22:08:41 +00002647 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
Dale Johannesenca57b842009-02-02 23:46:53 +00002648 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
Bill Wendling74c37652008-12-09 22:08:41 +00002649 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002650 MVT OType = Node->getValueType(1);
Dale Johannesenca57b842009-02-02 23:46:53 +00002651 SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00002652 Node->getOpcode () == ISD::UADDO ?
Bill Wendling74c37652008-12-09 22:08:41 +00002653 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002654
Bill Wendling41ea7e72008-11-24 19:21:46 +00002655 MVT ValueVTs[] = { LHS.getValueType(), OType };
2656 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002657
Scott Michelfdc40a02009-02-17 22:15:04 +00002658 Result = DAG.getNode(ISD::MERGE_VALUES, dl,
Dale Johannesenca57b842009-02-02 23:46:53 +00002659 DAG.getVTList(&ValueVTs[0], 2),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002660 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002661 SDNode *RNode = Result.getNode();
Dan Gohmanc23e4962009-04-15 20:06:30 +00002662 DAG.ReplaceAllUsesWith(Node, RNode);
Bill Wendling41ea7e72008-11-24 19:21:46 +00002663 break;
2664 }
2665 }
2666
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00002667 break;
2668 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002669 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002670
Chris Lattner4ddd2832006-04-08 04:13:17 +00002671 assert(Result.getValueType() == Op.getValueType() &&
2672 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002673
Chris Lattner456a93a2006-01-28 07:39:30 +00002674 // Make sure that the generated code is itself legal.
2675 if (Result != Op)
2676 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002677
Chris Lattner45982da2005-05-12 16:53:42 +00002678 // Note that LegalizeOp may be reentered even from single-use nodes, which
2679 // means that we always must cache transformed nodes.
2680 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002681 return Result;
2682}
2683
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002684SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
2685 SDValue Vec = Op.getOperand(0);
2686 SDValue Idx = Op.getOperand(1);
2687 DebugLoc dl = Op.getDebugLoc();
2688 // Store the value to a temporary stack slot, then LOAD the returned part.
2689 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
2690 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0);
2691
2692 // Add the offset to the index.
Eli Friedman2a35b1c2009-05-23 23:03:28 +00002693 unsigned EltSize =
2694 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00002695 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
2696 DAG.getConstant(EltSize, Idx.getValueType()));
2697
2698 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
2699 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
2700 else
2701 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
2702
2703 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
2704
2705 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0);
2706}
2707
Nate Begeman750ac1b2006-02-01 07:19:44 +00002708/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2709/// with condition CC on the current target. This usually involves legalizing
2710/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2711/// there may be no choice but to create a new SetCC node to represent the
2712/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00002713/// LHS, and the SDValue returned in RHS has a nil SDNode value.
2714void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
2715 SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002716 SDValue &CC,
2717 DebugLoc dl) {
Eli Friedman74807f22009-05-26 08:55:52 +00002718 LHS = LegalizeOp(LHS);
2719 RHS = LegalizeOp(RHS);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002720}
2721
Evan Cheng7f042682008-10-15 02:05:31 +00002722/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
2723/// condition code CC on the current target. This routine assumes LHS and rHS
2724/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
2725/// illegal condition code into AND / OR of multiple SETCC values.
2726void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
2727 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00002728 SDValue &CC,
2729 DebugLoc dl) {
Evan Cheng7f042682008-10-15 02:05:31 +00002730 MVT OpVT = LHS.getValueType();
2731 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
2732 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
2733 default: assert(0 && "Unknown condition code action!");
2734 case TargetLowering::Legal:
2735 // Nothing to do.
2736 break;
2737 case TargetLowering::Expand: {
2738 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
2739 unsigned Opc = 0;
2740 switch (CCCode) {
2741 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00002742 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
2743 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2744 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2745 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
2746 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2747 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
2748 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2749 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2750 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2751 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2752 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
2753 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00002754 // FIXME: Implement more expansions.
2755 }
2756
Dale Johannesenbb5da912009-02-02 20:41:04 +00002757 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
2758 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
2759 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00002760 RHS = SDValue();
2761 CC = SDValue();
2762 break;
2763 }
2764 }
2765}
2766
Chris Lattner1401d152008-01-16 07:45:30 +00002767/// EmitStackConvert - Emit a store/load combination to the stack. This stores
2768/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
2769/// a load from the stack slot to DestVT, extending it if needed.
2770/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00002771SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
2772 MVT SlotVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002773 MVT DestVT,
2774 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00002775 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002776 unsigned SrcAlign =
2777 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
2778 getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00002779 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002780
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002781 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002782 int SPFI = StackPtrFI->getIndex();
Dan Gohman15b38302009-01-29 21:02:43 +00002783 const Value *SV = PseudoSourceValue::getFixedStack(SPFI);
2784
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
2786 unsigned SlotSize = SlotVT.getSizeInBits();
2787 unsigned DestSize = DestVT.getSizeInBits();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002788 unsigned DestAlign =
2789 TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT());
Scott Michelfdc40a02009-02-17 22:15:04 +00002790
Chris Lattner1401d152008-01-16 07:45:30 +00002791 // Emit a store to the stack slot. Use a truncstore if the input value is
2792 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00002793 SDValue Store;
Scott Michelfdc40a02009-02-17 22:15:04 +00002794
Chris Lattner1401d152008-01-16 07:45:30 +00002795 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002796 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002797 SV, 0, SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002798 else {
2799 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002800 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Dan Gohman15b38302009-01-29 21:02:43 +00002801 SV, 0, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00002802 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002803
Chris Lattner35481892005-12-23 00:16:34 +00002804 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00002805 if (SlotSize == DestSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00002806 return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00002807
Chris Lattner1401d152008-01-16 07:45:30 +00002808 assert(SlotSize < DestSize && "Unknown extension!");
Dale Johannesen8a782a22009-02-02 22:12:50 +00002809 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT,
Mon P Wang364d73d2008-07-05 20:40:31 +00002810 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00002811}
2812
Dan Gohman475871a2008-07-27 21:46:04 +00002813SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00002814 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00002815 // Create a vector sized/aligned stack slot, store the value to element #0,
2816 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00002817 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00002818
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00002819 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00002820 int SPFI = StackPtrFI->getIndex();
2821
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002822 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
2823 StackPtr,
2824 PseudoSourceValue::getFixedStack(SPFI), 0,
2825 Node->getValueType(0).getVectorElementType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002826 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00002827 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00002828}
2829
2830
Chris Lattnerce872152006-03-19 06:31:19 +00002831/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00002832/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00002833SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002834 unsigned NumElems = Node->getNumOperands();
2835 SDValue SplatValue = Node->getOperand(0);
2836 DebugLoc dl = Node->getDebugLoc();
2837 MVT VT = Node->getValueType(0);
2838 MVT OpVT = SplatValue.getValueType();
2839 MVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00002840
2841 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00002842 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00002843 bool isOnlyLowElement = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00002844
Dan Gohman475871a2008-07-27 21:46:04 +00002845 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002846 // and use a bitmask instead of a list of elements.
Nate Begeman9008ca62009-04-27 18:41:29 +00002847 // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat.
Dan Gohman475871a2008-07-27 21:46:04 +00002848 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00002849 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002850 bool isConstant = true;
2851 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
2852 SplatValue.getOpcode() != ISD::UNDEF)
2853 isConstant = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00002854
Evan Cheng033e6812006-03-24 01:17:21 +00002855 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002856 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00002857 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00002858 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00002859 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00002860 if (SplatValue != V)
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002861 SplatValue = SDValue(0, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00002862
2863 // If this isn't a constant element or an undef, we can't use a constant
2864 // pool load.
2865 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
2866 V.getOpcode() != ISD::UNDEF)
2867 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00002868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002869
Chris Lattnerce872152006-03-19 06:31:19 +00002870 if (isOnlyLowElement) {
2871 // If the low element is an undef too, then this whole things is an undef.
2872 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002873 return DAG.getUNDEF(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00002874 // Otherwise, turn this into a scalar_to_vector node.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002875 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Chris Lattnerce872152006-03-19 06:31:19 +00002876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002877
Chris Lattner2eb86532006-03-24 07:29:17 +00002878 // If all elements are constants, create a load from the constant pool.
2879 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00002880 std::vector<Constant*> CV;
2881 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002882 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00002883 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002884 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002885 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002886 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00002887 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00002888 } else {
2889 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002890 const Type *OpNTy = OpVT.getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00002891 CV.push_back(UndefValue::get(OpNTy));
2892 }
2893 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00002894 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00002895 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00002896 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002897 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00002898 PseudoSourceValue::getConstantPool(), 0,
2899 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00002900 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002901
Gabor Greifba36cb52008-08-28 21:40:38 +00002902 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00002903 // Build the shuffle constant vector: <0, 0, 0, 0>
Nate Begeman9008ca62009-04-27 18:41:29 +00002904 SmallVector<int, 8> ZeroVec(NumElems, 0);
Chris Lattner87100e02006-03-20 01:52:29 +00002905
2906 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Nate Begeman9008ca62009-04-27 18:41:29 +00002907 if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002908 // Get the splatted value into the low element of a vector register.
Scott Michelfdc40a02009-02-17 22:15:04 +00002909 SDValue LowValVec =
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002910 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002911
Chris Lattner87100e02006-03-20 01:52:29 +00002912 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT),
2914 &ZeroVec[0]);
Chris Lattner87100e02006-03-20 01:52:29 +00002915 }
2916 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002917
Evan Cheng033e6812006-03-24 01:17:21 +00002918 // If there are only two unique elements, we may be able to turn this into a
2919 // vector shuffle.
2920 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002921 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Val1 = Node->getOperand(1);
2923 SDValue Val2;
2924 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002925 if (MI->first != Val1)
2926 Val2 = MI->first;
2927 else
2928 Val2 = (++MI)->first;
Scott Michelfdc40a02009-02-17 22:15:04 +00002929
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002930 // If Val1 is an undef, make sure it ends up as Val2, to ensure that our
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002931 // vector shuffle has the undef vector on the RHS.
2932 if (Val1.getOpcode() == ISD::UNDEF)
2933 std::swap(Val1, Val2);
Scott Michelfdc40a02009-02-17 22:15:04 +00002934
Evan Cheng033e6812006-03-24 01:17:21 +00002935 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Nate Begeman9008ca62009-04-27 18:41:29 +00002936 SmallVector<int, 8> ShuffleMask(NumElems, -1);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002937
2938 // Set elements of the shuffle mask for Val1.
2939 std::vector<unsigned> &Val1Elts = Values[Val1];
2940 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002941 ShuffleMask[Val1Elts[i]] = 0;
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002942
2943 // Set elements of the shuffle mask for Val2.
2944 std::vector<unsigned> &Val2Elts = Values[Val2];
2945 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
2946 if (Val2.getOpcode() != ISD::UNDEF)
Nate Begeman9008ca62009-04-27 18:41:29 +00002947 ShuffleMask[Val2Elts[i]] = NumElems;
Evan Cheng033e6812006-03-24 01:17:21 +00002948
Chris Lattnerf9d95c82008-03-09 00:29:42 +00002949 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002950 if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) &&
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 TLI.isShuffleMaskLegal(ShuffleMask, VT)) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002952 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1);
2953 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2);
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]);
Evan Cheng033e6812006-03-24 01:17:21 +00002955 }
2956 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002957
Chris Lattnerce872152006-03-19 06:31:19 +00002958 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
2959 // aligned object on the stack, store each element into it, then load
2960 // the result as a vector.
Chris Lattnerce872152006-03-19 06:31:19 +00002961 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00002962 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Dan Gohman15b38302009-01-29 21:02:43 +00002963 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
2964 const Value *SV = PseudoSourceValue::getFixedStack(FI);
2965
Chris Lattnerce872152006-03-19 06:31:19 +00002966 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00002967 SmallVector<SDValue, 8> Stores;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00002968 unsigned TypeByteSize = OpVT.getSizeInBits() / 8;
Chris Lattnerce872152006-03-19 06:31:19 +00002969 // Store (in the right endianness) the elements to memory.
2970 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2971 // Ignore undef elements.
2972 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00002973
Chris Lattner841c8822006-03-22 01:46:54 +00002974 unsigned Offset = TypeByteSize*i;
Scott Michelfdc40a02009-02-17 22:15:04 +00002975
Dan Gohman475871a2008-07-27 21:46:04 +00002976 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Dale Johannesen8a782a22009-02-02 22:12:50 +00002977 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00002978
Dale Johannesen8a782a22009-02-02 22:12:50 +00002979 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
2980 Idx, SV, Offset));
Chris Lattnerce872152006-03-19 06:31:19 +00002981 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002982
Dan Gohman475871a2008-07-27 21:46:04 +00002983 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00002984 if (!Stores.empty()) // Not all undef elements?
Dale Johannesen8a782a22009-02-02 22:12:50 +00002985 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002986 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00002987 else
2988 StoreChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002989
Chris Lattnerce872152006-03-19 06:31:19 +00002990 // Result is a load from the stack slot.
Dale Johannesen8a782a22009-02-02 22:12:50 +00002991 return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00002992}
2993
Chris Lattner77e77a62005-01-21 06:05:23 +00002994// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2995// does not fit into a register, return the lo part and set the hi part to the
2996// by-reg argument. If it does fit into a single register, return the result
2997// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002998SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2999 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003000 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003001 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00003002 // Legalizing the call will automatically add the previous call to the
3003 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00003004 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003005
Chris Lattner77e77a62005-01-21 06:05:23 +00003006 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00003007 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00003008 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003009 MVT ArgVT = Node->getOperand(i).getValueType();
3010 const Type *ArgTy = ArgVT.getTypeForMVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00003011 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00003012 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00003013 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00003014 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00003015 }
Bill Wendling056292f2008-09-16 21:48:12 +00003016 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00003017 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003018
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003019 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003020 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003021 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00003022 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003023 CallingConv::C, false, Callee, Args, DAG,
3024 Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003025
Chris Lattner6831a812006-02-13 09:18:02 +00003026 // Legalize the call sequence, starting with the chain. This will advance
3027 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3028 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3029 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00003030 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003031}
3032
Chris Lattner22cde6a2006-01-28 08:25:58 +00003033/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3034/// INT_TO_FP operation of the specified operand when the target requests that
3035/// we expand it. At this point, we know that the result and operand types are
3036/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00003037SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3038 SDValue Op0,
Dale Johannesenaf435272009-02-02 19:03:57 +00003039 MVT DestVT,
3040 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003041 if (Op0.getValueType() == MVT::i32) {
3042 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00003043
Chris Lattner23594d42008-01-16 07:03:22 +00003044 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00003045 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00003046
Chris Lattner22cde6a2006-01-28 08:25:58 +00003047 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00003049 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00003050 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00003051 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003052 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00003053 if (TLI.isLittleEndian())
3054 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00003055
Chris Lattner22cde6a2006-01-28 08:25:58 +00003056 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00003057 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003058 if (isSigned) {
3059 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00003060 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Dale Johannesenaf435272009-02-02 19:03:57 +00003061 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003062 } else {
3063 Op0Mapped = Op0;
3064 }
3065 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00003066 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003067 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003068 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00003069 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003070 // store the hi of the constructed double - biased exponent
Dale Johannesenaf435272009-02-02 19:03:57 +00003071 SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003072 // load the constructed double
Dale Johannesenaf435272009-02-02 19:03:57 +00003073 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003074 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00003075 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00003076 BitsToDouble(0x4330000080000000ULL) :
3077 BitsToDouble(0x4330000000000000ULL),
Chris Lattner22cde6a2006-01-28 08:25:58 +00003078 MVT::f64);
3079 // subtract the bias
Dale Johannesenaf435272009-02-02 19:03:57 +00003080 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003081 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00003082 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003083 // handle final rounding
3084 if (DestVT == MVT::f64) {
3085 // do nothing
3086 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003087 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00003088 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00003089 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003090 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00003091 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003092 }
3093 return Result;
3094 }
3095 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesenaf435272009-02-02 19:03:57 +00003096 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003097
Dale Johannesenaf435272009-02-02 19:03:57 +00003098 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00003099 Op0, DAG.getConstant(0, Op0.getValueType()),
3100 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00003101 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Dale Johannesenaf435272009-02-02 19:03:57 +00003102 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00003103 SignSet, Four, Zero);
3104
3105 // If the sign bit of the integer is set, the large number will be treated
3106 // as a negative number. To counteract this, the dynamic code adds an
3107 // offset depending on the data type.
3108 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003109 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003110 default: assert(0 && "Unsupported integer type!");
3111 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3112 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3113 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3114 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3115 }
3116 if (TLI.isLittleEndian()) FF <<= 32;
Chris Lattnerd2e936a2009-03-08 01:47:41 +00003117 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003118
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00003120 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenaf435272009-02-02 19:03:57 +00003121 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00003122 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003124 if (DestVT == MVT::f32)
Dale Johannesenaf435272009-02-02 19:03:57 +00003125 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00003126 PseudoSourceValue::getConstantPool(), 0,
3127 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003128 else {
Dan Gohman69de1932008-02-06 22:27:42 +00003129 FudgeInReg =
Dale Johannesenaf435272009-02-02 19:03:57 +00003130 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohman69de1932008-02-06 22:27:42 +00003131 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00003132 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00003133 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003134 }
3135
Dale Johannesenaf435272009-02-02 19:03:57 +00003136 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003137}
3138
3139/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3140/// *INT_TO_FP operation of the specified operand when the target requests that
3141/// we promote it. At this point, we know that the result and operand types are
3142/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3143/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00003144SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
3145 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003146 bool isSigned,
3147 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003148 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003150
3151 unsigned OpToUse = 0;
3152
3153 // Scan for the appropriate larger type to use.
3154 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003155 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
3156 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003157
3158 // If the target supports SINT_TO_FP of this type, use it.
3159 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3160 default: break;
3161 case TargetLowering::Legal:
3162 if (!TLI.isTypeLegal(NewInTy))
3163 break; // Can't use this datatype.
3164 // FALL THROUGH.
3165 case TargetLowering::Custom:
3166 OpToUse = ISD::SINT_TO_FP;
3167 break;
3168 }
3169 if (OpToUse) break;
3170 if (isSigned) continue;
3171
3172 // If the target supports UINT_TO_FP of this type, use it.
3173 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3174 default: break;
3175 case TargetLowering::Legal:
3176 if (!TLI.isTypeLegal(NewInTy))
3177 break; // Can't use this datatype.
3178 // FALL THROUGH.
3179 case TargetLowering::Custom:
3180 OpToUse = ISD::UINT_TO_FP;
3181 break;
3182 }
3183 if (OpToUse) break;
3184
3185 // Otherwise, try a larger type.
3186 }
3187
3188 // Okay, we found the operation and type to use. Zero extend our input to the
3189 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00003190 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00003191 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00003192 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003193}
3194
3195/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3196/// FP_TO_*INT operation of the specified operand when the target requests that
3197/// we promote it. At this point, we know that the result and operand types are
3198/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3199/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00003200SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
3201 MVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00003202 bool isSigned,
3203 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003204 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003205 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00003206
3207 unsigned OpToUse = 0;
3208
3209 // Scan for the appropriate larger type to use.
3210 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003211 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
3212 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00003213
3214 // If the target supports FP_TO_SINT returning this type, use it.
3215 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3216 default: break;
3217 case TargetLowering::Legal:
3218 if (!TLI.isTypeLegal(NewOutTy))
3219 break; // Can't use this datatype.
3220 // FALL THROUGH.
3221 case TargetLowering::Custom:
3222 OpToUse = ISD::FP_TO_SINT;
3223 break;
3224 }
3225 if (OpToUse) break;
3226
3227 // If the target supports FP_TO_UINT of this type, use it.
3228 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3229 default: break;
3230 case TargetLowering::Legal:
3231 if (!TLI.isTypeLegal(NewOutTy))
3232 break; // Can't use this datatype.
3233 // FALL THROUGH.
3234 case TargetLowering::Custom:
3235 OpToUse = ISD::FP_TO_UINT;
3236 break;
3237 }
3238 if (OpToUse) break;
3239
3240 // Otherwise, try a larger type.
3241 }
3242
Scott Michelfdc40a02009-02-17 22:15:04 +00003243
Chris Lattner27a6c732007-11-24 07:07:01 +00003244 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00003245 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00003246
Chris Lattner27a6c732007-11-24 07:07:01 +00003247 // If the operation produces an invalid type, it must be custom lowered. Use
3248 // the target lowering hooks to expand it. Just keep the low part of the
3249 // expanded operation, we know that we're truncating anyway.
3250 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00003251 SmallVector<SDValue, 2> Results;
3252 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
3253 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
3254 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00003255 }
Duncan Sands126d9072008-07-04 11:47:58 +00003256
Chris Lattner27a6c732007-11-24 07:07:01 +00003257 // Truncate the result of the extended FP_TO_*INT operation to the desired
3258 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00003259 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003260}
3261
3262/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3263///
Dale Johannesen8a782a22009-02-02 22:12:50 +00003264SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003265 MVT VT = Op.getValueType();
3266 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003267 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003268 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003269 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3270 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003271 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3272 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3273 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003274 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003275 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3276 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3277 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3278 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3279 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3280 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3281 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3282 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3283 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003284 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00003285 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
3286 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
3287 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
3288 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
3289 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
3290 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
3291 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
3292 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
3293 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3294 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3295 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3296 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3297 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3298 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3299 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
3300 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
3301 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
3302 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
3303 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
3304 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
3305 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003306 }
3307}
3308
3309/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3310///
Scott Michelfdc40a02009-02-17 22:15:04 +00003311SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00003312 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00003313 switch (Opc) {
3314 default: assert(0 && "Cannot expand this yet!");
3315 case ISD::CTPOP: {
3316 static const uint64_t mask[6] = {
3317 0x5555555555555555ULL, 0x3333333333333333ULL,
3318 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3319 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3320 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00003321 MVT VT = Op.getValueType();
3322 MVT ShVT = TLI.getShiftAmountTy();
3323 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003324 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3325 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003326 unsigned EltSize = VT.isVector() ?
3327 VT.getVectorElementType().getSizeInBits() : len;
3328 SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003329 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003330 Op = DAG.getNode(ISD::ADD, dl, VT,
Dale Johannesene72c5962009-02-06 21:55:48 +00003331 DAG.getNode(ISD::AND, dl, VT, Op, Tmp2),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003332 DAG.getNode(ISD::AND, dl, VT,
3333 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3),
3334 Tmp2));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003335 }
3336 return Op;
3337 }
3338 case ISD::CTLZ: {
3339 // for now, we do this:
3340 // x = x | (x >> 1);
3341 // x = x | (x >> 2);
3342 // ...
3343 // x = x | (x >>16);
3344 // x = x | (x >>32); // for 64-bit input
3345 // return popcount(~x);
3346 //
3347 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003348 MVT VT = Op.getValueType();
3349 MVT ShVT = TLI.getShiftAmountTy();
3350 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00003351 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003352 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003353 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00003354 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003355 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00003356 Op = DAG.getNOT(dl, Op, VT);
3357 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003358 }
3359 case ISD::CTTZ: {
3360 // for now, we use: { return popcount(~x & (x - 1)); }
3361 // unless the target has ctlz but not ctpop, in which case we use:
3362 // { return 32 - nlz(~x & (x-1)); }
3363 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00003364 MVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00003365 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
3366 DAG.getNOT(dl, Op, VT),
3367 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00003368 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00003369 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003370 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
3371 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00003372 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003373 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00003374 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
3375 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00003376 }
3377 }
3378}
Chris Lattnere34b3962005-01-19 04:19:40 +00003379
Eli Friedman8c377c72009-05-27 01:25:56 +00003380void SelectionDAGLegalize::ExpandNode(SDNode *Node,
3381 SmallVectorImpl<SDValue> &Results) {
3382 DebugLoc dl = Node->getDebugLoc();
3383 SDValue Tmp1, Tmp2;
3384 switch (Node->getOpcode()) {
3385 case ISD::CTPOP:
3386 case ISD::CTLZ:
3387 case ISD::CTTZ:
3388 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
3389 Results.push_back(Tmp1);
3390 break;
3391 case ISD::BSWAP:
3392 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
3393 break;
3394 case ISD::FRAMEADDR:
3395 case ISD::RETURNADDR:
3396 case ISD::FRAME_TO_ARGS_OFFSET:
3397 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
3398 break;
3399 case ISD::FLT_ROUNDS_:
3400 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
3401 break;
3402 case ISD::EH_RETURN:
3403 case ISD::DECLARE:
3404 case ISD::DBG_LABEL:
3405 case ISD::EH_LABEL:
3406 case ISD::PREFETCH:
3407 case ISD::MEMBARRIER:
3408 case ISD::VAEND:
3409 Results.push_back(Node->getOperand(0));
3410 break;
3411 case ISD::MERGE_VALUES:
3412 for (unsigned i = 0; i < Node->getNumValues(); i++)
3413 Results.push_back(Node->getOperand(i));
3414 break;
3415 case ISD::UNDEF: {
3416 MVT VT = Node->getValueType(0);
3417 if (VT.isInteger())
3418 Results.push_back(DAG.getConstant(0, VT));
3419 else if (VT.isFloatingPoint())
3420 Results.push_back(DAG.getConstantFP(0, VT));
3421 else
3422 assert(0 && "Unknown value type!");
3423 break;
3424 }
3425 case ISD::TRAP: {
3426 // If this operation is not supported, lower it to 'abort()' call
3427 TargetLowering::ArgListTy Args;
3428 std::pair<SDValue, SDValue> CallResult =
3429 TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy,
3430 false, false, false, false, CallingConv::C, false,
3431 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3432 Args, DAG, dl);
3433 Results.push_back(CallResult.second);
3434 break;
3435 }
3436 case ISD::FP_ROUND:
3437 case ISD::BIT_CONVERT:
3438 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3439 Node->getValueType(0), dl);
3440 Results.push_back(Tmp1);
3441 break;
3442 case ISD::FP_EXTEND:
3443 Tmp1 = EmitStackConvert(Node->getOperand(0),
3444 Node->getOperand(0).getValueType(),
3445 Node->getValueType(0), dl);
3446 Results.push_back(Tmp1);
3447 break;
3448 case ISD::SIGN_EXTEND_INREG: {
3449 // NOTE: we could fall back on load/store here too for targets without
3450 // SAR. However, it is doubtful that any exist.
3451 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3452 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3453 ExtraVT.getSizeInBits();
3454 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
3455 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3456 Node->getOperand(0), ShiftCst);
3457 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3458 Results.push_back(Tmp1);
3459 break;
3460 }
3461 case ISD::FP_ROUND_INREG: {
3462 // The only way we can lower this is to turn it into a TRUNCSTORE,
3463 // EXTLOAD pair, targetting a temporary location (a stack slot).
3464
3465 // NOTE: there is a choice here between constantly creating new stack
3466 // slots and always reusing the same one. We currently always create
3467 // new ones, as reuse may inhibit scheduling.
3468 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3469 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
3470 Node->getValueType(0), dl);
3471 Results.push_back(Tmp1);
3472 break;
3473 }
3474 case ISD::SINT_TO_FP:
3475 case ISD::UINT_TO_FP:
3476 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
3477 Node->getOperand(0), Node->getValueType(0), dl);
3478 Results.push_back(Tmp1);
3479 break;
3480 case ISD::FP_TO_UINT: {
3481 SDValue True, False;
3482 MVT VT = Node->getOperand(0).getValueType();
3483 MVT NVT = Node->getValueType(0);
3484 const uint64_t zero[] = {0, 0};
3485 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3486 APInt x = APInt::getSignBit(NVT.getSizeInBits());
3487 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
3488 Tmp1 = DAG.getConstantFP(apf, VT);
3489 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
3490 Node->getOperand(0),
3491 Tmp1, ISD::SETLT);
3492 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
3493 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
3494 DAG.getNode(ISD::FSUB, dl, VT,
3495 Node->getOperand(0), Tmp1));
3496 False = DAG.getNode(ISD::XOR, dl, NVT, False,
3497 DAG.getConstant(x, NVT));
3498 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
3499 Results.push_back(Tmp1);
3500 break;
3501 }
3502 case ISD::VACOPY: {
3503 // This defaults to loading a pointer from the input and storing it to the
3504 // output, returning the chain.
3505 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3506 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3507 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
3508 Node->getOperand(2), VS, 0);
3509 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0);
3510 Results.push_back(Tmp1);
3511 break;
3512 }
3513 case ISD::EXTRACT_VECTOR_ELT:
3514 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
3515 // This must be an access of the only element. Return it.
3516 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0),
3517 Node->getOperand(0));
3518 else
3519 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3520 Results.push_back(Tmp1);
3521 break;
3522 case ISD::EXTRACT_SUBVECTOR:
3523 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3524 break;
3525 case ISD::SCALAR_TO_VECTOR:
3526 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3527 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003528 case ISD::INSERT_VECTOR_ELT:
3529 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3530 Node->getOperand(1),
3531 Node->getOperand(2), dl));
3532 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003533 case ISD::EXTRACT_ELEMENT: {
3534 MVT OpTy = Node->getOperand(0).getValueType();
3535 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3536 // 1 -> Hi
3537 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3538 DAG.getConstant(OpTy.getSizeInBits()/2,
3539 TLI.getShiftAmountTy()));
3540 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3541 } else {
3542 // 0 -> Lo
3543 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3544 Node->getOperand(0));
3545 }
3546 Results.push_back(Tmp1);
3547 break;
3548 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003549 case ISD::STACKSAVE:
3550 // Expand to CopyFromReg if the target set
3551 // StackPointerRegisterToSaveRestore.
3552 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3553 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3554 Node->getValueType(0)));
3555 Results.push_back(Results[0].getValue(1));
3556 } else {
3557 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3558 Results.push_back(Node->getOperand(0));
3559 }
3560 break;
3561 case ISD::STACKRESTORE:
3562 // Expand to CopyToReg if the target set
3563 // StackPointerRegisterToSaveRestore.
3564 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3565 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3566 Node->getOperand(1)));
3567 } else {
3568 Results.push_back(Node->getOperand(0));
3569 }
3570 break;
3571 case ISD::GLOBAL_OFFSET_TABLE:
3572 case ISD::GlobalAddress:
3573 case ISD::GlobalTLSAddress:
3574 case ISD::ExternalSymbol:
3575 case ISD::ConstantPool:
3576 case ISD::JumpTable:
3577 case ISD::INTRINSIC_W_CHAIN:
3578 case ISD::INTRINSIC_WO_CHAIN:
3579 case ISD::INTRINSIC_VOID:
3580 // FIXME: Custom lowering for these operations shouldn't return null!
3581 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3582 Results.push_back(SDValue(Node, i));
3583 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003584 }
3585}
3586void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3587 SmallVectorImpl<SDValue> &Results) {
3588 MVT OVT = Node->getValueType(0);
3589 if (Node->getOpcode() == ISD::UINT_TO_FP ||
3590 Node->getOpcode() == ISD::SINT_TO_FP) {
3591 OVT = Node->getOperand(0).getValueType();
3592 }
3593 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3594 DebugLoc dl = Node->getDebugLoc();
3595 SDValue Tmp1, Tmp2;
3596 switch (Node->getOpcode()) {
3597 case ISD::CTTZ:
3598 case ISD::CTLZ:
3599 case ISD::CTPOP:
3600 // Zero extend the argument.
3601 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3602 // Perform the larger operation.
3603 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
3604 if (Node->getOpcode() == ISD::CTTZ) {
3605 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
3606 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
3607 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3608 ISD::SETEQ);
3609 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3610 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3611 } else if (Node->getOpcode() == ISD::CTLZ) {
3612 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3613 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3614 DAG.getConstant(NVT.getSizeInBits() -
3615 OVT.getSizeInBits(), NVT));
3616 }
3617 Results.push_back(Tmp1);
3618 break;
3619 case ISD::BSWAP: {
3620 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
3621 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1);
3622 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3623 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3624 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3625 Results.push_back(Tmp1);
3626 break;
3627 }
3628 case ISD::FP_TO_UINT:
3629 case ISD::FP_TO_SINT:
3630 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3631 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3632 Results.push_back(Tmp1);
3633 break;
3634 case ISD::UINT_TO_FP:
3635 case ISD::SINT_TO_FP:
3636 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3637 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3638 Results.push_back(Tmp1);
3639 break;
3640 }
3641}
3642
Chris Lattner3e928bb2005-01-07 07:47:09 +00003643// SelectionDAG::Legalize - This is the entry point for the file.
3644//
Bill Wendling98a366d2009-04-29 23:29:43 +00003645void SelectionDAG::Legalize(bool TypesNeedLegalizing,
3646 CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003647 /// run - This is the main entry point to this class.
3648 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003649 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003650}
3651