blob: 80eeab645d1fa775a25f1b30964df0d48a7adf45 [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
Evan Cheng3d2125c2010-11-30 23:55:39 +000014#include "llvm/Analysis/DebugInfo.h"
15#include "llvm/CodeGen/Analysis.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000018#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000020#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng3d2125c2010-11-30 23:55:39 +000021#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000022#include "llvm/Target/TargetFrameLowering.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"
Chris Lattneradf6a962005-05-13 18:50:42 +000027#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000028#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000029#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000030#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000031#include "llvm/GlobalVariable.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000032#include "llvm/LLVMContext.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
David Greene993aace2010-01-05 01:24:53 +000034#include "llvm/Support/Debug.h"
Jim Grosbache03262f2010-06-18 21:43:38 +000035#include "llvm/Support/ErrorHandling.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000036#include "llvm/Support/MathExtras.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000037#include "llvm/Support/raw_ostream.h"
Chris Lattner79715142007-02-03 01:12:36 +000038#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000039#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000041using namespace llvm;
42
43//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000056class SelectionDAGLegalize {
Dan Gohman55e59c12010-04-19 19:05:59 +000057 const TargetMachine &TM;
Dan Gohmand858e902010-04-17 15:26:15 +000058 const TargetLowering &TLI;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000060 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000063
Stuart Hastingsfc521632011-04-19 16:16:58 +000064 /// LastCALLSEQ - This keeps track of the CALLSEQ_END node that has been
Chris Lattner6831a812006-02-13 09:18:02 +000065 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
Stuart Hastingsfc521632011-04-19 16:16:58 +000067 SmallVector<SDValue, 8> LastCALLSEQ;
Scott Michelfdc40a02009-02-17 22:15:04 +000068
Stuart Hastingsfc521632011-04-19 16:16:58 +000069 // Track CALLSEQ_BEGIN/CALLSEQ_END nesting.
70 int depthCALLSEQ;
Stuart Hastings1809d5f2011-04-05 00:37:28 +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));
Owen Anderson95771af2011-02-25 21:41:48 +000093
Devang Patela778f5c2011-02-18 22:43:42 +000094 // Transfer SDDbgValues.
95 DAG.TransferDbgValues(From, To);
Chris Lattner8afc48e2005-01-07 22:28:47 +000096 }
97
Chris Lattner3e928bb2005-01-07 07:47:09 +000098public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000099 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000100
Chris Lattner3e928bb2005-01-07 07:47:09 +0000101 /// getTypeAction - Return how we should legalize values of this type, either
102 /// it is already legal or we need to expand it into multiple registers of
103 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +0000104 LegalizeAction getTypeAction(EVT VT) const {
Chris Lattneraafe6262010-08-25 23:00:45 +0000105 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000106 }
107
108 /// isTypeLegal - Return true if this type is legal on this target.
109 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000110 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 return getTypeAction(VT) == Legal;
112 }
113
Chris Lattner3e928bb2005-01-07 07:47:09 +0000114 void LegalizeDAG();
115
Chris Lattner456a93a2006-01-28 07:39:30 +0000116private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000117 /// LegalizeOp - We know that the specified value has a legal type.
118 /// Recursively ensure that the operands have legal types, then return the
119 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000121
Eli Friedman7ef3d172009-06-06 07:04:42 +0000122 SDValue OptimizeFloatStore(StoreSDNode *ST);
123
Nate Begeman68679912008-04-25 18:07:40 +0000124 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
125 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
126 /// is necessary to spill the vector being inserted into to memory, perform
127 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000128 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000129 SDValue Idx, DebugLoc dl);
130 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
131 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000132
Nate Begeman5a5ca152009-04-29 05:20:52 +0000133 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
134 /// performs the same shuffe in terms of order or result bytes, but on a type
135 /// whose vector element type is narrower than the original shuffle type.
136 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000137 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000138 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000139 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000140
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000141 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000142 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000143
Owen Andersone50ed302009-08-10 22:56:29 +0000144 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000145 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000146
Eli Friedman47b41f72009-05-27 02:21:29 +0000147 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Jim Grosbache03262f2010-06-18 21:43:38 +0000148 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
149 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000150 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
151 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
152 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000153 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
154 RTLIB::Libcall Call_I8,
155 RTLIB::Libcall Call_I16,
156 RTLIB::Libcall Call_I32,
157 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000158 RTLIB::Libcall Call_I128);
Evan Cheng65279cb2011-04-16 03:08:26 +0000159 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnercad063f2005-07-16 00:19:57 +0000160
Owen Andersone50ed302009-08-10 22:56:29 +0000161 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000162 SDValue ExpandBUILD_VECTOR(SDNode *Node);
163 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000164 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
165 SmallVectorImpl<SDValue> &Results);
166 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000167 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000168 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000169 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000170 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000171 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000172 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000173
Dale Johannesen8a782a22009-02-02 22:12:50 +0000174 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
175 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000176
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000177 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000178 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000179 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000180
Jim Grosbache03262f2010-06-18 21:43:38 +0000181 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
182
Eli Friedman8c377c72009-05-27 01:25:56 +0000183 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
184 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Stuart Hastingsfc521632011-04-19 16:16:58 +0000185
186 SDValue getLastCALLSEQ() { return LastCALLSEQ.back(); /*[depthCALLSEQ];*/ }
187 void setLastCALLSEQ(const SDValue s) { LastCALLSEQ.back() /*[depthCALLSEQ]*/ = s; }
188 void pushLastCALLSEQ(SDValue s) {
189 depthCALLSEQ++;
190 LastCALLSEQ.push_back(s);
191 }
192 void popLastCALLSEQ() {
193 LastCALLSEQ.pop_back();
194 depthCALLSEQ--;
195 assert(depthCALLSEQ >= 0 && "excess pop of LastCALLSEQ");
196 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000197};
198}
199
Nate Begeman5a5ca152009-04-29 05:20:52 +0000200/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
201/// performs the same shuffe in terms of order or result bytes, but on a type
202/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000203/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000204SDValue
205SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000206 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000207 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000208 unsigned NumMaskElts = VT.getVectorNumElements();
209 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000210 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000211
Nate Begeman9008ca62009-04-27 18:41:29 +0000212 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
213
214 if (NumEltsGrowth == 1)
215 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000216
Nate Begeman9008ca62009-04-27 18:41:29 +0000217 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000218 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000219 int Idx = Mask[i];
220 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000221 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000222 NewMask.push_back(-1);
223 else
224 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000225 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000226 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000227 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000228 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
229 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000230}
231
Bill Wendling5aa49772009-02-24 02:35:30 +0000232SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000233 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000234 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
235 DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000236 ValueTypeActions(TLI.getValueTypeActions()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000238 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000239}
240
Chris Lattner3e928bb2005-01-07 07:47:09 +0000241void SelectionDAGLegalize::LegalizeDAG() {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000242 depthCALLSEQ = 0;
243 pushLastCALLSEQ(DAG.getEntryNode());
Mon P Wange5ab34e2009-02-04 19:38:14 +0000244
Chris Lattnerab510a72005-10-02 17:49:46 +0000245 // The legalize process is inherently a bottom-up recursive process (users
246 // legalize their uses before themselves). Given infinite stack space, we
247 // could just start legalizing on the root and traverse the whole graph. In
248 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000249 // blocks. To avoid this problem, compute an ordering of the nodes where each
250 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000251 DAG.AssignTopologicalOrder();
252 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000253 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000254 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000255
256 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000257 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000258 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
259 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000260
Chris Lattner3e928bb2005-01-07 07:47:09 +0000261 LegalizedNodes.clear();
262
263 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000264 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000265}
266
Chris Lattner6831a812006-02-13 09:18:02 +0000267
268/// FindCallEndFromCallStart - Given a chained node that is part of a call
269/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000270static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastingsfc521632011-04-19 16:16:58 +0000271 int next_depth = depth;
Stuart Hastingsa304d022010-12-09 21:25:20 +0000272 if (Node->getOpcode() == ISD::CALLSEQ_START)
Stuart Hastingsfc521632011-04-19 16:16:58 +0000273 next_depth = depth + 1;
274 if (Node->getOpcode() == ISD::CALLSEQ_END) {
275 assert(depth > 0 && "negative depth!");
276 if (depth == 1)
Stuart Hastings56500ed2010-12-21 17:16:58 +0000277 return Node;
Stuart Hastingsfc521632011-04-19 16:16:58 +0000278 else
279 next_depth = depth - 1;
Stuart Hastings56500ed2010-12-21 17:16:58 +0000280 }
Chris Lattner6831a812006-02-13 09:18:02 +0000281 if (Node->use_empty())
282 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Chris Lattner6831a812006-02-13 09:18:02 +0000284 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000285 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000287 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000288 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000290 // Otherwise, hunt for it.
291 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000293 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000294 break;
295 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000296
297 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000299 return 0;
300 }
301 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000302
Chris Lattner6831a812006-02-13 09:18:02 +0000303 for (SDNode::use_iterator UI = Node->use_begin(),
304 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000305
Chris Lattner6831a812006-02-13 09:18:02 +0000306 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000307 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000308 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
309 if (User->getOperand(i) == TheChain)
Stuart Hastingsfc521632011-04-19 16:16:58 +0000310 if (SDNode *Result = FindCallEndFromCallStart(User, next_depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000311 return Result;
312 }
313 return 0;
314}
315
Scott Michelfdc40a02009-02-17 22:15:04 +0000316/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000317/// sequence, find the CALLSEQ_START node that initiates the call sequence.
318static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000319 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000320 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000321 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
322 Node = Node->getOperand(0).getNode();
323 assert(Node->getOperand(0).getValueType() == MVT::Other &&
324 "Node doesn't have a token chain argument!");
325 switch (Node->getOpcode()) {
326 default:
327 break;
328 case ISD::CALLSEQ_START:
329 if (!nested)
330 return Node;
331 nested--;
332 break;
333 case ISD::CALLSEQ_END:
334 nested++;
335 break;
336 }
337 }
338 return 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000339}
340
341/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000342/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000343/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000344///
345/// Keep track of the nodes we fine that actually do lead to Dest in
346/// NodesLeadingTo. This avoids retraversing them exponential number of times.
347///
348bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000349 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000350 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000351
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000352 // If we've already processed this node and it does lead to Dest, there is no
353 // need to reprocess it.
354 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000355
Chris Lattner6831a812006-02-13 09:18:02 +0000356 // If the first result of this node has been already legalized, then it cannot
357 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000358 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Chris Lattner6831a812006-02-13 09:18:02 +0000360 // Okay, this node has not already been legalized. Check and legalize all
361 // operands. If none lead to Dest, then we can legalize this node.
362 bool OperandsLeadToDest = false;
363 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
364 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000365 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
366 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000367
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000368 if (OperandsLeadToDest) {
369 NodesLeadingTo.insert(N);
370 return true;
371 }
Chris Lattner6831a812006-02-13 09:18:02 +0000372
373 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000374 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000375 return false;
376}
377
Evan Cheng9f877882006-12-13 20:57:08 +0000378/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
379/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000380static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000381 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000382 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000383 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000384
385 // If a FP immediate is precise when represented as a float and if the
386 // target can do an extending load from float to double, we put it into
387 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000388 // double. This shrinks FP constants and canonicalizes them for targets where
389 // an FP extending load is the same cost as a normal load (such as on the x87
390 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000391 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000392 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000393 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000395 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000397 }
398
Owen Andersone50ed302009-08-10 22:56:29 +0000399 EVT OrigVT = VT;
400 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 while (SVT != MVT::f32) {
402 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000403 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000404 // Only do this if the target has a native EXTLOAD instruction from
405 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000406 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000407 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000408 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000409 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000410 VT = SVT;
411 Extend = true;
412 }
Evan Cheng00495212006-12-12 21:32:44 +0000413 }
414
Dan Gohman475871a2008-07-27 21:46:04 +0000415 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000416 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000417 if (Extend)
Stuart Hastingsa9011292011-02-16 16:23:55 +0000418 return DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
Evan Chengbcc80172010-07-07 22:15:37 +0000419 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000420 CPIdx, MachinePointerInfo::getConstantPool(),
421 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000422 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000423 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000424 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000425}
426
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000427/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
428static
Dan Gohman475871a2008-07-27 21:46:04 +0000429SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000430 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000431 SDValue Chain = ST->getChain();
432 SDValue Ptr = ST->getBasePtr();
433 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000434 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000435 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000436 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000437 if (ST->getMemoryVT().isFloatingPoint() ||
438 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000439 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000440 if (TLI.isTypeLegal(intVT)) {
441 // Expand to a bitconvert of the value to the integer type of the
442 // same size, then a (misaligned) int store.
443 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000444 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000445 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
446 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000447 } else {
448 // Do a (aligned) store to a stack slot, then copy from the stack slot
449 // to the final destination using (unaligned) integer loads and stores.
Owen Andersone50ed302009-08-10 22:56:29 +0000450 EVT StoredVT = ST->getMemoryVT();
451 EVT RegVT =
Evan Chengadf97992010-04-15 01:25:27 +0000452 TLI.getRegisterType(*DAG.getContext(),
453 EVT::getIntegerVT(*DAG.getContext(),
454 StoredVT.getSizeInBits()));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000455 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
456 unsigned RegBytes = RegVT.getSizeInBits() / 8;
457 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000458
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000459 // Make sure the stack slot is also aligned for the register type.
460 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
461
462 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000463 SDValue Store = DAG.getTruncStore(Chain, dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000464 Val, StackPtr, MachinePointerInfo(),
465 StoredVT, false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000466 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
467 SmallVector<SDValue, 8> Stores;
468 unsigned Offset = 0;
469
470 // Do all but one copies using the full register width.
471 for (unsigned i = 1; i < NumRegs; i++) {
472 // Load one integer register's worth from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000473 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
474 MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000475 false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000476 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000477 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000478 ST->getPointerInfo().getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +0000479 ST->isVolatile(), ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000480 MinAlign(ST->getAlignment(), Offset)));
481 // Increment the pointers.
482 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000483 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000484 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000485 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000486 }
487
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000488 // The last store may be partial. Do a truncating store. On big-endian
489 // machines this requires an extending load from the stack slot to ensure
490 // that the bits are in the right place.
Evan Chengadf97992010-04-15 01:25:27 +0000491 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
492 8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000493
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000494 // Load from the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000495 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000496 MachinePointerInfo(),
497 MemVT, false, false, 0);
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000498
Dale Johannesenbb5da912009-02-02 20:41:04 +0000499 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000500 ST->getPointerInfo()
501 .getWithOffset(Offset),
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000502 MemVT, ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000503 ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000504 MinAlign(ST->getAlignment(), Offset)));
505 // The order of the stores doesn't matter - say it with a TokenFactor.
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000507 Stores.size());
508 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000509 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000510 assert(ST->getMemoryVT().isInteger() &&
511 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000512 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000513 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000514 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000515 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000516 int IncrementSize = NumBits / 8;
517
518 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000519 SDValue ShiftAmount = DAG.getConstant(NumBits,
520 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000521 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000522 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000523
524 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000525 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000526 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000527 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000528 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000529 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000530 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000531 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000532 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000533 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000534 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
535 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000536
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000538}
539
540/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
541static
Dan Gohman475871a2008-07-27 21:46:04 +0000542SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000543 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000544 SDValue Chain = LD->getChain();
545 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000546 EVT VT = LD->getValueType(0);
547 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000548 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000549 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000550 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000551 if (TLI.isTypeLegal(intVT)) {
552 // Expand to a (misaligned) integer load of the same size,
553 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000554 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
555 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000556 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000557 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000558 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000559 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000560
Duncan Sands05e11fa2008-12-12 21:47:02 +0000561 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000562 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000563 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000564
Chris Lattnerecf42c42010-09-21 16:36:31 +0000565 // Copy the value to a (aligned) stack slot using (unaligned) integer
566 // loads and stores, then do a (aligned) load from the stack slot.
567 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
568 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
569 unsigned RegBytes = RegVT.getSizeInBits() / 8;
570 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
571
572 // Make sure the stack slot is also aligned for the register type.
573 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
574
575 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
576 SmallVector<SDValue, 8> Stores;
577 SDValue StackPtr = StackBase;
578 unsigned Offset = 0;
579
580 // Do all but one copies using the full register width.
581 for (unsigned i = 1; i < NumRegs; i++) {
582 // Load one integer register's worth from the original location.
583 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
584 LD->getPointerInfo().getWithOffset(Offset),
585 LD->isVolatile(), LD->isNonTemporal(),
586 MinAlign(LD->getAlignment(), Offset));
587 // Follow the load with a store to the stack slot. Remember the store.
588 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000589 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000590 // Increment the pointers.
591 Offset += RegBytes;
592 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
593 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
594 Increment);
595 }
596
597 // The last copy may be partial. Do an extending load.
598 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
599 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000600 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000601 LD->getPointerInfo().getWithOffset(Offset),
602 MemVT, LD->isVolatile(),
603 LD->isNonTemporal(),
604 MinAlign(LD->getAlignment(), Offset));
605 // Follow the load with a store to the stack slot. Remember the store.
606 // On big-endian machines this requires a truncating store to ensure
607 // that the bits end up in the right place.
608 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
609 MachinePointerInfo(), MemVT,
610 false, false, 0));
611
612 // The order of the stores doesn't matter - say it with a TokenFactor.
613 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
614 Stores.size());
615
616 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000617 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000618 MachinePointerInfo(), LoadedVT, false, false, 0);
619
620 // Callers expect a MERGE_VALUES node.
621 SDValue Ops[] = { Load, TF };
622 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000623 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000624 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000625 "Unaligned load of unsupported type.");
626
Dale Johannesen8155d642008-02-27 22:36:00 +0000627 // Compute the new VT that is half the size of the old one. This is an
628 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000630 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000631 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000632 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000633
Chris Lattnere400af82007-11-19 21:38:03 +0000634 unsigned Alignment = LD->getAlignment();
635 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000636 ISD::LoadExtType HiExtType = LD->getExtensionType();
637
638 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
639 if (HiExtType == ISD::NON_EXTLOAD)
640 HiExtType = ISD::ZEXTLOAD;
641
642 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000643 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000644 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000645 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000646 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000647 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000648 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000649 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000650 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000651 LD->getPointerInfo().getWithOffset(IncrementSize),
652 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000653 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000654 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000655 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000656 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000657 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000658 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000659 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000660 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000661 LD->getPointerInfo().getWithOffset(IncrementSize),
662 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000663 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000664 }
665
666 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000667 SDValue ShiftAmount = DAG.getConstant(NumBits,
668 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000669 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
670 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000673 Hi.getValue(1));
674
Dan Gohman475871a2008-07-27 21:46:04 +0000675 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000676 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000677}
Evan Cheng912095b2007-01-04 21:56:39 +0000678
Nate Begeman68679912008-04-25 18:07:40 +0000679/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
680/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
681/// is necessary to spill the vector being inserted into to memory, perform
682/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000683SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000684PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
685 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000686 SDValue Tmp1 = Vec;
687 SDValue Tmp2 = Val;
688 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000689
Nate Begeman68679912008-04-25 18:07:40 +0000690 // If the target doesn't support this, we have to spill the input vector
691 // to a temporary stack slot, update the element, then reload it. This is
692 // badness. We could also load the value into a vector register (either
693 // with a "move to register" or "extload into register" instruction, then
694 // permute it into place, if the idx is a constant and if the idx is
695 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000696 EVT VT = Tmp1.getValueType();
697 EVT EltVT = VT.getVectorElementType();
698 EVT IdxVT = Tmp3.getValueType();
699 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000700 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000701
Evan Chengff89dcb2009-10-18 18:16:27 +0000702 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
703
Nate Begeman68679912008-04-25 18:07:40 +0000704 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000705 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000706 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000707 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000708
709 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000710 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000711 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000712 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000713 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000714 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
715 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000716 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000717 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000718 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000719 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000720 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000721 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000722}
723
Mon P Wange9f10152008-12-09 05:46:39 +0000724
Eli Friedman3f727d62009-05-27 02:16:40 +0000725SDValue SelectionDAGLegalize::
726ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
727 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
728 // SCALAR_TO_VECTOR requires that the type of the value being inserted
729 // match the element type of the vector being created, except for
730 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000731 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000732 if (Val.getValueType() == EltVT ||
733 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
734 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
735 Vec.getValueType(), Val);
736
737 unsigned NumElts = Vec.getValueType().getVectorNumElements();
738 // We generate a shuffle of InVec and ScVec, so the shuffle mask
739 // should be 0,1,2,3,4,5... with the appropriate element replaced with
740 // elt 0 of the RHS.
741 SmallVector<int, 8> ShufOps;
742 for (unsigned i = 0; i != NumElts; ++i)
743 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
744
745 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
746 &ShufOps[0]);
747 }
748 }
749 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
750}
751
Eli Friedman7ef3d172009-06-06 07:04:42 +0000752SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
753 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
754 // FIXME: We shouldn't do this for TargetConstantFP's.
755 // FIXME: move this to the DAG Combiner! Note that we can't regress due
756 // to phase ordering between legalized code and the dag combiner. This
757 // probably means that we need to integrate dag combiner and legalizer
758 // together.
759 // We generally can't do this one for long doubles.
760 SDValue Tmp1 = ST->getChain();
761 SDValue Tmp2 = ST->getBasePtr();
762 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000763 unsigned Alignment = ST->getAlignment();
764 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000765 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000766 DebugLoc dl = ST->getDebugLoc();
767 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 if (CFP->getValueType(0) == MVT::f32 &&
769 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000770 Tmp3 = DAG.getConstant(CFP->getValueAPF().
771 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000773 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
774 isVolatile, isNonTemporal, Alignment);
775 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000776
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000777 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000778 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000780 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000782 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
783 isVolatile, isNonTemporal, Alignment);
784 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000785
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000786 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000787 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
788 // stores. If the target supports neither 32- nor 64-bits, this
789 // xform is certainly not worth it.
790 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000791 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000793 if (TLI.isBigEndian()) std::swap(Lo, Hi);
794
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000795 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
796 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000797 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
798 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000799 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
800 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000801 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000802
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000804 }
805 }
806 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000807 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000808}
809
Dan Gohmana3466152007-07-13 20:14:11 +0000810/// LegalizeOp - We know that the specified value has a legal type, and
811/// that its operands are legal. Now ensure that the operation itself
812/// is legal, recursively ensuring that the operands' operations remain
813/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000814SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000815 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
816 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
Gabor Greifba36cb52008-08-28 21:40:38 +0000818 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000819 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000820
Eli Friedman1fde9c52009-05-24 02:46:31 +0000821 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
822 assert(getTypeAction(Node->getValueType(i)) == Legal &&
823 "Unexpected illegal type!");
824
825 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000826 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000827 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
828 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829
Chris Lattner45982da2005-05-12 16:53:42 +0000830 // Note that LegalizeOp may be reentered even from single-use nodes, which
831 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000832 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000833 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000834
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
836 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000837 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000838
Eli Friedman8c377c72009-05-27 01:25:56 +0000839 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000840 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000841 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000842 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000843 case ISD::INTRINSIC_W_CHAIN:
844 case ISD::INTRINSIC_WO_CHAIN:
845 case ISD::INTRINSIC_VOID:
846 case ISD::VAARG:
847 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000849 break;
850 case ISD::SINT_TO_FP:
851 case ISD::UINT_TO_FP:
852 case ISD::EXTRACT_VECTOR_ELT:
853 Action = TLI.getOperationAction(Node->getOpcode(),
854 Node->getOperand(0).getValueType());
855 break;
856 case ISD::FP_ROUND_INREG:
857 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000858 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000859 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
860 break;
861 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000862 case ISD::SELECT_CC:
863 case ISD::SETCC:
864 case ISD::BR_CC: {
865 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
866 Node->getOpcode() == ISD::SETCC ? 2 : 1;
867 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000868 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000869 ISD::CondCode CCCode =
870 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
871 Action = TLI.getCondCodeAction(CCCode, OpVT);
872 if (Action == TargetLowering::Legal) {
873 if (Node->getOpcode() == ISD::SELECT_CC)
874 Action = TLI.getOperationAction(Node->getOpcode(),
875 Node->getValueType(0));
876 else
877 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
878 }
879 break;
880 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000881 case ISD::LOAD:
882 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000883 // FIXME: Model these properly. LOAD and STORE are complicated, and
884 // STORE expects the unlegalized operand in some cases.
885 SimpleFinishLegalizing = false;
886 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000887 case ISD::CALLSEQ_START:
888 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000889 // FIXME: This shouldn't be necessary. These nodes have special properties
890 // dealing with the recursive nature of legalization. Removing this
891 // special case should be done as part of making LegalizeDAG non-recursive.
892 SimpleFinishLegalizing = false;
893 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000894 case ISD::EXTRACT_ELEMENT:
895 case ISD::FLT_ROUNDS_:
896 case ISD::SADDO:
897 case ISD::SSUBO:
898 case ISD::UADDO:
899 case ISD::USUBO:
900 case ISD::SMULO:
901 case ISD::UMULO:
902 case ISD::FPOWI:
903 case ISD::MERGE_VALUES:
904 case ISD::EH_RETURN:
905 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000906 case ISD::EH_SJLJ_SETJMP:
907 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000908 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000909 // These operations lie about being legal: when they claim to be legal,
910 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000911 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
912 if (Action == TargetLowering::Legal)
913 Action = TargetLowering::Expand;
914 break;
915 case ISD::TRAMPOLINE:
916 case ISD::FRAMEADDR:
917 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000918 // These operations lie about being legal: when they claim to be legal,
919 // they should actually be custom-lowered.
920 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
921 if (Action == TargetLowering::Legal)
922 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000923 break;
924 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000925 // A weird case: legalization for BUILD_VECTOR never legalizes the
926 // operands!
927 // FIXME: This really sucks... changing it isn't semantically incorrect,
928 // but it massively pessimizes the code for floating-point BUILD_VECTORs
929 // because ConstantFP operands get legalized into constant pool loads
930 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
931 // though, because BUILD_VECTORS usually get lowered into other nodes
932 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000933 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000934 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000935 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000936 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000937 Action = TargetLowering::Legal;
938 } else {
939 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000940 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000941 break;
942 }
943
944 if (SimpleFinishLegalizing) {
945 SmallVector<SDValue, 8> Ops, ResultVals;
946 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
947 Ops.push_back(LegalizeOp(Node->getOperand(i)));
948 switch (Node->getOpcode()) {
949 default: break;
950 case ISD::BR:
951 case ISD::BRIND:
952 case ISD::BR_JT:
953 case ISD::BR_CC:
954 case ISD::BRCOND:
Stuart Hastingsfc521632011-04-19 16:16:58 +0000955 assert(depthCALLSEQ == 1 && "branch inside CALLSEQ_BEGIN/END?");
956 // Branches tweak the chain to include LastCALLSEQ
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Stuart Hastingsfc521632011-04-19 16:16:58 +0000958 getLastCALLSEQ());
Eli Friedman8c377c72009-05-27 01:25:56 +0000959 Ops[0] = LegalizeOp(Ops[0]);
Stuart Hastingsfc521632011-04-19 16:16:58 +0000960 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedman8c377c72009-05-27 01:25:56 +0000961 break;
962 case ISD::SHL:
963 case ISD::SRL:
964 case ISD::SRA:
965 case ISD::ROTL:
966 case ISD::ROTR:
967 // Legalizing shifts/rotates requires adjusting the shift amount
968 // to the appropriate width.
969 if (!Ops[1].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000970 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
971 Ops[1]));
Eli Friedman8c377c72009-05-27 01:25:56 +0000972 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000973 case ISD::SRL_PARTS:
974 case ISD::SRA_PARTS:
975 case ISD::SHL_PARTS:
976 // Legalizing shifts/rotates requires adjusting the shift amount
977 // to the appropriate width.
978 if (!Ops[2].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000979 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
980 Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000981 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000982 }
983
Dan Gohman027657d2010-06-18 15:30:29 +0000984 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
985 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000986 switch (Action) {
987 case TargetLowering::Legal:
988 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
989 ResultVals.push_back(Result.getValue(i));
990 break;
991 case TargetLowering::Custom:
992 // FIXME: The handling for custom lowering with multiple results is
993 // a complete mess.
994 Tmp1 = TLI.LowerOperation(Result, DAG);
995 if (Tmp1.getNode()) {
996 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
997 if (e == 1)
998 ResultVals.push_back(Tmp1);
999 else
1000 ResultVals.push_back(Tmp1.getValue(i));
1001 }
1002 break;
1003 }
1004
1005 // FALL THROUGH
1006 case TargetLowering::Expand:
1007 ExpandNode(Result.getNode(), ResultVals);
1008 break;
1009 case TargetLowering::Promote:
1010 PromoteNode(Result.getNode(), ResultVals);
1011 break;
1012 }
1013 if (!ResultVals.empty()) {
1014 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
1015 if (ResultVals[i] != SDValue(Node, i))
1016 ResultVals[i] = LegalizeOp(ResultVals[i]);
1017 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
1018 }
1019 return ResultVals[Op.getResNo()];
1020 }
1021 }
1022
1023 switch (Node->getOpcode()) {
1024 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001025#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001026 dbgs() << "NODE: ";
1027 Node->dump( &DAG);
1028 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001029#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001030 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001031
Chris Lattnerb2827b02006-03-19 00:52:58 +00001032 case ISD::BUILD_VECTOR:
1033 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001034 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001035 case TargetLowering::Custom:
1036 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001037 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001038 Result = Tmp3;
1039 break;
1040 }
1041 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001042 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001043 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001044 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001045 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001046 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001047 case ISD::CALLSEQ_START: {
1048 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Stuart Hastingsfc521632011-04-19 16:16:58 +00001049 assert(CallEnd && "didn't find CALLSEQ_END!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001050
Chris Lattner6831a812006-02-13 09:18:02 +00001051 // Recursively Legalize all of the inputs of the call end that do not lead
1052 // to this call start. This ensures that any libcalls that need be inserted
1053 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001054 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001055 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001056 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001057 NodesLeadingTo);
1058 }
Chris Lattner6831a812006-02-13 09:18:02 +00001059
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001060 // Now that we have legalized all of the inputs (which may have inserted
1061 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1063
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001064 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001065 // call ended.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001066 if (getLastCALLSEQ().getOpcode() != ISD::EntryToken) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001067 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Stuart Hastingsfc521632011-04-19 16:16:58 +00001068 Tmp1, getLastCALLSEQ());
Chris Lattnerb248e162006-05-17 17:55:45 +00001069 Tmp1 = LegalizeOp(Tmp1);
1070 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Chris Lattner6831a812006-02-13 09:18:02 +00001072 // Do not try to legalize the target-specific arguments (#1+).
1073 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001074 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001075 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001076 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1077 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Chris Lattner6831a812006-02-13 09:18:02 +00001080 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001081 AddLegalizedOperand(Op.getValue(0), Result);
1082 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1083 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001084
Chris Lattner6831a812006-02-13 09:18:02 +00001085 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001086 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001087 // process, no libcalls can/will be inserted, guaranteeing that no calls
1088 // can overlap.
Chris Lattner6831a812006-02-13 09:18:02 +00001089 // Note that we are selecting this call!
Stuart Hastingsfc521632011-04-19 16:16:58 +00001090 setLastCALLSEQ(SDValue(CallEnd, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001091
Chris Lattner6831a812006-02-13 09:18:02 +00001092 // Legalize the call, starting from the CALLSEQ_END.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001093 LegalizeOp(getLastCALLSEQ());
Chris Lattner6831a812006-02-13 09:18:02 +00001094 return Result;
1095 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001096 case ISD::CALLSEQ_END:
Stuart Hastingsfc521632011-04-19 16:16:58 +00001097 {
1098 SDNode *myCALLSEQ_BEGIN = FindCallStartFromCallEnd(Node);
1099
1100 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1101 // will cause this node to be legalized as well as handling libcalls right.
1102 if (getLastCALLSEQ().getNode() != Node) {
1103 LegalizeOp(SDValue(myCALLSEQ_BEGIN, 0));
1104 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
1105 assert(I != LegalizedNodes.end() &&
1106 "Legalizing the call start should have legalized this node!");
1107 return I->second;
1108 }
1109
1110 pushLastCALLSEQ(SDValue(myCALLSEQ_BEGIN, 0));
Chris Lattner6831a812006-02-13 09:18:02 +00001111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001112
1113 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001114 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001115 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001116 // Do not try to legalize the target-specific arguments (#1+), except for
1117 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001118 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001119 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001120 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001121 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001122 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1123 &Ops[0], Ops.size()),
1124 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001125 }
1126 } else {
1127 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1128 if (Tmp1 != Node->getOperand(0) ||
1129 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001130 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001131 Ops[0] = Tmp1;
1132 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001133 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1134 &Ops[0], Ops.size()),
1135 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001136 }
Chris Lattner6a542892006-01-24 05:48:21 +00001137 }
Chris Lattner6831a812006-02-13 09:18:02 +00001138 // This finishes up call legalization.
Stuart Hastingsfc521632011-04-19 16:16:58 +00001139 popLastCALLSEQ();
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001140
Chris Lattner4b653a02006-02-14 00:55:02 +00001141 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001142 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001143 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001144 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001145 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001146 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001147 LoadSDNode *LD = cast<LoadSDNode>(Node);
1148 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1149 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001150
Evan Cheng466685d2006-10-09 20:57:25 +00001151 ISD::LoadExtType ExtType = LD->getExtensionType();
1152 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001153 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001154 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1155 Tmp1, Tmp2, LD->getOffset()),
1156 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001157 Tmp3 = Result.getValue(0);
1158 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001159
Evan Cheng466685d2006-10-09 20:57:25 +00001160 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001161 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001162 case TargetLowering::Legal:
1163 // If this is an unaligned load and the target doesn't support it,
1164 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001165 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001166 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1167 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001168 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001169 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001170 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001171 Tmp3 = Result.getOperand(0);
1172 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001173 Tmp3 = LegalizeOp(Tmp3);
1174 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001175 }
1176 }
1177 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001178 case TargetLowering::Custom:
1179 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001180 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001181 Tmp3 = LegalizeOp(Tmp1);
1182 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001183 }
Evan Cheng466685d2006-10-09 20:57:25 +00001184 break;
1185 case TargetLowering::Promote: {
1186 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001187 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001188 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001189 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001190
Chris Lattnerecf42c42010-09-21 16:36:31 +00001191 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001192 LD->isVolatile(), LD->isNonTemporal(),
1193 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001195 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001196 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001197 }
Evan Cheng466685d2006-10-09 20:57:25 +00001198 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001199 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001200 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001201 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1202 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001203 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001204 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001205
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001206 EVT SrcVT = LD->getMemoryVT();
1207 unsigned SrcWidth = SrcVT.getSizeInBits();
1208 unsigned Alignment = LD->getAlignment();
1209 bool isVolatile = LD->isVolatile();
1210 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001211
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001212 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1213 // Some targets pretend to have an i1 loading operation, and actually
1214 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1215 // bits are guaranteed to be zero; it helps the optimizers understand
1216 // that these bits are zero. It is also useful for EXTLOAD, since it
1217 // tells the optimizers that those bits are undefined. It would be
1218 // nice to have an effective generic way of getting these benefits...
1219 // Until such a way is found, don't insist on promoting i1 here.
1220 (SrcVT != MVT::i1 ||
1221 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1222 // Promote to a byte-sized load if not loading an integral number of
1223 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1224 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1225 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1226 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001227
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001228 // The extra bits are guaranteed to be zero, since we stored them that
1229 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001230
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001231 ISD::LoadExtType NewExtType =
1232 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001233
Stuart Hastingsa9011292011-02-16 16:23:55 +00001234 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001235 Tmp1, Tmp2, LD->getPointerInfo(),
1236 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001237
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001238 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001239
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001240 if (ExtType == ISD::SEXTLOAD)
1241 // Having the top bits zero doesn't help when sign extending.
1242 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1243 Result.getValueType(),
1244 Result, DAG.getValueType(SrcVT));
1245 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1246 // All the top bits are guaranteed to be zero - inform the optimizers.
1247 Result = DAG.getNode(ISD::AssertZext, dl,
1248 Result.getValueType(), Result,
1249 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001250
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001251 Tmp1 = LegalizeOp(Result);
1252 Tmp2 = LegalizeOp(Ch);
1253 } else if (SrcWidth & (SrcWidth - 1)) {
1254 // If not loading a power-of-2 number of bits, expand as two loads.
1255 assert(!SrcVT.isVector() && "Unsupported extload!");
1256 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1257 assert(RoundWidth < SrcWidth);
1258 unsigned ExtraWidth = SrcWidth - RoundWidth;
1259 assert(ExtraWidth < RoundWidth);
1260 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1261 "Load size not an integral number of bytes!");
1262 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1263 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1264 SDValue Lo, Hi, Ch;
1265 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001266
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001267 if (TLI.isLittleEndian()) {
1268 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1269 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001270 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001271 Tmp1, Tmp2,
1272 LD->getPointerInfo(), RoundVT, isVolatile,
1273 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001274
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001275 // Load the remaining ExtraWidth bits.
1276 IncrementSize = RoundWidth / 8;
1277 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1278 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001279 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001280 LD->getPointerInfo().getWithOffset(IncrementSize),
1281 ExtraVT, isVolatile, isNonTemporal,
1282 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001283
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001284 // Build a factor node to remember that this load is independent of
1285 // the other one.
1286 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1287 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001288
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001289 // Move the top bits to the right place.
1290 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001291 DAG.getConstant(RoundWidth,
1292 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001293
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001294 // Join the hi and lo parts.
1295 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001296 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001297 // Big endian - avoid unaligned loads.
1298 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1299 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001300 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001301 LD->getPointerInfo(), RoundVT, isVolatile,
1302 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001303
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001304 // Load the remaining ExtraWidth bits.
1305 IncrementSize = RoundWidth / 8;
1306 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1307 DAG.getIntPtrConstant(IncrementSize));
1308 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001309 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001310 LD->getPointerInfo().getWithOffset(IncrementSize),
1311 ExtraVT, isVolatile, isNonTemporal,
1312 MinAlign(Alignment, IncrementSize));
1313
1314 // Build a factor node to remember that this load is independent of
1315 // the other one.
1316 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1317 Hi.getValue(1));
1318
1319 // Move the top bits to the right place.
1320 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001321 DAG.getConstant(ExtraWidth,
1322 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001323
1324 // Join the hi and lo parts.
1325 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001326 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001327
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001328 Tmp1 = LegalizeOp(Result);
1329 Tmp2 = LegalizeOp(Ch);
1330 } else {
1331 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1332 default: assert(0 && "This action is not supported yet!");
1333 case TargetLowering::Custom:
1334 isCustom = true;
1335 // FALLTHROUGH
1336 case TargetLowering::Legal:
1337 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1338 Tmp1, Tmp2, LD->getOffset()),
1339 Result.getResNo());
1340 Tmp1 = Result.getValue(0);
1341 Tmp2 = Result.getValue(1);
1342
1343 if (isCustom) {
1344 Tmp3 = TLI.LowerOperation(Result, DAG);
1345 if (Tmp3.getNode()) {
1346 Tmp1 = LegalizeOp(Tmp3);
1347 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1348 }
1349 } else {
1350 // If this is an unaligned load and the target doesn't support it,
1351 // expand it.
1352 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1353 const Type *Ty =
1354 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1355 unsigned ABIAlignment =
1356 TLI.getTargetData()->getABITypeAlignment(Ty);
1357 if (LD->getAlignment() < ABIAlignment){
1358 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1359 DAG, TLI);
1360 Tmp1 = Result.getOperand(0);
1361 Tmp2 = Result.getOperand(1);
1362 Tmp1 = LegalizeOp(Tmp1);
1363 Tmp2 = LegalizeOp(Tmp2);
1364 }
1365 }
1366 }
1367 break;
1368 case TargetLowering::Expand:
1369 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1370 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1371 LD->getPointerInfo(),
1372 LD->isVolatile(), LD->isNonTemporal(),
1373 LD->getAlignment());
1374 unsigned ExtendOp;
1375 switch (ExtType) {
1376 case ISD::EXTLOAD:
1377 ExtendOp = (SrcVT.isFloatingPoint() ?
1378 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1379 break;
1380 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1381 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1382 default: llvm_unreachable("Unexpected extend load type!");
1383 }
1384 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1385 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1386 Tmp2 = LegalizeOp(Load.getValue(1));
1387 break;
1388 }
1389 // FIXME: This does not work for vectors on most targets. Sign- and
1390 // zero-extend operations are currently folded into extending loads,
1391 // whether they are legal or not, and then we end up here without any
1392 // support for legalizing them.
1393 assert(ExtType != ISD::EXTLOAD &&
1394 "EXTLOAD should always be supported!");
1395 // Turn the unsupported load into an EXTLOAD followed by an explicit
1396 // zero/sign extend inreg.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001397 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001398 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1399 LD->isVolatile(), LD->isNonTemporal(),
1400 LD->getAlignment());
1401 SDValue ValRes;
1402 if (ExtType == ISD::SEXTLOAD)
1403 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1404 Result.getValueType(),
1405 Result, DAG.getValueType(SrcVT));
1406 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001407 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001408 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1409 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1410 break;
1411 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001412 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001413
1414 // Since loads produce two values, make sure to remember that we legalized
1415 // both of them.
1416 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1417 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1418 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001419 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001420 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001421 StoreSDNode *ST = cast<StoreSDNode>(Node);
1422 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1423 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001424 unsigned Alignment = ST->getAlignment();
1425 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001426 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001427
Evan Cheng8b2794a2006-10-13 21:14:26 +00001428 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001429 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1430 Result = SDValue(OptStore, 0);
1431 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001433
Eli Friedman957bffa2009-05-24 08:42:01 +00001434 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001435 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001436 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1437 Tmp1, Tmp3, Tmp2,
1438 ST->getOffset()),
1439 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001440
Owen Andersone50ed302009-08-10 22:56:29 +00001441 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001442 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001443 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001444 case TargetLowering::Legal:
1445 // If this is an unaligned store and the target doesn't support it,
1446 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001447 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001448 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001449 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001450 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001451 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1452 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001453 }
1454 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001455 case TargetLowering::Custom:
1456 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001457 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001458 break;
1459 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001460 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001461 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001462 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001463 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001464 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001465 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001466 break;
1467 }
1468 break;
1469 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001470 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001471 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001472
Owen Andersone50ed302009-08-10 22:56:29 +00001473 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001474 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001475
Duncan Sands83ec4b62008-06-06 12:08:01 +00001476 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001477 // Promote to a byte-sized store with upper bits zero if not
1478 // storing an integral number of bytes. For example, promote
1479 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001480 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1481 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001482 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001483 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1484 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001485 } else if (StWidth & (StWidth - 1)) {
1486 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001487 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001488 unsigned RoundWidth = 1 << Log2_32(StWidth);
1489 assert(RoundWidth < StWidth);
1490 unsigned ExtraWidth = StWidth - RoundWidth;
1491 assert(ExtraWidth < RoundWidth);
1492 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1493 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001494 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1495 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001496 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001497 unsigned IncrementSize;
1498
1499 if (TLI.isLittleEndian()) {
1500 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1501 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001502 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1503 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001504 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001505
1506 // Store the remaining ExtraWidth bits.
1507 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001508 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001509 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001510 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001511 DAG.getConstant(RoundWidth,
1512 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001513 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1514 ST->getPointerInfo().getWithOffset(IncrementSize),
1515 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001516 MinAlign(Alignment, IncrementSize));
1517 } else {
1518 // Big endian - avoid unaligned stores.
1519 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1520 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001521 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001522 DAG.getConstant(ExtraWidth,
1523 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001524 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1525 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001526
1527 // Store the remaining ExtraWidth bits.
1528 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001529 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001530 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001531 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1532 ST->getPointerInfo().getWithOffset(IncrementSize),
1533 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001534 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001535 }
Duncan Sands7e857202008-01-22 07:17:34 +00001536
1537 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001539 } else {
1540 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1541 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001542 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1543 Tmp1, Tmp3, Tmp2,
1544 ST->getOffset()),
1545 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001546
1547 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001548 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001549 case TargetLowering::Legal:
1550 // If this is an unaligned store and the target doesn't support it,
1551 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001552 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001553 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001554 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001555 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001556 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1557 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001558 }
1559 break;
1560 case TargetLowering::Custom:
1561 Result = TLI.LowerOperation(Result, DAG);
1562 break;
1563 case Expand:
1564 // TRUNCSTORE:i16 i32 -> STORE i16
1565 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001566 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001567 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1568 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001569 break;
1570 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001571 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001572 }
1573 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001574 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001575 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001576 assert(Result.getValueType() == Op.getValueType() &&
1577 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001578
Chris Lattner456a93a2006-01-28 07:39:30 +00001579 // Make sure that the generated code is itself legal.
1580 if (Result != Op)
1581 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001582
Chris Lattner45982da2005-05-12 16:53:42 +00001583 // Note that LegalizeOp may be reentered even from single-use nodes, which
1584 // means that we always must cache transformed nodes.
1585 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001586 return Result;
1587}
1588
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001589SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1590 SDValue Vec = Op.getOperand(0);
1591 SDValue Idx = Op.getOperand(1);
1592 DebugLoc dl = Op.getDebugLoc();
1593 // Store the value to a temporary stack slot, then LOAD the returned part.
1594 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001595 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1596 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001597
1598 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001599 unsigned EltSize =
1600 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001601 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1602 DAG.getConstant(EltSize, Idx.getValueType()));
1603
1604 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1605 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1606 else
1607 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1608
1609 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1610
Eli Friedmanc680ac92009-07-09 22:01:03 +00001611 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001612 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001613 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001614 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001615 MachinePointerInfo(),
1616 Vec.getValueType().getVectorElementType(),
1617 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001618}
1619
David Greenecfe33c42011-01-26 19:13:22 +00001620SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1621 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1622
1623 SDValue Vec = Op.getOperand(0);
1624 SDValue Part = Op.getOperand(1);
1625 SDValue Idx = Op.getOperand(2);
1626 DebugLoc dl = Op.getDebugLoc();
1627
1628 // Store the value to a temporary stack slot, then LOAD the returned part.
1629
1630 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1631 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1632 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1633
1634 // First store the whole vector.
1635 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1636 false, false, 0);
1637
1638 // Then store the inserted part.
1639
1640 // Add the offset to the index.
1641 unsigned EltSize =
1642 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1643
1644 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1645 DAG.getConstant(EltSize, Idx.getValueType()));
1646
1647 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1648 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1649 else
1650 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1651
1652 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1653 StackPtr);
1654
1655 // Store the subvector.
1656 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1657 MachinePointerInfo(), false, false, 0);
1658
1659 // Finally, load the updated vector.
1660 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1661 false, false, 0);
1662}
1663
Eli Friedman7ef3d172009-06-06 07:04:42 +00001664SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1665 // We can't handle this case efficiently. Allocate a sufficiently
1666 // aligned object on the stack, store each element into it, then load
1667 // the result as a vector.
1668 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001669 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001670 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001671 DebugLoc dl = Node->getDebugLoc();
1672 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001673 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001674 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001675
1676 // Emit a store of each element to the stack slot.
1677 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001678 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001679 // Store (in the right endianness) the elements to memory.
1680 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1681 // Ignore undef elements.
1682 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1683
1684 unsigned Offset = TypeByteSize*i;
1685
1686 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1687 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1688
Dan Gohman9949dd62010-02-25 20:30:49 +00001689 // If the destination vector element type is narrower than the source
1690 // element type, only store the bits necessary.
1691 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001692 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001693 Node->getOperand(i), Idx,
1694 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001695 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001696 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001697 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001698 Node->getOperand(i), Idx,
1699 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001700 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001701 }
1702
1703 SDValue StoreChain;
1704 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001706 &Stores[0], Stores.size());
1707 else
1708 StoreChain = DAG.getEntryNode();
1709
1710 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001711 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001712}
1713
Eli Friedman4bc8c712009-05-27 12:20:41 +00001714SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1715 DebugLoc dl = Node->getDebugLoc();
1716 SDValue Tmp1 = Node->getOperand(0);
1717 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001718
1719 // Get the sign bit of the RHS. First obtain a value that has the same
1720 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001721 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001722 EVT FloatVT = Tmp2.getValueType();
1723 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001724 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001725 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001726 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001727 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001728 // Store the float to memory, then load the sign part out as an integer.
1729 MVT LoadTy = TLI.getPointerTy();
1730 // First create a temporary that is aligned for both the load and store.
1731 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1732 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001733 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001734 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001735 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001736 if (TLI.isBigEndian()) {
1737 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1738 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001739 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1740 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001741 } else { // Little endian
1742 SDValue LoadPtr = StackPtr;
1743 // The float may be wider than the integer we are going to load. Advance
1744 // the pointer so that the loaded integer will contain the sign bit.
1745 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1746 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1747 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1748 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1749 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001750 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1751 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001752 // Move the sign bit to the top bit of the loaded integer.
1753 unsigned BitShift = LoadTy.getSizeInBits() -
1754 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1755 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1756 if (BitShift)
1757 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001758 DAG.getConstant(BitShift,
1759 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001760 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001761 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001762 // Now get the sign bit proper, by seeing whether the value is negative.
1763 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1764 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1765 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001766 // Get the absolute value of the result.
1767 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1768 // Select between the nabs and abs value based on the sign bit of
1769 // the input.
1770 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1771 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1772 AbsVal);
1773}
1774
Eli Friedman4bc8c712009-05-27 12:20:41 +00001775void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1776 SmallVectorImpl<SDValue> &Results) {
1777 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1778 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1779 " not tell us which reg is the stack pointer!");
1780 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001781 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001782 SDValue Tmp1 = SDValue(Node, 0);
1783 SDValue Tmp2 = SDValue(Node, 1);
1784 SDValue Tmp3 = Node->getOperand(2);
1785 SDValue Chain = Tmp1.getOperand(0);
1786
1787 // Chain the dynamic stack allocation so that it doesn't modify the stack
1788 // pointer when other instructions are using the stack.
1789 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1790
1791 SDValue Size = Tmp2.getOperand(1);
1792 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1793 Chain = SP.getValue(1);
1794 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001795 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001796 if (Align > StackAlign)
1797 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1798 DAG.getConstant(-(uint64_t)Align, VT));
1799 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1800 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1801
1802 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1803 DAG.getIntPtrConstant(0, true), SDValue());
1804
1805 Results.push_back(Tmp1);
1806 Results.push_back(Tmp2);
1807}
1808
Evan Cheng7f042682008-10-15 02:05:31 +00001809/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001810/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001811/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001812void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001813 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001814 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001815 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001816 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001817 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1818 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001819 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001820 case TargetLowering::Legal:
1821 // Nothing to do.
1822 break;
1823 case TargetLowering::Expand: {
1824 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1825 unsigned Opc = 0;
1826 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001827 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001828 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1829 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1830 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1831 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1832 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1833 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1834 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1835 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1836 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1837 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1838 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1839 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001840 // FIXME: Implement more expansions.
1841 }
1842
Dale Johannesenbb5da912009-02-02 20:41:04 +00001843 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1844 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1845 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001846 RHS = SDValue();
1847 CC = SDValue();
1848 break;
1849 }
1850 }
1851}
1852
Chris Lattner1401d152008-01-16 07:45:30 +00001853/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1854/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1855/// a load from the stack slot to DestVT, extending it if needed.
1856/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001857SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001858 EVT SlotVT,
1859 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001860 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001861 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001862 unsigned SrcAlign =
1863 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001864 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001866
Evan Chengff89dcb2009-10-18 18:16:27 +00001867 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1868 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001869 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001870
Duncan Sands83ec4b62008-06-06 12:08:01 +00001871 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1872 unsigned SlotSize = SlotVT.getSizeInBits();
1873 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001874 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1875 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001876
Chris Lattner1401d152008-01-16 07:45:30 +00001877 // Emit a store to the stack slot. Use a truncstore if the input value is
1878 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001879 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001880
Chris Lattner1401d152008-01-16 07:45:30 +00001881 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001882 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001883 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001884 else {
1885 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001886 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001887 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
Chris Lattner35481892005-12-23 00:16:34 +00001890 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001891 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001892 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001893 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001894
Chris Lattner1401d152008-01-16 07:45:30 +00001895 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001896 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001897 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001898}
1899
Dan Gohman475871a2008-07-27 21:46:04 +00001900SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001901 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001902 // Create a vector sized/aligned stack slot, store the value to element #0,
1903 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001904 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001905
Evan Chengff89dcb2009-10-18 18:16:27 +00001906 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1907 int SPFI = StackPtrFI->getIndex();
1908
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001909 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1910 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001911 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001912 Node->getValueType(0).getVectorElementType(),
1913 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001914 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001915 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001916 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001917}
1918
1919
Chris Lattnerce872152006-03-19 06:31:19 +00001920/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001921/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001922SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001923 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001924 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001925 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001926 EVT VT = Node->getValueType(0);
1927 EVT OpVT = Node->getOperand(0).getValueType();
1928 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
1930 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001931 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001932 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001933 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001934 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001935 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001936 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001937 if (V.getOpcode() == ISD::UNDEF)
1938 continue;
1939 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001940 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001941 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001942 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001943
1944 if (!Value1.getNode()) {
1945 Value1 = V;
1946 } else if (!Value2.getNode()) {
1947 if (V != Value1)
1948 Value2 = V;
1949 } else if (V != Value1 && V != Value2) {
1950 MoreThanTwoValues = true;
1951 }
Chris Lattnerce872152006-03-19 06:31:19 +00001952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001953
Eli Friedman7a5e5552009-06-07 06:52:44 +00001954 if (!Value1.getNode())
1955 return DAG.getUNDEF(VT);
1956
1957 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001958 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001959
Chris Lattner2eb86532006-03-24 07:29:17 +00001960 // If all elements are constants, create a load from the constant pool.
1961 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001962 std::vector<Constant*> CV;
1963 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001964 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001965 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001966 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001967 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001968 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001969 if (OpVT==EltVT)
1970 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1971 else {
1972 // If OpVT and EltVT don't match, EltVT is not legal and the
1973 // element values have been promoted/truncated earlier. Undo this;
1974 // we don't want a v16i8 to become a v16i32 for example.
1975 const ConstantInt *CI = V->getConstantIntValue();
1976 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1977 CI->getZExtValue()));
1978 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001979 } else {
1980 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001981 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001982 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001983 }
1984 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001985 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001986 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001987 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001988 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001989 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001990 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001991 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001992
Eli Friedman7a5e5552009-06-07 06:52:44 +00001993 if (!MoreThanTwoValues) {
1994 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1995 for (unsigned i = 0; i < NumElems; ++i) {
1996 SDValue V = Node->getOperand(i);
1997 if (V.getOpcode() == ISD::UNDEF)
1998 continue;
1999 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2000 }
2001 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00002002 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00002003 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2004 SDValue Vec2;
2005 if (Value2.getNode())
2006 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2007 else
2008 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Chris Lattner87100e02006-03-20 01:52:29 +00002010 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00002011 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00002012 }
2013 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002014
Eli Friedman7ef3d172009-06-06 07:04:42 +00002015 // Otherwise, we can't handle this case efficiently.
2016 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00002017}
2018
Chris Lattner77e77a62005-01-21 06:05:23 +00002019// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2020// does not fit into a register, return the lo part and set the hi part to the
2021// by-reg argument. If it does fit into a single register, return the result
2022// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002023SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002024 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002025 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002026 // Legalizing the call will automatically add the previous call to the
2027 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002029
Chris Lattner77e77a62005-01-21 06:05:23 +00002030 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002031 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002032 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002033 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00002034 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002035 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002036 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002037 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002038 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002039 }
Bill Wendling056292f2008-09-16 21:48:12 +00002040 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002041 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002042
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002043 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00002044 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002045
2046 // isTailCall may be true since the callee does not reference caller stack
2047 // frame. Check if it's in the right position.
2048 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002049 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002050 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002051 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002053 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002054
Evan Cheng3d2125c2010-11-30 23:55:39 +00002055 if (!CallInfo.second.getNode())
2056 // It's a tailcall, return the chain (which is the DAG root).
2057 return DAG.getRoot();
2058
Chris Lattner6831a812006-02-13 09:18:02 +00002059 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002060 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Chris Lattner6831a812006-02-13 09:18:02 +00002061 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2062 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002063 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002064}
2065
Jim Grosbache03262f2010-06-18 21:43:38 +00002066// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2067// ExpandLibCall except that the first operand is the in-chain.
2068std::pair<SDValue, SDValue>
2069SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2070 SDNode *Node,
2071 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002072 SDValue InChain = Node->getOperand(0);
2073
2074 TargetLowering::ArgListTy Args;
2075 TargetLowering::ArgListEntry Entry;
2076 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2077 EVT ArgVT = Node->getOperand(i).getValueType();
2078 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2079 Entry.Node = Node->getOperand(i);
2080 Entry.Ty = ArgTy;
2081 Entry.isSExt = isSigned;
2082 Entry.isZExt = !isSigned;
2083 Args.push_back(Entry);
2084 }
2085 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2086 TLI.getPointerTy());
2087
2088 // Splice the libcall in wherever FindInputOutputChains tells us to.
2089 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2090 std::pair<SDValue, SDValue> CallInfo =
2091 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002092 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002093 /*isReturnValueUsed=*/true,
2094 Callee, Args, DAG, Node->getDebugLoc());
2095
2096 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002097 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Jim Grosbache03262f2010-06-18 21:43:38 +00002098 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2099 LegalizeOp(CallInfo.second);
2100 return CallInfo;
2101}
2102
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002103SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2104 RTLIB::Libcall Call_F32,
2105 RTLIB::Libcall Call_F64,
2106 RTLIB::Libcall Call_F80,
2107 RTLIB::Libcall Call_PPCF128) {
2108 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002109 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002110 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002111 case MVT::f32: LC = Call_F32; break;
2112 case MVT::f64: LC = Call_F64; break;
2113 case MVT::f80: LC = Call_F80; break;
2114 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002115 }
2116 return ExpandLibCall(LC, Node, false);
2117}
2118
2119SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002120 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002121 RTLIB::Libcall Call_I16,
2122 RTLIB::Libcall Call_I32,
2123 RTLIB::Libcall Call_I64,
2124 RTLIB::Libcall Call_I128) {
2125 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002127 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002128 case MVT::i8: LC = Call_I8; break;
2129 case MVT::i16: LC = Call_I16; break;
2130 case MVT::i32: LC = Call_I32; break;
2131 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002133 }
2134 return ExpandLibCall(LC, Node, isSigned);
2135}
2136
Evan Cheng65279cb2011-04-16 03:08:26 +00002137/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
2138static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
2139 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002140 RTLIB::Libcall LC;
2141 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2142 default: assert(0 && "Unexpected request for libcall!");
2143 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2144 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2145 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2146 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2147 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2148 }
2149
Evan Cheng65279cb2011-04-16 03:08:26 +00002150 return TLI.getLibcallName(LC) != 0;
2151}
Evan Cheng8e23e812011-04-01 00:42:02 +00002152
Evan Cheng65279cb2011-04-16 03:08:26 +00002153/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
2154/// needed.
2155static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002156 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00002157 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00002158 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002159 else
Evan Cheng8e23e812011-04-01 00:42:02 +00002160 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002161
Evan Cheng8e23e812011-04-01 00:42:02 +00002162 SDValue Op0 = Node->getOperand(0);
2163 SDValue Op1 = Node->getOperand(1);
2164 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2165 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2166 SDNode *User = *UI;
2167 if (User == Node)
2168 continue;
2169 if (User->getOpcode() == OtherOpcode &&
2170 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00002171 User->getOperand(1) == Op1)
2172 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00002173 }
Evan Cheng65279cb2011-04-16 03:08:26 +00002174 return false;
2175}
Evan Cheng8e23e812011-04-01 00:42:02 +00002176
Evan Cheng65279cb2011-04-16 03:08:26 +00002177/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
2178/// pairs.
2179void
2180SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2181 SmallVectorImpl<SDValue> &Results) {
2182 unsigned Opcode = Node->getOpcode();
2183 bool isSigned = Opcode == ISD::SDIVREM;
2184
2185 RTLIB::Libcall LC;
2186 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2187 default: assert(0 && "Unexpected request for libcall!");
2188 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2189 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2190 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2191 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2192 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00002193 }
2194
2195 // The input chain to this libcall is the entry node of the function.
2196 // Legalizing the call will automatically add the previous call to the
2197 // dependence.
2198 SDValue InChain = DAG.getEntryNode();
2199
2200 EVT RetVT = Node->getValueType(0);
2201 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2202
2203 TargetLowering::ArgListTy Args;
2204 TargetLowering::ArgListEntry Entry;
2205 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2206 EVT ArgVT = Node->getOperand(i).getValueType();
2207 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2208 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
2209 Entry.isSExt = isSigned;
2210 Entry.isZExt = !isSigned;
2211 Args.push_back(Entry);
2212 }
2213
2214 // Also pass the return address of the remainder.
2215 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2216 Entry.Node = FIPtr;
2217 Entry.Ty = RetTy->getPointerTo();
2218 Entry.isSExt = isSigned;
2219 Entry.isZExt = !isSigned;
2220 Args.push_back(Entry);
2221
2222 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2223 TLI.getPointerTy());
2224
2225 // Splice the libcall in wherever FindInputOutputChains tells us to.
2226 DebugLoc dl = Node->getDebugLoc();
2227 std::pair<SDValue, SDValue> CallInfo =
2228 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
2229 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
2230 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
2231
2232 // Legalize the call sequence, starting with the chain. This will advance
Stuart Hastingsfc521632011-04-19 16:16:58 +00002233 // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that
Evan Cheng8e23e812011-04-01 00:42:02 +00002234 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2235 LegalizeOp(CallInfo.second);
2236
2237 // Remainder is loaded back from the stack frame.
Stuart Hastingsfc521632011-04-19 16:16:58 +00002238 SDValue Rem = DAG.getLoad(RetVT, dl, getLastCALLSEQ(), FIPtr,
Evan Cheng8e23e812011-04-01 00:42:02 +00002239 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00002240 Results.push_back(CallInfo.first);
2241 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00002242}
2243
Chris Lattner22cde6a2006-01-28 08:25:58 +00002244/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2245/// INT_TO_FP operation of the specified operand when the target requests that
2246/// we expand it. At this point, we know that the result and operand types are
2247/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002248SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2249 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002250 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002251 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002252 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002253 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002254
Chris Lattner23594d42008-01-16 07:03:22 +00002255 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002256 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002257
Chris Lattner22cde6a2006-01-28 08:25:58 +00002258 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002259 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002260 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002261 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002262 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002263 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002264 if (TLI.isLittleEndian())
2265 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002266
Chris Lattner22cde6a2006-01-28 08:25:58 +00002267 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002269 if (isSigned) {
2270 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002271 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2272 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002273 } else {
2274 Op0Mapped = Op0;
2275 }
2276 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002277 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002278 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002279 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002280 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002282 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002283 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2284 MachinePointerInfo(),
2285 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002286 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002287 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2288 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002289 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002290 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002291 BitsToDouble(0x4330000080000000ULL) :
2292 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002293 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002294 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002296 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002297 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002298 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002300 // do nothing
2301 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002302 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002303 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002304 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002305 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002306 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002307 }
2308 return Result;
2309 }
2310 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002311 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002312
2313 // Implementation of unsigned i64 to f64 following the algorithm in
2314 // __floatundidf in compiler_rt. This implementation has the advantage
2315 // of performing rounding correctly, both in the default rounding mode
2316 // and in all alternate rounding modes.
2317 // TODO: Generalize this for use with other types.
2318 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2319 SDValue TwoP52 =
2320 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2321 SDValue TwoP84PlusTwoP52 =
2322 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2323 SDValue TwoP84 =
2324 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2325
2326 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2327 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2328 DAG.getConstant(32, MVT::i64));
2329 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2330 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002331 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2332 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002333 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2334 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002335 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2336 }
2337
Owen Anderson3a9e7692010-10-05 17:24:05 +00002338 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002339 // TODO: Generalize this for use with other types.
2340 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002341 // For unsigned conversions, convert them to signed conversions using the
2342 // algorithm from the x86_64 __floatundidf in compiler_rt.
2343 if (!isSigned) {
2344 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002345
Owen Anderson95771af2011-02-25 21:41:48 +00002346 SDValue ShiftConst =
2347 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002348 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2349 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2350 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2351 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002352
Owen Anderson3a9e7692010-10-05 17:24:05 +00002353 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2354 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002355
Owen Anderson3a9e7692010-10-05 17:24:05 +00002356 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002357 // select. We happen to get lucky and machinesink does the right
2358 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002359 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002360 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002361 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2362 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2363 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002364
Owen Anderson3a9e7692010-10-05 17:24:05 +00002365 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002366
Jim Grosbach6e992612010-07-02 17:41:59 +00002367 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002368 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2369 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2370 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002371 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002372 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2373 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2374 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2375 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2376 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2377 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002378 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002379 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002380 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002381
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002382 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2383 DAG.getConstant(32, SHVT));
2384 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2385 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2386 SDValue TwoP32 =
2387 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2388 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2389 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2390 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2391 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2392 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2393 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002394 }
2395
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002396 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002397
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002398 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2399 Op0, DAG.getConstant(0, Op0.getValueType()),
2400 ISD::SETLT);
2401 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2402 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2403 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002404
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002405 // If the sign bit of the integer is set, the large number will be treated
2406 // as a negative number. To counteract this, the dynamic code adds an
2407 // offset depending on the data type.
2408 uint64_t FF;
2409 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002410 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002411 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2412 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2413 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2414 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2415 }
2416 if (TLI.isLittleEndian()) FF <<= 32;
2417 Constant *FudgeFactor = ConstantInt::get(
2418 Type::getInt64Ty(*DAG.getContext()), FF);
2419
2420 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2421 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2422 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2423 Alignment = std::min(Alignment, 4u);
2424 SDValue FudgeInReg;
2425 if (DestVT == MVT::f32)
2426 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002427 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002428 false, false, Alignment);
2429 else {
2430 FudgeInReg =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002431 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002432 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002433 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002434 MVT::f32, false, false, Alignment));
2435 }
2436
2437 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002438}
2439
2440/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2441/// *INT_TO_FP operation of the specified operand when the target requests that
2442/// we promote it. At this point, we know that the result and operand types are
2443/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2444/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002445SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002446 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002447 bool isSigned,
2448 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002449 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002450 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002451
2452 unsigned OpToUse = 0;
2453
2454 // Scan for the appropriate larger type to use.
2455 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002457 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002458
2459 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002460 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2461 OpToUse = ISD::SINT_TO_FP;
2462 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002463 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002464 if (isSigned) continue;
2465
2466 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002467 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2468 OpToUse = ISD::UINT_TO_FP;
2469 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002470 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002471
2472 // Otherwise, try a larger type.
2473 }
2474
2475 // Okay, we found the operation and type to use. Zero extend our input to the
2476 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002477 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002478 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002479 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002480}
2481
2482/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2483/// FP_TO_*INT operation of the specified operand when the target requests that
2484/// we promote it. At this point, we know that the result and operand types are
2485/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2486/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002487SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002488 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002489 bool isSigned,
2490 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002491 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002492 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002493
2494 unsigned OpToUse = 0;
2495
2496 // Scan for the appropriate larger type to use.
2497 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002499 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002500
Eli Friedman3be2e512009-05-28 03:06:16 +00002501 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002502 OpToUse = ISD::FP_TO_SINT;
2503 break;
2504 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002505
Eli Friedman3be2e512009-05-28 03:06:16 +00002506 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002507 OpToUse = ISD::FP_TO_UINT;
2508 break;
2509 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002510
2511 // Otherwise, try a larger type.
2512 }
2513
Scott Michelfdc40a02009-02-17 22:15:04 +00002514
Chris Lattner27a6c732007-11-24 07:07:01 +00002515 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002516 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002517
Chris Lattner27a6c732007-11-24 07:07:01 +00002518 // Truncate the result of the extended FP_TO_*INT operation to the desired
2519 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002520 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002521}
2522
2523/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2524///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002525SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002526 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002527 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002528 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002529 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002530 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002531 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002532 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2533 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2534 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002536 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2537 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2538 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2539 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2540 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2541 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2542 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2543 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2544 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002546 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2547 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2548 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2549 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2550 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2551 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2552 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2553 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2554 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2555 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2556 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2557 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2558 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2559 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2560 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2561 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2562 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2563 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2564 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2565 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2566 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002567 }
2568}
2569
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002570/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002571// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002572static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2573 APInt Val = APInt(NumBits, ByteVal);
2574 unsigned Shift = 8;
2575 for (unsigned i = NumBits; i > 8; i >>= 1) {
2576 Val = (Val << Shift) | Val;
2577 Shift <<= 1;
2578 }
2579 return Val;
2580}
2581
Chris Lattner22cde6a2006-01-28 08:25:58 +00002582/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2583///
Scott Michelfdc40a02009-02-17 22:15:04 +00002584SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002585 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002586 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002587 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002588 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002589 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002590 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002591 unsigned Len = VT.getSizeInBits();
2592
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002593 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2594 "CTPOP not implemented for this type.");
2595
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002596 // This is the "best" algorithm from
2597 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2598
2599 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2600 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2601 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2602 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2603
2604 // v = v - ((v >> 1) & 0x55555555...)
2605 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2606 DAG.getNode(ISD::AND, dl, VT,
2607 DAG.getNode(ISD::SRL, dl, VT, Op,
2608 DAG.getConstant(1, ShVT)),
2609 Mask55));
2610 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2611 Op = DAG.getNode(ISD::ADD, dl, VT,
2612 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2613 DAG.getNode(ISD::AND, dl, VT,
2614 DAG.getNode(ISD::SRL, dl, VT, Op,
2615 DAG.getConstant(2, ShVT)),
2616 Mask33));
2617 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2618 Op = DAG.getNode(ISD::AND, dl, VT,
2619 DAG.getNode(ISD::ADD, dl, VT, Op,
2620 DAG.getNode(ISD::SRL, dl, VT, Op,
2621 DAG.getConstant(4, ShVT))),
2622 Mask0F);
2623 // v = (v * 0x01010101...) >> (Len - 8)
2624 Op = DAG.getNode(ISD::SRL, dl, VT,
2625 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2626 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002627
Chris Lattner22cde6a2006-01-28 08:25:58 +00002628 return Op;
2629 }
2630 case ISD::CTLZ: {
2631 // for now, we do this:
2632 // x = x | (x >> 1);
2633 // x = x | (x >> 2);
2634 // ...
2635 // x = x | (x >>16);
2636 // x = x | (x >>32); // for 64-bit input
2637 // return popcount(~x);
2638 //
2639 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002640 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002641 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002642 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002643 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002644 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002645 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002646 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002647 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002648 Op = DAG.getNOT(dl, Op, VT);
2649 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002650 }
2651 case ISD::CTTZ: {
2652 // for now, we use: { return popcount(~x & (x - 1)); }
2653 // unless the target has ctlz but not ctpop, in which case we use:
2654 // { return 32 - nlz(~x & (x-1)); }
2655 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002656 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002657 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2658 DAG.getNOT(dl, Op, VT),
2659 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002660 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002661 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002662 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2663 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002664 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002665 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002666 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2667 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002668 }
2669 }
2670}
Chris Lattnere34b3962005-01-19 04:19:40 +00002671
Jim Grosbache03262f2010-06-18 21:43:38 +00002672std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2673 unsigned Opc = Node->getOpcode();
2674 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2675 RTLIB::Libcall LC;
2676
2677 switch (Opc) {
2678 default:
2679 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2680 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002681 case ISD::ATOMIC_SWAP:
2682 switch (VT.SimpleTy) {
2683 default: llvm_unreachable("Unexpected value type for atomic!");
2684 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2685 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2686 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2687 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2688 }
2689 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002690 case ISD::ATOMIC_CMP_SWAP:
2691 switch (VT.SimpleTy) {
2692 default: llvm_unreachable("Unexpected value type for atomic!");
2693 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2694 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2695 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2696 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2697 }
2698 break;
2699 case ISD::ATOMIC_LOAD_ADD:
2700 switch (VT.SimpleTy) {
2701 default: llvm_unreachable("Unexpected value type for atomic!");
2702 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2703 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2704 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2705 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2706 }
2707 break;
2708 case ISD::ATOMIC_LOAD_SUB:
2709 switch (VT.SimpleTy) {
2710 default: llvm_unreachable("Unexpected value type for atomic!");
2711 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2712 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2713 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2714 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2715 }
2716 break;
2717 case ISD::ATOMIC_LOAD_AND:
2718 switch (VT.SimpleTy) {
2719 default: llvm_unreachable("Unexpected value type for atomic!");
2720 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2721 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2722 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2723 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2724 }
2725 break;
2726 case ISD::ATOMIC_LOAD_OR:
2727 switch (VT.SimpleTy) {
2728 default: llvm_unreachable("Unexpected value type for atomic!");
2729 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2730 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2731 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2732 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2733 }
2734 break;
2735 case ISD::ATOMIC_LOAD_XOR:
2736 switch (VT.SimpleTy) {
2737 default: llvm_unreachable("Unexpected value type for atomic!");
2738 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2739 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2740 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2741 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2742 }
2743 break;
2744 case ISD::ATOMIC_LOAD_NAND:
2745 switch (VT.SimpleTy) {
2746 default: llvm_unreachable("Unexpected value type for atomic!");
2747 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2748 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2749 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2750 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2751 }
2752 break;
2753 }
2754
2755 return ExpandChainLibCall(LC, Node, false);
2756}
2757
Eli Friedman8c377c72009-05-27 01:25:56 +00002758void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2759 SmallVectorImpl<SDValue> &Results) {
2760 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002761 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002762 switch (Node->getOpcode()) {
2763 case ISD::CTPOP:
2764 case ISD::CTLZ:
2765 case ISD::CTTZ:
2766 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2767 Results.push_back(Tmp1);
2768 break;
2769 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002770 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002771 break;
2772 case ISD::FRAMEADDR:
2773 case ISD::RETURNADDR:
2774 case ISD::FRAME_TO_ARGS_OFFSET:
2775 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2776 break;
2777 case ISD::FLT_ROUNDS_:
2778 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2779 break;
2780 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002781 case ISD::EH_LABEL:
2782 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002783 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002784 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002785 case ISD::EH_SJLJ_DISPATCHSETUP:
2786 // If the target didn't expand these, there's nothing to do, so just
2787 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002788 Results.push_back(Node->getOperand(0));
2789 break;
2790 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002791 // If the target didn't expand this, just return 'zero' and preserve the
2792 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002793 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002794 Results.push_back(Node->getOperand(0));
2795 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002796 case ISD::MEMBARRIER: {
2797 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2798 TargetLowering::ArgListTy Args;
2799 std::pair<SDValue, SDValue> CallResult =
2800 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002801 false, false, false, false, 0, CallingConv::C,
2802 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002803 /*isReturnValueUsed=*/true,
2804 DAG.getExternalSymbol("__sync_synchronize",
2805 TLI.getPointerTy()),
2806 Args, DAG, dl);
2807 Results.push_back(CallResult.second);
2808 break;
2809 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002810 // By default, atomic intrinsics are marked Legal and lowered. Targets
2811 // which don't support them directly, however, may want libcalls, in which
2812 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002813 case ISD::ATOMIC_SWAP:
2814 case ISD::ATOMIC_LOAD_ADD:
2815 case ISD::ATOMIC_LOAD_SUB:
2816 case ISD::ATOMIC_LOAD_AND:
2817 case ISD::ATOMIC_LOAD_OR:
2818 case ISD::ATOMIC_LOAD_XOR:
2819 case ISD::ATOMIC_LOAD_NAND:
2820 case ISD::ATOMIC_LOAD_MIN:
2821 case ISD::ATOMIC_LOAD_MAX:
2822 case ISD::ATOMIC_LOAD_UMIN:
2823 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002824 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002825 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2826 Results.push_back(Tmp.first);
2827 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002828 break;
Evan Chenga8457062010-06-18 22:01:37 +00002829 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002830 case ISD::DYNAMIC_STACKALLOC:
2831 ExpandDYNAMIC_STACKALLOC(Node, Results);
2832 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002833 case ISD::MERGE_VALUES:
2834 for (unsigned i = 0; i < Node->getNumValues(); i++)
2835 Results.push_back(Node->getOperand(i));
2836 break;
2837 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002838 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002839 if (VT.isInteger())
2840 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002841 else {
2842 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002843 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002844 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002845 break;
2846 }
2847 case ISD::TRAP: {
2848 // If this operation is not supported, lower it to 'abort()' call
2849 TargetLowering::ArgListTy Args;
2850 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002851 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002852 false, false, false, false, 0, CallingConv::C,
2853 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002854 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002855 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002856 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002857 Results.push_back(CallResult.second);
2858 break;
2859 }
2860 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002861 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002862 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2863 Node->getValueType(0), dl);
2864 Results.push_back(Tmp1);
2865 break;
2866 case ISD::FP_EXTEND:
2867 Tmp1 = EmitStackConvert(Node->getOperand(0),
2868 Node->getOperand(0).getValueType(),
2869 Node->getValueType(0), dl);
2870 Results.push_back(Tmp1);
2871 break;
2872 case ISD::SIGN_EXTEND_INREG: {
2873 // NOTE: we could fall back on load/store here too for targets without
2874 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002875 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002876 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002877 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002878 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002879 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002880 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2881 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002882 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002883 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2884 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002885 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2886 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002887 break;
2888 }
2889 case ISD::FP_ROUND_INREG: {
2890 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002891 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002892
2893 // NOTE: there is a choice here between constantly creating new stack
2894 // slots and always reusing the same one. We currently always create
2895 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002896 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002897 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2898 Node->getValueType(0), dl);
2899 Results.push_back(Tmp1);
2900 break;
2901 }
2902 case ISD::SINT_TO_FP:
2903 case ISD::UINT_TO_FP:
2904 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2905 Node->getOperand(0), Node->getValueType(0), dl);
2906 Results.push_back(Tmp1);
2907 break;
2908 case ISD::FP_TO_UINT: {
2909 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002910 EVT VT = Node->getOperand(0).getValueType();
2911 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002912 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002913 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2914 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2915 Tmp1 = DAG.getConstantFP(apf, VT);
2916 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2917 Node->getOperand(0),
2918 Tmp1, ISD::SETLT);
2919 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002920 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2921 DAG.getNode(ISD::FSUB, dl, VT,
2922 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002923 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2924 DAG.getConstant(x, NVT));
2925 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2926 Results.push_back(Tmp1);
2927 break;
2928 }
Eli Friedman509150f2009-05-27 07:58:35 +00002929 case ISD::VAARG: {
2930 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002931 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002932 Tmp1 = Node->getOperand(0);
2933 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002934 unsigned Align = Node->getConstantOperandVal(3);
2935
Chris Lattnerecf42c42010-09-21 16:36:31 +00002936 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2937 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002938 SDValue VAList = VAListLoad;
2939
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002940 if (Align > TLI.getMinStackArgumentAlignment()) {
2941 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2942
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002943 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2944 DAG.getConstant(Align - 1,
2945 TLI.getPointerTy()));
2946
2947 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002948 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002949 TLI.getPointerTy()));
2950 }
2951
Eli Friedman509150f2009-05-27 07:58:35 +00002952 // Increment the pointer, VAList, to the next vaarg
2953 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2954 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002955 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002956 TLI.getPointerTy()));
2957 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002958 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2959 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002960 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002961 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002962 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002963 Results.push_back(Results[0].getValue(1));
2964 break;
2965 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002966 case ISD::VACOPY: {
2967 // This defaults to loading a pointer from the input and storing it to the
2968 // output, returning the chain.
2969 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2970 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2971 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002972 Node->getOperand(2), MachinePointerInfo(VS),
2973 false, false, 0);
2974 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2975 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002976 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002977 break;
2978 }
2979 case ISD::EXTRACT_VECTOR_ELT:
2980 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2981 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002982 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002983 Node->getOperand(0));
2984 else
2985 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2986 Results.push_back(Tmp1);
2987 break;
2988 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002989 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002990 break;
David Greenecfe33c42011-01-26 19:13:22 +00002991 case ISD::INSERT_SUBVECTOR:
2992 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2993 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002994 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002995 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002996 break;
2997 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002998 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002999 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00003000 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00003001 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00003002 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
3003 Node->getOperand(1),
3004 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00003005 break;
Eli Friedman509150f2009-05-27 07:58:35 +00003006 case ISD::VECTOR_SHUFFLE: {
3007 SmallVector<int, 8> Mask;
3008 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3009
Owen Andersone50ed302009-08-10 22:56:29 +00003010 EVT VT = Node->getValueType(0);
3011 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00003012 if (getTypeAction(EltVT) == Promote)
3013 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00003014 unsigned NumElems = VT.getVectorNumElements();
3015 SmallVector<SDValue, 8> Ops;
3016 for (unsigned i = 0; i != NumElems; ++i) {
3017 if (Mask[i] < 0) {
3018 Ops.push_back(DAG.getUNDEF(EltVT));
3019 continue;
3020 }
3021 unsigned Idx = Mask[i];
3022 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00003023 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3024 Node->getOperand(0),
3025 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00003026 else
Bill Wendling775db972009-12-23 00:28:23 +00003027 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3028 Node->getOperand(1),
3029 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00003030 }
3031 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
3032 Results.push_back(Tmp1);
3033 break;
3034 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003035 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00003036 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00003037 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3038 // 1 -> Hi
3039 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3040 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003041 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00003042 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3043 } else {
3044 // 0 -> Lo
3045 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3046 Node->getOperand(0));
3047 }
3048 Results.push_back(Tmp1);
3049 break;
3050 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003051 case ISD::STACKSAVE:
3052 // Expand to CopyFromReg if the target set
3053 // StackPointerRegisterToSaveRestore.
3054 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00003055 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3056 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003057 Results.push_back(Results[0].getValue(1));
3058 } else {
Bill Wendling775db972009-12-23 00:28:23 +00003059 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003060 Results.push_back(Node->getOperand(0));
3061 }
3062 break;
3063 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00003064 // Expand to CopyToReg if the target set
3065 // StackPointerRegisterToSaveRestore.
3066 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3067 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3068 Node->getOperand(1)));
3069 } else {
3070 Results.push_back(Node->getOperand(0));
3071 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003072 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003073 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00003074 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003075 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003076 case ISD::FNEG:
3077 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3078 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3079 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3080 Node->getOperand(0));
3081 Results.push_back(Tmp1);
3082 break;
3083 case ISD::FABS: {
3084 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00003085 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003086 Tmp1 = Node->getOperand(0);
3087 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00003088 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003089 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00003090 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3091 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003092 Results.push_back(Tmp1);
3093 break;
3094 }
3095 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00003096 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3097 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003098 break;
3099 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00003100 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3101 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003102 break;
3103 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00003104 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3105 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003106 break;
3107 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00003108 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3109 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003110 break;
3111 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00003112 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3113 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003114 break;
3115 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00003116 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3117 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003118 break;
3119 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00003120 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3121 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003122 break;
3123 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00003124 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3125 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003126 break;
3127 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003128 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3129 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003130 break;
3131 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003132 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3133 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003134 break;
3135 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003136 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3137 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003138 break;
3139 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003140 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3141 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003142 break;
3143 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003144 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3145 RTLIB::NEARBYINT_F64,
3146 RTLIB::NEARBYINT_F80,
3147 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003148 break;
3149 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003150 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3151 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003152 break;
3153 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003154 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3155 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003156 break;
3157 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003158 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3159 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003160 break;
3161 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003162 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3163 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003164 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003165 case ISD::FP16_TO_FP32:
3166 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3167 break;
3168 case ISD::FP32_TO_FP16:
3169 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3170 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003171 case ISD::ConstantFP: {
3172 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003173 // Check to see if this FP immediate is already legal.
3174 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003175 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003176 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003177 else
Bill Wendling775db972009-12-23 00:28:23 +00003178 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003179 break;
3180 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003181 case ISD::EHSELECTION: {
3182 unsigned Reg = TLI.getExceptionSelectorRegister();
3183 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003184 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3185 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003186 Results.push_back(Results[0].getValue(1));
3187 break;
3188 }
3189 case ISD::EXCEPTIONADDR: {
3190 unsigned Reg = TLI.getExceptionAddressRegister();
3191 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003192 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3193 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003194 Results.push_back(Results[0].getValue(1));
3195 break;
3196 }
3197 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003198 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003199 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3200 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3201 "Don't know how to expand this subtraction!");
3202 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3203 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003204 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3205 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003206 break;
3207 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003208 case ISD::UREM:
3209 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003210 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003211 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003212 bool isSigned = Node->getOpcode() == ISD::SREM;
3213 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3214 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3215 Tmp2 = Node->getOperand(0);
3216 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003217 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3218 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3219 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00003220 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3221 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003222 // X % Y -> X-X/Y*Y
3223 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3224 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3225 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003226 } else if (isSigned)
3227 Tmp1 = ExpandIntLibCall(Node, true,
3228 RTLIB::SREM_I8,
3229 RTLIB::SREM_I16, RTLIB::SREM_I32,
3230 RTLIB::SREM_I64, RTLIB::SREM_I128);
3231 else
3232 Tmp1 = ExpandIntLibCall(Node, false,
3233 RTLIB::UREM_I8,
3234 RTLIB::UREM_I16, RTLIB::UREM_I32,
3235 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003236 Results.push_back(Tmp1);
3237 break;
3238 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003239 case ISD::UDIV:
3240 case ISD::SDIV: {
3241 bool isSigned = Node->getOpcode() == ISD::SDIV;
3242 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003243 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003244 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003245 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3246 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3247 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003248 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3249 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003250 else if (isSigned)
3251 Tmp1 = ExpandIntLibCall(Node, true,
3252 RTLIB::SDIV_I8,
3253 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3254 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3255 else
3256 Tmp1 = ExpandIntLibCall(Node, false,
3257 RTLIB::UDIV_I8,
3258 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3259 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003260 Results.push_back(Tmp1);
3261 break;
3262 }
3263 case ISD::MULHU:
3264 case ISD::MULHS: {
3265 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3266 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003267 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003268 SDVTList VTs = DAG.getVTList(VT, VT);
3269 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3270 "If this wasn't legal, it shouldn't have been created!");
3271 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3272 Node->getOperand(1));
3273 Results.push_back(Tmp1.getValue(1));
3274 break;
3275 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003276 case ISD::SDIVREM:
3277 case ISD::UDIVREM:
3278 // Expand into divrem libcall
3279 ExpandDivRemLibCall(Node, Results);
3280 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003281 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003282 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003283 SDVTList VTs = DAG.getVTList(VT, VT);
3284 // See if multiply or divide can be lowered using two-result operations.
3285 // We just need the low half of the multiply; try both the signed
3286 // and unsigned forms. If the target supports both SMUL_LOHI and
3287 // UMUL_LOHI, form a preference by checking which forms of plain
3288 // MULH it supports.
3289 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3290 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3291 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3292 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3293 unsigned OpToUse = 0;
3294 if (HasSMUL_LOHI && !HasMULHS) {
3295 OpToUse = ISD::SMUL_LOHI;
3296 } else if (HasUMUL_LOHI && !HasMULHU) {
3297 OpToUse = ISD::UMUL_LOHI;
3298 } else if (HasSMUL_LOHI) {
3299 OpToUse = ISD::SMUL_LOHI;
3300 } else if (HasUMUL_LOHI) {
3301 OpToUse = ISD::UMUL_LOHI;
3302 }
3303 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003304 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3305 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003306 break;
3307 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003308 Tmp1 = ExpandIntLibCall(Node, false,
3309 RTLIB::MUL_I8,
3310 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003311 RTLIB::MUL_I64, RTLIB::MUL_I128);
3312 Results.push_back(Tmp1);
3313 break;
3314 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003315 case ISD::SADDO:
3316 case ISD::SSUBO: {
3317 SDValue LHS = Node->getOperand(0);
3318 SDValue RHS = Node->getOperand(1);
3319 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3320 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3321 LHS, RHS);
3322 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003323 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003324
Eli Friedman4bc8c712009-05-27 12:20:41 +00003325 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3326
3327 // LHSSign -> LHS >= 0
3328 // RHSSign -> RHS >= 0
3329 // SumSign -> Sum >= 0
3330 //
3331 // Add:
3332 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3333 // Sub:
3334 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3335 //
3336 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3337 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3338 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3339 Node->getOpcode() == ISD::SADDO ?
3340 ISD::SETEQ : ISD::SETNE);
3341
3342 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3343 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3344
3345 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3346 Results.push_back(Cmp);
3347 break;
3348 }
3349 case ISD::UADDO:
3350 case ISD::USUBO: {
3351 SDValue LHS = Node->getOperand(0);
3352 SDValue RHS = Node->getOperand(1);
3353 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3354 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3355 LHS, RHS);
3356 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003357 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3358 Node->getOpcode () == ISD::UADDO ?
3359 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003360 break;
3361 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003362 case ISD::UMULO:
3363 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003364 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003365 SDValue LHS = Node->getOperand(0);
3366 SDValue RHS = Node->getOperand(1);
3367 SDValue BottomHalf;
3368 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003369 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003370 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3371 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3372 bool isSigned = Node->getOpcode() == ISD::SMULO;
3373 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3374 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3375 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3376 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3377 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3378 RHS);
3379 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003380 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3381 VT.getSizeInBits() * 2))) {
Owen Anderson23b9b192009-08-12 00:36:31 +00003382 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003383 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3384 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3385 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3386 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3387 DAG.getIntPtrConstant(0));
3388 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3389 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003390 } else {
3391 // We can fall back to a libcall with an illegal type for the MUL if we
3392 // have a libcall big enough.
3393 // Also, we can fall back to a division in some cases, but that's a big
3394 // performance hit in the general case.
3395 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3396 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3397 if (WideVT == MVT::i16)
3398 LC = RTLIB::MUL_I16;
3399 else if (WideVT == MVT::i32)
3400 LC = RTLIB::MUL_I32;
3401 else if (WideVT == MVT::i64)
3402 LC = RTLIB::MUL_I64;
3403 else if (WideVT == MVT::i128)
3404 LC = RTLIB::MUL_I128;
3405 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3406 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3407 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
Owen Anderson95771af2011-02-25 21:41:48 +00003408
Eric Christophere3385812011-01-20 01:29:23 +00003409 SDValue Ret = ExpandLibCall(LC, Node, isSigned);
Eric Christopher38a18262011-01-20 00:29:24 +00003410 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Ret);
3411 TopHalf = DAG.getNode(ISD::SRL, dl, Ret.getValueType(), Ret,
3412 DAG.getConstant(VT.getSizeInBits(), TLI.getPointerTy()));
3413 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, TopHalf);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003414 }
3415 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003416 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3417 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003418 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3419 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3420 ISD::SETNE);
3421 } else {
3422 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3423 DAG.getConstant(0, VT), ISD::SETNE);
3424 }
3425 Results.push_back(BottomHalf);
3426 Results.push_back(TopHalf);
3427 break;
3428 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003429 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003430 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003431 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3432 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003433 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003434 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003435 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003436 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003437 break;
3438 }
Eli Friedman509150f2009-05-27 07:58:35 +00003439 case ISD::SELECT:
3440 Tmp1 = Node->getOperand(0);
3441 Tmp2 = Node->getOperand(1);
3442 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003443 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003444 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3445 Tmp2, Tmp3,
3446 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003447 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003448 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3449 DAG.getConstant(0, Tmp1.getValueType()),
3450 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003451 }
Eli Friedman509150f2009-05-27 07:58:35 +00003452 Results.push_back(Tmp1);
3453 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003454 case ISD::BR_JT: {
3455 SDValue Chain = Node->getOperand(0);
3456 SDValue Table = Node->getOperand(1);
3457 SDValue Index = Node->getOperand(2);
3458
Owen Andersone50ed302009-08-10 22:56:29 +00003459 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003460
3461 const TargetData &TD = *TLI.getTargetData();
3462 unsigned EntrySize =
3463 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003464
Chris Lattner071c62f2010-01-25 23:26:13 +00003465 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003466 Index, DAG.getConstant(EntrySize, PTy));
3467 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3468
Owen Anderson23b9b192009-08-12 00:36:31 +00003469 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003470 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003471 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003472 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003473 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003474 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003475 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003476 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003477 // RelocBase can be JumpTable, GOT or some sort of global base.
3478 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3479 TLI.getPICJumpTableRelocBase(Table, DAG));
3480 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003482 Results.push_back(Tmp1);
3483 break;
3484 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003485 case ISD::BRCOND:
3486 // Expand brcond's setcc into its constituent parts and create a BR_CC
3487 // Node.
3488 Tmp1 = Node->getOperand(0);
3489 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003490 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003492 Tmp1, Tmp2.getOperand(2),
3493 Tmp2.getOperand(0), Tmp2.getOperand(1),
3494 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003495 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003496 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003497 DAG.getCondCode(ISD::SETNE), Tmp2,
3498 DAG.getConstant(0, Tmp2.getValueType()),
3499 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003500 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003501 Results.push_back(Tmp1);
3502 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003503 case ISD::SETCC: {
3504 Tmp1 = Node->getOperand(0);
3505 Tmp2 = Node->getOperand(1);
3506 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003507 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003508
3509 // If we expanded the SETCC into an AND/OR, return the new node
3510 if (Tmp2.getNode() == 0) {
3511 Results.push_back(Tmp1);
3512 break;
3513 }
3514
3515 // Otherwise, SETCC for the given comparison type must be completely
3516 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003517 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003518 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3519 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3520 Results.push_back(Tmp1);
3521 break;
3522 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003523 case ISD::SELECT_CC: {
3524 Tmp1 = Node->getOperand(0); // LHS
3525 Tmp2 = Node->getOperand(1); // RHS
3526 Tmp3 = Node->getOperand(2); // True
3527 Tmp4 = Node->getOperand(3); // False
3528 SDValue CC = Node->getOperand(4);
3529
3530 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003531 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003532
3533 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3534 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3535 CC = DAG.getCondCode(ISD::SETNE);
3536 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3537 Tmp3, Tmp4, CC);
3538 Results.push_back(Tmp1);
3539 break;
3540 }
3541 case ISD::BR_CC: {
3542 Tmp1 = Node->getOperand(0); // Chain
3543 Tmp2 = Node->getOperand(2); // LHS
3544 Tmp3 = Node->getOperand(3); // RHS
3545 Tmp4 = Node->getOperand(1); // CC
3546
3547 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003548 Tmp2, Tmp3, Tmp4, dl);
Stuart Hastingsfc521632011-04-19 16:16:58 +00003549 assert(depthCALLSEQ == 1 && "branch inside CALLSEQ_BEGIN/END?");
3550 setLastCALLSEQ(DAG.getEntryNode());
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003551
3552 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3553 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3554 Tmp4 = DAG.getCondCode(ISD::SETNE);
3555 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3556 Tmp3, Node->getOperand(4));
3557 Results.push_back(Tmp1);
3558 break;
3559 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003560 case ISD::GLOBAL_OFFSET_TABLE:
3561 case ISD::GlobalAddress:
3562 case ISD::GlobalTLSAddress:
3563 case ISD::ExternalSymbol:
3564 case ISD::ConstantPool:
3565 case ISD::JumpTable:
3566 case ISD::INTRINSIC_W_CHAIN:
3567 case ISD::INTRINSIC_WO_CHAIN:
3568 case ISD::INTRINSIC_VOID:
3569 // FIXME: Custom lowering for these operations shouldn't return null!
3570 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3571 Results.push_back(SDValue(Node, i));
3572 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003573 }
3574}
3575void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3576 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003577 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003578 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003579 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003580 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003581 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003582 }
Owen Andersone50ed302009-08-10 22:56:29 +00003583 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003584 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003585 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003586 switch (Node->getOpcode()) {
3587 case ISD::CTTZ:
3588 case ISD::CTLZ:
3589 case ISD::CTPOP:
3590 // Zero extend the argument.
3591 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3592 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003593 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003594 if (Node->getOpcode() == ISD::CTTZ) {
3595 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003596 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003597 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3598 ISD::SETEQ);
3599 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3600 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3601 } else if (Node->getOpcode() == ISD::CTLZ) {
3602 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3603 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3604 DAG.getConstant(NVT.getSizeInBits() -
3605 OVT.getSizeInBits(), NVT));
3606 }
Bill Wendling775db972009-12-23 00:28:23 +00003607 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003608 break;
3609 case ISD::BSWAP: {
3610 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003611 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003612 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3613 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003614 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003615 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003616 break;
3617 }
3618 case ISD::FP_TO_UINT:
3619 case ISD::FP_TO_SINT:
3620 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3621 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3622 Results.push_back(Tmp1);
3623 break;
3624 case ISD::UINT_TO_FP:
3625 case ISD::SINT_TO_FP:
3626 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3627 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3628 Results.push_back(Tmp1);
3629 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003630 case ISD::AND:
3631 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003632 case ISD::XOR: {
3633 unsigned ExtOp, TruncOp;
3634 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003635 ExtOp = ISD::BITCAST;
3636 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003637 } else {
3638 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003639 ExtOp = ISD::ANY_EXTEND;
3640 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003641 }
3642 // Promote each of the values to the new type.
3643 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3644 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3645 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003646 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3647 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003648 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003649 }
3650 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003651 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003652 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003653 ExtOp = ISD::BITCAST;
3654 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003655 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003656 ExtOp = ISD::ANY_EXTEND;
3657 TruncOp = ISD::TRUNCATE;
3658 } else {
3659 ExtOp = ISD::FP_EXTEND;
3660 TruncOp = ISD::FP_ROUND;
3661 }
3662 Tmp1 = Node->getOperand(0);
3663 // Promote each of the values to the new type.
3664 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3665 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3666 // Perform the larger operation, then round down.
3667 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3668 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003669 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003670 else
Bill Wendling775db972009-12-23 00:28:23 +00003671 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003672 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003673 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003674 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003675 }
Eli Friedman509150f2009-05-27 07:58:35 +00003676 case ISD::VECTOR_SHUFFLE: {
3677 SmallVector<int, 8> Mask;
3678 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3679
3680 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003681 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3682 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003683
3684 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003685 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003686 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003687 Results.push_back(Tmp1);
3688 break;
3689 }
Eli Friedmanad754602009-05-28 03:56:57 +00003690 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003691 unsigned ExtOp = ISD::FP_EXTEND;
3692 if (NVT.isInteger()) {
3693 ISD::CondCode CCCode =
3694 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3695 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003696 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003697 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3698 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003699 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3700 Tmp1, Tmp2, Node->getOperand(2)));
3701 break;
3702 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003703 }
3704}
3705
Chris Lattner3e928bb2005-01-07 07:47:09 +00003706// SelectionDAG::Legalize - This is the entry point for the file.
3707//
Dan Gohman714efc62009-12-05 17:51:33 +00003708void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003709 /// run - This is the main entry point to this class.
3710 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003711 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003712}
3713