blob: 477cf093f106c21bbd2d1910e2f5dcb77a6d8420 [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
Chris Lattner6831a812006-02-13 09:18:02 +000069 /// IsLegalizingCall - This member is used *only* for purposes of providing
Scott Michelfdc40a02009-02-17 22:15:04 +000070 /// helpful assertions that a libcall isn't created while another call is
Chris Lattner6831a812006-02-13 09:18:02 +000071 /// being legalized (which could lead to non-serialized call sequences).
72 bool IsLegalizingCall;
Scott Michelfdc40a02009-02-17 22:15:04 +000073
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));
Chris Lattner8afc48e2005-01-07 22:28:47 +000095 }
96
Chris Lattner3e928bb2005-01-07 07:47:09 +000097public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000098 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000099
Chris Lattner3e928bb2005-01-07 07:47:09 +0000100 /// getTypeAction - Return how we should legalize values of this type, either
101 /// it is already legal or we need to expand it into multiple registers of
102 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +0000103 LegalizeAction getTypeAction(EVT VT) const {
Chris Lattneraafe6262010-08-25 23:00:45 +0000104 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105 }
106
107 /// isTypeLegal - Return true if this type is legal on this target.
108 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000109 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000110 return getTypeAction(VT) == Legal;
111 }
112
Chris Lattner3e928bb2005-01-07 07:47:09 +0000113 void LegalizeDAG();
114
Chris Lattner456a93a2006-01-28 07:39:30 +0000115private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000116 /// LegalizeOp - We know that the specified value has a legal type.
117 /// Recursively ensure that the operands have legal types, then return the
118 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000119 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000120
Eli Friedman7ef3d172009-06-06 07:04:42 +0000121 SDValue OptimizeFloatStore(StoreSDNode *ST);
122
Nate Begeman68679912008-04-25 18:07:40 +0000123 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
124 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
125 /// is necessary to spill the vector being inserted into to memory, perform
126 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000128 SDValue Idx, DebugLoc dl);
129 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
130 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000131
Nate Begeman5a5ca152009-04-29 05:20:52 +0000132 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
133 /// performs the same shuffe in terms of order or result bytes, but on a type
134 /// whose vector element type is narrower than the original shuffle type.
135 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000136 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000137 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000138 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000139
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000140 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000141 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000142
Owen Andersone50ed302009-08-10 22:56:29 +0000143 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000144 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000145
Eli Friedman47b41f72009-05-27 02:21:29 +0000146 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Jim Grosbache03262f2010-06-18 21:43:38 +0000147 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
148 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000149 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
150 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
151 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000152 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
153 RTLIB::Libcall Call_I8,
154 RTLIB::Libcall Call_I16,
155 RTLIB::Libcall Call_I32,
156 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000157 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000158
Owen Andersone50ed302009-08-10 22:56:29 +0000159 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000160 SDValue ExpandBUILD_VECTOR(SDNode *Node);
161 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000162 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
163 SmallVectorImpl<SDValue> &Results);
164 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000165 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000166 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000167 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000168 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000169 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000170 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000171
Dale Johannesen8a782a22009-02-02 22:12:50 +0000172 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
173 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000174
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000175 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000176 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000177
Jim Grosbache03262f2010-06-18 21:43:38 +0000178 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
179
Eli Friedman8c377c72009-05-27 01:25:56 +0000180 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
181 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000182};
183}
184
Nate Begeman5a5ca152009-04-29 05:20:52 +0000185/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
186/// performs the same shuffe in terms of order or result bytes, but on a type
187/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000188/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000189SDValue
190SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000191 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000192 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000193 unsigned NumMaskElts = VT.getVectorNumElements();
194 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000196
Nate Begeman9008ca62009-04-27 18:41:29 +0000197 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
198
199 if (NumEltsGrowth == 1)
200 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000201
Nate Begeman9008ca62009-04-27 18:41:29 +0000202 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000203 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000204 int Idx = Mask[i];
205 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000206 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000207 NewMask.push_back(-1);
208 else
209 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000210 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000211 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000212 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000213 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
214 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000215}
216
Bill Wendling5aa49772009-02-24 02:35:30 +0000217SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000218 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000219 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
220 DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000221 ValueTypeActions(TLI.getValueTypeActions()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224}
225
Chris Lattner3e928bb2005-01-07 07:47:09 +0000226void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000227 LastCALLSEQ_END = DAG.getEntryNode();
228 IsLegalizingCall = false;
Mon P Wange5ab34e2009-02-04 19:38:14 +0000229
Chris Lattnerab510a72005-10-02 17:49:46 +0000230 // The legalize process is inherently a bottom-up recursive process (users
231 // legalize their uses before themselves). Given infinite stack space, we
232 // could just start legalizing on the root and traverse the whole graph. In
233 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000234 // blocks. To avoid this problem, compute an ordering of the nodes where each
235 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000236 DAG.AssignTopologicalOrder();
237 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000238 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000239 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000240
241 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000242 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000243 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
244 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245
Chris Lattner3e928bb2005-01-07 07:47:09 +0000246 LegalizedNodes.clear();
247
248 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000249 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000250}
251
Chris Lattner6831a812006-02-13 09:18:02 +0000252
253/// FindCallEndFromCallStart - Given a chained node that is part of a call
254/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000255static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000256 // Nested CALLSEQ_START/END constructs aren't yet legal,
257 // but we can DTRT and handle them correctly here.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000258 if (Node->getOpcode() == ISD::CALLSEQ_START)
259 depth++;
Stuart Hastings2965e692010-12-21 17:07:24 +0000260 else if (Node->getOpcode() == ISD::CALLSEQ_END) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000261 depth--;
262 if (depth == 0)
263 return Node;
264 }
Chris Lattner6831a812006-02-13 09:18:02 +0000265 if (Node->use_empty())
266 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000267
Chris Lattner6831a812006-02-13 09:18:02 +0000268 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000269 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000271 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000272 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000274 // Otherwise, hunt for it.
275 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000277 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000278 break;
279 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000280
281 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000283 return 0;
284 }
285 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000286
Chris Lattner6831a812006-02-13 09:18:02 +0000287 for (SDNode::use_iterator UI = Node->use_begin(),
288 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000289
Chris Lattner6831a812006-02-13 09:18:02 +0000290 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000291 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000292 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
293 if (User->getOperand(i) == TheChain)
Stuart Hastingsa304d022010-12-09 21:25:20 +0000294 if (SDNode *Result = FindCallEndFromCallStart(User, depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000295 return Result;
296 }
297 return 0;
298}
299
Scott Michelfdc40a02009-02-17 22:15:04 +0000300/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000301/// sequence, find the CALLSEQ_START node that initiates the call sequence.
302static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000303 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000304 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000305 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
306 Node = Node->getOperand(0).getNode();
307 assert(Node->getOperand(0).getValueType() == MVT::Other &&
308 "Node doesn't have a token chain argument!");
309 switch (Node->getOpcode()) {
310 default:
311 break;
312 case ISD::CALLSEQ_START:
313 if (!nested)
314 return Node;
315 nested--;
316 break;
317 case ISD::CALLSEQ_END:
318 nested++;
319 break;
320 }
321 }
322 return 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000323}
324
325/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000326/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000327/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000328///
329/// Keep track of the nodes we fine that actually do lead to Dest in
330/// NodesLeadingTo. This avoids retraversing them exponential number of times.
331///
332bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000333 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000334 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000335
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000336 // If we've already processed this node and it does lead to Dest, there is no
337 // need to reprocess it.
338 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000339
Chris Lattner6831a812006-02-13 09:18:02 +0000340 // If the first result of this node has been already legalized, then it cannot
341 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000342 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000343
Chris Lattner6831a812006-02-13 09:18:02 +0000344 // Okay, this node has not already been legalized. Check and legalize all
345 // operands. If none lead to Dest, then we can legalize this node.
346 bool OperandsLeadToDest = false;
347 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
348 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000349 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
350 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000351
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000352 if (OperandsLeadToDest) {
353 NodesLeadingTo.insert(N);
354 return true;
355 }
Chris Lattner6831a812006-02-13 09:18:02 +0000356
357 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000358 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000359 return false;
360}
361
Evan Cheng9f877882006-12-13 20:57:08 +0000362/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
363/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000364static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000365 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000366 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000367 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000368
369 // If a FP immediate is precise when represented as a float and if the
370 // target can do an extending load from float to double, we put it into
371 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000372 // double. This shrinks FP constants and canonicalizes them for targets where
373 // an FP extending load is the same cost as a normal load (such as on the x87
374 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000375 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000376 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000377 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000379 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000381 }
382
Owen Andersone50ed302009-08-10 22:56:29 +0000383 EVT OrigVT = VT;
384 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 while (SVT != MVT::f32) {
386 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000387 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000388 // Only do this if the target has a native EXTLOAD instruction from
389 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000390 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000391 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000392 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000393 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000394 VT = SVT;
395 Extend = true;
396 }
Evan Cheng00495212006-12-12 21:32:44 +0000397 }
398
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000400 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000401 if (Extend)
Evan Chengbcc80172010-07-07 22:15:37 +0000402 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, dl,
403 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000404 CPIdx, MachinePointerInfo::getConstantPool(),
405 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000406 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000407 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000408 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000409}
410
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000411/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
412static
Dan Gohman475871a2008-07-27 21:46:04 +0000413SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000414 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000415 SDValue Chain = ST->getChain();
416 SDValue Ptr = ST->getBasePtr();
417 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000418 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000419 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000420 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000421 if (ST->getMemoryVT().isFloatingPoint() ||
422 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000423 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000424 if (TLI.isTypeLegal(intVT)) {
425 // Expand to a bitconvert of the value to the integer type of the
426 // same size, then a (misaligned) int store.
427 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000428 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000429 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
430 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000431 } else {
432 // Do a (aligned) store to a stack slot, then copy from the stack slot
433 // to the final destination using (unaligned) integer loads and stores.
Owen Andersone50ed302009-08-10 22:56:29 +0000434 EVT StoredVT = ST->getMemoryVT();
435 EVT RegVT =
Evan Chengadf97992010-04-15 01:25:27 +0000436 TLI.getRegisterType(*DAG.getContext(),
437 EVT::getIntegerVT(*DAG.getContext(),
438 StoredVT.getSizeInBits()));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000439 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
440 unsigned RegBytes = RegVT.getSizeInBits() / 8;
441 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000442
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000443 // Make sure the stack slot is also aligned for the register type.
444 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
445
446 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000447 SDValue Store = DAG.getTruncStore(Chain, dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000448 Val, StackPtr, MachinePointerInfo(),
449 StoredVT, false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000450 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
451 SmallVector<SDValue, 8> Stores;
452 unsigned Offset = 0;
453
454 // Do all but one copies using the full register width.
455 for (unsigned i = 1; i < NumRegs; i++) {
456 // Load one integer register's worth from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000457 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
458 MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000459 false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000460 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000461 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000462 ST->getPointerInfo().getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +0000463 ST->isVolatile(), ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000464 MinAlign(ST->getAlignment(), Offset)));
465 // Increment the pointers.
466 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000467 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000468 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000469 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000470 }
471
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000472 // The last store may be partial. Do a truncating store. On big-endian
473 // machines this requires an extending load from the stack slot to ensure
474 // that the bits are in the right place.
Evan Chengadf97992010-04-15 01:25:27 +0000475 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
476 8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000477
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000478 // Load from the stack slot.
Evan Chengbcc80172010-07-07 22:15:37 +0000479 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Store, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000480 MachinePointerInfo(),
481 MemVT, false, false, 0);
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000482
Dale Johannesenbb5da912009-02-02 20:41:04 +0000483 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000484 ST->getPointerInfo()
485 .getWithOffset(Offset),
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000486 MemVT, ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000487 ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000488 MinAlign(ST->getAlignment(), Offset)));
489 // The order of the stores doesn't matter - say it with a TokenFactor.
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000491 Stores.size());
492 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000493 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000494 assert(ST->getMemoryVT().isInteger() &&
495 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000496 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000497 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000498 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000499 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000500 int IncrementSize = NumBits / 8;
501
502 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000503 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
504 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000505 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000506
507 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000508 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000509 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000510 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000511 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000513 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000514 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000515 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000516 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000517 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
518 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000519
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000521}
522
523/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
524static
Dan Gohman475871a2008-07-27 21:46:04 +0000525SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000526 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000527 SDValue Chain = LD->getChain();
528 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000529 EVT VT = LD->getValueType(0);
530 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000531 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000532 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000533 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000534 if (TLI.isTypeLegal(intVT)) {
535 // Expand to a (misaligned) integer load of the same size,
536 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000537 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
538 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000539 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000540 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000541 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000542 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000543
Duncan Sands05e11fa2008-12-12 21:47:02 +0000544 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000545 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000546 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000547
Chris Lattnerecf42c42010-09-21 16:36:31 +0000548 // Copy the value to a (aligned) stack slot using (unaligned) integer
549 // loads and stores, then do a (aligned) load from the stack slot.
550 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
551 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
552 unsigned RegBytes = RegVT.getSizeInBits() / 8;
553 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
554
555 // Make sure the stack slot is also aligned for the register type.
556 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
557
558 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
559 SmallVector<SDValue, 8> Stores;
560 SDValue StackPtr = StackBase;
561 unsigned Offset = 0;
562
563 // Do all but one copies using the full register width.
564 for (unsigned i = 1; i < NumRegs; i++) {
565 // Load one integer register's worth from the original location.
566 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
567 LD->getPointerInfo().getWithOffset(Offset),
568 LD->isVolatile(), LD->isNonTemporal(),
569 MinAlign(LD->getAlignment(), Offset));
570 // Follow the load with a store to the stack slot. Remember the store.
571 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000572 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000573 // Increment the pointers.
574 Offset += RegBytes;
575 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
576 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
577 Increment);
578 }
579
580 // The last copy may be partial. Do an extending load.
581 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
582 8 * (LoadedBytes - Offset));
583 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Chain, Ptr,
584 LD->getPointerInfo().getWithOffset(Offset),
585 MemVT, LD->isVolatile(),
586 LD->isNonTemporal(),
587 MinAlign(LD->getAlignment(), Offset));
588 // Follow the load with a store to the stack slot. Remember the store.
589 // On big-endian machines this requires a truncating store to ensure
590 // that the bits end up in the right place.
591 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
592 MachinePointerInfo(), MemVT,
593 false, false, 0));
594
595 // The order of the stores doesn't matter - say it with a TokenFactor.
596 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
597 Stores.size());
598
599 // Finally, perform the original load only redirected to the stack slot.
600 Load = DAG.getExtLoad(LD->getExtensionType(), VT, dl, TF, StackBase,
601 MachinePointerInfo(), LoadedVT, false, false, 0);
602
603 // Callers expect a MERGE_VALUES node.
604 SDValue Ops[] = { Load, TF };
605 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000606 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000607 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000608 "Unaligned load of unsupported type.");
609
Dale Johannesen8155d642008-02-27 22:36:00 +0000610 // Compute the new VT that is half the size of the old one. This is an
611 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000612 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000613 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000614 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000615 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000616
Chris Lattnere400af82007-11-19 21:38:03 +0000617 unsigned Alignment = LD->getAlignment();
618 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000619 ISD::LoadExtType HiExtType = LD->getExtensionType();
620
621 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
622 if (HiExtType == ISD::NON_EXTLOAD)
623 HiExtType = ISD::ZEXTLOAD;
624
625 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000626 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000627 if (TLI.isLittleEndian()) {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000628 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr, LD->getPointerInfo(),
629 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000630 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000631 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000633 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr,
634 LD->getPointerInfo().getWithOffset(IncrementSize),
635 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000636 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000637 } else {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000638 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr, LD->getPointerInfo(),
639 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000640 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000641 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000642 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000643 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr,
644 LD->getPointerInfo().getWithOffset(IncrementSize),
645 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000646 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000647 }
648
649 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000650 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000651 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
652 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000655 Hi.getValue(1));
656
Dan Gohman475871a2008-07-27 21:46:04 +0000657 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000658 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000659}
Evan Cheng912095b2007-01-04 21:56:39 +0000660
Nate Begeman68679912008-04-25 18:07:40 +0000661/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
662/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
663/// is necessary to spill the vector being inserted into to memory, perform
664/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000665SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000666PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
667 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000668 SDValue Tmp1 = Vec;
669 SDValue Tmp2 = Val;
670 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000671
Nate Begeman68679912008-04-25 18:07:40 +0000672 // If the target doesn't support this, we have to spill the input vector
673 // to a temporary stack slot, update the element, then reload it. This is
674 // badness. We could also load the value into a vector register (either
675 // with a "move to register" or "extload into register" instruction, then
676 // permute it into place, if the idx is a constant and if the idx is
677 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000678 EVT VT = Tmp1.getValueType();
679 EVT EltVT = VT.getVectorElementType();
680 EVT IdxVT = Tmp3.getValueType();
681 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000682 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000683
Evan Chengff89dcb2009-10-18 18:16:27 +0000684 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
685
Nate Begeman68679912008-04-25 18:07:40 +0000686 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000687 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000688 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000689 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000690
691 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000692 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000693 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000694 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000695 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000696 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
697 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000698 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000699 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000700 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000701 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000702 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000703 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000704}
705
Mon P Wange9f10152008-12-09 05:46:39 +0000706
Eli Friedman3f727d62009-05-27 02:16:40 +0000707SDValue SelectionDAGLegalize::
708ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
709 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
710 // SCALAR_TO_VECTOR requires that the type of the value being inserted
711 // match the element type of the vector being created, except for
712 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000713 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000714 if (Val.getValueType() == EltVT ||
715 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
716 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
717 Vec.getValueType(), Val);
718
719 unsigned NumElts = Vec.getValueType().getVectorNumElements();
720 // We generate a shuffle of InVec and ScVec, so the shuffle mask
721 // should be 0,1,2,3,4,5... with the appropriate element replaced with
722 // elt 0 of the RHS.
723 SmallVector<int, 8> ShufOps;
724 for (unsigned i = 0; i != NumElts; ++i)
725 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
726
727 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
728 &ShufOps[0]);
729 }
730 }
731 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
732}
733
Eli Friedman7ef3d172009-06-06 07:04:42 +0000734SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
735 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
736 // FIXME: We shouldn't do this for TargetConstantFP's.
737 // FIXME: move this to the DAG Combiner! Note that we can't regress due
738 // to phase ordering between legalized code and the dag combiner. This
739 // probably means that we need to integrate dag combiner and legalizer
740 // together.
741 // We generally can't do this one for long doubles.
742 SDValue Tmp1 = ST->getChain();
743 SDValue Tmp2 = ST->getBasePtr();
744 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000745 unsigned Alignment = ST->getAlignment();
746 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000747 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000748 DebugLoc dl = ST->getDebugLoc();
749 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 if (CFP->getValueType(0) == MVT::f32 &&
751 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000752 Tmp3 = DAG.getConstant(CFP->getValueAPF().
753 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000755 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
756 isVolatile, isNonTemporal, Alignment);
757 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000759 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000760 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000762 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000763 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000764 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
765 isVolatile, isNonTemporal, Alignment);
766 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000768 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000769 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
770 // stores. If the target supports neither 32- nor 64-bits, this
771 // xform is certainly not worth it.
772 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000773 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000775 if (TLI.isBigEndian()) std::swap(Lo, Hi);
776
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000777 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
778 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000779 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
780 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000781 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
782 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000783 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000784
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000786 }
787 }
788 }
789 return SDValue();
790}
791
Dan Gohmana3466152007-07-13 20:14:11 +0000792/// LegalizeOp - We know that the specified value has a legal type, and
793/// that its operands are legal. Now ensure that the operation itself
794/// is legal, recursively ensuring that the operands' operations remain
795/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000796SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000797 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
798 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000799
Gabor Greifba36cb52008-08-28 21:40:38 +0000800 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000801 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000802
Eli Friedman1fde9c52009-05-24 02:46:31 +0000803 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
804 assert(getTypeAction(Node->getValueType(i)) == Legal &&
805 "Unexpected illegal type!");
806
807 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000808 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000809 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
810 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000811
Chris Lattner45982da2005-05-12 16:53:42 +0000812 // Note that LegalizeOp may be reentered even from single-use nodes, which
813 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000814 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000815 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000816
Dan Gohman475871a2008-07-27 21:46:04 +0000817 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
818 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000819 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000820
Eli Friedman8c377c72009-05-27 01:25:56 +0000821 // Figure out the correct action; the way to query this varies by opcode
822 TargetLowering::LegalizeAction Action;
823 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000824 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000825 case ISD::INTRINSIC_W_CHAIN:
826 case ISD::INTRINSIC_WO_CHAIN:
827 case ISD::INTRINSIC_VOID:
828 case ISD::VAARG:
829 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000831 break;
832 case ISD::SINT_TO_FP:
833 case ISD::UINT_TO_FP:
834 case ISD::EXTRACT_VECTOR_ELT:
835 Action = TLI.getOperationAction(Node->getOpcode(),
836 Node->getOperand(0).getValueType());
837 break;
838 case ISD::FP_ROUND_INREG:
839 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000840 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000841 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
842 break;
843 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000844 case ISD::SELECT_CC:
845 case ISD::SETCC:
846 case ISD::BR_CC: {
847 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
848 Node->getOpcode() == ISD::SETCC ? 2 : 1;
849 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000850 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000851 ISD::CondCode CCCode =
852 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
853 Action = TLI.getCondCodeAction(CCCode, OpVT);
854 if (Action == TargetLowering::Legal) {
855 if (Node->getOpcode() == ISD::SELECT_CC)
856 Action = TLI.getOperationAction(Node->getOpcode(),
857 Node->getValueType(0));
858 else
859 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
860 }
861 break;
862 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000863 case ISD::LOAD:
864 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000865 // FIXME: Model these properly. LOAD and STORE are complicated, and
866 // STORE expects the unlegalized operand in some cases.
867 SimpleFinishLegalizing = false;
868 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000869 case ISD::CALLSEQ_START:
870 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000871 // FIXME: This shouldn't be necessary. These nodes have special properties
872 // dealing with the recursive nature of legalization. Removing this
873 // special case should be done as part of making LegalizeDAG non-recursive.
874 SimpleFinishLegalizing = false;
875 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000876 case ISD::EXTRACT_ELEMENT:
877 case ISD::FLT_ROUNDS_:
878 case ISD::SADDO:
879 case ISD::SSUBO:
880 case ISD::UADDO:
881 case ISD::USUBO:
882 case ISD::SMULO:
883 case ISD::UMULO:
884 case ISD::FPOWI:
885 case ISD::MERGE_VALUES:
886 case ISD::EH_RETURN:
887 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000888 case ISD::EH_SJLJ_SETJMP:
889 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000890 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000891 // These operations lie about being legal: when they claim to be legal,
892 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000893 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
894 if (Action == TargetLowering::Legal)
895 Action = TargetLowering::Expand;
896 break;
897 case ISD::TRAMPOLINE:
898 case ISD::FRAMEADDR:
899 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000900 // These operations lie about being legal: when they claim to be legal,
901 // they should actually be custom-lowered.
902 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
903 if (Action == TargetLowering::Legal)
904 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000905 break;
906 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000907 // A weird case: legalization for BUILD_VECTOR never legalizes the
908 // operands!
909 // FIXME: This really sucks... changing it isn't semantically incorrect,
910 // but it massively pessimizes the code for floating-point BUILD_VECTORs
911 // because ConstantFP operands get legalized into constant pool loads
912 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
913 // though, because BUILD_VECTORS usually get lowered into other nodes
914 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000915 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000916 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000917 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000918 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000919 Action = TargetLowering::Legal;
920 } else {
921 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000922 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000923 break;
924 }
925
926 if (SimpleFinishLegalizing) {
927 SmallVector<SDValue, 8> Ops, ResultVals;
928 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
929 Ops.push_back(LegalizeOp(Node->getOperand(i)));
930 switch (Node->getOpcode()) {
931 default: break;
932 case ISD::BR:
933 case ISD::BRIND:
934 case ISD::BR_JT:
935 case ISD::BR_CC:
936 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000937 // Branches tweak the chain to include LastCALLSEQ_END
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Eli Friedman8c377c72009-05-27 01:25:56 +0000939 LastCALLSEQ_END);
940 Ops[0] = LegalizeOp(Ops[0]);
941 LastCALLSEQ_END = DAG.getEntryNode();
942 break;
943 case ISD::SHL:
944 case ISD::SRL:
945 case ISD::SRA:
946 case ISD::ROTL:
947 case ISD::ROTR:
948 // Legalizing shifts/rotates requires adjusting the shift amount
949 // to the appropriate width.
950 if (!Ops[1].getValueType().isVector())
951 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
952 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000953 case ISD::SRL_PARTS:
954 case ISD::SRA_PARTS:
955 case ISD::SHL_PARTS:
956 // Legalizing shifts/rotates requires adjusting the shift amount
957 // to the appropriate width.
958 if (!Ops[2].getValueType().isVector())
959 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000960 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000961 }
962
Dan Gohman027657d2010-06-18 15:30:29 +0000963 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
964 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000965 switch (Action) {
966 case TargetLowering::Legal:
967 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
968 ResultVals.push_back(Result.getValue(i));
969 break;
970 case TargetLowering::Custom:
971 // FIXME: The handling for custom lowering with multiple results is
972 // a complete mess.
973 Tmp1 = TLI.LowerOperation(Result, DAG);
974 if (Tmp1.getNode()) {
975 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
976 if (e == 1)
977 ResultVals.push_back(Tmp1);
978 else
979 ResultVals.push_back(Tmp1.getValue(i));
980 }
981 break;
982 }
983
984 // FALL THROUGH
985 case TargetLowering::Expand:
986 ExpandNode(Result.getNode(), ResultVals);
987 break;
988 case TargetLowering::Promote:
989 PromoteNode(Result.getNode(), ResultVals);
990 break;
991 }
992 if (!ResultVals.empty()) {
993 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
994 if (ResultVals[i] != SDValue(Node, i))
995 ResultVals[i] = LegalizeOp(ResultVals[i]);
996 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
997 }
998 return ResultVals[Op.getResNo()];
999 }
1000 }
1001
1002 switch (Node->getOpcode()) {
1003 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001004#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001005 dbgs() << "NODE: ";
1006 Node->dump( &DAG);
1007 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001008#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001009 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001010
Chris Lattnerb2827b02006-03-19 00:52:58 +00001011 case ISD::BUILD_VECTOR:
1012 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001013 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001014 case TargetLowering::Custom:
1015 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001016 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001017 Result = Tmp3;
1018 break;
1019 }
1020 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001021 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001022 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001023 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001024 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001025 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001026 case ISD::CALLSEQ_START: {
1027 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001028
Chris Lattner6831a812006-02-13 09:18:02 +00001029 // Recursively Legalize all of the inputs of the call end that do not lead
1030 // to this call start. This ensures that any libcalls that need be inserted
1031 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001032 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001033 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001034 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001035 NodesLeadingTo);
1036 }
Chris Lattner6831a812006-02-13 09:18:02 +00001037
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001038 // Now that we have legalized all of the inputs (which may have inserted
1039 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1041
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001042 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001043 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001044 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001045 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001046 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001047 Tmp1 = LegalizeOp(Tmp1);
1048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001049
Chris Lattner6831a812006-02-13 09:18:02 +00001050 // Do not try to legalize the target-specific arguments (#1+).
1051 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001052 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001053 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001054 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1055 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001056 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001057
Chris Lattner6831a812006-02-13 09:18:02 +00001058 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001059 AddLegalizedOperand(Op.getValue(0), Result);
1060 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1061 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001062
Chris Lattner6831a812006-02-13 09:18:02 +00001063 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001064 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001065 // process, no libcalls can/will be inserted, guaranteeing that no calls
1066 // can overlap.
1067 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001068 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001069 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001070 IsLegalizingCall = true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Chris Lattner6831a812006-02-13 09:18:02 +00001072 // Legalize the call, starting from the CALLSEQ_END.
1073 LegalizeOp(LastCALLSEQ_END);
1074 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1075 return Result;
1076 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001077 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001078 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1079 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001081 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1082 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001083 assert(I != LegalizedNodes.end() &&
1084 "Legalizing the call start should have legalized this node!");
1085 return I->second;
1086 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001087
1088 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001089 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001091 // Do not try to legalize the target-specific arguments (#1+), except for
1092 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001093 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001094 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001095 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001096 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001097 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1098 &Ops[0], Ops.size()),
1099 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001100 }
1101 } else {
1102 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1103 if (Tmp1 != Node->getOperand(0) ||
1104 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001105 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001106 Ops[0] = Tmp1;
1107 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001108 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1109 &Ops[0], Ops.size()),
1110 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001111 }
Chris Lattner6a542892006-01-24 05:48:21 +00001112 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001113 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001114 // This finishes up call legalization.
1115 IsLegalizingCall = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001116
Chris Lattner4b653a02006-02-14 00:55:02 +00001117 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001118 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001119 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001120 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001121 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001122 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001123 LoadSDNode *LD = cast<LoadSDNode>(Node);
1124 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1125 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001126
Evan Cheng466685d2006-10-09 20:57:25 +00001127 ISD::LoadExtType ExtType = LD->getExtensionType();
1128 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001129 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001130 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1131 Tmp1, Tmp2, LD->getOffset()),
1132 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001133 Tmp3 = Result.getValue(0);
1134 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001135
Evan Cheng466685d2006-10-09 20:57:25 +00001136 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001137 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001138 case TargetLowering::Legal:
1139 // If this is an unaligned load and the target doesn't support it,
1140 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001141 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001142 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1143 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001144 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001145 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001146 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001147 Tmp3 = Result.getOperand(0);
1148 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001149 Tmp3 = LegalizeOp(Tmp3);
1150 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001151 }
1152 }
1153 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001154 case TargetLowering::Custom:
1155 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001156 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001157 Tmp3 = LegalizeOp(Tmp1);
1158 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001159 }
Evan Cheng466685d2006-10-09 20:57:25 +00001160 break;
1161 case TargetLowering::Promote: {
1162 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001163 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001164 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001165 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001166
Chris Lattnerecf42c42010-09-21 16:36:31 +00001167 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001168 LD->isVolatile(), LD->isNonTemporal(),
1169 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001170 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001171 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001172 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001173 }
Evan Cheng466685d2006-10-09 20:57:25 +00001174 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001175 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001176 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001177 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1178 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001179 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001180 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001181
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001182 EVT SrcVT = LD->getMemoryVT();
1183 unsigned SrcWidth = SrcVT.getSizeInBits();
1184 unsigned Alignment = LD->getAlignment();
1185 bool isVolatile = LD->isVolatile();
1186 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001187
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001188 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1189 // Some targets pretend to have an i1 loading operation, and actually
1190 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1191 // bits are guaranteed to be zero; it helps the optimizers understand
1192 // that these bits are zero. It is also useful for EXTLOAD, since it
1193 // tells the optimizers that those bits are undefined. It would be
1194 // nice to have an effective generic way of getting these benefits...
1195 // Until such a way is found, don't insist on promoting i1 here.
1196 (SrcVT != MVT::i1 ||
1197 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1198 // Promote to a byte-sized load if not loading an integral number of
1199 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1200 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1201 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1202 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001203
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001204 // The extra bits are guaranteed to be zero, since we stored them that
1205 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001206
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001207 ISD::LoadExtType NewExtType =
1208 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001209
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001210 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0), dl,
1211 Tmp1, Tmp2, LD->getPointerInfo(),
1212 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001213
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001214 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001215
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001216 if (ExtType == ISD::SEXTLOAD)
1217 // Having the top bits zero doesn't help when sign extending.
1218 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1219 Result.getValueType(),
1220 Result, DAG.getValueType(SrcVT));
1221 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1222 // All the top bits are guaranteed to be zero - inform the optimizers.
1223 Result = DAG.getNode(ISD::AssertZext, dl,
1224 Result.getValueType(), Result,
1225 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001226
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001227 Tmp1 = LegalizeOp(Result);
1228 Tmp2 = LegalizeOp(Ch);
1229 } else if (SrcWidth & (SrcWidth - 1)) {
1230 // If not loading a power-of-2 number of bits, expand as two loads.
1231 assert(!SrcVT.isVector() && "Unsupported extload!");
1232 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1233 assert(RoundWidth < SrcWidth);
1234 unsigned ExtraWidth = SrcWidth - RoundWidth;
1235 assert(ExtraWidth < RoundWidth);
1236 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1237 "Load size not an integral number of bytes!");
1238 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1239 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1240 SDValue Lo, Hi, Ch;
1241 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001242
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001243 if (TLI.isLittleEndian()) {
1244 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1245 // Load the bottom RoundWidth bits.
1246 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), dl,
1247 Tmp1, Tmp2,
1248 LD->getPointerInfo(), RoundVT, isVolatile,
1249 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001250
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001251 // Load the remaining ExtraWidth bits.
1252 IncrementSize = RoundWidth / 8;
1253 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1254 DAG.getIntPtrConstant(IncrementSize));
1255 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), dl, Tmp1, Tmp2,
1256 LD->getPointerInfo().getWithOffset(IncrementSize),
1257 ExtraVT, isVolatile, isNonTemporal,
1258 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001259
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001260 // Build a factor node to remember that this load is independent of
1261 // the other one.
1262 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1263 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001264
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001265 // Move the top bits to the right place.
1266 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1267 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001268
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001269 // Join the hi and lo parts.
1270 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001271 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001272 // Big endian - avoid unaligned loads.
1273 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1274 // Load the top RoundWidth bits.
1275 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), dl, Tmp1, Tmp2,
1276 LD->getPointerInfo(), RoundVT, isVolatile,
1277 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001278
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001279 // Load the remaining ExtraWidth bits.
1280 IncrementSize = RoundWidth / 8;
1281 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1282 DAG.getIntPtrConstant(IncrementSize));
1283 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
1284 Node->getValueType(0), dl, Tmp1, Tmp2,
1285 LD->getPointerInfo().getWithOffset(IncrementSize),
1286 ExtraVT, isVolatile, isNonTemporal,
1287 MinAlign(Alignment, IncrementSize));
1288
1289 // Build a factor node to remember that this load is independent of
1290 // the other one.
1291 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1292 Hi.getValue(1));
1293
1294 // Move the top bits to the right place.
1295 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1296 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1297
1298 // Join the hi and lo parts.
1299 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001300 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001301
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001302 Tmp1 = LegalizeOp(Result);
1303 Tmp2 = LegalizeOp(Ch);
1304 } else {
1305 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1306 default: assert(0 && "This action is not supported yet!");
1307 case TargetLowering::Custom:
1308 isCustom = true;
1309 // FALLTHROUGH
1310 case TargetLowering::Legal:
1311 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1312 Tmp1, Tmp2, LD->getOffset()),
1313 Result.getResNo());
1314 Tmp1 = Result.getValue(0);
1315 Tmp2 = Result.getValue(1);
1316
1317 if (isCustom) {
1318 Tmp3 = TLI.LowerOperation(Result, DAG);
1319 if (Tmp3.getNode()) {
1320 Tmp1 = LegalizeOp(Tmp3);
1321 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1322 }
1323 } else {
1324 // If this is an unaligned load and the target doesn't support it,
1325 // expand it.
1326 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1327 const Type *Ty =
1328 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1329 unsigned ABIAlignment =
1330 TLI.getTargetData()->getABITypeAlignment(Ty);
1331 if (LD->getAlignment() < ABIAlignment){
1332 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1333 DAG, TLI);
1334 Tmp1 = Result.getOperand(0);
1335 Tmp2 = Result.getOperand(1);
1336 Tmp1 = LegalizeOp(Tmp1);
1337 Tmp2 = LegalizeOp(Tmp2);
1338 }
1339 }
1340 }
1341 break;
1342 case TargetLowering::Expand:
1343 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1344 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1345 LD->getPointerInfo(),
1346 LD->isVolatile(), LD->isNonTemporal(),
1347 LD->getAlignment());
1348 unsigned ExtendOp;
1349 switch (ExtType) {
1350 case ISD::EXTLOAD:
1351 ExtendOp = (SrcVT.isFloatingPoint() ?
1352 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1353 break;
1354 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1355 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1356 default: llvm_unreachable("Unexpected extend load type!");
1357 }
1358 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1359 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1360 Tmp2 = LegalizeOp(Load.getValue(1));
1361 break;
1362 }
1363 // FIXME: This does not work for vectors on most targets. Sign- and
1364 // zero-extend operations are currently folded into extending loads,
1365 // whether they are legal or not, and then we end up here without any
1366 // support for legalizing them.
1367 assert(ExtType != ISD::EXTLOAD &&
1368 "EXTLOAD should always be supported!");
1369 // Turn the unsupported load into an EXTLOAD followed by an explicit
1370 // zero/sign extend inreg.
1371 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), dl,
1372 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1373 LD->isVolatile(), LD->isNonTemporal(),
1374 LD->getAlignment());
1375 SDValue ValRes;
1376 if (ExtType == ISD::SEXTLOAD)
1377 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1378 Result.getValueType(),
1379 Result, DAG.getValueType(SrcVT));
1380 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001381 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001382 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1383 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1384 break;
1385 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001386 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001387
1388 // Since loads produce two values, make sure to remember that we legalized
1389 // both of them.
1390 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1391 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1392 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001393 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001394 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001395 StoreSDNode *ST = cast<StoreSDNode>(Node);
1396 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1397 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001398 unsigned Alignment = ST->getAlignment();
1399 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001400 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001401
Evan Cheng8b2794a2006-10-13 21:14:26 +00001402 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001403 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1404 Result = SDValue(OptStore, 0);
1405 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001407
Eli Friedman957bffa2009-05-24 08:42:01 +00001408 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001409 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001410 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1411 Tmp1, Tmp3, Tmp2,
1412 ST->getOffset()),
1413 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001414
Owen Andersone50ed302009-08-10 22:56:29 +00001415 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001416 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001417 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001418 case TargetLowering::Legal:
1419 // If this is an unaligned store and the target doesn't support it,
1420 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001421 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001422 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001423 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001424 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001425 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1426 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001427 }
1428 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001429 case TargetLowering::Custom:
1430 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001431 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001432 break;
1433 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001434 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001436 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001437 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001438 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001439 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001440 break;
1441 }
1442 break;
1443 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001444 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001445 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001446
Owen Andersone50ed302009-08-10 22:56:29 +00001447 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001448 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001449
Duncan Sands83ec4b62008-06-06 12:08:01 +00001450 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001451 // Promote to a byte-sized store with upper bits zero if not
1452 // storing an integral number of bytes. For example, promote
1453 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001454 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1455 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001456 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001457 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1458 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001459 } else if (StWidth & (StWidth - 1)) {
1460 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001461 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001462 unsigned RoundWidth = 1 << Log2_32(StWidth);
1463 assert(RoundWidth < StWidth);
1464 unsigned ExtraWidth = StWidth - RoundWidth;
1465 assert(ExtraWidth < RoundWidth);
1466 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1467 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001468 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1469 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001470 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001471 unsigned IncrementSize;
1472
1473 if (TLI.isLittleEndian()) {
1474 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1475 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001476 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1477 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001478 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001479
1480 // Store the remaining ExtraWidth bits.
1481 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001482 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001483 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001484 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001485 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001486 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1487 ST->getPointerInfo().getWithOffset(IncrementSize),
1488 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001489 MinAlign(Alignment, IncrementSize));
1490 } else {
1491 // Big endian - avoid unaligned stores.
1492 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1493 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001494 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001495 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001496 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1497 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001498
1499 // Store the remaining ExtraWidth bits.
1500 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001501 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001502 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001503 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1504 ST->getPointerInfo().getWithOffset(IncrementSize),
1505 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001506 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001507 }
Duncan Sands7e857202008-01-22 07:17:34 +00001508
1509 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001510 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001511 } else {
1512 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1513 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001514 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1515 Tmp1, Tmp3, Tmp2,
1516 ST->getOffset()),
1517 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001518
1519 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001520 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001521 case TargetLowering::Legal:
1522 // If this is an unaligned store and the target doesn't support it,
1523 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001524 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001525 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001526 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001527 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001528 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1529 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001530 }
1531 break;
1532 case TargetLowering::Custom:
1533 Result = TLI.LowerOperation(Result, DAG);
1534 break;
1535 case Expand:
1536 // TRUNCSTORE:i16 i32 -> STORE i16
1537 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001538 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001539 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1540 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001541 break;
1542 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001543 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001544 }
1545 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001546 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001547 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001548 assert(Result.getValueType() == Op.getValueType() &&
1549 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Chris Lattner456a93a2006-01-28 07:39:30 +00001551 // Make sure that the generated code is itself legal.
1552 if (Result != Op)
1553 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001554
Chris Lattner45982da2005-05-12 16:53:42 +00001555 // Note that LegalizeOp may be reentered even from single-use nodes, which
1556 // means that we always must cache transformed nodes.
1557 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001558 return Result;
1559}
1560
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001561SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1562 SDValue Vec = Op.getOperand(0);
1563 SDValue Idx = Op.getOperand(1);
1564 DebugLoc dl = Op.getDebugLoc();
1565 // Store the value to a temporary stack slot, then LOAD the returned part.
1566 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001567 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1568 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001569
1570 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001571 unsigned EltSize =
1572 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001573 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1574 DAG.getConstant(EltSize, Idx.getValueType()));
1575
1576 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1577 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1578 else
1579 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1580
1581 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1582
Eli Friedmanc680ac92009-07-09 22:01:03 +00001583 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001584 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001585 false, false, 0);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001586 return DAG.getExtLoad(ISD::EXTLOAD, Op.getValueType(), dl, Ch, StackPtr,
1587 MachinePointerInfo(),
1588 Vec.getValueType().getVectorElementType(),
1589 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001590}
1591
Eli Friedman7ef3d172009-06-06 07:04:42 +00001592SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1593 // We can't handle this case efficiently. Allocate a sufficiently
1594 // aligned object on the stack, store each element into it, then load
1595 // the result as a vector.
1596 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001597 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001598 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001599 DebugLoc dl = Node->getDebugLoc();
1600 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001601 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001602 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001603
1604 // Emit a store of each element to the stack slot.
1605 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001606 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001607 // Store (in the right endianness) the elements to memory.
1608 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1609 // Ignore undef elements.
1610 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1611
1612 unsigned Offset = TypeByteSize*i;
1613
1614 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1615 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1616
Dan Gohman9949dd62010-02-25 20:30:49 +00001617 // If the destination vector element type is narrower than the source
1618 // element type, only store the bits necessary.
1619 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001620 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001621 Node->getOperand(i), Idx,
1622 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001623 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001624 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001625 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001626 Node->getOperand(i), Idx,
1627 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001628 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001629 }
1630
1631 SDValue StoreChain;
1632 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001634 &Stores[0], Stores.size());
1635 else
1636 StoreChain = DAG.getEntryNode();
1637
1638 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001639 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001640}
1641
Eli Friedman4bc8c712009-05-27 12:20:41 +00001642SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1643 DebugLoc dl = Node->getDebugLoc();
1644 SDValue Tmp1 = Node->getOperand(0);
1645 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001646
1647 // Get the sign bit of the RHS. First obtain a value that has the same
1648 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001649 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001650 EVT FloatVT = Tmp2.getValueType();
1651 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001652 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001653 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001654 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001655 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001656 // Store the float to memory, then load the sign part out as an integer.
1657 MVT LoadTy = TLI.getPointerTy();
1658 // First create a temporary that is aligned for both the load and store.
1659 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1660 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001661 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001662 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001663 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001664 if (TLI.isBigEndian()) {
1665 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1666 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001667 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1668 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001669 } else { // Little endian
1670 SDValue LoadPtr = StackPtr;
1671 // The float may be wider than the integer we are going to load. Advance
1672 // the pointer so that the loaded integer will contain the sign bit.
1673 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1674 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1675 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1676 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1677 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001678 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1679 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001680 // Move the sign bit to the top bit of the loaded integer.
1681 unsigned BitShift = LoadTy.getSizeInBits() -
1682 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1683 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1684 if (BitShift)
1685 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
1686 DAG.getConstant(BitShift,TLI.getShiftAmountTy()));
1687 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001688 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001689 // Now get the sign bit proper, by seeing whether the value is negative.
1690 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1691 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1692 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001693 // Get the absolute value of the result.
1694 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1695 // Select between the nabs and abs value based on the sign bit of
1696 // the input.
1697 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1698 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1699 AbsVal);
1700}
1701
Eli Friedman4bc8c712009-05-27 12:20:41 +00001702void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1703 SmallVectorImpl<SDValue> &Results) {
1704 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1705 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1706 " not tell us which reg is the stack pointer!");
1707 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001708 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001709 SDValue Tmp1 = SDValue(Node, 0);
1710 SDValue Tmp2 = SDValue(Node, 1);
1711 SDValue Tmp3 = Node->getOperand(2);
1712 SDValue Chain = Tmp1.getOperand(0);
1713
1714 // Chain the dynamic stack allocation so that it doesn't modify the stack
1715 // pointer when other instructions are using the stack.
1716 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1717
1718 SDValue Size = Tmp2.getOperand(1);
1719 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1720 Chain = SP.getValue(1);
1721 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001722 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001723 if (Align > StackAlign)
1724 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1725 DAG.getConstant(-(uint64_t)Align, VT));
1726 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1727 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1728
1729 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1730 DAG.getIntPtrConstant(0, true), SDValue());
1731
1732 Results.push_back(Tmp1);
1733 Results.push_back(Tmp2);
1734}
1735
Evan Cheng7f042682008-10-15 02:05:31 +00001736/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001737/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001738/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001739void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001740 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001741 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001742 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001743 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001744 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1745 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001746 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001747 case TargetLowering::Legal:
1748 // Nothing to do.
1749 break;
1750 case TargetLowering::Expand: {
1751 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1752 unsigned Opc = 0;
1753 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001754 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001755 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1756 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1757 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1758 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1759 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1760 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1761 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1762 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1763 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1764 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1765 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1766 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001767 // FIXME: Implement more expansions.
1768 }
1769
Dale Johannesenbb5da912009-02-02 20:41:04 +00001770 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1771 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1772 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001773 RHS = SDValue();
1774 CC = SDValue();
1775 break;
1776 }
1777 }
1778}
1779
Chris Lattner1401d152008-01-16 07:45:30 +00001780/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1781/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1782/// a load from the stack slot to DestVT, extending it if needed.
1783/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001784SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001785 EVT SlotVT,
1786 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001787 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001788 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001789 unsigned SrcAlign =
1790 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001791 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001792 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001793
Evan Chengff89dcb2009-10-18 18:16:27 +00001794 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1795 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001796 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001797
Duncan Sands83ec4b62008-06-06 12:08:01 +00001798 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1799 unsigned SlotSize = SlotVT.getSizeInBits();
1800 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001801 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1802 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001803
Chris Lattner1401d152008-01-16 07:45:30 +00001804 // Emit a store to the stack slot. Use a truncstore if the input value is
1805 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001806 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001807
Chris Lattner1401d152008-01-16 07:45:30 +00001808 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001809 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001810 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001811 else {
1812 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001813 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001814 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Chris Lattner35481892005-12-23 00:16:34 +00001817 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001818 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001819 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001820 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001821
Chris Lattner1401d152008-01-16 07:45:30 +00001822 assert(SlotSize < DestSize && "Unknown extension!");
Chris Lattnerecf42c42010-09-21 16:36:31 +00001823 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001824 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001825}
1826
Dan Gohman475871a2008-07-27 21:46:04 +00001827SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001828 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001829 // Create a vector sized/aligned stack slot, store the value to element #0,
1830 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001832
Evan Chengff89dcb2009-10-18 18:16:27 +00001833 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1834 int SPFI = StackPtrFI->getIndex();
1835
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001836 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1837 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001838 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001839 Node->getValueType(0).getVectorElementType(),
1840 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001841 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001842 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001843 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001844}
1845
1846
Chris Lattnerce872152006-03-19 06:31:19 +00001847/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001848/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001849SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001850 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001851 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001852 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001853 EVT VT = Node->getValueType(0);
1854 EVT OpVT = Node->getOperand(0).getValueType();
1855 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001856
1857 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001858 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001859 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001860 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001861 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001862 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001863 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001864 if (V.getOpcode() == ISD::UNDEF)
1865 continue;
1866 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001867 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001868 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001869 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001870
1871 if (!Value1.getNode()) {
1872 Value1 = V;
1873 } else if (!Value2.getNode()) {
1874 if (V != Value1)
1875 Value2 = V;
1876 } else if (V != Value1 && V != Value2) {
1877 MoreThanTwoValues = true;
1878 }
Chris Lattnerce872152006-03-19 06:31:19 +00001879 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001880
Eli Friedman7a5e5552009-06-07 06:52:44 +00001881 if (!Value1.getNode())
1882 return DAG.getUNDEF(VT);
1883
1884 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001885 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001886
Chris Lattner2eb86532006-03-24 07:29:17 +00001887 // If all elements are constants, create a load from the constant pool.
1888 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001889 std::vector<Constant*> CV;
1890 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001891 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001892 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001893 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001894 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001895 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001896 if (OpVT==EltVT)
1897 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1898 else {
1899 // If OpVT and EltVT don't match, EltVT is not legal and the
1900 // element values have been promoted/truncated earlier. Undo this;
1901 // we don't want a v16i8 to become a v16i32 for example.
1902 const ConstantInt *CI = V->getConstantIntValue();
1903 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1904 CI->getZExtValue()));
1905 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001906 } else {
1907 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001908 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001909 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001910 }
1911 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001912 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001914 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001915 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001916 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001917 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001918 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001919
Eli Friedman7a5e5552009-06-07 06:52:44 +00001920 if (!MoreThanTwoValues) {
1921 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1922 for (unsigned i = 0; i < NumElems; ++i) {
1923 SDValue V = Node->getOperand(i);
1924 if (V.getOpcode() == ISD::UNDEF)
1925 continue;
1926 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1927 }
1928 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001929 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001930 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1931 SDValue Vec2;
1932 if (Value2.getNode())
1933 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1934 else
1935 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001936
Chris Lattner87100e02006-03-20 01:52:29 +00001937 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001938 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001939 }
1940 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001941
Eli Friedman7ef3d172009-06-06 07:04:42 +00001942 // Otherwise, we can't handle this case efficiently.
1943 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001944}
1945
Chris Lattner77e77a62005-01-21 06:05:23 +00001946// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1947// does not fit into a register, return the lo part and set the hi part to the
1948// by-reg argument. If it does fit into a single register, return the result
1949// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001950SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001951 bool isSigned) {
Chris Lattner6831a812006-02-13 09:18:02 +00001952 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001953 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001954 // Legalizing the call will automatically add the previous call to the
1955 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001956 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Chris Lattner77e77a62005-01-21 06:05:23 +00001958 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001959 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001960 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001961 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00001962 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001963 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001964 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001965 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001966 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001967 }
Bill Wendling056292f2008-09-16 21:48:12 +00001968 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001969 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001970
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001971 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00001972 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001973
1974 // isTailCall may be true since the callee does not reference caller stack
1975 // frame. Check if it's in the right position.
1976 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001977 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00001978 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001979 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001981 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001982
Evan Cheng3d2125c2010-11-30 23:55:39 +00001983 if (!CallInfo.second.getNode())
1984 // It's a tailcall, return the chain (which is the DAG root).
1985 return DAG.getRoot();
1986
Chris Lattner6831a812006-02-13 09:18:02 +00001987 // Legalize the call sequence, starting with the chain. This will advance
1988 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
1989 // was added by LowerCallTo (guaranteeing proper serialization of calls).
1990 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00001991 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001992}
1993
Jim Grosbache03262f2010-06-18 21:43:38 +00001994// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1995// ExpandLibCall except that the first operand is the in-chain.
1996std::pair<SDValue, SDValue>
1997SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1998 SDNode *Node,
1999 bool isSigned) {
2000 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
2001 SDValue InChain = Node->getOperand(0);
2002
2003 TargetLowering::ArgListTy Args;
2004 TargetLowering::ArgListEntry Entry;
2005 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2006 EVT ArgVT = Node->getOperand(i).getValueType();
2007 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2008 Entry.Node = Node->getOperand(i);
2009 Entry.Ty = ArgTy;
2010 Entry.isSExt = isSigned;
2011 Entry.isZExt = !isSigned;
2012 Args.push_back(Entry);
2013 }
2014 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2015 TLI.getPointerTy());
2016
2017 // Splice the libcall in wherever FindInputOutputChains tells us to.
2018 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2019 std::pair<SDValue, SDValue> CallInfo =
2020 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002021 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002022 /*isReturnValueUsed=*/true,
2023 Callee, Args, DAG, Node->getDebugLoc());
2024
2025 // Legalize the call sequence, starting with the chain. This will advance
2026 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2027 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2028 LegalizeOp(CallInfo.second);
2029 return CallInfo;
2030}
2031
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002032SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2033 RTLIB::Libcall Call_F32,
2034 RTLIB::Libcall Call_F64,
2035 RTLIB::Libcall Call_F80,
2036 RTLIB::Libcall Call_PPCF128) {
2037 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002039 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 case MVT::f32: LC = Call_F32; break;
2041 case MVT::f64: LC = Call_F64; break;
2042 case MVT::f80: LC = Call_F80; break;
2043 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002044 }
2045 return ExpandLibCall(LC, Node, false);
2046}
2047
2048SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002049 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002050 RTLIB::Libcall Call_I16,
2051 RTLIB::Libcall Call_I32,
2052 RTLIB::Libcall Call_I64,
2053 RTLIB::Libcall Call_I128) {
2054 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002055 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002056 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002057 case MVT::i8: LC = Call_I8; break;
2058 case MVT::i16: LC = Call_I16; break;
2059 case MVT::i32: LC = Call_I32; break;
2060 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002062 }
2063 return ExpandLibCall(LC, Node, isSigned);
2064}
2065
Chris Lattner22cde6a2006-01-28 08:25:58 +00002066/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2067/// INT_TO_FP operation of the specified operand when the target requests that
2068/// we expand it. At this point, we know that the result and operand types are
2069/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002070SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2071 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002072 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002073 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002075 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Chris Lattner23594d42008-01-16 07:03:22 +00002077 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002078 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002079
Chris Lattner22cde6a2006-01-28 08:25:58 +00002080 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002081 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002082 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002084 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002085 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002086 if (TLI.isLittleEndian())
2087 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Chris Lattner22cde6a2006-01-28 08:25:58 +00002089 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002090 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002091 if (isSigned) {
2092 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2094 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002095 } else {
2096 Op0Mapped = Op0;
2097 }
2098 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002099 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002100 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002101 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002102 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002104 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002105 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2106 MachinePointerInfo(),
2107 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002108 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002109 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2110 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002111 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002112 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002113 BitsToDouble(0x4330000080000000ULL) :
2114 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002115 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002116 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002118 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002120 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002121 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002122 // do nothing
2123 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002125 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002126 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002127 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002128 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002129 }
2130 return Result;
2131 }
2132 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002133 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002134
2135 // Implementation of unsigned i64 to f64 following the algorithm in
2136 // __floatundidf in compiler_rt. This implementation has the advantage
2137 // of performing rounding correctly, both in the default rounding mode
2138 // and in all alternate rounding modes.
2139 // TODO: Generalize this for use with other types.
2140 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2141 SDValue TwoP52 =
2142 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2143 SDValue TwoP84PlusTwoP52 =
2144 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2145 SDValue TwoP84 =
2146 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2147
2148 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2149 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2150 DAG.getConstant(32, MVT::i64));
2151 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2152 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002153 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2154 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002155 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2156 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002157 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2158 }
2159
Owen Anderson3a9e7692010-10-05 17:24:05 +00002160 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002161 // TODO: Generalize this for use with other types.
2162 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002163 // For unsigned conversions, convert them to signed conversions using the
2164 // algorithm from the x86_64 __floatundidf in compiler_rt.
2165 if (!isSigned) {
2166 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002167
Owen Anderson3a9e7692010-10-05 17:24:05 +00002168 SDValue ShiftConst = DAG.getConstant(1, TLI.getShiftAmountTy());
2169 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2170 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2171 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2172 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002173
Owen Anderson3a9e7692010-10-05 17:24:05 +00002174 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2175 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002176
Owen Anderson3a9e7692010-10-05 17:24:05 +00002177 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002178 // select. We happen to get lucky and machinesink does the right
2179 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002180 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002181 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002182 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2183 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2184 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002185
Owen Anderson3a9e7692010-10-05 17:24:05 +00002186 // Otherwise, implement the fully general conversion.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002187 EVT SHVT = TLI.getShiftAmountTy();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188
Jim Grosbach6e992612010-07-02 17:41:59 +00002189 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002190 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2191 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2192 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002193 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002194 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2195 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2196 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2197 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2198 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2199 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002200 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002201 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002202
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002203 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2204 DAG.getConstant(32, SHVT));
2205 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2206 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2207 SDValue TwoP32 =
2208 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2209 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2210 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2211 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2212 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2213 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2214 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002215 }
2216
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002217 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002218
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002219 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2220 Op0, DAG.getConstant(0, Op0.getValueType()),
2221 ISD::SETLT);
2222 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2223 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2224 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002225
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002226 // If the sign bit of the integer is set, the large number will be treated
2227 // as a negative number. To counteract this, the dynamic code adds an
2228 // offset depending on the data type.
2229 uint64_t FF;
2230 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002231 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002232 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2233 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2234 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2235 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2236 }
2237 if (TLI.isLittleEndian()) FF <<= 32;
2238 Constant *FudgeFactor = ConstantInt::get(
2239 Type::getInt64Ty(*DAG.getContext()), FF);
2240
2241 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2242 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2243 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2244 Alignment = std::min(Alignment, 4u);
2245 SDValue FudgeInReg;
2246 if (DestVT == MVT::f32)
2247 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002248 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002249 false, false, Alignment);
2250 else {
2251 FudgeInReg =
Evan Chengbcc80172010-07-07 22:15:37 +00002252 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002253 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002254 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002255 MVT::f32, false, false, Alignment));
2256 }
2257
2258 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002259}
2260
2261/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2262/// *INT_TO_FP operation of the specified operand when the target requests that
2263/// we promote it. At this point, we know that the result and operand types are
2264/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2265/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002266SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002267 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002268 bool isSigned,
2269 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002270 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002271 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002272
2273 unsigned OpToUse = 0;
2274
2275 // Scan for the appropriate larger type to use.
2276 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002278 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002279
2280 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002281 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2282 OpToUse = ISD::SINT_TO_FP;
2283 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002284 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002285 if (isSigned) continue;
2286
2287 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002288 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2289 OpToUse = ISD::UINT_TO_FP;
2290 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002291 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002292
2293 // Otherwise, try a larger type.
2294 }
2295
2296 // Okay, we found the operation and type to use. Zero extend our input to the
2297 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002298 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002299 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002300 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002301}
2302
2303/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2304/// FP_TO_*INT operation of the specified operand when the target requests that
2305/// we promote it. At this point, we know that the result and operand types are
2306/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2307/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002308SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002309 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002310 bool isSigned,
2311 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002312 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002313 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002314
2315 unsigned OpToUse = 0;
2316
2317 // Scan for the appropriate larger type to use.
2318 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002319 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002320 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002321
Eli Friedman3be2e512009-05-28 03:06:16 +00002322 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002323 OpToUse = ISD::FP_TO_SINT;
2324 break;
2325 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002326
Eli Friedman3be2e512009-05-28 03:06:16 +00002327 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002328 OpToUse = ISD::FP_TO_UINT;
2329 break;
2330 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002331
2332 // Otherwise, try a larger type.
2333 }
2334
Scott Michelfdc40a02009-02-17 22:15:04 +00002335
Chris Lattner27a6c732007-11-24 07:07:01 +00002336 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002337 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002338
Chris Lattner27a6c732007-11-24 07:07:01 +00002339 // Truncate the result of the extended FP_TO_*INT operation to the desired
2340 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002341 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002342}
2343
2344/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2345///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002346SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002347 EVT VT = Op.getValueType();
2348 EVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002349 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002350 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002351 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002352 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002353 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2354 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2355 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002356 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002357 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2358 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2359 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2360 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2361 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2362 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2363 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2364 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2365 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002366 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002367 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2368 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2369 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2370 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2371 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2372 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2373 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2374 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2375 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2376 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2377 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2378 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2379 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2380 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2381 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2382 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2383 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2384 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2385 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2386 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2387 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002388 }
2389}
2390
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002391/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002392// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002393static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2394 APInt Val = APInt(NumBits, ByteVal);
2395 unsigned Shift = 8;
2396 for (unsigned i = NumBits; i > 8; i >>= 1) {
2397 Val = (Val << Shift) | Val;
2398 Shift <<= 1;
2399 }
2400 return Val;
2401}
2402
Chris Lattner22cde6a2006-01-28 08:25:58 +00002403/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2404///
Scott Michelfdc40a02009-02-17 22:15:04 +00002405SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002406 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002407 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002408 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002409 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002410 EVT VT = Op.getValueType();
2411 EVT ShVT = TLI.getShiftAmountTy();
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002412 unsigned Len = VT.getSizeInBits();
2413
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002414 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2415 "CTPOP not implemented for this type.");
2416
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002417 // This is the "best" algorithm from
2418 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2419
2420 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2421 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2422 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2423 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2424
2425 // v = v - ((v >> 1) & 0x55555555...)
2426 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2427 DAG.getNode(ISD::AND, dl, VT,
2428 DAG.getNode(ISD::SRL, dl, VT, Op,
2429 DAG.getConstant(1, ShVT)),
2430 Mask55));
2431 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2432 Op = DAG.getNode(ISD::ADD, dl, VT,
2433 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2434 DAG.getNode(ISD::AND, dl, VT,
2435 DAG.getNode(ISD::SRL, dl, VT, Op,
2436 DAG.getConstant(2, ShVT)),
2437 Mask33));
2438 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2439 Op = DAG.getNode(ISD::AND, dl, VT,
2440 DAG.getNode(ISD::ADD, dl, VT, Op,
2441 DAG.getNode(ISD::SRL, dl, VT, Op,
2442 DAG.getConstant(4, ShVT))),
2443 Mask0F);
2444 // v = (v * 0x01010101...) >> (Len - 8)
2445 Op = DAG.getNode(ISD::SRL, dl, VT,
2446 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2447 DAG.getConstant(Len - 8, ShVT));
2448
Chris Lattner22cde6a2006-01-28 08:25:58 +00002449 return Op;
2450 }
2451 case ISD::CTLZ: {
2452 // for now, we do this:
2453 // x = x | (x >> 1);
2454 // x = x | (x >> 2);
2455 // ...
2456 // x = x | (x >>16);
2457 // x = x | (x >>32); // for 64-bit input
2458 // return popcount(~x);
2459 //
2460 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002461 EVT VT = Op.getValueType();
2462 EVT ShVT = TLI.getShiftAmountTy();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002463 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002464 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002465 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002466 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002467 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002468 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002469 Op = DAG.getNOT(dl, Op, VT);
2470 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002471 }
2472 case ISD::CTTZ: {
2473 // for now, we use: { return popcount(~x & (x - 1)); }
2474 // unless the target has ctlz but not ctpop, in which case we use:
2475 // { return 32 - nlz(~x & (x-1)); }
2476 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002477 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002478 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2479 DAG.getNOT(dl, Op, VT),
2480 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002481 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002482 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002483 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2484 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002485 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002486 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002487 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2488 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002489 }
2490 }
2491}
Chris Lattnere34b3962005-01-19 04:19:40 +00002492
Jim Grosbache03262f2010-06-18 21:43:38 +00002493std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2494 unsigned Opc = Node->getOpcode();
2495 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2496 RTLIB::Libcall LC;
2497
2498 switch (Opc) {
2499 default:
2500 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2501 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002502 case ISD::ATOMIC_SWAP:
2503 switch (VT.SimpleTy) {
2504 default: llvm_unreachable("Unexpected value type for atomic!");
2505 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2506 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2507 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2508 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2509 }
2510 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002511 case ISD::ATOMIC_CMP_SWAP:
2512 switch (VT.SimpleTy) {
2513 default: llvm_unreachable("Unexpected value type for atomic!");
2514 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2515 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2516 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2517 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2518 }
2519 break;
2520 case ISD::ATOMIC_LOAD_ADD:
2521 switch (VT.SimpleTy) {
2522 default: llvm_unreachable("Unexpected value type for atomic!");
2523 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2524 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2525 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2526 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2527 }
2528 break;
2529 case ISD::ATOMIC_LOAD_SUB:
2530 switch (VT.SimpleTy) {
2531 default: llvm_unreachable("Unexpected value type for atomic!");
2532 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2533 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2534 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2535 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2536 }
2537 break;
2538 case ISD::ATOMIC_LOAD_AND:
2539 switch (VT.SimpleTy) {
2540 default: llvm_unreachable("Unexpected value type for atomic!");
2541 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2542 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2543 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2544 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2545 }
2546 break;
2547 case ISD::ATOMIC_LOAD_OR:
2548 switch (VT.SimpleTy) {
2549 default: llvm_unreachable("Unexpected value type for atomic!");
2550 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2551 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2552 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2553 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2554 }
2555 break;
2556 case ISD::ATOMIC_LOAD_XOR:
2557 switch (VT.SimpleTy) {
2558 default: llvm_unreachable("Unexpected value type for atomic!");
2559 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2560 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2561 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2562 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2563 }
2564 break;
2565 case ISD::ATOMIC_LOAD_NAND:
2566 switch (VT.SimpleTy) {
2567 default: llvm_unreachable("Unexpected value type for atomic!");
2568 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2569 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2570 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2571 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2572 }
2573 break;
2574 }
2575
2576 return ExpandChainLibCall(LC, Node, false);
2577}
2578
Eli Friedman8c377c72009-05-27 01:25:56 +00002579void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2580 SmallVectorImpl<SDValue> &Results) {
2581 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002582 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002583 switch (Node->getOpcode()) {
2584 case ISD::CTPOP:
2585 case ISD::CTLZ:
2586 case ISD::CTTZ:
2587 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2588 Results.push_back(Tmp1);
2589 break;
2590 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002591 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002592 break;
2593 case ISD::FRAMEADDR:
2594 case ISD::RETURNADDR:
2595 case ISD::FRAME_TO_ARGS_OFFSET:
2596 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2597 break;
2598 case ISD::FLT_ROUNDS_:
2599 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2600 break;
2601 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002602 case ISD::EH_LABEL:
2603 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002604 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002605 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002606 case ISD::EH_SJLJ_DISPATCHSETUP:
2607 // If the target didn't expand these, there's nothing to do, so just
2608 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002609 Results.push_back(Node->getOperand(0));
2610 break;
2611 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002612 // If the target didn't expand this, just return 'zero' and preserve the
2613 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002614 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002615 Results.push_back(Node->getOperand(0));
2616 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002617 case ISD::MEMBARRIER: {
2618 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2619 TargetLowering::ArgListTy Args;
2620 std::pair<SDValue, SDValue> CallResult =
2621 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002622 false, false, false, false, 0, CallingConv::C,
2623 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002624 /*isReturnValueUsed=*/true,
2625 DAG.getExternalSymbol("__sync_synchronize",
2626 TLI.getPointerTy()),
2627 Args, DAG, dl);
2628 Results.push_back(CallResult.second);
2629 break;
2630 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002631 // By default, atomic intrinsics are marked Legal and lowered. Targets
2632 // which don't support them directly, however, may want libcalls, in which
2633 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002634 case ISD::ATOMIC_SWAP:
2635 case ISD::ATOMIC_LOAD_ADD:
2636 case ISD::ATOMIC_LOAD_SUB:
2637 case ISD::ATOMIC_LOAD_AND:
2638 case ISD::ATOMIC_LOAD_OR:
2639 case ISD::ATOMIC_LOAD_XOR:
2640 case ISD::ATOMIC_LOAD_NAND:
2641 case ISD::ATOMIC_LOAD_MIN:
2642 case ISD::ATOMIC_LOAD_MAX:
2643 case ISD::ATOMIC_LOAD_UMIN:
2644 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002645 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002646 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2647 Results.push_back(Tmp.first);
2648 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002649 break;
Evan Chenga8457062010-06-18 22:01:37 +00002650 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002651 case ISD::DYNAMIC_STACKALLOC:
2652 ExpandDYNAMIC_STACKALLOC(Node, Results);
2653 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002654 case ISD::MERGE_VALUES:
2655 for (unsigned i = 0; i < Node->getNumValues(); i++)
2656 Results.push_back(Node->getOperand(i));
2657 break;
2658 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002659 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002660 if (VT.isInteger())
2661 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002662 else {
2663 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002664 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002665 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002666 break;
2667 }
2668 case ISD::TRAP: {
2669 // If this operation is not supported, lower it to 'abort()' call
2670 TargetLowering::ArgListTy Args;
2671 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002672 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002673 false, false, false, false, 0, CallingConv::C,
2674 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002675 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002676 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002677 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002678 Results.push_back(CallResult.second);
2679 break;
2680 }
2681 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002682 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002683 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2684 Node->getValueType(0), dl);
2685 Results.push_back(Tmp1);
2686 break;
2687 case ISD::FP_EXTEND:
2688 Tmp1 = EmitStackConvert(Node->getOperand(0),
2689 Node->getOperand(0).getValueType(),
2690 Node->getValueType(0), dl);
2691 Results.push_back(Tmp1);
2692 break;
2693 case ISD::SIGN_EXTEND_INREG: {
2694 // NOTE: we could fall back on load/store here too for targets without
2695 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002696 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002697 EVT VT = Node->getValueType(0);
2698 EVT ShiftAmountTy = TLI.getShiftAmountTy();
Dan Gohmand1996362010-01-09 02:13:55 +00002699 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002700 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002701 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2702 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002703 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002704 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2705 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002706 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2707 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002708 break;
2709 }
2710 case ISD::FP_ROUND_INREG: {
2711 // The only way we can lower this is to turn it into a TRUNCSTORE,
2712 // EXTLOAD pair, targetting a temporary location (a stack slot).
2713
2714 // NOTE: there is a choice here between constantly creating new stack
2715 // slots and always reusing the same one. We currently always create
2716 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002717 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002718 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2719 Node->getValueType(0), dl);
2720 Results.push_back(Tmp1);
2721 break;
2722 }
2723 case ISD::SINT_TO_FP:
2724 case ISD::UINT_TO_FP:
2725 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2726 Node->getOperand(0), Node->getValueType(0), dl);
2727 Results.push_back(Tmp1);
2728 break;
2729 case ISD::FP_TO_UINT: {
2730 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002731 EVT VT = Node->getOperand(0).getValueType();
2732 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002733 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002734 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2735 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2736 Tmp1 = DAG.getConstantFP(apf, VT);
2737 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2738 Node->getOperand(0),
2739 Tmp1, ISD::SETLT);
2740 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002741 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2742 DAG.getNode(ISD::FSUB, dl, VT,
2743 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002744 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2745 DAG.getConstant(x, NVT));
2746 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2747 Results.push_back(Tmp1);
2748 break;
2749 }
Eli Friedman509150f2009-05-27 07:58:35 +00002750 case ISD::VAARG: {
2751 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002752 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002753 Tmp1 = Node->getOperand(0);
2754 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002755 unsigned Align = Node->getConstantOperandVal(3);
2756
Chris Lattnerecf42c42010-09-21 16:36:31 +00002757 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2758 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002759 SDValue VAList = VAListLoad;
2760
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002761 if (Align > TLI.getMinStackArgumentAlignment()) {
2762 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2763
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002764 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2765 DAG.getConstant(Align - 1,
2766 TLI.getPointerTy()));
2767
2768 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002769 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002770 TLI.getPointerTy()));
2771 }
2772
Eli Friedman509150f2009-05-27 07:58:35 +00002773 // Increment the pointer, VAList, to the next vaarg
2774 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2775 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002776 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002777 TLI.getPointerTy()));
2778 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002779 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2780 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002781 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002782 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002783 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002784 Results.push_back(Results[0].getValue(1));
2785 break;
2786 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002787 case ISD::VACOPY: {
2788 // This defaults to loading a pointer from the input and storing it to the
2789 // output, returning the chain.
2790 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2791 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2792 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002793 Node->getOperand(2), MachinePointerInfo(VS),
2794 false, false, 0);
2795 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2796 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002797 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002798 break;
2799 }
2800 case ISD::EXTRACT_VECTOR_ELT:
2801 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2802 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002804 Node->getOperand(0));
2805 else
2806 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2807 Results.push_back(Tmp1);
2808 break;
2809 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002810 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002811 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002812 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002813 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002814 break;
2815 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002816 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002817 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002818 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002819 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002820 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2821 Node->getOperand(1),
2822 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002823 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002824 case ISD::VECTOR_SHUFFLE: {
2825 SmallVector<int, 8> Mask;
2826 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2827
Owen Andersone50ed302009-08-10 22:56:29 +00002828 EVT VT = Node->getValueType(0);
2829 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00002830 if (getTypeAction(EltVT) == Promote)
2831 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002832 unsigned NumElems = VT.getVectorNumElements();
2833 SmallVector<SDValue, 8> Ops;
2834 for (unsigned i = 0; i != NumElems; ++i) {
2835 if (Mask[i] < 0) {
2836 Ops.push_back(DAG.getUNDEF(EltVT));
2837 continue;
2838 }
2839 unsigned Idx = Mask[i];
2840 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002841 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2842 Node->getOperand(0),
2843 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002844 else
Bill Wendling775db972009-12-23 00:28:23 +00002845 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2846 Node->getOperand(1),
2847 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002848 }
2849 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2850 Results.push_back(Tmp1);
2851 break;
2852 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002853 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002854 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002855 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2856 // 1 -> Hi
2857 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2858 DAG.getConstant(OpTy.getSizeInBits()/2,
2859 TLI.getShiftAmountTy()));
2860 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2861 } else {
2862 // 0 -> Lo
2863 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2864 Node->getOperand(0));
2865 }
2866 Results.push_back(Tmp1);
2867 break;
2868 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002869 case ISD::STACKSAVE:
2870 // Expand to CopyFromReg if the target set
2871 // StackPointerRegisterToSaveRestore.
2872 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002873 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2874 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002875 Results.push_back(Results[0].getValue(1));
2876 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002877 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002878 Results.push_back(Node->getOperand(0));
2879 }
2880 break;
2881 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002882 // Expand to CopyToReg if the target set
2883 // StackPointerRegisterToSaveRestore.
2884 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2885 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2886 Node->getOperand(1)));
2887 } else {
2888 Results.push_back(Node->getOperand(0));
2889 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002890 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002891 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002892 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002893 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002894 case ISD::FNEG:
2895 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2896 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2897 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2898 Node->getOperand(0));
2899 Results.push_back(Tmp1);
2900 break;
2901 case ISD::FABS: {
2902 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002903 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002904 Tmp1 = Node->getOperand(0);
2905 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002906 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002907 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002908 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2909 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002910 Results.push_back(Tmp1);
2911 break;
2912 }
2913 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002914 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2915 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002916 break;
2917 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002918 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2919 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002920 break;
2921 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002922 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2923 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002924 break;
2925 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002926 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2927 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002928 break;
2929 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002930 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2931 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002932 break;
2933 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002934 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2935 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002936 break;
2937 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002938 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2939 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002940 break;
2941 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002942 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2943 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002944 break;
2945 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002946 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2947 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002948 break;
2949 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002950 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2951 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002952 break;
2953 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002954 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2955 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002956 break;
2957 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00002958 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2959 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002960 break;
2961 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00002962 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2963 RTLIB::NEARBYINT_F64,
2964 RTLIB::NEARBYINT_F80,
2965 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002966 break;
2967 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00002968 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2969 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002970 break;
2971 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00002972 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2973 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002974 break;
2975 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00002976 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2977 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002978 break;
2979 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00002980 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2981 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002982 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00002983 case ISD::FP16_TO_FP32:
2984 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2985 break;
2986 case ISD::FP32_TO_FP16:
2987 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2988 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002989 case ISD::ConstantFP: {
2990 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00002991 // Check to see if this FP immediate is already legal.
2992 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002993 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00002994 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002995 else
Bill Wendling775db972009-12-23 00:28:23 +00002996 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002997 break;
2998 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00002999 case ISD::EHSELECTION: {
3000 unsigned Reg = TLI.getExceptionSelectorRegister();
3001 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003002 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3003 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003004 Results.push_back(Results[0].getValue(1));
3005 break;
3006 }
3007 case ISD::EXCEPTIONADDR: {
3008 unsigned Reg = TLI.getExceptionAddressRegister();
3009 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003010 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3011 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003012 Results.push_back(Results[0].getValue(1));
3013 break;
3014 }
3015 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003016 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003017 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3018 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3019 "Don't know how to expand this subtraction!");
3020 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3021 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003022 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3023 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003024 break;
3025 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003026 case ISD::UREM:
3027 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003028 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003029 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003030 bool isSigned = Node->getOpcode() == ISD::SREM;
3031 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3032 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3033 Tmp2 = Node->getOperand(0);
3034 Tmp3 = Node->getOperand(1);
Eli Friedman3be2e512009-05-28 03:06:16 +00003035 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3036 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3037 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003038 // X % Y -> X-X/Y*Y
3039 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3040 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3041 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003042 } else if (isSigned) {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003043 Tmp1 = ExpandIntLibCall(Node, true,
3044 RTLIB::SREM_I8,
3045 RTLIB::SREM_I16, RTLIB::SREM_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003046 RTLIB::SREM_I64, RTLIB::SREM_I128);
3047 } else {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003048 Tmp1 = ExpandIntLibCall(Node, false,
3049 RTLIB::UREM_I8,
3050 RTLIB::UREM_I16, RTLIB::UREM_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003051 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003052 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003053 Results.push_back(Tmp1);
3054 break;
3055 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003056 case ISD::UDIV:
3057 case ISD::SDIV: {
3058 bool isSigned = Node->getOpcode() == ISD::SDIV;
3059 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003060 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003061 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003062 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3063 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3064 Node->getOperand(1));
3065 else if (isSigned)
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003066 Tmp1 = ExpandIntLibCall(Node, true,
3067 RTLIB::SDIV_I8,
3068 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003069 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003070 else
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003071 Tmp1 = ExpandIntLibCall(Node, false,
3072 RTLIB::UDIV_I8,
3073 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003074 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003075 Results.push_back(Tmp1);
3076 break;
3077 }
3078 case ISD::MULHU:
3079 case ISD::MULHS: {
3080 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3081 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003082 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003083 SDVTList VTs = DAG.getVTList(VT, VT);
3084 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3085 "If this wasn't legal, it shouldn't have been created!");
3086 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3087 Node->getOperand(1));
3088 Results.push_back(Tmp1.getValue(1));
3089 break;
3090 }
3091 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003092 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003093 SDVTList VTs = DAG.getVTList(VT, VT);
3094 // See if multiply or divide can be lowered using two-result operations.
3095 // We just need the low half of the multiply; try both the signed
3096 // and unsigned forms. If the target supports both SMUL_LOHI and
3097 // UMUL_LOHI, form a preference by checking which forms of plain
3098 // MULH it supports.
3099 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3100 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3101 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3102 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3103 unsigned OpToUse = 0;
3104 if (HasSMUL_LOHI && !HasMULHS) {
3105 OpToUse = ISD::SMUL_LOHI;
3106 } else if (HasUMUL_LOHI && !HasMULHU) {
3107 OpToUse = ISD::UMUL_LOHI;
3108 } else if (HasSMUL_LOHI) {
3109 OpToUse = ISD::SMUL_LOHI;
3110 } else if (HasUMUL_LOHI) {
3111 OpToUse = ISD::UMUL_LOHI;
3112 }
3113 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003114 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3115 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003116 break;
3117 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003118 Tmp1 = ExpandIntLibCall(Node, false,
3119 RTLIB::MUL_I8,
3120 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003121 RTLIB::MUL_I64, RTLIB::MUL_I128);
3122 Results.push_back(Tmp1);
3123 break;
3124 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003125 case ISD::SADDO:
3126 case ISD::SSUBO: {
3127 SDValue LHS = Node->getOperand(0);
3128 SDValue RHS = Node->getOperand(1);
3129 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3130 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3131 LHS, RHS);
3132 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003133 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003134
Eli Friedman4bc8c712009-05-27 12:20:41 +00003135 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3136
3137 // LHSSign -> LHS >= 0
3138 // RHSSign -> RHS >= 0
3139 // SumSign -> Sum >= 0
3140 //
3141 // Add:
3142 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3143 // Sub:
3144 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3145 //
3146 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3147 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3148 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3149 Node->getOpcode() == ISD::SADDO ?
3150 ISD::SETEQ : ISD::SETNE);
3151
3152 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3153 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3154
3155 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3156 Results.push_back(Cmp);
3157 break;
3158 }
3159 case ISD::UADDO:
3160 case ISD::USUBO: {
3161 SDValue LHS = Node->getOperand(0);
3162 SDValue RHS = Node->getOperand(1);
3163 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3164 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3165 LHS, RHS);
3166 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003167 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3168 Node->getOpcode () == ISD::UADDO ?
3169 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003170 break;
3171 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003172 case ISD::UMULO:
3173 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003174 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003175 SDValue LHS = Node->getOperand(0);
3176 SDValue RHS = Node->getOperand(1);
3177 SDValue BottomHalf;
3178 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003179 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003180 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3181 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3182 bool isSigned = Node->getOpcode() == ISD::SMULO;
3183 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3184 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3185 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3186 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3187 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3188 RHS);
3189 TopHalf = BottomHalf.getValue(1);
Chris Lattner35a38932010-04-07 23:47:51 +00003190 } else {
3191 // FIXME: We should be able to fall back to a libcall with an illegal
3192 // type in some cases.
3193 // Also, we can fall back to a division in some cases, but that's a big
3194 // performance hit in the general case.
3195 assert(TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3196 VT.getSizeInBits() * 2)) &&
3197 "Don't know how to expand this operation yet!");
Owen Anderson23b9b192009-08-12 00:36:31 +00003198 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003199 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3200 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3201 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3202 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3203 DAG.getIntPtrConstant(0));
3204 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3205 DAG.getIntPtrConstant(1));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003206 }
3207 if (isSigned) {
3208 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, TLI.getShiftAmountTy());
3209 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3210 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3211 ISD::SETNE);
3212 } else {
3213 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3214 DAG.getConstant(0, VT), ISD::SETNE);
3215 }
3216 Results.push_back(BottomHalf);
3217 Results.push_back(TopHalf);
3218 break;
3219 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003220 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003221 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003222 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3223 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003224 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003225 DAG.getConstant(PairTy.getSizeInBits()/2,
3226 TLI.getShiftAmountTy()));
Bill Wendling775db972009-12-23 00:28:23 +00003227 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003228 break;
3229 }
Eli Friedman509150f2009-05-27 07:58:35 +00003230 case ISD::SELECT:
3231 Tmp1 = Node->getOperand(0);
3232 Tmp2 = Node->getOperand(1);
3233 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003234 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003235 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3236 Tmp2, Tmp3,
3237 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003238 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003239 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3240 DAG.getConstant(0, Tmp1.getValueType()),
3241 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003242 }
Eli Friedman509150f2009-05-27 07:58:35 +00003243 Results.push_back(Tmp1);
3244 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003245 case ISD::BR_JT: {
3246 SDValue Chain = Node->getOperand(0);
3247 SDValue Table = Node->getOperand(1);
3248 SDValue Index = Node->getOperand(2);
3249
Owen Andersone50ed302009-08-10 22:56:29 +00003250 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003251
3252 const TargetData &TD = *TLI.getTargetData();
3253 unsigned EntrySize =
3254 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003255
Chris Lattner071c62f2010-01-25 23:26:13 +00003256 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003257 Index, DAG.getConstant(EntrySize, PTy));
3258 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3259
Owen Anderson23b9b192009-08-12 00:36:31 +00003260 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Evan Chengbcc80172010-07-07 22:15:37 +00003261 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, dl, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003262 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003263 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003264 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003265 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003266 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003267 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003268 // RelocBase can be JumpTable, GOT or some sort of global base.
3269 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3270 TLI.getPICJumpTableRelocBase(Table, DAG));
3271 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003272 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003273 Results.push_back(Tmp1);
3274 break;
3275 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003276 case ISD::BRCOND:
3277 // Expand brcond's setcc into its constituent parts and create a BR_CC
3278 // Node.
3279 Tmp1 = Node->getOperand(0);
3280 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003281 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003282 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003283 Tmp1, Tmp2.getOperand(2),
3284 Tmp2.getOperand(0), Tmp2.getOperand(1),
3285 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003286 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003288 DAG.getCondCode(ISD::SETNE), Tmp2,
3289 DAG.getConstant(0, Tmp2.getValueType()),
3290 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003291 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003292 Results.push_back(Tmp1);
3293 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003294 case ISD::SETCC: {
3295 Tmp1 = Node->getOperand(0);
3296 Tmp2 = Node->getOperand(1);
3297 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003298 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003299
3300 // If we expanded the SETCC into an AND/OR, return the new node
3301 if (Tmp2.getNode() == 0) {
3302 Results.push_back(Tmp1);
3303 break;
3304 }
3305
3306 // Otherwise, SETCC for the given comparison type must be completely
3307 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003308 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003309 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3310 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3311 Results.push_back(Tmp1);
3312 break;
3313 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003314 case ISD::SELECT_CC: {
3315 Tmp1 = Node->getOperand(0); // LHS
3316 Tmp2 = Node->getOperand(1); // RHS
3317 Tmp3 = Node->getOperand(2); // True
3318 Tmp4 = Node->getOperand(3); // False
3319 SDValue CC = Node->getOperand(4);
3320
3321 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003322 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003323
3324 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3325 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3326 CC = DAG.getCondCode(ISD::SETNE);
3327 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3328 Tmp3, Tmp4, CC);
3329 Results.push_back(Tmp1);
3330 break;
3331 }
3332 case ISD::BR_CC: {
3333 Tmp1 = Node->getOperand(0); // Chain
3334 Tmp2 = Node->getOperand(2); // LHS
3335 Tmp3 = Node->getOperand(3); // RHS
3336 Tmp4 = Node->getOperand(1); // CC
3337
3338 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003339 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003340 LastCALLSEQ_END = DAG.getEntryNode();
3341
3342 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3343 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3344 Tmp4 = DAG.getCondCode(ISD::SETNE);
3345 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3346 Tmp3, Node->getOperand(4));
3347 Results.push_back(Tmp1);
3348 break;
3349 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003350 case ISD::GLOBAL_OFFSET_TABLE:
3351 case ISD::GlobalAddress:
3352 case ISD::GlobalTLSAddress:
3353 case ISD::ExternalSymbol:
3354 case ISD::ConstantPool:
3355 case ISD::JumpTable:
3356 case ISD::INTRINSIC_W_CHAIN:
3357 case ISD::INTRINSIC_WO_CHAIN:
3358 case ISD::INTRINSIC_VOID:
3359 // FIXME: Custom lowering for these operations shouldn't return null!
3360 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3361 Results.push_back(SDValue(Node, i));
3362 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003363 }
3364}
3365void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3366 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003367 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003368 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003369 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003370 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003371 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003372 }
Owen Andersone50ed302009-08-10 22:56:29 +00003373 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003374 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003375 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003376 switch (Node->getOpcode()) {
3377 case ISD::CTTZ:
3378 case ISD::CTLZ:
3379 case ISD::CTPOP:
3380 // Zero extend the argument.
3381 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3382 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003383 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003384 if (Node->getOpcode() == ISD::CTTZ) {
3385 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003386 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003387 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3388 ISD::SETEQ);
3389 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3390 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3391 } else if (Node->getOpcode() == ISD::CTLZ) {
3392 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3393 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3394 DAG.getConstant(NVT.getSizeInBits() -
3395 OVT.getSizeInBits(), NVT));
3396 }
Bill Wendling775db972009-12-23 00:28:23 +00003397 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003398 break;
3399 case ISD::BSWAP: {
3400 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003401 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003402 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3403 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3404 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3405 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003406 break;
3407 }
3408 case ISD::FP_TO_UINT:
3409 case ISD::FP_TO_SINT:
3410 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3411 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3412 Results.push_back(Tmp1);
3413 break;
3414 case ISD::UINT_TO_FP:
3415 case ISD::SINT_TO_FP:
3416 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3417 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3418 Results.push_back(Tmp1);
3419 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003420 case ISD::AND:
3421 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003422 case ISD::XOR: {
3423 unsigned ExtOp, TruncOp;
3424 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003425 ExtOp = ISD::BITCAST;
3426 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003427 } else {
3428 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003429 ExtOp = ISD::ANY_EXTEND;
3430 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003431 }
3432 // Promote each of the values to the new type.
3433 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3434 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3435 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003436 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3437 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003438 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003439 }
3440 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003441 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003442 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003443 ExtOp = ISD::BITCAST;
3444 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003445 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003446 ExtOp = ISD::ANY_EXTEND;
3447 TruncOp = ISD::TRUNCATE;
3448 } else {
3449 ExtOp = ISD::FP_EXTEND;
3450 TruncOp = ISD::FP_ROUND;
3451 }
3452 Tmp1 = Node->getOperand(0);
3453 // Promote each of the values to the new type.
3454 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3455 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3456 // Perform the larger operation, then round down.
3457 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3458 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003459 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003460 else
Bill Wendling775db972009-12-23 00:28:23 +00003461 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003462 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003463 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003464 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003465 }
Eli Friedman509150f2009-05-27 07:58:35 +00003466 case ISD::VECTOR_SHUFFLE: {
3467 SmallVector<int, 8> Mask;
3468 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3469
3470 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003471 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3472 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003473
3474 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003475 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003476 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003477 Results.push_back(Tmp1);
3478 break;
3479 }
Eli Friedmanad754602009-05-28 03:56:57 +00003480 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003481 unsigned ExtOp = ISD::FP_EXTEND;
3482 if (NVT.isInteger()) {
3483 ISD::CondCode CCCode =
3484 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3485 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003486 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003487 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3488 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003489 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3490 Tmp1, Tmp2, Node->getOperand(2)));
3491 break;
3492 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003493 }
3494}
3495
Chris Lattner3e928bb2005-01-07 07:47:09 +00003496// SelectionDAG::Legalize - This is the entry point for the file.
3497//
Dan Gohman714efc62009-12-05 17:51:33 +00003498void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003499 /// run - This is the main entry point to this class.
3500 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003501 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003502}
3503