blob: bf06adbf6bc78c7b8ee613053d5cce373de43b5b [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
Chris Lattner6831a812006-02-13 09:18:02 +000064 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
65 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000067 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000068
Stuart Hastings1809d5f2011-04-05 00:37:28 +000069 /// IsLegalizingCall - This member is used *only* for purposes of providing
70 /// helpful assertions that a libcall isn't created while another call is
71 /// being legalized (which could lead to non-serialized call sequences).
72 bool IsLegalizingCall;
73
Chris Lattner3e928bb2005-01-07 07:47:09 +000074 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000075 Legal, // The target natively supports this operation.
76 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000077 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 };
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 /// ValueTypeActions - This is a bitvector that contains two bits for each
81 /// value type, where the two bits correspond to the LegalizeAction enum.
82 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000083 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000084
Chris Lattner3e928bb2005-01-07 07:47:09 +000085 /// LegalizedNodes - For nodes that are of legal width, and that have more
86 /// than one use, this map indicates what regularized operand to use. This
87 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000088 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
Dan Gohman475871a2008-07-27 21:46:04 +000090 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000091 LegalizedNodes.insert(std::make_pair(From, To));
92 // If someone requests legalization of the new node, return itself.
93 if (From != To)
94 LegalizedNodes.insert(std::make_pair(To, To));
Owen Anderson95771af2011-02-25 21:41:48 +000095
Devang Patela778f5c2011-02-18 22:43:42 +000096 // Transfer SDDbgValues.
97 DAG.TransferDbgValues(From, To);
Chris Lattner8afc48e2005-01-07 22:28:47 +000098 }
99
Chris Lattner3e928bb2005-01-07 07:47:09 +0000100public:
Eli Friedman1fde9c52009-05-24 02:46:31 +0000101 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000102
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 /// getTypeAction - Return how we should legalize values of this type, either
104 /// it is already legal or we need to expand it into multiple registers of
105 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +0000106 LegalizeAction getTypeAction(EVT VT) const {
Chris Lattneraafe6262010-08-25 23:00:45 +0000107 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 }
109
110 /// isTypeLegal - Return true if this type is legal on this target.
111 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000112 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000113 return getTypeAction(VT) == Legal;
114 }
115
Chris Lattner3e928bb2005-01-07 07:47:09 +0000116 void LegalizeDAG();
117
Chris Lattner456a93a2006-01-28 07:39:30 +0000118private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000119 /// LegalizeOp - We know that the specified value has a legal type.
120 /// Recursively ensure that the operands have legal types, then return the
121 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Eli Friedman7ef3d172009-06-06 07:04:42 +0000124 SDValue OptimizeFloatStore(StoreSDNode *ST);
125
Nate Begeman68679912008-04-25 18:07:40 +0000126 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
127 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
128 /// is necessary to spill the vector being inserted into to memory, perform
129 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000130 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000131 SDValue Idx, DebugLoc dl);
132 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
133 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000134
Nate Begeman5a5ca152009-04-29 05:20:52 +0000135 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
136 /// performs the same shuffe in terms of order or result bytes, but on a type
137 /// whose vector element type is narrower than the original shuffle type.
138 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000139 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000140 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000141 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000142
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000143 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000144 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000145
Owen Andersone50ed302009-08-10 22:56:29 +0000146 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000147 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000148
Eli Friedman47b41f72009-05-27 02:21:29 +0000149 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Jim Grosbache03262f2010-06-18 21:43:38 +0000150 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
151 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000152 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
153 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
154 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000155 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
156 RTLIB::Libcall Call_I8,
157 RTLIB::Libcall Call_I16,
158 RTLIB::Libcall Call_I32,
159 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000160 RTLIB::Libcall Call_I128);
Evan Cheng65279cb2011-04-16 03:08:26 +0000161 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattnercad063f2005-07-16 00:19:57 +0000162
Owen Andersone50ed302009-08-10 22:56:29 +0000163 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue ExpandBUILD_VECTOR(SDNode *Node);
165 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000166 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
167 SmallVectorImpl<SDValue> &Results);
168 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000169 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000170 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000171 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000172 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000173 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000174 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000175
Dale Johannesen8a782a22009-02-02 22:12:50 +0000176 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
177 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000178
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000179 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000180 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000181 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000182
Jim Grosbache03262f2010-06-18 21:43:38 +0000183 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
184
Eli Friedman8c377c72009-05-27 01:25:56 +0000185 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
186 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187};
188}
189
Nate Begeman5a5ca152009-04-29 05:20:52 +0000190/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
191/// performs the same shuffe in terms of order or result bytes, but on a type
192/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000193/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000194SDValue
195SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000196 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000197 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000198 unsigned NumMaskElts = VT.getVectorNumElements();
199 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000200 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000201
Nate Begeman9008ca62009-04-27 18:41:29 +0000202 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
203
204 if (NumEltsGrowth == 1)
205 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000206
Nate Begeman9008ca62009-04-27 18:41:29 +0000207 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000208 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000209 int Idx = Mask[i];
210 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000211 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000212 NewMask.push_back(-1);
213 else
214 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000215 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000216 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000217 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000218 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
219 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000220}
221
Bill Wendling5aa49772009-02-24 02:35:30 +0000222SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000223 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000224 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
225 DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000226 ValueTypeActions(TLI.getValueTypeActions()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000228 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000229}
230
Chris Lattner3e928bb2005-01-07 07:47:09 +0000231void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000232 LastCALLSEQ_END = DAG.getEntryNode();
Stuart Hastings1809d5f2011-04-05 00:37:28 +0000233 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000234
Chris Lattnerab510a72005-10-02 17:49:46 +0000235 // The legalize process is inherently a bottom-up recursive process (users
236 // legalize their uses before themselves). Given infinite stack space, we
237 // could just start legalizing on the root and traverse the whole graph. In
238 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000239 // blocks. To avoid this problem, compute an ordering of the nodes where each
240 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000241 DAG.AssignTopologicalOrder();
242 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000243 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000244 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000245
246 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000248 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
249 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000250
Chris Lattner3e928bb2005-01-07 07:47:09 +0000251 LegalizedNodes.clear();
252
253 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000254 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000255}
256
Chris Lattner6831a812006-02-13 09:18:02 +0000257
258/// FindCallEndFromCallStart - Given a chained node that is part of a call
259/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000260static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000261 // Nested CALLSEQ_START/END constructs aren't yet legal,
262 // but we can DTRT and handle them correctly here.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000263 if (Node->getOpcode() == ISD::CALLSEQ_START)
264 depth++;
Stuart Hastings2965e692010-12-21 17:07:24 +0000265 else if (Node->getOpcode() == ISD::CALLSEQ_END) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000266 depth--;
267 if (depth == 0)
268 return Node;
269 }
Chris Lattner6831a812006-02-13 09:18:02 +0000270 if (Node->use_empty())
271 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Chris Lattner6831a812006-02-13 09:18:02 +0000273 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000274 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000276 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000277 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000279 // Otherwise, hunt for it.
280 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000282 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000283 break;
284 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000285
286 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000288 return 0;
289 }
290 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000291
Chris Lattner6831a812006-02-13 09:18:02 +0000292 for (SDNode::use_iterator UI = Node->use_begin(),
293 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000294
Chris Lattner6831a812006-02-13 09:18:02 +0000295 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000296 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000297 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
298 if (User->getOperand(i) == TheChain)
Stuart Hastingsa304d022010-12-09 21:25:20 +0000299 if (SDNode *Result = FindCallEndFromCallStart(User, depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000300 return Result;
301 }
302 return 0;
303}
304
Scott Michelfdc40a02009-02-17 22:15:04 +0000305/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000306/// sequence, find the CALLSEQ_START node that initiates the call sequence.
307static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000308 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000309 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000310 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
311 Node = Node->getOperand(0).getNode();
312 assert(Node->getOperand(0).getValueType() == MVT::Other &&
313 "Node doesn't have a token chain argument!");
314 switch (Node->getOpcode()) {
315 default:
316 break;
317 case ISD::CALLSEQ_START:
318 if (!nested)
319 return Node;
320 nested--;
321 break;
322 case ISD::CALLSEQ_END:
323 nested++;
324 break;
325 }
326 }
327 return 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000328}
329
330/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000331/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000332/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000333///
334/// Keep track of the nodes we fine that actually do lead to Dest in
335/// NodesLeadingTo. This avoids retraversing them exponential number of times.
336///
337bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000338 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000339 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000340
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000341 // If we've already processed this node and it does lead to Dest, there is no
342 // need to reprocess it.
343 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000344
Chris Lattner6831a812006-02-13 09:18:02 +0000345 // If the first result of this node has been already legalized, then it cannot
346 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000347 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000348
Chris Lattner6831a812006-02-13 09:18:02 +0000349 // Okay, this node has not already been legalized. Check and legalize all
350 // operands. If none lead to Dest, then we can legalize this node.
351 bool OperandsLeadToDest = false;
352 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
353 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000354 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
355 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000356
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000357 if (OperandsLeadToDest) {
358 NodesLeadingTo.insert(N);
359 return true;
360 }
Chris Lattner6831a812006-02-13 09:18:02 +0000361
362 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000363 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000364 return false;
365}
366
Evan Cheng9f877882006-12-13 20:57:08 +0000367/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
368/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000369static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000370 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000371 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000372 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000373
374 // If a FP immediate is precise when represented as a float and if the
375 // target can do an extending load from float to double, we put it into
376 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000377 // double. This shrinks FP constants and canonicalizes them for targets where
378 // an FP extending load is the same cost as a normal load (such as on the x87
379 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000380 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000381 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000382 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000384 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000386 }
387
Owen Andersone50ed302009-08-10 22:56:29 +0000388 EVT OrigVT = VT;
389 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 while (SVT != MVT::f32) {
391 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000392 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000393 // Only do this if the target has a native EXTLOAD instruction from
394 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000395 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000396 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000397 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000398 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000399 VT = SVT;
400 Extend = true;
401 }
Evan Cheng00495212006-12-12 21:32:44 +0000402 }
403
Dan Gohman475871a2008-07-27 21:46:04 +0000404 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000405 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000406 if (Extend)
Stuart Hastingsa9011292011-02-16 16:23:55 +0000407 return DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
Evan Chengbcc80172010-07-07 22:15:37 +0000408 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000409 CPIdx, MachinePointerInfo::getConstantPool(),
410 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000411 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000412 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000413 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000414}
415
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000416/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
417static
Dan Gohman475871a2008-07-27 21:46:04 +0000418SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000419 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000420 SDValue Chain = ST->getChain();
421 SDValue Ptr = ST->getBasePtr();
422 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000423 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000424 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000425 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000426 if (ST->getMemoryVT().isFloatingPoint() ||
427 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000428 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000429 if (TLI.isTypeLegal(intVT)) {
430 // Expand to a bitconvert of the value to the integer type of the
431 // same size, then a (misaligned) int store.
432 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000433 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000434 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
435 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000436 } else {
437 // Do a (aligned) store to a stack slot, then copy from the stack slot
438 // to the final destination using (unaligned) integer loads and stores.
Owen Andersone50ed302009-08-10 22:56:29 +0000439 EVT StoredVT = ST->getMemoryVT();
440 EVT RegVT =
Evan Chengadf97992010-04-15 01:25:27 +0000441 TLI.getRegisterType(*DAG.getContext(),
442 EVT::getIntegerVT(*DAG.getContext(),
443 StoredVT.getSizeInBits()));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000444 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
445 unsigned RegBytes = RegVT.getSizeInBits() / 8;
446 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000447
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000448 // Make sure the stack slot is also aligned for the register type.
449 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
450
451 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 SDValue Store = DAG.getTruncStore(Chain, dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000453 Val, StackPtr, MachinePointerInfo(),
454 StoredVT, false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000455 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
456 SmallVector<SDValue, 8> Stores;
457 unsigned Offset = 0;
458
459 // Do all but one copies using the full register width.
460 for (unsigned i = 1; i < NumRegs; i++) {
461 // Load one integer register's worth from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000462 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
463 MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000464 false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000465 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000466 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000467 ST->getPointerInfo().getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +0000468 ST->isVolatile(), ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000469 MinAlign(ST->getAlignment(), Offset)));
470 // Increment the pointers.
471 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000472 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000473 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000474 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000475 }
476
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000477 // The last store may be partial. Do a truncating store. On big-endian
478 // machines this requires an extending load from the stack slot to ensure
479 // that the bits are in the right place.
Evan Chengadf97992010-04-15 01:25:27 +0000480 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
481 8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000482
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000483 // Load from the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000484 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000485 MachinePointerInfo(),
486 MemVT, false, false, 0);
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000487
Dale Johannesenbb5da912009-02-02 20:41:04 +0000488 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000489 ST->getPointerInfo()
490 .getWithOffset(Offset),
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000491 MemVT, ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000492 ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000493 MinAlign(ST->getAlignment(), Offset)));
494 // The order of the stores doesn't matter - say it with a TokenFactor.
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000496 Stores.size());
497 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000498 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000499 assert(ST->getMemoryVT().isInteger() &&
500 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000501 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000502 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000503 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000504 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000505 int IncrementSize = NumBits / 8;
506
507 // Divide the stored value in two parts.
Owen Anderson95771af2011-02-25 21:41:48 +0000508 SDValue ShiftAmount = DAG.getConstant(NumBits,
509 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000510 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000511 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000512
513 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000514 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000515 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000516 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000517 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000518 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000519 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000520 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000521 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000522 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000523 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
524 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000525
Owen Anderson825b72b2009-08-11 20:47:22 +0000526 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000527}
528
529/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
530static
Dan Gohman475871a2008-07-27 21:46:04 +0000531SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000532 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000533 SDValue Chain = LD->getChain();
534 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000535 EVT VT = LD->getValueType(0);
536 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000537 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000538 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000539 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000540 if (TLI.isTypeLegal(intVT)) {
541 // Expand to a (misaligned) integer load of the same size,
542 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000543 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
544 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000545 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000546 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000547 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000548 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000549
Duncan Sands05e11fa2008-12-12 21:47:02 +0000550 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000551 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000552 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000553
Chris Lattnerecf42c42010-09-21 16:36:31 +0000554 // Copy the value to a (aligned) stack slot using (unaligned) integer
555 // loads and stores, then do a (aligned) load from the stack slot.
556 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
557 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
558 unsigned RegBytes = RegVT.getSizeInBits() / 8;
559 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
560
561 // Make sure the stack slot is also aligned for the register type.
562 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
563
564 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
565 SmallVector<SDValue, 8> Stores;
566 SDValue StackPtr = StackBase;
567 unsigned Offset = 0;
568
569 // Do all but one copies using the full register width.
570 for (unsigned i = 1; i < NumRegs; i++) {
571 // Load one integer register's worth from the original location.
572 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
573 LD->getPointerInfo().getWithOffset(Offset),
574 LD->isVolatile(), LD->isNonTemporal(),
575 MinAlign(LD->getAlignment(), Offset));
576 // Follow the load with a store to the stack slot. Remember the store.
577 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000578 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000579 // Increment the pointers.
580 Offset += RegBytes;
581 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
582 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
583 Increment);
584 }
585
586 // The last copy may be partial. Do an extending load.
587 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
588 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000589 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000590 LD->getPointerInfo().getWithOffset(Offset),
591 MemVT, LD->isVolatile(),
592 LD->isNonTemporal(),
593 MinAlign(LD->getAlignment(), Offset));
594 // Follow the load with a store to the stack slot. Remember the store.
595 // On big-endian machines this requires a truncating store to ensure
596 // that the bits end up in the right place.
597 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
598 MachinePointerInfo(), MemVT,
599 false, false, 0));
600
601 // The order of the stores doesn't matter - say it with a TokenFactor.
602 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
603 Stores.size());
604
605 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000606 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000607 MachinePointerInfo(), LoadedVT, false, false, 0);
608
609 // Callers expect a MERGE_VALUES node.
610 SDValue Ops[] = { Load, TF };
611 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000612 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000613 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000614 "Unaligned load of unsupported type.");
615
Dale Johannesen8155d642008-02-27 22:36:00 +0000616 // Compute the new VT that is half the size of the old one. This is an
617 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000618 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000619 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000620 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000621 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000622
Chris Lattnere400af82007-11-19 21:38:03 +0000623 unsigned Alignment = LD->getAlignment();
624 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000625 ISD::LoadExtType HiExtType = LD->getExtensionType();
626
627 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
628 if (HiExtType == ISD::NON_EXTLOAD)
629 HiExtType = ISD::ZEXTLOAD;
630
631 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000632 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000633 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000634 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000635 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000636 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000637 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000638 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000639 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000640 LD->getPointerInfo().getWithOffset(IncrementSize),
641 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000642 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000643 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000644 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000645 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000646 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000647 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000648 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000649 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000650 LD->getPointerInfo().getWithOffset(IncrementSize),
651 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000652 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000653 }
654
655 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000656 SDValue ShiftAmount = DAG.getConstant(NumBits,
657 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000658 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
659 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000662 Hi.getValue(1));
663
Dan Gohman475871a2008-07-27 21:46:04 +0000664 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000665 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000666}
Evan Cheng912095b2007-01-04 21:56:39 +0000667
Nate Begeman68679912008-04-25 18:07:40 +0000668/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
669/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
670/// is necessary to spill the vector being inserted into to memory, perform
671/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000672SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000673PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
674 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000675 SDValue Tmp1 = Vec;
676 SDValue Tmp2 = Val;
677 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000678
Nate Begeman68679912008-04-25 18:07:40 +0000679 // If the target doesn't support this, we have to spill the input vector
680 // to a temporary stack slot, update the element, then reload it. This is
681 // badness. We could also load the value into a vector register (either
682 // with a "move to register" or "extload into register" instruction, then
683 // permute it into place, if the idx is a constant and if the idx is
684 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000685 EVT VT = Tmp1.getValueType();
686 EVT EltVT = VT.getVectorElementType();
687 EVT IdxVT = Tmp3.getValueType();
688 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000689 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000690
Evan Chengff89dcb2009-10-18 18:16:27 +0000691 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
692
Nate Begeman68679912008-04-25 18:07:40 +0000693 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000694 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000695 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000696 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000697
698 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000699 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000700 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000701 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000702 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000703 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
704 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000705 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000706 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000707 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000708 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000709 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000710 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000711}
712
Mon P Wange9f10152008-12-09 05:46:39 +0000713
Eli Friedman3f727d62009-05-27 02:16:40 +0000714SDValue SelectionDAGLegalize::
715ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
716 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
717 // SCALAR_TO_VECTOR requires that the type of the value being inserted
718 // match the element type of the vector being created, except for
719 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000720 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000721 if (Val.getValueType() == EltVT ||
722 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
723 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
724 Vec.getValueType(), Val);
725
726 unsigned NumElts = Vec.getValueType().getVectorNumElements();
727 // We generate a shuffle of InVec and ScVec, so the shuffle mask
728 // should be 0,1,2,3,4,5... with the appropriate element replaced with
729 // elt 0 of the RHS.
730 SmallVector<int, 8> ShufOps;
731 for (unsigned i = 0; i != NumElts; ++i)
732 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
733
734 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
735 &ShufOps[0]);
736 }
737 }
738 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
739}
740
Eli Friedman7ef3d172009-06-06 07:04:42 +0000741SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
742 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
743 // FIXME: We shouldn't do this for TargetConstantFP's.
744 // FIXME: move this to the DAG Combiner! Note that we can't regress due
745 // to phase ordering between legalized code and the dag combiner. This
746 // probably means that we need to integrate dag combiner and legalizer
747 // together.
748 // We generally can't do this one for long doubles.
749 SDValue Tmp1 = ST->getChain();
750 SDValue Tmp2 = ST->getBasePtr();
751 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000752 unsigned Alignment = ST->getAlignment();
753 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000754 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000755 DebugLoc dl = ST->getDebugLoc();
756 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 if (CFP->getValueType(0) == MVT::f32 &&
758 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000759 Tmp3 = DAG.getConstant(CFP->getValueAPF().
760 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000762 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
763 isVolatile, isNonTemporal, Alignment);
764 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000765
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000766 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000767 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000769 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000771 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
772 isVolatile, isNonTemporal, Alignment);
773 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000774
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000775 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000776 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
777 // stores. If the target supports neither 32- nor 64-bits, this
778 // xform is certainly not worth it.
779 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000780 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000782 if (TLI.isBigEndian()) std::swap(Lo, Hi);
783
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000784 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
785 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000786 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
787 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000788 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
789 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000790 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000791
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000793 }
794 }
795 }
Evan Cheng8e23e812011-04-01 00:42:02 +0000796 return SDValue(0, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000797}
798
Dan Gohmana3466152007-07-13 20:14:11 +0000799/// LegalizeOp - We know that the specified value has a legal type, and
800/// that its operands are legal. Now ensure that the operation itself
801/// is legal, recursively ensuring that the operands' operations remain
802/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000803SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000804 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
805 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000806
Gabor Greifba36cb52008-08-28 21:40:38 +0000807 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000808 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000809
Eli Friedman1fde9c52009-05-24 02:46:31 +0000810 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
811 assert(getTypeAction(Node->getValueType(i)) == Legal &&
812 "Unexpected illegal type!");
813
814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000815 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000816 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
817 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000818
Chris Lattner45982da2005-05-12 16:53:42 +0000819 // Note that LegalizeOp may be reentered even from single-use nodes, which
820 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000821 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000822 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000823
Dan Gohman475871a2008-07-27 21:46:04 +0000824 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
825 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000826 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000827
Eli Friedman8c377c72009-05-27 01:25:56 +0000828 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000829 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000830 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000831 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000832 case ISD::INTRINSIC_W_CHAIN:
833 case ISD::INTRINSIC_WO_CHAIN:
834 case ISD::INTRINSIC_VOID:
835 case ISD::VAARG:
836 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000838 break;
839 case ISD::SINT_TO_FP:
840 case ISD::UINT_TO_FP:
841 case ISD::EXTRACT_VECTOR_ELT:
842 Action = TLI.getOperationAction(Node->getOpcode(),
843 Node->getOperand(0).getValueType());
844 break;
845 case ISD::FP_ROUND_INREG:
846 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000847 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000848 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
849 break;
850 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000851 case ISD::SELECT_CC:
852 case ISD::SETCC:
853 case ISD::BR_CC: {
854 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
855 Node->getOpcode() == ISD::SETCC ? 2 : 1;
856 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000857 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000858 ISD::CondCode CCCode =
859 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
860 Action = TLI.getCondCodeAction(CCCode, OpVT);
861 if (Action == TargetLowering::Legal) {
862 if (Node->getOpcode() == ISD::SELECT_CC)
863 Action = TLI.getOperationAction(Node->getOpcode(),
864 Node->getValueType(0));
865 else
866 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
867 }
868 break;
869 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000870 case ISD::LOAD:
871 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000872 // FIXME: Model these properly. LOAD and STORE are complicated, and
873 // STORE expects the unlegalized operand in some cases.
874 SimpleFinishLegalizing = false;
875 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000876 case ISD::CALLSEQ_START:
877 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000878 // FIXME: This shouldn't be necessary. These nodes have special properties
879 // dealing with the recursive nature of legalization. Removing this
880 // special case should be done as part of making LegalizeDAG non-recursive.
881 SimpleFinishLegalizing = false;
882 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000883 case ISD::EXTRACT_ELEMENT:
884 case ISD::FLT_ROUNDS_:
885 case ISD::SADDO:
886 case ISD::SSUBO:
887 case ISD::UADDO:
888 case ISD::USUBO:
889 case ISD::SMULO:
890 case ISD::UMULO:
891 case ISD::FPOWI:
892 case ISD::MERGE_VALUES:
893 case ISD::EH_RETURN:
894 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000895 case ISD::EH_SJLJ_SETJMP:
896 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000897 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000898 // These operations lie about being legal: when they claim to be legal,
899 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000900 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
901 if (Action == TargetLowering::Legal)
902 Action = TargetLowering::Expand;
903 break;
904 case ISD::TRAMPOLINE:
905 case ISD::FRAMEADDR:
906 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000907 // These operations lie about being legal: when they claim to be legal,
908 // they should actually be custom-lowered.
909 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
910 if (Action == TargetLowering::Legal)
911 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000912 break;
913 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000914 // A weird case: legalization for BUILD_VECTOR never legalizes the
915 // operands!
916 // FIXME: This really sucks... changing it isn't semantically incorrect,
917 // but it massively pessimizes the code for floating-point BUILD_VECTORs
918 // because ConstantFP operands get legalized into constant pool loads
919 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
920 // though, because BUILD_VECTORS usually get lowered into other nodes
921 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000922 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000923 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000924 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000925 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000926 Action = TargetLowering::Legal;
927 } else {
928 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000929 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000930 break;
931 }
932
933 if (SimpleFinishLegalizing) {
934 SmallVector<SDValue, 8> Ops, ResultVals;
935 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
936 Ops.push_back(LegalizeOp(Node->getOperand(i)));
937 switch (Node->getOpcode()) {
938 default: break;
939 case ISD::BR:
940 case ISD::BRIND:
941 case ISD::BR_JT:
942 case ISD::BR_CC:
943 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000944 // Branches tweak the chain to include LastCALLSEQ_END
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Eli Friedman8c377c72009-05-27 01:25:56 +0000946 LastCALLSEQ_END);
947 Ops[0] = LegalizeOp(Ops[0]);
948 LastCALLSEQ_END = DAG.getEntryNode();
949 break;
950 case ISD::SHL:
951 case ISD::SRL:
952 case ISD::SRA:
953 case ISD::ROTL:
954 case ISD::ROTR:
955 // Legalizing shifts/rotates requires adjusting the shift amount
956 // to the appropriate width.
957 if (!Ops[1].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000958 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
959 Ops[1]));
Eli Friedman8c377c72009-05-27 01:25:56 +0000960 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000961 case ISD::SRL_PARTS:
962 case ISD::SRA_PARTS:
963 case ISD::SHL_PARTS:
964 // Legalizing shifts/rotates requires adjusting the shift amount
965 // to the appropriate width.
966 if (!Ops[2].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000967 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
968 Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000969 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000970 }
971
Dan Gohman027657d2010-06-18 15:30:29 +0000972 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
973 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000974 switch (Action) {
975 case TargetLowering::Legal:
976 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
977 ResultVals.push_back(Result.getValue(i));
978 break;
979 case TargetLowering::Custom:
980 // FIXME: The handling for custom lowering with multiple results is
981 // a complete mess.
982 Tmp1 = TLI.LowerOperation(Result, DAG);
983 if (Tmp1.getNode()) {
984 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
985 if (e == 1)
986 ResultVals.push_back(Tmp1);
987 else
988 ResultVals.push_back(Tmp1.getValue(i));
989 }
990 break;
991 }
992
993 // FALL THROUGH
994 case TargetLowering::Expand:
995 ExpandNode(Result.getNode(), ResultVals);
996 break;
997 case TargetLowering::Promote:
998 PromoteNode(Result.getNode(), ResultVals);
999 break;
1000 }
1001 if (!ResultVals.empty()) {
1002 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
1003 if (ResultVals[i] != SDValue(Node, i))
1004 ResultVals[i] = LegalizeOp(ResultVals[i]);
1005 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
1006 }
1007 return ResultVals[Op.getResNo()];
1008 }
1009 }
1010
1011 switch (Node->getOpcode()) {
1012 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001013#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001014 dbgs() << "NODE: ";
1015 Node->dump( &DAG);
1016 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001017#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001018 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001019
Chris Lattnerb2827b02006-03-19 00:52:58 +00001020 case ISD::BUILD_VECTOR:
1021 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001022 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001023 case TargetLowering::Custom:
1024 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001025 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001026 Result = Tmp3;
1027 break;
1028 }
1029 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001030 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001031 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001032 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001033 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001034 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001035 case ISD::CALLSEQ_START: {
1036 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001037
Chris Lattner6831a812006-02-13 09:18:02 +00001038 // Recursively Legalize all of the inputs of the call end that do not lead
1039 // to this call start. This ensures that any libcalls that need be inserted
1040 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001041 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001042 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001043 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001044 NodesLeadingTo);
1045 }
Chris Lattner6831a812006-02-13 09:18:02 +00001046
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001047 // Now that we have legalized all of the inputs (which may have inserted
1048 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001049 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1050
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001051 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001052 // call ended.
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001053 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001054 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001055 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001056 Tmp1 = LegalizeOp(Tmp1);
1057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001058
Chris Lattner6831a812006-02-13 09:18:02 +00001059 // Do not try to legalize the target-specific arguments (#1+).
1060 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001061 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001062 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001063 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1064 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001065 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001066
Chris Lattner6831a812006-02-13 09:18:02 +00001067 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001068 AddLegalizedOperand(Op.getValue(0), Result);
1069 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1070 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Chris Lattner6831a812006-02-13 09:18:02 +00001072 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001073 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001074 // process, no libcalls can/will be inserted, guaranteeing that no calls
1075 // can overlap.
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001076 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001077 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001078 LastCALLSEQ_END = SDValue(CallEnd, 0);
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001079 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001080
Chris Lattner6831a812006-02-13 09:18:02 +00001081 // Legalize the call, starting from the CALLSEQ_END.
1082 LegalizeOp(LastCALLSEQ_END);
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001083 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
Chris Lattner6831a812006-02-13 09:18:02 +00001084 return Result;
1085 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001086 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001087 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1088 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001089 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001090 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1091 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001092 assert(I != LegalizedNodes.end() &&
1093 "Legalizing the call start should have legalized this node!");
1094 return I->second;
1095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001096
1097 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001098 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001099 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001100 // Do not try to legalize the target-specific arguments (#1+), except for
1101 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001102 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001103 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001104 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001105 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001106 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1107 &Ops[0], Ops.size()),
1108 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001109 }
1110 } else {
1111 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1112 if (Tmp1 != Node->getOperand(0) ||
1113 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001114 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001115 Ops[0] = Tmp1;
1116 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001117 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1118 &Ops[0], Ops.size()),
1119 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001120 }
Chris Lattner6a542892006-01-24 05:48:21 +00001121 }
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001122 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001123 // This finishes up call legalization.
Stuart Hastings1809d5f2011-04-05 00:37:28 +00001124 IsLegalizingCall = false;
1125
Chris Lattner4b653a02006-02-14 00:55:02 +00001126 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001127 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001128 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001129 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001130 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001131 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001132 LoadSDNode *LD = cast<LoadSDNode>(Node);
1133 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1134 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001135
Evan Cheng466685d2006-10-09 20:57:25 +00001136 ISD::LoadExtType ExtType = LD->getExtensionType();
1137 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001138 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001139 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1140 Tmp1, Tmp2, LD->getOffset()),
1141 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001142 Tmp3 = Result.getValue(0);
1143 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001144
Evan Cheng466685d2006-10-09 20:57:25 +00001145 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001146 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001147 case TargetLowering::Legal:
1148 // If this is an unaligned load and the target doesn't support it,
1149 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001150 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001151 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1152 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001153 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001154 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001155 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001156 Tmp3 = Result.getOperand(0);
1157 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001158 Tmp3 = LegalizeOp(Tmp3);
1159 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001160 }
1161 }
1162 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001163 case TargetLowering::Custom:
1164 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001165 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001166 Tmp3 = LegalizeOp(Tmp1);
1167 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001168 }
Evan Cheng466685d2006-10-09 20:57:25 +00001169 break;
1170 case TargetLowering::Promote: {
1171 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001172 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001173 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001174 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001175
Chris Lattnerecf42c42010-09-21 16:36:31 +00001176 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001177 LD->isVolatile(), LD->isNonTemporal(),
1178 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001179 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001180 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001181 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001182 }
Evan Cheng466685d2006-10-09 20:57:25 +00001183 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001184 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001185 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001186 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1187 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001188 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001189 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001190
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001191 EVT SrcVT = LD->getMemoryVT();
1192 unsigned SrcWidth = SrcVT.getSizeInBits();
1193 unsigned Alignment = LD->getAlignment();
1194 bool isVolatile = LD->isVolatile();
1195 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001196
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001197 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1198 // Some targets pretend to have an i1 loading operation, and actually
1199 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1200 // bits are guaranteed to be zero; it helps the optimizers understand
1201 // that these bits are zero. It is also useful for EXTLOAD, since it
1202 // tells the optimizers that those bits are undefined. It would be
1203 // nice to have an effective generic way of getting these benefits...
1204 // Until such a way is found, don't insist on promoting i1 here.
1205 (SrcVT != MVT::i1 ||
1206 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1207 // Promote to a byte-sized load if not loading an integral number of
1208 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1209 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1210 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1211 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001212
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001213 // The extra bits are guaranteed to be zero, since we stored them that
1214 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001215
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001216 ISD::LoadExtType NewExtType =
1217 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001218
Stuart Hastingsa9011292011-02-16 16:23:55 +00001219 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001220 Tmp1, Tmp2, LD->getPointerInfo(),
1221 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001222
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001223 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001224
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001225 if (ExtType == ISD::SEXTLOAD)
1226 // Having the top bits zero doesn't help when sign extending.
1227 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1228 Result.getValueType(),
1229 Result, DAG.getValueType(SrcVT));
1230 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1231 // All the top bits are guaranteed to be zero - inform the optimizers.
1232 Result = DAG.getNode(ISD::AssertZext, dl,
1233 Result.getValueType(), Result,
1234 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001235
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001236 Tmp1 = LegalizeOp(Result);
1237 Tmp2 = LegalizeOp(Ch);
1238 } else if (SrcWidth & (SrcWidth - 1)) {
1239 // If not loading a power-of-2 number of bits, expand as two loads.
1240 assert(!SrcVT.isVector() && "Unsupported extload!");
1241 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1242 assert(RoundWidth < SrcWidth);
1243 unsigned ExtraWidth = SrcWidth - RoundWidth;
1244 assert(ExtraWidth < RoundWidth);
1245 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1246 "Load size not an integral number of bytes!");
1247 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1248 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1249 SDValue Lo, Hi, Ch;
1250 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001251
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001252 if (TLI.isLittleEndian()) {
1253 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1254 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001255 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001256 Tmp1, Tmp2,
1257 LD->getPointerInfo(), RoundVT, isVolatile,
1258 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001259
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001260 // Load the remaining ExtraWidth bits.
1261 IncrementSize = RoundWidth / 8;
1262 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1263 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001264 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001265 LD->getPointerInfo().getWithOffset(IncrementSize),
1266 ExtraVT, isVolatile, isNonTemporal,
1267 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001268
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001269 // Build a factor node to remember that this load is independent of
1270 // the other one.
1271 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1272 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001273
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001274 // Move the top bits to the right place.
1275 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001276 DAG.getConstant(RoundWidth,
1277 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001278
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001279 // Join the hi and lo parts.
1280 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001281 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001282 // Big endian - avoid unaligned loads.
1283 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1284 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001285 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001286 LD->getPointerInfo(), RoundVT, isVolatile,
1287 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001288
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001289 // Load the remaining ExtraWidth bits.
1290 IncrementSize = RoundWidth / 8;
1291 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1292 DAG.getIntPtrConstant(IncrementSize));
1293 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001294 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001295 LD->getPointerInfo().getWithOffset(IncrementSize),
1296 ExtraVT, isVolatile, isNonTemporal,
1297 MinAlign(Alignment, IncrementSize));
1298
1299 // Build a factor node to remember that this load is independent of
1300 // the other one.
1301 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1302 Hi.getValue(1));
1303
1304 // Move the top bits to the right place.
1305 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001306 DAG.getConstant(ExtraWidth,
1307 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001308
1309 // Join the hi and lo parts.
1310 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001311 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001312
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001313 Tmp1 = LegalizeOp(Result);
1314 Tmp2 = LegalizeOp(Ch);
1315 } else {
1316 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1317 default: assert(0 && "This action is not supported yet!");
1318 case TargetLowering::Custom:
1319 isCustom = true;
1320 // FALLTHROUGH
1321 case TargetLowering::Legal:
1322 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1323 Tmp1, Tmp2, LD->getOffset()),
1324 Result.getResNo());
1325 Tmp1 = Result.getValue(0);
1326 Tmp2 = Result.getValue(1);
1327
1328 if (isCustom) {
1329 Tmp3 = TLI.LowerOperation(Result, DAG);
1330 if (Tmp3.getNode()) {
1331 Tmp1 = LegalizeOp(Tmp3);
1332 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1333 }
1334 } else {
1335 // If this is an unaligned load and the target doesn't support it,
1336 // expand it.
1337 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1338 const Type *Ty =
1339 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1340 unsigned ABIAlignment =
1341 TLI.getTargetData()->getABITypeAlignment(Ty);
1342 if (LD->getAlignment() < ABIAlignment){
1343 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1344 DAG, TLI);
1345 Tmp1 = Result.getOperand(0);
1346 Tmp2 = Result.getOperand(1);
1347 Tmp1 = LegalizeOp(Tmp1);
1348 Tmp2 = LegalizeOp(Tmp2);
1349 }
1350 }
1351 }
1352 break;
1353 case TargetLowering::Expand:
1354 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1355 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1356 LD->getPointerInfo(),
1357 LD->isVolatile(), LD->isNonTemporal(),
1358 LD->getAlignment());
1359 unsigned ExtendOp;
1360 switch (ExtType) {
1361 case ISD::EXTLOAD:
1362 ExtendOp = (SrcVT.isFloatingPoint() ?
1363 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1364 break;
1365 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1366 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1367 default: llvm_unreachable("Unexpected extend load type!");
1368 }
1369 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1370 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1371 Tmp2 = LegalizeOp(Load.getValue(1));
1372 break;
1373 }
1374 // FIXME: This does not work for vectors on most targets. Sign- and
1375 // zero-extend operations are currently folded into extending loads,
1376 // whether they are legal or not, and then we end up here without any
1377 // support for legalizing them.
1378 assert(ExtType != ISD::EXTLOAD &&
1379 "EXTLOAD should always be supported!");
1380 // Turn the unsupported load into an EXTLOAD followed by an explicit
1381 // zero/sign extend inreg.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001382 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001383 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1384 LD->isVolatile(), LD->isNonTemporal(),
1385 LD->getAlignment());
1386 SDValue ValRes;
1387 if (ExtType == ISD::SEXTLOAD)
1388 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1389 Result.getValueType(),
1390 Result, DAG.getValueType(SrcVT));
1391 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001392 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001393 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1394 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1395 break;
1396 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001397 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001398
1399 // Since loads produce two values, make sure to remember that we legalized
1400 // both of them.
1401 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1402 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1403 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001404 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001405 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001406 StoreSDNode *ST = cast<StoreSDNode>(Node);
1407 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1408 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001409 unsigned Alignment = ST->getAlignment();
1410 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001411 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001412
Evan Cheng8b2794a2006-10-13 21:14:26 +00001413 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001414 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1415 Result = SDValue(OptStore, 0);
1416 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Eli Friedman957bffa2009-05-24 08:42:01 +00001419 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001420 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001421 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1422 Tmp1, Tmp3, Tmp2,
1423 ST->getOffset()),
1424 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001425
Owen Andersone50ed302009-08-10 22:56:29 +00001426 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001427 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001428 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001429 case TargetLowering::Legal:
1430 // If this is an unaligned store and the target doesn't support it,
1431 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001432 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001433 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001434 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001435 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001436 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1437 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001438 }
1439 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001440 case TargetLowering::Custom:
1441 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001442 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001443 break;
1444 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001445 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001446 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001447 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001448 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001449 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001450 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001451 break;
1452 }
1453 break;
1454 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001455 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001456 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001457
Owen Andersone50ed302009-08-10 22:56:29 +00001458 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001459 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001460
Duncan Sands83ec4b62008-06-06 12:08:01 +00001461 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001462 // Promote to a byte-sized store with upper bits zero if not
1463 // storing an integral number of bytes. For example, promote
1464 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001465 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1466 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001467 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001468 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1469 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001470 } else if (StWidth & (StWidth - 1)) {
1471 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001472 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001473 unsigned RoundWidth = 1 << Log2_32(StWidth);
1474 assert(RoundWidth < StWidth);
1475 unsigned ExtraWidth = StWidth - RoundWidth;
1476 assert(ExtraWidth < RoundWidth);
1477 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1478 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001479 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1480 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001481 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001482 unsigned IncrementSize;
1483
1484 if (TLI.isLittleEndian()) {
1485 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1486 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001487 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1488 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001489 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001490
1491 // Store the remaining ExtraWidth bits.
1492 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001493 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001494 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001495 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001496 DAG.getConstant(RoundWidth,
1497 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001498 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1499 ST->getPointerInfo().getWithOffset(IncrementSize),
1500 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001501 MinAlign(Alignment, IncrementSize));
1502 } else {
1503 // Big endian - avoid unaligned stores.
1504 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1505 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001506 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001507 DAG.getConstant(ExtraWidth,
1508 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001509 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1510 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001511
1512 // Store the remaining ExtraWidth bits.
1513 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001514 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001515 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001516 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1517 ST->getPointerInfo().getWithOffset(IncrementSize),
1518 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001519 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001520 }
Duncan Sands7e857202008-01-22 07:17:34 +00001521
1522 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001524 } else {
1525 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1526 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001527 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1528 Tmp1, Tmp3, Tmp2,
1529 ST->getOffset()),
1530 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001531
1532 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001533 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001534 case TargetLowering::Legal:
1535 // If this is an unaligned store and the target doesn't support it,
1536 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001537 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001538 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001539 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001540 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001541 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1542 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001543 }
1544 break;
1545 case TargetLowering::Custom:
1546 Result = TLI.LowerOperation(Result, DAG);
1547 break;
1548 case Expand:
1549 // TRUNCSTORE:i16 i32 -> STORE i16
1550 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001551 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001552 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1553 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001554 break;
1555 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001556 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001557 }
1558 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001559 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001560 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001561 assert(Result.getValueType() == Op.getValueType() &&
1562 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001563
Chris Lattner456a93a2006-01-28 07:39:30 +00001564 // Make sure that the generated code is itself legal.
1565 if (Result != Op)
1566 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001567
Chris Lattner45982da2005-05-12 16:53:42 +00001568 // Note that LegalizeOp may be reentered even from single-use nodes, which
1569 // means that we always must cache transformed nodes.
1570 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001571 return Result;
1572}
1573
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001574SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1575 SDValue Vec = Op.getOperand(0);
1576 SDValue Idx = Op.getOperand(1);
1577 DebugLoc dl = Op.getDebugLoc();
1578 // Store the value to a temporary stack slot, then LOAD the returned part.
1579 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001580 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1581 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001582
1583 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001584 unsigned EltSize =
1585 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001586 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1587 DAG.getConstant(EltSize, Idx.getValueType()));
1588
1589 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1590 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1591 else
1592 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1593
1594 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1595
Eli Friedmanc680ac92009-07-09 22:01:03 +00001596 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001597 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001598 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001599 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001600 MachinePointerInfo(),
1601 Vec.getValueType().getVectorElementType(),
1602 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001603}
1604
David Greenecfe33c42011-01-26 19:13:22 +00001605SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1606 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1607
1608 SDValue Vec = Op.getOperand(0);
1609 SDValue Part = Op.getOperand(1);
1610 SDValue Idx = Op.getOperand(2);
1611 DebugLoc dl = Op.getDebugLoc();
1612
1613 // Store the value to a temporary stack slot, then LOAD the returned part.
1614
1615 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1616 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1617 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1618
1619 // First store the whole vector.
1620 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1621 false, false, 0);
1622
1623 // Then store the inserted part.
1624
1625 // Add the offset to the index.
1626 unsigned EltSize =
1627 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1628
1629 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1630 DAG.getConstant(EltSize, Idx.getValueType()));
1631
1632 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1633 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1634 else
1635 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1636
1637 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1638 StackPtr);
1639
1640 // Store the subvector.
1641 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1642 MachinePointerInfo(), false, false, 0);
1643
1644 // Finally, load the updated vector.
1645 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1646 false, false, 0);
1647}
1648
Eli Friedman7ef3d172009-06-06 07:04:42 +00001649SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1650 // We can't handle this case efficiently. Allocate a sufficiently
1651 // aligned object on the stack, store each element into it, then load
1652 // the result as a vector.
1653 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001654 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001655 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001656 DebugLoc dl = Node->getDebugLoc();
1657 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001658 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001659 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001660
1661 // Emit a store of each element to the stack slot.
1662 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001663 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001664 // Store (in the right endianness) the elements to memory.
1665 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1666 // Ignore undef elements.
1667 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1668
1669 unsigned Offset = TypeByteSize*i;
1670
1671 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1672 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1673
Dan Gohman9949dd62010-02-25 20:30:49 +00001674 // If the destination vector element type is narrower than the source
1675 // element type, only store the bits necessary.
1676 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001677 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001678 Node->getOperand(i), Idx,
1679 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001680 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001681 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001682 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001683 Node->getOperand(i), Idx,
1684 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001685 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001686 }
1687
1688 SDValue StoreChain;
1689 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001690 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001691 &Stores[0], Stores.size());
1692 else
1693 StoreChain = DAG.getEntryNode();
1694
1695 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001696 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001697}
1698
Eli Friedman4bc8c712009-05-27 12:20:41 +00001699SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1700 DebugLoc dl = Node->getDebugLoc();
1701 SDValue Tmp1 = Node->getOperand(0);
1702 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001703
1704 // Get the sign bit of the RHS. First obtain a value that has the same
1705 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001706 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001707 EVT FloatVT = Tmp2.getValueType();
1708 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001709 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001710 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001711 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001712 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001713 // Store the float to memory, then load the sign part out as an integer.
1714 MVT LoadTy = TLI.getPointerTy();
1715 // First create a temporary that is aligned for both the load and store.
1716 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1717 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001718 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001719 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001720 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001721 if (TLI.isBigEndian()) {
1722 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1723 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001724 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1725 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001726 } else { // Little endian
1727 SDValue LoadPtr = StackPtr;
1728 // The float may be wider than the integer we are going to load. Advance
1729 // the pointer so that the loaded integer will contain the sign bit.
1730 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1731 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1732 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1733 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1734 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001735 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1736 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001737 // Move the sign bit to the top bit of the loaded integer.
1738 unsigned BitShift = LoadTy.getSizeInBits() -
1739 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1740 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1741 if (BitShift)
1742 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001743 DAG.getConstant(BitShift,
1744 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001745 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001746 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001747 // Now get the sign bit proper, by seeing whether the value is negative.
1748 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1749 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1750 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001751 // Get the absolute value of the result.
1752 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1753 // Select between the nabs and abs value based on the sign bit of
1754 // the input.
1755 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1756 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1757 AbsVal);
1758}
1759
Eli Friedman4bc8c712009-05-27 12:20:41 +00001760void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1761 SmallVectorImpl<SDValue> &Results) {
1762 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1763 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1764 " not tell us which reg is the stack pointer!");
1765 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001766 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001767 SDValue Tmp1 = SDValue(Node, 0);
1768 SDValue Tmp2 = SDValue(Node, 1);
1769 SDValue Tmp3 = Node->getOperand(2);
1770 SDValue Chain = Tmp1.getOperand(0);
1771
1772 // Chain the dynamic stack allocation so that it doesn't modify the stack
1773 // pointer when other instructions are using the stack.
1774 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1775
1776 SDValue Size = Tmp2.getOperand(1);
1777 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1778 Chain = SP.getValue(1);
1779 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001780 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001781 if (Align > StackAlign)
1782 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1783 DAG.getConstant(-(uint64_t)Align, VT));
1784 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1785 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1786
1787 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1788 DAG.getIntPtrConstant(0, true), SDValue());
1789
1790 Results.push_back(Tmp1);
1791 Results.push_back(Tmp2);
1792}
1793
Evan Cheng7f042682008-10-15 02:05:31 +00001794/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001795/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001796/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001797void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001798 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001799 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001800 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001802 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1803 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001804 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001805 case TargetLowering::Legal:
1806 // Nothing to do.
1807 break;
1808 case TargetLowering::Expand: {
1809 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1810 unsigned Opc = 0;
1811 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001812 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001813 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1814 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1815 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1816 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1817 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1818 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1819 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1820 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1821 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1822 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1823 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1824 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001825 // FIXME: Implement more expansions.
1826 }
1827
Dale Johannesenbb5da912009-02-02 20:41:04 +00001828 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1829 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1830 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001831 RHS = SDValue();
1832 CC = SDValue();
1833 break;
1834 }
1835 }
1836}
1837
Chris Lattner1401d152008-01-16 07:45:30 +00001838/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1839/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1840/// a load from the stack slot to DestVT, extending it if needed.
1841/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001842SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001843 EVT SlotVT,
1844 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001845 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001846 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001847 unsigned SrcAlign =
1848 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001849 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001851
Evan Chengff89dcb2009-10-18 18:16:27 +00001852 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1853 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001854 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001855
Duncan Sands83ec4b62008-06-06 12:08:01 +00001856 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1857 unsigned SlotSize = SlotVT.getSizeInBits();
1858 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001859 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1860 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001861
Chris Lattner1401d152008-01-16 07:45:30 +00001862 // Emit a store to the stack slot. Use a truncstore if the input value is
1863 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001864 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001865
Chris Lattner1401d152008-01-16 07:45:30 +00001866 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001867 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001868 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001869 else {
1870 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001871 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001872 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001873 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Chris Lattner35481892005-12-23 00:16:34 +00001875 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001876 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001877 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001878 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001879
Chris Lattner1401d152008-01-16 07:45:30 +00001880 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001881 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001882 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001883}
1884
Dan Gohman475871a2008-07-27 21:46:04 +00001885SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001886 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001887 // Create a vector sized/aligned stack slot, store the value to element #0,
1888 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001889 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001890
Evan Chengff89dcb2009-10-18 18:16:27 +00001891 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1892 int SPFI = StackPtrFI->getIndex();
1893
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001894 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1895 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001896 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001897 Node->getValueType(0).getVectorElementType(),
1898 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001899 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001900 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001901 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001902}
1903
1904
Chris Lattnerce872152006-03-19 06:31:19 +00001905/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001906/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001907SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001908 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001909 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001910 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001911 EVT VT = Node->getValueType(0);
1912 EVT OpVT = Node->getOperand(0).getValueType();
1913 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001914
1915 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001916 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001917 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001918 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001919 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001920 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001921 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001922 if (V.getOpcode() == ISD::UNDEF)
1923 continue;
1924 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001925 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001926 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001927 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001928
1929 if (!Value1.getNode()) {
1930 Value1 = V;
1931 } else if (!Value2.getNode()) {
1932 if (V != Value1)
1933 Value2 = V;
1934 } else if (V != Value1 && V != Value2) {
1935 MoreThanTwoValues = true;
1936 }
Chris Lattnerce872152006-03-19 06:31:19 +00001937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001938
Eli Friedman7a5e5552009-06-07 06:52:44 +00001939 if (!Value1.getNode())
1940 return DAG.getUNDEF(VT);
1941
1942 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001943 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Chris Lattner2eb86532006-03-24 07:29:17 +00001945 // If all elements are constants, create a load from the constant pool.
1946 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001947 std::vector<Constant*> CV;
1948 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001949 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001950 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001951 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001952 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001953 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001954 if (OpVT==EltVT)
1955 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1956 else {
1957 // If OpVT and EltVT don't match, EltVT is not legal and the
1958 // element values have been promoted/truncated earlier. Undo this;
1959 // we don't want a v16i8 to become a v16i32 for example.
1960 const ConstantInt *CI = V->getConstantIntValue();
1961 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1962 CI->getZExtValue()));
1963 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001964 } else {
1965 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001966 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001967 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001968 }
1969 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001970 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001972 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001973 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001974 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001975 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001976 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001977
Eli Friedman7a5e5552009-06-07 06:52:44 +00001978 if (!MoreThanTwoValues) {
1979 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1980 for (unsigned i = 0; i < NumElems; ++i) {
1981 SDValue V = Node->getOperand(i);
1982 if (V.getOpcode() == ISD::UNDEF)
1983 continue;
1984 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1985 }
1986 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001987 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001988 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1989 SDValue Vec2;
1990 if (Value2.getNode())
1991 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1992 else
1993 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Chris Lattner87100e02006-03-20 01:52:29 +00001995 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001996 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001997 }
1998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Eli Friedman7ef3d172009-06-06 07:04:42 +00002000 // Otherwise, we can't handle this case efficiently.
2001 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00002002}
2003
Chris Lattner77e77a62005-01-21 06:05:23 +00002004// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2005// does not fit into a register, return the lo part and set the hi part to the
2006// by-reg argument. If it does fit into a single register, return the result
2007// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002008SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002009 bool isSigned) {
Stuart Hastings1809d5f2011-04-05 00:37:28 +00002010 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002011 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002012 // Legalizing the call will automatically add the previous call to the
2013 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002014 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002015
Chris Lattner77e77a62005-01-21 06:05:23 +00002016 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002017 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002018 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002019 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00002020 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002021 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002022 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002023 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002024 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002025 }
Bill Wendling056292f2008-09-16 21:48:12 +00002026 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002027 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002028
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002029 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00002030 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002031
2032 // isTailCall may be true since the callee does not reference caller stack
2033 // frame. Check if it's in the right position.
2034 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002035 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002036 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002037 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002038 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002039 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002040
Evan Cheng3d2125c2010-11-30 23:55:39 +00002041 if (!CallInfo.second.getNode())
2042 // It's a tailcall, return the chain (which is the DAG root).
2043 return DAG.getRoot();
2044
Chris Lattner6831a812006-02-13 09:18:02 +00002045 // Legalize the call sequence, starting with the chain. This will advance
2046 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2047 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2048 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002049 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002050}
2051
Jim Grosbache03262f2010-06-18 21:43:38 +00002052// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2053// ExpandLibCall except that the first operand is the in-chain.
2054std::pair<SDValue, SDValue>
2055SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2056 SDNode *Node,
2057 bool isSigned) {
Stuart Hastings1809d5f2011-04-05 00:37:28 +00002058 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Jim Grosbache03262f2010-06-18 21:43:38 +00002059 SDValue InChain = Node->getOperand(0);
2060
2061 TargetLowering::ArgListTy Args;
2062 TargetLowering::ArgListEntry Entry;
2063 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2064 EVT ArgVT = Node->getOperand(i).getValueType();
2065 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2066 Entry.Node = Node->getOperand(i);
2067 Entry.Ty = ArgTy;
2068 Entry.isSExt = isSigned;
2069 Entry.isZExt = !isSigned;
2070 Args.push_back(Entry);
2071 }
2072 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2073 TLI.getPointerTy());
2074
2075 // Splice the libcall in wherever FindInputOutputChains tells us to.
2076 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2077 std::pair<SDValue, SDValue> CallInfo =
2078 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002079 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002080 /*isReturnValueUsed=*/true,
2081 Callee, Args, DAG, Node->getDebugLoc());
2082
2083 // Legalize the call sequence, starting with the chain. This will advance
2084 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2085 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2086 LegalizeOp(CallInfo.second);
2087 return CallInfo;
2088}
2089
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002090SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2091 RTLIB::Libcall Call_F32,
2092 RTLIB::Libcall Call_F64,
2093 RTLIB::Libcall Call_F80,
2094 RTLIB::Libcall Call_PPCF128) {
2095 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002096 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002097 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 case MVT::f32: LC = Call_F32; break;
2099 case MVT::f64: LC = Call_F64; break;
2100 case MVT::f80: LC = Call_F80; break;
2101 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002102 }
2103 return ExpandLibCall(LC, Node, false);
2104}
2105
2106SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002107 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002108 RTLIB::Libcall Call_I16,
2109 RTLIB::Libcall Call_I32,
2110 RTLIB::Libcall Call_I64,
2111 RTLIB::Libcall Call_I128) {
2112 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002113 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002114 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002115 case MVT::i8: LC = Call_I8; break;
2116 case MVT::i16: LC = Call_I16; break;
2117 case MVT::i32: LC = Call_I32; break;
2118 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002119 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002120 }
2121 return ExpandLibCall(LC, Node, isSigned);
2122}
2123
Evan Cheng65279cb2011-04-16 03:08:26 +00002124/// isDivRemLibcallAvailable - Return true if divmod libcall is available.
2125static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned,
2126 const TargetLowering &TLI) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002127 RTLIB::Libcall LC;
2128 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2129 default: assert(0 && "Unexpected request for libcall!");
2130 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2131 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2132 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2133 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2134 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2135 }
2136
Evan Cheng65279cb2011-04-16 03:08:26 +00002137 return TLI.getLibcallName(LC) != 0;
2138}
Evan Cheng8e23e812011-04-01 00:42:02 +00002139
Evan Cheng65279cb2011-04-16 03:08:26 +00002140/// UseDivRem - Only issue divrem libcall if both quotient and remainder are
2141/// needed.
2142static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) {
Evan Cheng8e23e812011-04-01 00:42:02 +00002143 unsigned OtherOpcode = 0;
Evan Cheng65279cb2011-04-16 03:08:26 +00002144 if (isSigned)
Evan Cheng8e23e812011-04-01 00:42:02 +00002145 OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002146 else
Evan Cheng8e23e812011-04-01 00:42:02 +00002147 OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV;
Evan Cheng65279cb2011-04-16 03:08:26 +00002148
Evan Cheng8e23e812011-04-01 00:42:02 +00002149 SDValue Op0 = Node->getOperand(0);
2150 SDValue Op1 = Node->getOperand(1);
2151 for (SDNode::use_iterator UI = Op0.getNode()->use_begin(),
2152 UE = Op0.getNode()->use_end(); UI != UE; ++UI) {
2153 SDNode *User = *UI;
2154 if (User == Node)
2155 continue;
2156 if (User->getOpcode() == OtherOpcode &&
2157 User->getOperand(0) == Op0 &&
Evan Cheng65279cb2011-04-16 03:08:26 +00002158 User->getOperand(1) == Op1)
2159 return true;
Evan Cheng8e23e812011-04-01 00:42:02 +00002160 }
Evan Cheng65279cb2011-04-16 03:08:26 +00002161 return false;
2162}
Evan Cheng8e23e812011-04-01 00:42:02 +00002163
Evan Cheng65279cb2011-04-16 03:08:26 +00002164/// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem
2165/// pairs.
2166void
2167SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2168 SmallVectorImpl<SDValue> &Results) {
2169 unsigned Opcode = Node->getOpcode();
2170 bool isSigned = Opcode == ISD::SDIVREM;
2171
2172 RTLIB::Libcall LC;
2173 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
2174 default: assert(0 && "Unexpected request for libcall!");
2175 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2176 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2177 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2178 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2179 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
Evan Cheng8e23e812011-04-01 00:42:02 +00002180 }
2181
2182 // The input chain to this libcall is the entry node of the function.
2183 // Legalizing the call will automatically add the previous call to the
2184 // dependence.
2185 SDValue InChain = DAG.getEntryNode();
2186
2187 EVT RetVT = Node->getValueType(0);
2188 const Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2189
2190 TargetLowering::ArgListTy Args;
2191 TargetLowering::ArgListEntry Entry;
2192 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2193 EVT ArgVT = Node->getOperand(i).getValueType();
2194 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2195 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
2196 Entry.isSExt = isSigned;
2197 Entry.isZExt = !isSigned;
2198 Args.push_back(Entry);
2199 }
2200
2201 // Also pass the return address of the remainder.
2202 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2203 Entry.Node = FIPtr;
2204 Entry.Ty = RetTy->getPointerTo();
2205 Entry.isSExt = isSigned;
2206 Entry.isZExt = !isSigned;
2207 Args.push_back(Entry);
2208
2209 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2210 TLI.getPointerTy());
2211
2212 // Splice the libcall in wherever FindInputOutputChains tells us to.
2213 DebugLoc dl = Node->getDebugLoc();
2214 std::pair<SDValue, SDValue> CallInfo =
2215 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
2216 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
2217 /*isReturnValueUsed=*/true, Callee, Args, DAG, dl);
2218
2219 // Legalize the call sequence, starting with the chain. This will advance
2220 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2221 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2222 LegalizeOp(CallInfo.second);
2223
2224 // Remainder is loaded back from the stack frame.
2225 SDValue Rem = DAG.getLoad(RetVT, dl, LastCALLSEQ_END, FIPtr,
2226 MachinePointerInfo(), false, false, 0);
Evan Cheng65279cb2011-04-16 03:08:26 +00002227 Results.push_back(CallInfo.first);
2228 Results.push_back(Rem);
Evan Cheng8e23e812011-04-01 00:42:02 +00002229}
2230
Chris Lattner22cde6a2006-01-28 08:25:58 +00002231/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2232/// INT_TO_FP operation of the specified operand when the target requests that
2233/// we expand it. At this point, we know that the result and operand types are
2234/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002235SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2236 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002237 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002238 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002239 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002240 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002241
Chris Lattner23594d42008-01-16 07:03:22 +00002242 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002243 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002244
Chris Lattner22cde6a2006-01-28 08:25:58 +00002245 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002247 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002248 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002249 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002250 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002251 if (TLI.isLittleEndian())
2252 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002253
Chris Lattner22cde6a2006-01-28 08:25:58 +00002254 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002255 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002256 if (isSigned) {
2257 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2259 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002260 } else {
2261 Op0Mapped = Op0;
2262 }
2263 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002264 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002265 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002266 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002267 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002268 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002269 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002270 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2271 MachinePointerInfo(),
2272 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002273 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002274 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2275 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002276 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002277 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002278 BitsToDouble(0x4330000080000000ULL) :
2279 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002281 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002282 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002283 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002285 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002287 // do nothing
2288 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002290 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002291 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002292 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002293 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002294 }
2295 return Result;
2296 }
2297 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002298 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002299
2300 // Implementation of unsigned i64 to f64 following the algorithm in
2301 // __floatundidf in compiler_rt. This implementation has the advantage
2302 // of performing rounding correctly, both in the default rounding mode
2303 // and in all alternate rounding modes.
2304 // TODO: Generalize this for use with other types.
2305 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2306 SDValue TwoP52 =
2307 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2308 SDValue TwoP84PlusTwoP52 =
2309 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2310 SDValue TwoP84 =
2311 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2312
2313 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2314 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2315 DAG.getConstant(32, MVT::i64));
2316 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2317 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002318 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2319 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002320 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2321 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002322 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2323 }
2324
Owen Anderson3a9e7692010-10-05 17:24:05 +00002325 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002326 // TODO: Generalize this for use with other types.
2327 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002328 // For unsigned conversions, convert them to signed conversions using the
2329 // algorithm from the x86_64 __floatundidf in compiler_rt.
2330 if (!isSigned) {
2331 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002332
Owen Anderson95771af2011-02-25 21:41:48 +00002333 SDValue ShiftConst =
2334 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002335 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2336 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2337 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2338 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002339
Owen Anderson3a9e7692010-10-05 17:24:05 +00002340 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2341 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002342
Owen Anderson3a9e7692010-10-05 17:24:05 +00002343 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002344 // select. We happen to get lucky and machinesink does the right
2345 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002346 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002347 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002348 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2349 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2350 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002351
Owen Anderson3a9e7692010-10-05 17:24:05 +00002352 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002353
Jim Grosbach6e992612010-07-02 17:41:59 +00002354 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002355 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2356 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2357 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002358 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002359 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2360 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2361 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2362 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2363 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2364 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002365 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002366 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002367 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002368
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002369 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2370 DAG.getConstant(32, SHVT));
2371 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2372 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2373 SDValue TwoP32 =
2374 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2375 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2376 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2377 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2378 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2379 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2380 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002381 }
2382
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002383 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002384
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002385 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2386 Op0, DAG.getConstant(0, Op0.getValueType()),
2387 ISD::SETLT);
2388 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2389 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2390 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002391
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002392 // If the sign bit of the integer is set, the large number will be treated
2393 // as a negative number. To counteract this, the dynamic code adds an
2394 // offset depending on the data type.
2395 uint64_t FF;
2396 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002397 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002398 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2399 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2400 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2401 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2402 }
2403 if (TLI.isLittleEndian()) FF <<= 32;
2404 Constant *FudgeFactor = ConstantInt::get(
2405 Type::getInt64Ty(*DAG.getContext()), FF);
2406
2407 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2408 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2409 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2410 Alignment = std::min(Alignment, 4u);
2411 SDValue FudgeInReg;
2412 if (DestVT == MVT::f32)
2413 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002414 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002415 false, false, Alignment);
2416 else {
2417 FudgeInReg =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002418 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002419 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002420 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002421 MVT::f32, false, false, Alignment));
2422 }
2423
2424 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002425}
2426
2427/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2428/// *INT_TO_FP operation of the specified operand when the target requests that
2429/// we promote it. At this point, we know that the result and operand types are
2430/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2431/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002432SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002433 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002434 bool isSigned,
2435 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002436 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002437 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002438
2439 unsigned OpToUse = 0;
2440
2441 // Scan for the appropriate larger type to use.
2442 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002443 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002444 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002445
2446 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002447 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2448 OpToUse = ISD::SINT_TO_FP;
2449 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002450 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002451 if (isSigned) continue;
2452
2453 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002454 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2455 OpToUse = ISD::UINT_TO_FP;
2456 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002457 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002458
2459 // Otherwise, try a larger type.
2460 }
2461
2462 // Okay, we found the operation and type to use. Zero extend our input to the
2463 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002464 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002465 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002466 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002467}
2468
2469/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2470/// FP_TO_*INT operation of the specified operand when the target requests that
2471/// we promote it. At this point, we know that the result and operand types are
2472/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2473/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002474SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002475 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002476 bool isSigned,
2477 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002478 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002479 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002480
2481 unsigned OpToUse = 0;
2482
2483 // Scan for the appropriate larger type to use.
2484 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002485 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002486 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002487
Eli Friedman3be2e512009-05-28 03:06:16 +00002488 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002489 OpToUse = ISD::FP_TO_SINT;
2490 break;
2491 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002492
Eli Friedman3be2e512009-05-28 03:06:16 +00002493 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002494 OpToUse = ISD::FP_TO_UINT;
2495 break;
2496 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002497
2498 // Otherwise, try a larger type.
2499 }
2500
Scott Michelfdc40a02009-02-17 22:15:04 +00002501
Chris Lattner27a6c732007-11-24 07:07:01 +00002502 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002503 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002504
Chris Lattner27a6c732007-11-24 07:07:01 +00002505 // Truncate the result of the extended FP_TO_*INT operation to the desired
2506 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002507 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002508}
2509
2510/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2511///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002512SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002513 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002514 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002515 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002516 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002517 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002518 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002519 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2520 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2521 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002522 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002523 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2524 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2525 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2526 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2527 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2528 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2529 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2530 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2531 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002532 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002533 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2534 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2535 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2536 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2537 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2538 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2539 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2540 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2541 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2542 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2543 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2544 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2545 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2546 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2547 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2548 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2549 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2550 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2551 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2552 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2553 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002554 }
2555}
2556
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002557/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002558// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002559static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2560 APInt Val = APInt(NumBits, ByteVal);
2561 unsigned Shift = 8;
2562 for (unsigned i = NumBits; i > 8; i >>= 1) {
2563 Val = (Val << Shift) | Val;
2564 Shift <<= 1;
2565 }
2566 return Val;
2567}
2568
Chris Lattner22cde6a2006-01-28 08:25:58 +00002569/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2570///
Scott Michelfdc40a02009-02-17 22:15:04 +00002571SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002572 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002573 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002574 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002575 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002576 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002577 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002578 unsigned Len = VT.getSizeInBits();
2579
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002580 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2581 "CTPOP not implemented for this type.");
2582
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002583 // This is the "best" algorithm from
2584 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2585
2586 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2587 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2588 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2589 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2590
2591 // v = v - ((v >> 1) & 0x55555555...)
2592 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2593 DAG.getNode(ISD::AND, dl, VT,
2594 DAG.getNode(ISD::SRL, dl, VT, Op,
2595 DAG.getConstant(1, ShVT)),
2596 Mask55));
2597 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2598 Op = DAG.getNode(ISD::ADD, dl, VT,
2599 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2600 DAG.getNode(ISD::AND, dl, VT,
2601 DAG.getNode(ISD::SRL, dl, VT, Op,
2602 DAG.getConstant(2, ShVT)),
2603 Mask33));
2604 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2605 Op = DAG.getNode(ISD::AND, dl, VT,
2606 DAG.getNode(ISD::ADD, dl, VT, Op,
2607 DAG.getNode(ISD::SRL, dl, VT, Op,
2608 DAG.getConstant(4, ShVT))),
2609 Mask0F);
2610 // v = (v * 0x01010101...) >> (Len - 8)
2611 Op = DAG.getNode(ISD::SRL, dl, VT,
2612 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2613 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002614
Chris Lattner22cde6a2006-01-28 08:25:58 +00002615 return Op;
2616 }
2617 case ISD::CTLZ: {
2618 // for now, we do this:
2619 // x = x | (x >> 1);
2620 // x = x | (x >> 2);
2621 // ...
2622 // x = x | (x >>16);
2623 // x = x | (x >>32); // for 64-bit input
2624 // return popcount(~x);
2625 //
2626 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002627 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002628 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002629 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002630 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002631 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002632 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002633 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002634 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002635 Op = DAG.getNOT(dl, Op, VT);
2636 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002637 }
2638 case ISD::CTTZ: {
2639 // for now, we use: { return popcount(~x & (x - 1)); }
2640 // unless the target has ctlz but not ctpop, in which case we use:
2641 // { return 32 - nlz(~x & (x-1)); }
2642 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002643 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002644 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2645 DAG.getNOT(dl, Op, VT),
2646 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002647 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002648 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002649 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2650 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002651 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002652 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002653 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2654 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002655 }
2656 }
2657}
Chris Lattnere34b3962005-01-19 04:19:40 +00002658
Jim Grosbache03262f2010-06-18 21:43:38 +00002659std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2660 unsigned Opc = Node->getOpcode();
2661 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2662 RTLIB::Libcall LC;
2663
2664 switch (Opc) {
2665 default:
2666 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2667 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002668 case ISD::ATOMIC_SWAP:
2669 switch (VT.SimpleTy) {
2670 default: llvm_unreachable("Unexpected value type for atomic!");
2671 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2672 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2673 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2674 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2675 }
2676 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002677 case ISD::ATOMIC_CMP_SWAP:
2678 switch (VT.SimpleTy) {
2679 default: llvm_unreachable("Unexpected value type for atomic!");
2680 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2681 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2682 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2683 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2684 }
2685 break;
2686 case ISD::ATOMIC_LOAD_ADD:
2687 switch (VT.SimpleTy) {
2688 default: llvm_unreachable("Unexpected value type for atomic!");
2689 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2690 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2691 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2692 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2693 }
2694 break;
2695 case ISD::ATOMIC_LOAD_SUB:
2696 switch (VT.SimpleTy) {
2697 default: llvm_unreachable("Unexpected value type for atomic!");
2698 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2699 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2700 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2701 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2702 }
2703 break;
2704 case ISD::ATOMIC_LOAD_AND:
2705 switch (VT.SimpleTy) {
2706 default: llvm_unreachable("Unexpected value type for atomic!");
2707 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2708 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2709 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2710 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2711 }
2712 break;
2713 case ISD::ATOMIC_LOAD_OR:
2714 switch (VT.SimpleTy) {
2715 default: llvm_unreachable("Unexpected value type for atomic!");
2716 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2717 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2718 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2719 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2720 }
2721 break;
2722 case ISD::ATOMIC_LOAD_XOR:
2723 switch (VT.SimpleTy) {
2724 default: llvm_unreachable("Unexpected value type for atomic!");
2725 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2726 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2727 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2728 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2729 }
2730 break;
2731 case ISD::ATOMIC_LOAD_NAND:
2732 switch (VT.SimpleTy) {
2733 default: llvm_unreachable("Unexpected value type for atomic!");
2734 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2735 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2736 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2737 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2738 }
2739 break;
2740 }
2741
2742 return ExpandChainLibCall(LC, Node, false);
2743}
2744
Eli Friedman8c377c72009-05-27 01:25:56 +00002745void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2746 SmallVectorImpl<SDValue> &Results) {
2747 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002748 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002749 switch (Node->getOpcode()) {
2750 case ISD::CTPOP:
2751 case ISD::CTLZ:
2752 case ISD::CTTZ:
2753 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2754 Results.push_back(Tmp1);
2755 break;
2756 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002757 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002758 break;
2759 case ISD::FRAMEADDR:
2760 case ISD::RETURNADDR:
2761 case ISD::FRAME_TO_ARGS_OFFSET:
2762 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2763 break;
2764 case ISD::FLT_ROUNDS_:
2765 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2766 break;
2767 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002768 case ISD::EH_LABEL:
2769 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002770 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002771 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002772 case ISD::EH_SJLJ_DISPATCHSETUP:
2773 // If the target didn't expand these, there's nothing to do, so just
2774 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002775 Results.push_back(Node->getOperand(0));
2776 break;
2777 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002778 // If the target didn't expand this, just return 'zero' and preserve the
2779 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002780 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002781 Results.push_back(Node->getOperand(0));
2782 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002783 case ISD::MEMBARRIER: {
2784 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2785 TargetLowering::ArgListTy Args;
2786 std::pair<SDValue, SDValue> CallResult =
2787 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002788 false, false, false, false, 0, CallingConv::C,
2789 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002790 /*isReturnValueUsed=*/true,
2791 DAG.getExternalSymbol("__sync_synchronize",
2792 TLI.getPointerTy()),
2793 Args, DAG, dl);
2794 Results.push_back(CallResult.second);
2795 break;
2796 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002797 // By default, atomic intrinsics are marked Legal and lowered. Targets
2798 // which don't support them directly, however, may want libcalls, in which
2799 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002800 case ISD::ATOMIC_SWAP:
2801 case ISD::ATOMIC_LOAD_ADD:
2802 case ISD::ATOMIC_LOAD_SUB:
2803 case ISD::ATOMIC_LOAD_AND:
2804 case ISD::ATOMIC_LOAD_OR:
2805 case ISD::ATOMIC_LOAD_XOR:
2806 case ISD::ATOMIC_LOAD_NAND:
2807 case ISD::ATOMIC_LOAD_MIN:
2808 case ISD::ATOMIC_LOAD_MAX:
2809 case ISD::ATOMIC_LOAD_UMIN:
2810 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002811 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002812 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2813 Results.push_back(Tmp.first);
2814 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002815 break;
Evan Chenga8457062010-06-18 22:01:37 +00002816 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002817 case ISD::DYNAMIC_STACKALLOC:
2818 ExpandDYNAMIC_STACKALLOC(Node, Results);
2819 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002820 case ISD::MERGE_VALUES:
2821 for (unsigned i = 0; i < Node->getNumValues(); i++)
2822 Results.push_back(Node->getOperand(i));
2823 break;
2824 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002825 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002826 if (VT.isInteger())
2827 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002828 else {
2829 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002830 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002831 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002832 break;
2833 }
2834 case ISD::TRAP: {
2835 // If this operation is not supported, lower it to 'abort()' call
2836 TargetLowering::ArgListTy Args;
2837 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002838 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002839 false, false, false, false, 0, CallingConv::C,
2840 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002841 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002842 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002843 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002844 Results.push_back(CallResult.second);
2845 break;
2846 }
2847 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002848 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002849 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2850 Node->getValueType(0), dl);
2851 Results.push_back(Tmp1);
2852 break;
2853 case ISD::FP_EXTEND:
2854 Tmp1 = EmitStackConvert(Node->getOperand(0),
2855 Node->getOperand(0).getValueType(),
2856 Node->getValueType(0), dl);
2857 Results.push_back(Tmp1);
2858 break;
2859 case ISD::SIGN_EXTEND_INREG: {
2860 // NOTE: we could fall back on load/store here too for targets without
2861 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002862 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002863 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002864 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002865 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002866 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002867 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2868 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002869 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002870 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2871 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002872 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2873 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002874 break;
2875 }
2876 case ISD::FP_ROUND_INREG: {
2877 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002878 // EXTLOAD pair, targeting a temporary location (a stack slot).
Eli Friedman8c377c72009-05-27 01:25:56 +00002879
2880 // NOTE: there is a choice here between constantly creating new stack
2881 // slots and always reusing the same one. We currently always create
2882 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002883 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002884 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2885 Node->getValueType(0), dl);
2886 Results.push_back(Tmp1);
2887 break;
2888 }
2889 case ISD::SINT_TO_FP:
2890 case ISD::UINT_TO_FP:
2891 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2892 Node->getOperand(0), Node->getValueType(0), dl);
2893 Results.push_back(Tmp1);
2894 break;
2895 case ISD::FP_TO_UINT: {
2896 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002897 EVT VT = Node->getOperand(0).getValueType();
2898 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002899 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002900 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2901 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2902 Tmp1 = DAG.getConstantFP(apf, VT);
2903 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2904 Node->getOperand(0),
2905 Tmp1, ISD::SETLT);
2906 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002907 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2908 DAG.getNode(ISD::FSUB, dl, VT,
2909 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002910 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2911 DAG.getConstant(x, NVT));
2912 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2913 Results.push_back(Tmp1);
2914 break;
2915 }
Eli Friedman509150f2009-05-27 07:58:35 +00002916 case ISD::VAARG: {
2917 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002918 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002919 Tmp1 = Node->getOperand(0);
2920 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002921 unsigned Align = Node->getConstantOperandVal(3);
2922
Chris Lattnerecf42c42010-09-21 16:36:31 +00002923 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2924 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002925 SDValue VAList = VAListLoad;
2926
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002927 if (Align > TLI.getMinStackArgumentAlignment()) {
2928 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2929
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002930 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2931 DAG.getConstant(Align - 1,
2932 TLI.getPointerTy()));
2933
2934 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002935 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002936 TLI.getPointerTy()));
2937 }
2938
Eli Friedman509150f2009-05-27 07:58:35 +00002939 // Increment the pointer, VAList, to the next vaarg
2940 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2941 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002942 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002943 TLI.getPointerTy()));
2944 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002945 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2946 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002947 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002948 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002949 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002950 Results.push_back(Results[0].getValue(1));
2951 break;
2952 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002953 case ISD::VACOPY: {
2954 // This defaults to loading a pointer from the input and storing it to the
2955 // output, returning the chain.
2956 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2957 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2958 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002959 Node->getOperand(2), MachinePointerInfo(VS),
2960 false, false, 0);
2961 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2962 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002963 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002964 break;
2965 }
2966 case ISD::EXTRACT_VECTOR_ELT:
2967 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2968 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002969 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002970 Node->getOperand(0));
2971 else
2972 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2973 Results.push_back(Tmp1);
2974 break;
2975 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002976 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002977 break;
David Greenecfe33c42011-01-26 19:13:22 +00002978 case ISD::INSERT_SUBVECTOR:
2979 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2980 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002981 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002982 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002983 break;
2984 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002985 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002986 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002987 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002988 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002989 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2990 Node->getOperand(1),
2991 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002992 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002993 case ISD::VECTOR_SHUFFLE: {
2994 SmallVector<int, 8> Mask;
2995 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2996
Owen Andersone50ed302009-08-10 22:56:29 +00002997 EVT VT = Node->getValueType(0);
2998 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00002999 if (getTypeAction(EltVT) == Promote)
3000 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00003001 unsigned NumElems = VT.getVectorNumElements();
3002 SmallVector<SDValue, 8> Ops;
3003 for (unsigned i = 0; i != NumElems; ++i) {
3004 if (Mask[i] < 0) {
3005 Ops.push_back(DAG.getUNDEF(EltVT));
3006 continue;
3007 }
3008 unsigned Idx = Mask[i];
3009 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00003010 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3011 Node->getOperand(0),
3012 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00003013 else
Bill Wendling775db972009-12-23 00:28:23 +00003014 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
3015 Node->getOperand(1),
3016 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00003017 }
3018 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
3019 Results.push_back(Tmp1);
3020 break;
3021 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003022 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00003023 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00003024 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
3025 // 1 -> Hi
3026 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3027 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003028 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00003029 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3030 } else {
3031 // 0 -> Lo
3032 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3033 Node->getOperand(0));
3034 }
3035 Results.push_back(Tmp1);
3036 break;
3037 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003038 case ISD::STACKSAVE:
3039 // Expand to CopyFromReg if the target set
3040 // StackPointerRegisterToSaveRestore.
3041 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00003042 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3043 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003044 Results.push_back(Results[0].getValue(1));
3045 } else {
Bill Wendling775db972009-12-23 00:28:23 +00003046 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00003047 Results.push_back(Node->getOperand(0));
3048 }
3049 break;
3050 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00003051 // Expand to CopyToReg if the target set
3052 // StackPointerRegisterToSaveRestore.
3053 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
3054 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3055 Node->getOperand(1)));
3056 } else {
3057 Results.push_back(Node->getOperand(0));
3058 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003059 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003060 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00003061 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003062 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003063 case ISD::FNEG:
3064 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3065 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
3066 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
3067 Node->getOperand(0));
3068 Results.push_back(Tmp1);
3069 break;
3070 case ISD::FABS: {
3071 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00003072 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003073 Tmp1 = Node->getOperand(0);
3074 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00003075 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003076 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00003077 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
3078 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003079 Results.push_back(Tmp1);
3080 break;
3081 }
3082 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00003083 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3084 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003085 break;
3086 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00003087 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
3088 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003089 break;
3090 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00003091 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
3092 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003093 break;
3094 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00003095 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
3096 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003097 break;
3098 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00003099 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3100 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003101 break;
3102 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00003103 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3104 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003105 break;
3106 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00003107 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
3108 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003109 break;
3110 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00003111 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3112 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003113 break;
3114 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003115 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3116 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003117 break;
3118 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003119 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3120 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003121 break;
3122 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003123 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3124 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003125 break;
3126 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003127 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3128 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003129 break;
3130 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003131 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3132 RTLIB::NEARBYINT_F64,
3133 RTLIB::NEARBYINT_F80,
3134 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003135 break;
3136 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003137 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3138 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003139 break;
3140 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003141 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3142 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003143 break;
3144 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003145 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3146 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003147 break;
3148 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003149 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3150 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003151 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003152 case ISD::FP16_TO_FP32:
3153 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3154 break;
3155 case ISD::FP32_TO_FP16:
3156 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3157 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003158 case ISD::ConstantFP: {
3159 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003160 // Check to see if this FP immediate is already legal.
3161 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003162 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003163 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003164 else
Bill Wendling775db972009-12-23 00:28:23 +00003165 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003166 break;
3167 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003168 case ISD::EHSELECTION: {
3169 unsigned Reg = TLI.getExceptionSelectorRegister();
3170 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003171 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3172 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003173 Results.push_back(Results[0].getValue(1));
3174 break;
3175 }
3176 case ISD::EXCEPTIONADDR: {
3177 unsigned Reg = TLI.getExceptionAddressRegister();
3178 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003179 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3180 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003181 Results.push_back(Results[0].getValue(1));
3182 break;
3183 }
3184 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003185 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003186 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3187 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3188 "Don't know how to expand this subtraction!");
3189 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3190 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003191 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3192 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003193 break;
3194 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003195 case ISD::UREM:
3196 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003197 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003198 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003199 bool isSigned = Node->getOpcode() == ISD::SREM;
3200 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3201 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3202 Tmp2 = Node->getOperand(0);
3203 Tmp3 = Node->getOperand(1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003204 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3205 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3206 UseDivRem(Node, isSigned, false))) {
Eli Friedman3be2e512009-05-28 03:06:16 +00003207 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3208 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003209 // X % Y -> X-X/Y*Y
3210 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3211 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3212 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Evan Cheng65279cb2011-04-16 03:08:26 +00003213 } else if (isSigned)
3214 Tmp1 = ExpandIntLibCall(Node, true,
3215 RTLIB::SREM_I8,
3216 RTLIB::SREM_I16, RTLIB::SREM_I32,
3217 RTLIB::SREM_I64, RTLIB::SREM_I128);
3218 else
3219 Tmp1 = ExpandIntLibCall(Node, false,
3220 RTLIB::UREM_I8,
3221 RTLIB::UREM_I16, RTLIB::UREM_I32,
3222 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003223 Results.push_back(Tmp1);
3224 break;
3225 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003226 case ISD::UDIV:
3227 case ISD::SDIV: {
3228 bool isSigned = Node->getOpcode() == ISD::SDIV;
3229 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003230 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003231 SDVTList VTs = DAG.getVTList(VT, VT);
Evan Cheng65279cb2011-04-16 03:08:26 +00003232 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) ||
3233 (isDivRemLibcallAvailable(Node, isSigned, TLI) &&
3234 UseDivRem(Node, isSigned, true)))
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003235 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3236 Node->getOperand(1));
Evan Cheng65279cb2011-04-16 03:08:26 +00003237 else if (isSigned)
3238 Tmp1 = ExpandIntLibCall(Node, true,
3239 RTLIB::SDIV_I8,
3240 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
3241 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
3242 else
3243 Tmp1 = ExpandIntLibCall(Node, false,
3244 RTLIB::UDIV_I8,
3245 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
3246 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003247 Results.push_back(Tmp1);
3248 break;
3249 }
3250 case ISD::MULHU:
3251 case ISD::MULHS: {
3252 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3253 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003254 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003255 SDVTList VTs = DAG.getVTList(VT, VT);
3256 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3257 "If this wasn't legal, it shouldn't have been created!");
3258 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3259 Node->getOperand(1));
3260 Results.push_back(Tmp1.getValue(1));
3261 break;
3262 }
Evan Cheng65279cb2011-04-16 03:08:26 +00003263 case ISD::SDIVREM:
3264 case ISD::UDIVREM:
3265 // Expand into divrem libcall
3266 ExpandDivRemLibCall(Node, Results);
3267 break;
Eli Friedman26ea8f92009-05-27 07:05:37 +00003268 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003269 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003270 SDVTList VTs = DAG.getVTList(VT, VT);
3271 // See if multiply or divide can be lowered using two-result operations.
3272 // We just need the low half of the multiply; try both the signed
3273 // and unsigned forms. If the target supports both SMUL_LOHI and
3274 // UMUL_LOHI, form a preference by checking which forms of plain
3275 // MULH it supports.
3276 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3277 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3278 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3279 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3280 unsigned OpToUse = 0;
3281 if (HasSMUL_LOHI && !HasMULHS) {
3282 OpToUse = ISD::SMUL_LOHI;
3283 } else if (HasUMUL_LOHI && !HasMULHU) {
3284 OpToUse = ISD::UMUL_LOHI;
3285 } else if (HasSMUL_LOHI) {
3286 OpToUse = ISD::SMUL_LOHI;
3287 } else if (HasUMUL_LOHI) {
3288 OpToUse = ISD::UMUL_LOHI;
3289 }
3290 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003291 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3292 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003293 break;
3294 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003295 Tmp1 = ExpandIntLibCall(Node, false,
3296 RTLIB::MUL_I8,
3297 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003298 RTLIB::MUL_I64, RTLIB::MUL_I128);
3299 Results.push_back(Tmp1);
3300 break;
3301 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003302 case ISD::SADDO:
3303 case ISD::SSUBO: {
3304 SDValue LHS = Node->getOperand(0);
3305 SDValue RHS = Node->getOperand(1);
3306 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3307 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3308 LHS, RHS);
3309 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003310 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003311
Eli Friedman4bc8c712009-05-27 12:20:41 +00003312 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3313
3314 // LHSSign -> LHS >= 0
3315 // RHSSign -> RHS >= 0
3316 // SumSign -> Sum >= 0
3317 //
3318 // Add:
3319 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3320 // Sub:
3321 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3322 //
3323 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3324 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3325 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3326 Node->getOpcode() == ISD::SADDO ?
3327 ISD::SETEQ : ISD::SETNE);
3328
3329 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3330 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3331
3332 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3333 Results.push_back(Cmp);
3334 break;
3335 }
3336 case ISD::UADDO:
3337 case ISD::USUBO: {
3338 SDValue LHS = Node->getOperand(0);
3339 SDValue RHS = Node->getOperand(1);
3340 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3341 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3342 LHS, RHS);
3343 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003344 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3345 Node->getOpcode () == ISD::UADDO ?
3346 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003347 break;
3348 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003349 case ISD::UMULO:
3350 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003351 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003352 SDValue LHS = Node->getOperand(0);
3353 SDValue RHS = Node->getOperand(1);
3354 SDValue BottomHalf;
3355 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003356 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003357 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3358 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3359 bool isSigned = Node->getOpcode() == ISD::SMULO;
3360 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3361 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3362 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3363 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3364 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3365 RHS);
3366 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003367 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3368 VT.getSizeInBits() * 2))) {
Owen Anderson23b9b192009-08-12 00:36:31 +00003369 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003370 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3371 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3372 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3373 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3374 DAG.getIntPtrConstant(0));
3375 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3376 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003377 } else {
3378 // We can fall back to a libcall with an illegal type for the MUL if we
3379 // have a libcall big enough.
3380 // Also, we can fall back to a division in some cases, but that's a big
3381 // performance hit in the general case.
3382 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3383 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3384 if (WideVT == MVT::i16)
3385 LC = RTLIB::MUL_I16;
3386 else if (WideVT == MVT::i32)
3387 LC = RTLIB::MUL_I32;
3388 else if (WideVT == MVT::i64)
3389 LC = RTLIB::MUL_I64;
3390 else if (WideVT == MVT::i128)
3391 LC = RTLIB::MUL_I128;
3392 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3393 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3394 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
Owen Anderson95771af2011-02-25 21:41:48 +00003395
Eric Christophere3385812011-01-20 01:29:23 +00003396 SDValue Ret = ExpandLibCall(LC, Node, isSigned);
Eric Christopher38a18262011-01-20 00:29:24 +00003397 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Ret);
3398 TopHalf = DAG.getNode(ISD::SRL, dl, Ret.getValueType(), Ret,
3399 DAG.getConstant(VT.getSizeInBits(), TLI.getPointerTy()));
3400 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, TopHalf);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003401 }
3402 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003403 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3404 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003405 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3406 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3407 ISD::SETNE);
3408 } else {
3409 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3410 DAG.getConstant(0, VT), ISD::SETNE);
3411 }
3412 Results.push_back(BottomHalf);
3413 Results.push_back(TopHalf);
3414 break;
3415 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003416 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003417 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003418 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3419 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003420 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003421 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003422 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003423 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003424 break;
3425 }
Eli Friedman509150f2009-05-27 07:58:35 +00003426 case ISD::SELECT:
3427 Tmp1 = Node->getOperand(0);
3428 Tmp2 = Node->getOperand(1);
3429 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003430 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003431 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3432 Tmp2, Tmp3,
3433 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003434 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003435 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3436 DAG.getConstant(0, Tmp1.getValueType()),
3437 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003438 }
Eli Friedman509150f2009-05-27 07:58:35 +00003439 Results.push_back(Tmp1);
3440 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003441 case ISD::BR_JT: {
3442 SDValue Chain = Node->getOperand(0);
3443 SDValue Table = Node->getOperand(1);
3444 SDValue Index = Node->getOperand(2);
3445
Owen Andersone50ed302009-08-10 22:56:29 +00003446 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003447
3448 const TargetData &TD = *TLI.getTargetData();
3449 unsigned EntrySize =
3450 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003451
Chris Lattner071c62f2010-01-25 23:26:13 +00003452 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003453 Index, DAG.getConstant(EntrySize, PTy));
3454 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3455
Owen Anderson23b9b192009-08-12 00:36:31 +00003456 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003457 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003458 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003459 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003460 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003461 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003462 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003463 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003464 // RelocBase can be JumpTable, GOT or some sort of global base.
3465 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3466 TLI.getPICJumpTableRelocBase(Table, DAG));
3467 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003469 Results.push_back(Tmp1);
3470 break;
3471 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003472 case ISD::BRCOND:
3473 // Expand brcond's setcc into its constituent parts and create a BR_CC
3474 // Node.
3475 Tmp1 = Node->getOperand(0);
3476 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003477 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003478 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003479 Tmp1, Tmp2.getOperand(2),
3480 Tmp2.getOperand(0), Tmp2.getOperand(1),
3481 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003482 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003484 DAG.getCondCode(ISD::SETNE), Tmp2,
3485 DAG.getConstant(0, Tmp2.getValueType()),
3486 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003487 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003488 Results.push_back(Tmp1);
3489 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003490 case ISD::SETCC: {
3491 Tmp1 = Node->getOperand(0);
3492 Tmp2 = Node->getOperand(1);
3493 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003494 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003495
3496 // If we expanded the SETCC into an AND/OR, return the new node
3497 if (Tmp2.getNode() == 0) {
3498 Results.push_back(Tmp1);
3499 break;
3500 }
3501
3502 // Otherwise, SETCC for the given comparison type must be completely
3503 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003504 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003505 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3506 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3507 Results.push_back(Tmp1);
3508 break;
3509 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003510 case ISD::SELECT_CC: {
3511 Tmp1 = Node->getOperand(0); // LHS
3512 Tmp2 = Node->getOperand(1); // RHS
3513 Tmp3 = Node->getOperand(2); // True
3514 Tmp4 = Node->getOperand(3); // False
3515 SDValue CC = Node->getOperand(4);
3516
3517 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003518 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003519
3520 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3521 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3522 CC = DAG.getCondCode(ISD::SETNE);
3523 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3524 Tmp3, Tmp4, CC);
3525 Results.push_back(Tmp1);
3526 break;
3527 }
3528 case ISD::BR_CC: {
3529 Tmp1 = Node->getOperand(0); // Chain
3530 Tmp2 = Node->getOperand(2); // LHS
3531 Tmp3 = Node->getOperand(3); // RHS
3532 Tmp4 = Node->getOperand(1); // CC
3533
3534 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003535 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003536 LastCALLSEQ_END = DAG.getEntryNode();
3537
3538 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3539 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3540 Tmp4 = DAG.getCondCode(ISD::SETNE);
3541 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3542 Tmp3, Node->getOperand(4));
3543 Results.push_back(Tmp1);
3544 break;
3545 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003546 case ISD::GLOBAL_OFFSET_TABLE:
3547 case ISD::GlobalAddress:
3548 case ISD::GlobalTLSAddress:
3549 case ISD::ExternalSymbol:
3550 case ISD::ConstantPool:
3551 case ISD::JumpTable:
3552 case ISD::INTRINSIC_W_CHAIN:
3553 case ISD::INTRINSIC_WO_CHAIN:
3554 case ISD::INTRINSIC_VOID:
3555 // FIXME: Custom lowering for these operations shouldn't return null!
3556 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3557 Results.push_back(SDValue(Node, i));
3558 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003559 }
3560}
3561void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3562 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003563 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003564 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003565 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003566 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003567 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003568 }
Owen Andersone50ed302009-08-10 22:56:29 +00003569 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003570 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003571 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003572 switch (Node->getOpcode()) {
3573 case ISD::CTTZ:
3574 case ISD::CTLZ:
3575 case ISD::CTPOP:
3576 // Zero extend the argument.
3577 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3578 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003579 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003580 if (Node->getOpcode() == ISD::CTTZ) {
3581 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003582 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003583 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3584 ISD::SETEQ);
3585 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3586 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3587 } else if (Node->getOpcode() == ISD::CTLZ) {
3588 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3589 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3590 DAG.getConstant(NVT.getSizeInBits() -
3591 OVT.getSizeInBits(), NVT));
3592 }
Bill Wendling775db972009-12-23 00:28:23 +00003593 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003594 break;
3595 case ISD::BSWAP: {
3596 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003597 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003598 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3599 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003600 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003601 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003602 break;
3603 }
3604 case ISD::FP_TO_UINT:
3605 case ISD::FP_TO_SINT:
3606 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3607 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3608 Results.push_back(Tmp1);
3609 break;
3610 case ISD::UINT_TO_FP:
3611 case ISD::SINT_TO_FP:
3612 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3613 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3614 Results.push_back(Tmp1);
3615 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003616 case ISD::AND:
3617 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003618 case ISD::XOR: {
3619 unsigned ExtOp, TruncOp;
3620 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003621 ExtOp = ISD::BITCAST;
3622 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003623 } else {
3624 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003625 ExtOp = ISD::ANY_EXTEND;
3626 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003627 }
3628 // Promote each of the values to the new type.
3629 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3630 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3631 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003632 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3633 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003634 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003635 }
3636 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003637 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003638 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003639 ExtOp = ISD::BITCAST;
3640 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003641 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003642 ExtOp = ISD::ANY_EXTEND;
3643 TruncOp = ISD::TRUNCATE;
3644 } else {
3645 ExtOp = ISD::FP_EXTEND;
3646 TruncOp = ISD::FP_ROUND;
3647 }
3648 Tmp1 = Node->getOperand(0);
3649 // Promote each of the values to the new type.
3650 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3651 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3652 // Perform the larger operation, then round down.
3653 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3654 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003655 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003656 else
Bill Wendling775db972009-12-23 00:28:23 +00003657 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003658 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003659 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003660 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003661 }
Eli Friedman509150f2009-05-27 07:58:35 +00003662 case ISD::VECTOR_SHUFFLE: {
3663 SmallVector<int, 8> Mask;
3664 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3665
3666 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003667 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3668 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003669
3670 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003671 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003672 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003673 Results.push_back(Tmp1);
3674 break;
3675 }
Eli Friedmanad754602009-05-28 03:56:57 +00003676 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003677 unsigned ExtOp = ISD::FP_EXTEND;
3678 if (NVT.isInteger()) {
3679 ISD::CondCode CCCode =
3680 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3681 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003682 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003683 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3684 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003685 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3686 Tmp1, Tmp2, Node->getOperand(2)));
3687 break;
3688 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003689 }
3690}
3691
Chris Lattner3e928bb2005-01-07 07:47:09 +00003692// SelectionDAG::Legalize - This is the entry point for the file.
3693//
Dan Gohman714efc62009-12-05 17:51:33 +00003694void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003695 /// run - This is the main entry point to this class.
3696 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003697 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003698}
3699