blob: 6b7f83fb7f212d13531865147ec1b79c6fdba868 [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 Lattner3e928bb2005-01-07 07:47:09 +000069 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000070 Legal, // The target natively supports this operation.
71 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000072 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 };
Scott Michelfdc40a02009-02-17 22:15:04 +000074
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 /// ValueTypeActions - This is a bitvector that contains two bits for each
76 /// value type, where the two bits correspond to the LegalizeAction enum.
77 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000078 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000079
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 /// LegalizedNodes - For nodes that are of legal width, and that have more
81 /// than one use, this map indicates what regularized operand to use. This
82 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000083 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000084
Dan Gohman475871a2008-07-27 21:46:04 +000085 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000086 LegalizedNodes.insert(std::make_pair(From, To));
87 // If someone requests legalization of the new node, return itself.
88 if (From != To)
89 LegalizedNodes.insert(std::make_pair(To, To));
Devang Pateld2c97932011-01-26 18:55:05 +000090
91 // Transfer SDDbgValues.
92 DAG.TransferDbgValues(From, To);
Chris Lattner8afc48e2005-01-07 22:28:47 +000093 }
94
Chris Lattner3e928bb2005-01-07 07:47:09 +000095public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000096 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +0000101 LegalizeAction getTypeAction(EVT VT) const {
Chris Lattneraafe6262010-08-25 23:00:45 +0000102 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 }
104
105 /// isTypeLegal - Return true if this type is legal on this target.
106 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000107 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 return getTypeAction(VT) == Legal;
109 }
110
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 void LegalizeDAG();
112
Chris Lattner456a93a2006-01-28 07:39:30 +0000113private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000114 /// LegalizeOp - We know that the specified value has a legal type.
115 /// Recursively ensure that the operands have legal types, then return the
116 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Eli Friedman7ef3d172009-06-06 07:04:42 +0000119 SDValue OptimizeFloatStore(StoreSDNode *ST);
120
Nate Begeman68679912008-04-25 18:07:40 +0000121 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
122 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
123 /// is necessary to spill the vector being inserted into to memory, perform
124 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000126 SDValue Idx, DebugLoc dl);
127 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
128 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000129
Nate Begeman5a5ca152009-04-29 05:20:52 +0000130 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
131 /// performs the same shuffe in terms of order or result bytes, but on a type
132 /// whose vector element type is narrower than the original shuffle type.
133 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000134 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000135 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000136 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000137
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000138 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000139 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000140
Owen Andersone50ed302009-08-10 22:56:29 +0000141 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000142 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000143
Eli Friedman47b41f72009-05-27 02:21:29 +0000144 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Jim Grosbache03262f2010-06-18 21:43:38 +0000145 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
146 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000147 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
148 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
149 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000150 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
151 RTLIB::Libcall Call_I8,
152 RTLIB::Libcall Call_I16,
153 RTLIB::Libcall Call_I32,
154 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000155 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000156
Owen Andersone50ed302009-08-10 22:56:29 +0000157 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000158 SDValue ExpandBUILD_VECTOR(SDNode *Node);
159 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000160 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
161 SmallVectorImpl<SDValue> &Results);
162 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000163 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000164 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000165 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000166 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000167 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000168 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Dale Johannesen8a782a22009-02-02 22:12:50 +0000170 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
171 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000172
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000173 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000174 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000175 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000176
Jim Grosbache03262f2010-06-18 21:43:38 +0000177 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
178
Eli Friedman8c377c72009-05-27 01:25:56 +0000179 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
180 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000181};
182}
183
Nate Begeman5a5ca152009-04-29 05:20:52 +0000184/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
185/// performs the same shuffe in terms of order or result bytes, but on a type
186/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000187/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000188SDValue
189SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000190 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000191 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000192 unsigned NumMaskElts = VT.getVectorNumElements();
193 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000194 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000195
Nate Begeman9008ca62009-04-27 18:41:29 +0000196 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
197
198 if (NumEltsGrowth == 1)
199 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000200
Nate Begeman9008ca62009-04-27 18:41:29 +0000201 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000202 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000203 int Idx = Mask[i];
204 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000205 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000206 NewMask.push_back(-1);
207 else
208 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000209 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000210 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000211 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000212 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
213 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000214}
215
Bill Wendling5aa49772009-02-24 02:35:30 +0000216SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000217 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000218 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
219 DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000220 ValueTypeActions(TLI.getValueTypeActions()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223}
224
Chris Lattner3e928bb2005-01-07 07:47:09 +0000225void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000226 LastCALLSEQ_END = DAG.getEntryNode();
Mon P Wange5ab34e2009-02-04 19:38:14 +0000227
Chris Lattnerab510a72005-10-02 17:49:46 +0000228 // The legalize process is inherently a bottom-up recursive process (users
229 // legalize their uses before themselves). Given infinite stack space, we
230 // could just start legalizing on the root and traverse the whole graph. In
231 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000232 // blocks. To avoid this problem, compute an ordering of the nodes where each
233 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000234 DAG.AssignTopologicalOrder();
235 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000236 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000237 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000238
239 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000240 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000241 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
242 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000243
Chris Lattner3e928bb2005-01-07 07:47:09 +0000244 LegalizedNodes.clear();
245
246 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000247 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000248}
249
Chris Lattner6831a812006-02-13 09:18:02 +0000250
251/// FindCallEndFromCallStart - Given a chained node that is part of a call
252/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000253static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000254 // Nested CALLSEQ_START/END constructs aren't yet legal,
255 // but we can DTRT and handle them correctly here.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000256 if (Node->getOpcode() == ISD::CALLSEQ_START)
257 depth++;
Stuart Hastings2965e692010-12-21 17:07:24 +0000258 else if (Node->getOpcode() == ISD::CALLSEQ_END) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000259 depth--;
260 if (depth == 0)
261 return Node;
262 }
Chris Lattner6831a812006-02-13 09:18:02 +0000263 if (Node->use_empty())
264 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000265
Chris Lattner6831a812006-02-13 09:18:02 +0000266 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000269 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000270 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000272 // Otherwise, hunt for it.
273 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000275 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000276 break;
277 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
279 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000281 return 0;
282 }
283 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Chris Lattner6831a812006-02-13 09:18:02 +0000285 for (SDNode::use_iterator UI = Node->use_begin(),
286 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000287
Chris Lattner6831a812006-02-13 09:18:02 +0000288 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000289 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000290 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
291 if (User->getOperand(i) == TheChain)
Stuart Hastingsa304d022010-12-09 21:25:20 +0000292 if (SDNode *Result = FindCallEndFromCallStart(User, depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000293 return Result;
294 }
295 return 0;
296}
297
Scott Michelfdc40a02009-02-17 22:15:04 +0000298/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000299/// sequence, find the CALLSEQ_START node that initiates the call sequence.
300static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000301 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000302 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000303 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
304 Node = Node->getOperand(0).getNode();
305 assert(Node->getOperand(0).getValueType() == MVT::Other &&
306 "Node doesn't have a token chain argument!");
307 switch (Node->getOpcode()) {
308 default:
309 break;
310 case ISD::CALLSEQ_START:
311 if (!nested)
312 return Node;
313 nested--;
314 break;
315 case ISD::CALLSEQ_END:
316 nested++;
317 break;
318 }
319 }
320 return 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000321}
322
323/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000324/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000325/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000326///
327/// Keep track of the nodes we fine that actually do lead to Dest in
328/// NodesLeadingTo. This avoids retraversing them exponential number of times.
329///
330bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000331 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000332 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000333
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000334 // If we've already processed this node and it does lead to Dest, there is no
335 // need to reprocess it.
336 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000337
Chris Lattner6831a812006-02-13 09:18:02 +0000338 // If the first result of this node has been already legalized, then it cannot
339 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000340 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000341
Chris Lattner6831a812006-02-13 09:18:02 +0000342 // Okay, this node has not already been legalized. Check and legalize all
343 // operands. If none lead to Dest, then we can legalize this node.
344 bool OperandsLeadToDest = false;
345 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
346 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000347 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
348 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000349
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000350 if (OperandsLeadToDest) {
351 NodesLeadingTo.insert(N);
352 return true;
353 }
Chris Lattner6831a812006-02-13 09:18:02 +0000354
355 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000356 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000357 return false;
358}
359
Evan Cheng9f877882006-12-13 20:57:08 +0000360/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
361/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000362static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000363 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000364 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000365 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000366
367 // If a FP immediate is precise when represented as a float and if the
368 // target can do an extending load from float to double, we put it into
369 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000370 // double. This shrinks FP constants and canonicalizes them for targets where
371 // an FP extending load is the same cost as a normal load (such as on the x87
372 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000373 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000374 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000375 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000377 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000379 }
380
Owen Andersone50ed302009-08-10 22:56:29 +0000381 EVT OrigVT = VT;
382 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 while (SVT != MVT::f32) {
384 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000385 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000386 // Only do this if the target has a native EXTLOAD instruction from
387 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000388 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000389 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000390 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000391 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000392 VT = SVT;
393 Extend = true;
394 }
Evan Cheng00495212006-12-12 21:32:44 +0000395 }
396
Dan Gohman475871a2008-07-27 21:46:04 +0000397 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000398 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000399 if (Extend)
Evan Chengbcc80172010-07-07 22:15:37 +0000400 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, dl,
401 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000402 CPIdx, MachinePointerInfo::getConstantPool(),
403 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000404 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000405 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000406 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000407}
408
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000409/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
410static
Dan Gohman475871a2008-07-27 21:46:04 +0000411SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000412 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000413 SDValue Chain = ST->getChain();
414 SDValue Ptr = ST->getBasePtr();
415 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000416 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000417 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000418 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000419 if (ST->getMemoryVT().isFloatingPoint() ||
420 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000421 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000422 if (TLI.isTypeLegal(intVT)) {
423 // Expand to a bitconvert of the value to the integer type of the
424 // same size, then a (misaligned) int store.
425 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000426 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000427 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
428 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000429 } else {
430 // Do a (aligned) store to a stack slot, then copy from the stack slot
431 // to the final destination using (unaligned) integer loads and stores.
Owen Andersone50ed302009-08-10 22:56:29 +0000432 EVT StoredVT = ST->getMemoryVT();
433 EVT RegVT =
Evan Chengadf97992010-04-15 01:25:27 +0000434 TLI.getRegisterType(*DAG.getContext(),
435 EVT::getIntegerVT(*DAG.getContext(),
436 StoredVT.getSizeInBits()));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000437 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
438 unsigned RegBytes = RegVT.getSizeInBits() / 8;
439 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000440
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000441 // Make sure the stack slot is also aligned for the register type.
442 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
443
444 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000445 SDValue Store = DAG.getTruncStore(Chain, dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000446 Val, StackPtr, MachinePointerInfo(),
447 StoredVT, false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000448 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
449 SmallVector<SDValue, 8> Stores;
450 unsigned Offset = 0;
451
452 // Do all but one copies using the full register width.
453 for (unsigned i = 1; i < NumRegs; i++) {
454 // Load one integer register's worth from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000455 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
456 MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000457 false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000458 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000459 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000460 ST->getPointerInfo().getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +0000461 ST->isVolatile(), ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000462 MinAlign(ST->getAlignment(), Offset)));
463 // Increment the pointers.
464 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000465 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000466 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000467 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000468 }
469
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000470 // The last store may be partial. Do a truncating store. On big-endian
471 // machines this requires an extending load from the stack slot to ensure
472 // that the bits are in the right place.
Evan Chengadf97992010-04-15 01:25:27 +0000473 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
474 8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000475
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000476 // Load from the stack slot.
Evan Chengbcc80172010-07-07 22:15:37 +0000477 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Store, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000478 MachinePointerInfo(),
479 MemVT, false, false, 0);
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000480
Dale Johannesenbb5da912009-02-02 20:41:04 +0000481 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000482 ST->getPointerInfo()
483 .getWithOffset(Offset),
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000484 MemVT, ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000485 ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000486 MinAlign(ST->getAlignment(), Offset)));
487 // The order of the stores doesn't matter - say it with a TokenFactor.
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000489 Stores.size());
490 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000491 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000492 assert(ST->getMemoryVT().isInteger() &&
493 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000494 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000495 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000496 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000497 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000498 int IncrementSize = NumBits / 8;
499
500 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000501 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
502 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000503 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000504
505 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000506 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000507 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000508 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000509 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000510 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000511 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000512 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000513 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000514 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000515 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
516 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000517
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000519}
520
521/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
522static
Dan Gohman475871a2008-07-27 21:46:04 +0000523SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000524 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000525 SDValue Chain = LD->getChain();
526 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000527 EVT VT = LD->getValueType(0);
528 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000529 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000530 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000531 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000532 if (TLI.isTypeLegal(intVT)) {
533 // Expand to a (misaligned) integer load of the same size,
534 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000535 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
536 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000537 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000538 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000539 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000540 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000541
Duncan Sands05e11fa2008-12-12 21:47:02 +0000542 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000543 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000544 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000545
Chris Lattnerecf42c42010-09-21 16:36:31 +0000546 // Copy the value to a (aligned) stack slot using (unaligned) integer
547 // loads and stores, then do a (aligned) load from the stack slot.
548 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
549 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
550 unsigned RegBytes = RegVT.getSizeInBits() / 8;
551 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
552
553 // Make sure the stack slot is also aligned for the register type.
554 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
555
556 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
557 SmallVector<SDValue, 8> Stores;
558 SDValue StackPtr = StackBase;
559 unsigned Offset = 0;
560
561 // Do all but one copies using the full register width.
562 for (unsigned i = 1; i < NumRegs; i++) {
563 // Load one integer register's worth from the original location.
564 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
565 LD->getPointerInfo().getWithOffset(Offset),
566 LD->isVolatile(), LD->isNonTemporal(),
567 MinAlign(LD->getAlignment(), Offset));
568 // Follow the load with a store to the stack slot. Remember the store.
569 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000570 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000571 // Increment the pointers.
572 Offset += RegBytes;
573 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
574 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
575 Increment);
576 }
577
578 // The last copy may be partial. Do an extending load.
579 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
580 8 * (LoadedBytes - Offset));
581 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Chain, Ptr,
582 LD->getPointerInfo().getWithOffset(Offset),
583 MemVT, LD->isVolatile(),
584 LD->isNonTemporal(),
585 MinAlign(LD->getAlignment(), Offset));
586 // Follow the load with a store to the stack slot. Remember the store.
587 // On big-endian machines this requires a truncating store to ensure
588 // that the bits end up in the right place.
589 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
590 MachinePointerInfo(), MemVT,
591 false, false, 0));
592
593 // The order of the stores doesn't matter - say it with a TokenFactor.
594 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
595 Stores.size());
596
597 // Finally, perform the original load only redirected to the stack slot.
598 Load = DAG.getExtLoad(LD->getExtensionType(), VT, dl, TF, StackBase,
599 MachinePointerInfo(), LoadedVT, false, false, 0);
600
601 // Callers expect a MERGE_VALUES node.
602 SDValue Ops[] = { Load, TF };
603 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000604 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000605 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000606 "Unaligned load of unsupported type.");
607
Dale Johannesen8155d642008-02-27 22:36:00 +0000608 // Compute the new VT that is half the size of the old one. This is an
609 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000610 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000611 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000612 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000613 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000614
Chris Lattnere400af82007-11-19 21:38:03 +0000615 unsigned Alignment = LD->getAlignment();
616 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000617 ISD::LoadExtType HiExtType = LD->getExtensionType();
618
619 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
620 if (HiExtType == ISD::NON_EXTLOAD)
621 HiExtType = ISD::ZEXTLOAD;
622
623 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000624 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000625 if (TLI.isLittleEndian()) {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000626 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr, LD->getPointerInfo(),
627 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000628 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000629 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000630 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000631 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr,
632 LD->getPointerInfo().getWithOffset(IncrementSize),
633 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000634 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000635 } else {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000636 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr, LD->getPointerInfo(),
637 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000638 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000639 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000640 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000641 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr,
642 LD->getPointerInfo().getWithOffset(IncrementSize),
643 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000644 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000645 }
646
647 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000648 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000649 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
650 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000651
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000653 Hi.getValue(1));
654
Dan Gohman475871a2008-07-27 21:46:04 +0000655 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000656 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000657}
Evan Cheng912095b2007-01-04 21:56:39 +0000658
Nate Begeman68679912008-04-25 18:07:40 +0000659/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
660/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
661/// is necessary to spill the vector being inserted into to memory, perform
662/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000663SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000664PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
665 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000666 SDValue Tmp1 = Vec;
667 SDValue Tmp2 = Val;
668 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000669
Nate Begeman68679912008-04-25 18:07:40 +0000670 // If the target doesn't support this, we have to spill the input vector
671 // to a temporary stack slot, update the element, then reload it. This is
672 // badness. We could also load the value into a vector register (either
673 // with a "move to register" or "extload into register" instruction, then
674 // permute it into place, if the idx is a constant and if the idx is
675 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000676 EVT VT = Tmp1.getValueType();
677 EVT EltVT = VT.getVectorElementType();
678 EVT IdxVT = Tmp3.getValueType();
679 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000680 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000681
Evan Chengff89dcb2009-10-18 18:16:27 +0000682 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
683
Nate Begeman68679912008-04-25 18:07:40 +0000684 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000685 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000686 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000687 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000688
689 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000690 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000691 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000692 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000693 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000694 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
695 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000696 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000697 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000698 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000699 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000700 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000701 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000702}
703
Mon P Wange9f10152008-12-09 05:46:39 +0000704
Eli Friedman3f727d62009-05-27 02:16:40 +0000705SDValue SelectionDAGLegalize::
706ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
707 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
708 // SCALAR_TO_VECTOR requires that the type of the value being inserted
709 // match the element type of the vector being created, except for
710 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000711 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000712 if (Val.getValueType() == EltVT ||
713 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
714 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
715 Vec.getValueType(), Val);
716
717 unsigned NumElts = Vec.getValueType().getVectorNumElements();
718 // We generate a shuffle of InVec and ScVec, so the shuffle mask
719 // should be 0,1,2,3,4,5... with the appropriate element replaced with
720 // elt 0 of the RHS.
721 SmallVector<int, 8> ShufOps;
722 for (unsigned i = 0; i != NumElts; ++i)
723 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
724
725 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
726 &ShufOps[0]);
727 }
728 }
729 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
730}
731
Eli Friedman7ef3d172009-06-06 07:04:42 +0000732SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
733 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
734 // FIXME: We shouldn't do this for TargetConstantFP's.
735 // FIXME: move this to the DAG Combiner! Note that we can't regress due
736 // to phase ordering between legalized code and the dag combiner. This
737 // probably means that we need to integrate dag combiner and legalizer
738 // together.
739 // We generally can't do this one for long doubles.
740 SDValue Tmp1 = ST->getChain();
741 SDValue Tmp2 = ST->getBasePtr();
742 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000743 unsigned Alignment = ST->getAlignment();
744 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000745 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000746 DebugLoc dl = ST->getDebugLoc();
747 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 if (CFP->getValueType(0) == MVT::f32 &&
749 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000750 Tmp3 = DAG.getConstant(CFP->getValueAPF().
751 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000753 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
754 isVolatile, isNonTemporal, Alignment);
755 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000756
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000757 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000758 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000760 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000762 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
763 isVolatile, isNonTemporal, Alignment);
764 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000765
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000766 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000767 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
768 // stores. If the target supports neither 32- nor 64-bits, this
769 // xform is certainly not worth it.
770 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000771 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000773 if (TLI.isBigEndian()) std::swap(Lo, Hi);
774
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000775 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
776 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000777 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
778 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000779 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
780 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000781 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000782
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000784 }
785 }
786 }
787 return SDValue();
788}
789
Dan Gohmana3466152007-07-13 20:14:11 +0000790/// LegalizeOp - We know that the specified value has a legal type, and
791/// that its operands are legal. Now ensure that the operation itself
792/// is legal, recursively ensuring that the operands' operations remain
793/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000794SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000795 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
796 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000797
Gabor Greifba36cb52008-08-28 21:40:38 +0000798 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000799 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000800
Eli Friedman1fde9c52009-05-24 02:46:31 +0000801 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
802 assert(getTypeAction(Node->getValueType(i)) == Legal &&
803 "Unexpected illegal type!");
804
805 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000806 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000807 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
808 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000809
Chris Lattner45982da2005-05-12 16:53:42 +0000810 // Note that LegalizeOp may be reentered even from single-use nodes, which
811 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000812 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000813 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000814
Dan Gohman475871a2008-07-27 21:46:04 +0000815 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
816 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000817 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000818
Eli Friedman8c377c72009-05-27 01:25:56 +0000819 // Figure out the correct action; the way to query this varies by opcode
820 TargetLowering::LegalizeAction Action;
821 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000822 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000823 case ISD::INTRINSIC_W_CHAIN:
824 case ISD::INTRINSIC_WO_CHAIN:
825 case ISD::INTRINSIC_VOID:
826 case ISD::VAARG:
827 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000829 break;
830 case ISD::SINT_TO_FP:
831 case ISD::UINT_TO_FP:
832 case ISD::EXTRACT_VECTOR_ELT:
833 Action = TLI.getOperationAction(Node->getOpcode(),
834 Node->getOperand(0).getValueType());
835 break;
836 case ISD::FP_ROUND_INREG:
837 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000838 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000839 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
840 break;
841 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000842 case ISD::SELECT_CC:
843 case ISD::SETCC:
844 case ISD::BR_CC: {
845 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
846 Node->getOpcode() == ISD::SETCC ? 2 : 1;
847 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000848 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000849 ISD::CondCode CCCode =
850 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
851 Action = TLI.getCondCodeAction(CCCode, OpVT);
852 if (Action == TargetLowering::Legal) {
853 if (Node->getOpcode() == ISD::SELECT_CC)
854 Action = TLI.getOperationAction(Node->getOpcode(),
855 Node->getValueType(0));
856 else
857 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
858 }
859 break;
860 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000861 case ISD::LOAD:
862 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000863 // FIXME: Model these properly. LOAD and STORE are complicated, and
864 // STORE expects the unlegalized operand in some cases.
865 SimpleFinishLegalizing = false;
866 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000867 case ISD::CALLSEQ_START:
868 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000869 // FIXME: This shouldn't be necessary. These nodes have special properties
870 // dealing with the recursive nature of legalization. Removing this
871 // special case should be done as part of making LegalizeDAG non-recursive.
872 SimpleFinishLegalizing = false;
873 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000874 case ISD::EXTRACT_ELEMENT:
875 case ISD::FLT_ROUNDS_:
876 case ISD::SADDO:
877 case ISD::SSUBO:
878 case ISD::UADDO:
879 case ISD::USUBO:
880 case ISD::SMULO:
881 case ISD::UMULO:
882 case ISD::FPOWI:
883 case ISD::MERGE_VALUES:
884 case ISD::EH_RETURN:
885 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000886 case ISD::EH_SJLJ_SETJMP:
887 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000888 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000889 // These operations lie about being legal: when they claim to be legal,
890 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000891 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
892 if (Action == TargetLowering::Legal)
893 Action = TargetLowering::Expand;
894 break;
895 case ISD::TRAMPOLINE:
896 case ISD::FRAMEADDR:
897 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000898 // These operations lie about being legal: when they claim to be legal,
899 // they should actually be custom-lowered.
900 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
901 if (Action == TargetLowering::Legal)
902 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000903 break;
904 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000905 // A weird case: legalization for BUILD_VECTOR never legalizes the
906 // operands!
907 // FIXME: This really sucks... changing it isn't semantically incorrect,
908 // but it massively pessimizes the code for floating-point BUILD_VECTORs
909 // because ConstantFP operands get legalized into constant pool loads
910 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
911 // though, because BUILD_VECTORS usually get lowered into other nodes
912 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000913 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000914 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000915 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000916 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000917 Action = TargetLowering::Legal;
918 } else {
919 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000920 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000921 break;
922 }
923
924 if (SimpleFinishLegalizing) {
925 SmallVector<SDValue, 8> Ops, ResultVals;
926 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
927 Ops.push_back(LegalizeOp(Node->getOperand(i)));
928 switch (Node->getOpcode()) {
929 default: break;
930 case ISD::BR:
931 case ISD::BRIND:
932 case ISD::BR_JT:
933 case ISD::BR_CC:
934 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000935 // Branches tweak the chain to include LastCALLSEQ_END
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Eli Friedman8c377c72009-05-27 01:25:56 +0000937 LastCALLSEQ_END);
938 Ops[0] = LegalizeOp(Ops[0]);
939 LastCALLSEQ_END = DAG.getEntryNode();
940 break;
941 case ISD::SHL:
942 case ISD::SRL:
943 case ISD::SRA:
944 case ISD::ROTL:
945 case ISD::ROTR:
946 // Legalizing shifts/rotates requires adjusting the shift amount
947 // to the appropriate width.
948 if (!Ops[1].getValueType().isVector())
949 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
950 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000951 case ISD::SRL_PARTS:
952 case ISD::SRA_PARTS:
953 case ISD::SHL_PARTS:
954 // Legalizing shifts/rotates requires adjusting the shift amount
955 // to the appropriate width.
956 if (!Ops[2].getValueType().isVector())
957 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000958 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000959 }
960
Dan Gohman027657d2010-06-18 15:30:29 +0000961 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
962 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000963 switch (Action) {
964 case TargetLowering::Legal:
965 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
966 ResultVals.push_back(Result.getValue(i));
967 break;
968 case TargetLowering::Custom:
969 // FIXME: The handling for custom lowering with multiple results is
970 // a complete mess.
971 Tmp1 = TLI.LowerOperation(Result, DAG);
972 if (Tmp1.getNode()) {
973 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
974 if (e == 1)
975 ResultVals.push_back(Tmp1);
976 else
977 ResultVals.push_back(Tmp1.getValue(i));
978 }
979 break;
980 }
981
982 // FALL THROUGH
983 case TargetLowering::Expand:
984 ExpandNode(Result.getNode(), ResultVals);
985 break;
986 case TargetLowering::Promote:
987 PromoteNode(Result.getNode(), ResultVals);
988 break;
989 }
990 if (!ResultVals.empty()) {
991 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
992 if (ResultVals[i] != SDValue(Node, i))
993 ResultVals[i] = LegalizeOp(ResultVals[i]);
994 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
995 }
996 return ResultVals[Op.getResNo()];
997 }
998 }
999
1000 switch (Node->getOpcode()) {
1001 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001002#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001003 dbgs() << "NODE: ";
1004 Node->dump( &DAG);
1005 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001006#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001007 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001008
Chris Lattnerb2827b02006-03-19 00:52:58 +00001009 case ISD::BUILD_VECTOR:
1010 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001011 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001012 case TargetLowering::Custom:
1013 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001014 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001015 Result = Tmp3;
1016 break;
1017 }
1018 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001019 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001020 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001021 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001022 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001023 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001024 case ISD::CALLSEQ_START: {
Stuart Hastingsf2243222011-01-18 00:09:27 +00001025 static int depth = 0;
Chris Lattner6831a812006-02-13 09:18:02 +00001026 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Chris Lattner6831a812006-02-13 09:18:02 +00001028 // Recursively Legalize all of the inputs of the call end that do not lead
1029 // to this call start. This ensures that any libcalls that need be inserted
1030 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001031 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001032 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001033 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001034 NodesLeadingTo);
1035 }
Chris Lattner6831a812006-02-13 09:18:02 +00001036
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001037 // Now that we have legalized all of the inputs (which may have inserted
1038 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1040
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001041 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001042 // call ended.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001043 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken && depth == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001044 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001045 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001046 Tmp1 = LegalizeOp(Tmp1);
1047 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001048
Chris Lattner6831a812006-02-13 09:18:02 +00001049 // Do not try to legalize the target-specific arguments (#1+).
1050 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001051 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001052 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001053 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1054 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Chris Lattner6831a812006-02-13 09:18:02 +00001057 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001058 AddLegalizedOperand(Op.getValue(0), Result);
1059 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1060 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001061
Chris Lattner6831a812006-02-13 09:18:02 +00001062 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001063 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001064 // process, no libcalls can/will be inserted, guaranteeing that no calls
1065 // can overlap.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001066
1067 SDValue Saved_LastCALLSEQ_END = LastCALLSEQ_END ;
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);
Scott Michelfdc40a02009-02-17 22:15:04 +00001070
Stuart Hastingsf2243222011-01-18 00:09:27 +00001071 depth++;
Chris Lattner6831a812006-02-13 09:18:02 +00001072 // Legalize the call, starting from the CALLSEQ_END.
1073 LegalizeOp(LastCALLSEQ_END);
Stuart Hastingsf2243222011-01-18 00:09:27 +00001074 depth--;
1075 assert(depth >= 0 && "Un-matched CALLSEQ_START?");
1076 if (depth > 0)
1077 LastCALLSEQ_END = Saved_LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +00001078 return Result;
1079 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001080 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001081 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1082 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001083 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001084 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1085 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001086 assert(I != LegalizedNodes.end() &&
1087 "Legalizing the call start should have legalized this node!");
1088 return I->second;
1089 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001090
1091 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001092 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001094 // Do not try to legalize the target-specific arguments (#1+), except for
1095 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001096 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001097 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001098 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001099 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001100 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1101 &Ops[0], Ops.size()),
1102 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001103 }
1104 } else {
1105 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1106 if (Tmp1 != Node->getOperand(0) ||
1107 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001108 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001109 Ops[0] = Tmp1;
1110 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001111 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1112 &Ops[0], Ops.size()),
1113 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001114 }
Chris Lattner6a542892006-01-24 05:48:21 +00001115 }
Chris Lattner6831a812006-02-13 09:18:02 +00001116 // This finishes up call legalization.
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
David Greenecfe33c42011-01-26 19:13:22 +00001592SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1593 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1594
1595 SDValue Vec = Op.getOperand(0);
1596 SDValue Part = Op.getOperand(1);
1597 SDValue Idx = Op.getOperand(2);
1598 DebugLoc dl = Op.getDebugLoc();
1599
1600 // Store the value to a temporary stack slot, then LOAD the returned part.
1601
1602 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1603 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1604 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1605
1606 // First store the whole vector.
1607 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1608 false, false, 0);
1609
1610 // Then store the inserted part.
1611
1612 // Add the offset to the index.
1613 unsigned EltSize =
1614 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1615
1616 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1617 DAG.getConstant(EltSize, Idx.getValueType()));
1618
1619 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1620 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1621 else
1622 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1623
1624 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1625 StackPtr);
1626
1627 // Store the subvector.
1628 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1629 MachinePointerInfo(), false, false, 0);
1630
1631 // Finally, load the updated vector.
1632 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1633 false, false, 0);
1634}
1635
Eli Friedman7ef3d172009-06-06 07:04:42 +00001636SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1637 // We can't handle this case efficiently. Allocate a sufficiently
1638 // aligned object on the stack, store each element into it, then load
1639 // the result as a vector.
1640 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001641 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001642 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001643 DebugLoc dl = Node->getDebugLoc();
1644 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001645 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001646 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001647
1648 // Emit a store of each element to the stack slot.
1649 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001650 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001651 // Store (in the right endianness) the elements to memory.
1652 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1653 // Ignore undef elements.
1654 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1655
1656 unsigned Offset = TypeByteSize*i;
1657
1658 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1659 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1660
Dan Gohman9949dd62010-02-25 20:30:49 +00001661 // If the destination vector element type is narrower than the source
1662 // element type, only store the bits necessary.
1663 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001664 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001665 Node->getOperand(i), Idx,
1666 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001667 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001668 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001669 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001670 Node->getOperand(i), Idx,
1671 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001672 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001673 }
1674
1675 SDValue StoreChain;
1676 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001678 &Stores[0], Stores.size());
1679 else
1680 StoreChain = DAG.getEntryNode();
1681
1682 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001683 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001684}
1685
Eli Friedman4bc8c712009-05-27 12:20:41 +00001686SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1687 DebugLoc dl = Node->getDebugLoc();
1688 SDValue Tmp1 = Node->getOperand(0);
1689 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001690
1691 // Get the sign bit of the RHS. First obtain a value that has the same
1692 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001693 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001694 EVT FloatVT = Tmp2.getValueType();
1695 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001696 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001697 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001698 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001699 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001700 // Store the float to memory, then load the sign part out as an integer.
1701 MVT LoadTy = TLI.getPointerTy();
1702 // First create a temporary that is aligned for both the load and store.
1703 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1704 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001705 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001706 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001707 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001708 if (TLI.isBigEndian()) {
1709 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1710 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001711 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1712 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001713 } else { // Little endian
1714 SDValue LoadPtr = StackPtr;
1715 // The float may be wider than the integer we are going to load. Advance
1716 // the pointer so that the loaded integer will contain the sign bit.
1717 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1718 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1719 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1720 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1721 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001722 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1723 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001724 // Move the sign bit to the top bit of the loaded integer.
1725 unsigned BitShift = LoadTy.getSizeInBits() -
1726 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1727 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1728 if (BitShift)
1729 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
1730 DAG.getConstant(BitShift,TLI.getShiftAmountTy()));
1731 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001732 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001733 // Now get the sign bit proper, by seeing whether the value is negative.
1734 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1735 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1736 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001737 // Get the absolute value of the result.
1738 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1739 // Select between the nabs and abs value based on the sign bit of
1740 // the input.
1741 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1742 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1743 AbsVal);
1744}
1745
Eli Friedman4bc8c712009-05-27 12:20:41 +00001746void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1747 SmallVectorImpl<SDValue> &Results) {
1748 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1749 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1750 " not tell us which reg is the stack pointer!");
1751 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001752 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001753 SDValue Tmp1 = SDValue(Node, 0);
1754 SDValue Tmp2 = SDValue(Node, 1);
1755 SDValue Tmp3 = Node->getOperand(2);
1756 SDValue Chain = Tmp1.getOperand(0);
1757
1758 // Chain the dynamic stack allocation so that it doesn't modify the stack
1759 // pointer when other instructions are using the stack.
1760 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1761
1762 SDValue Size = Tmp2.getOperand(1);
1763 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1764 Chain = SP.getValue(1);
1765 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001766 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001767 if (Align > StackAlign)
1768 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1769 DAG.getConstant(-(uint64_t)Align, VT));
1770 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1771 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1772
1773 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1774 DAG.getIntPtrConstant(0, true), SDValue());
1775
1776 Results.push_back(Tmp1);
1777 Results.push_back(Tmp2);
1778}
1779
Evan Cheng7f042682008-10-15 02:05:31 +00001780/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001781/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001782/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001783void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001784 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001785 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001786 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001787 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001788 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1789 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001790 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001791 case TargetLowering::Legal:
1792 // Nothing to do.
1793 break;
1794 case TargetLowering::Expand: {
1795 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1796 unsigned Opc = 0;
1797 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001798 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001799 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1800 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1801 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1802 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1803 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1804 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1805 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1806 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1807 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1808 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1809 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1810 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001811 // FIXME: Implement more expansions.
1812 }
1813
Dale Johannesenbb5da912009-02-02 20:41:04 +00001814 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1815 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1816 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001817 RHS = SDValue();
1818 CC = SDValue();
1819 break;
1820 }
1821 }
1822}
1823
Chris Lattner1401d152008-01-16 07:45:30 +00001824/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1825/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1826/// a load from the stack slot to DestVT, extending it if needed.
1827/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001828SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001829 EVT SlotVT,
1830 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001831 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001832 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001833 unsigned SrcAlign =
1834 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001835 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001837
Evan Chengff89dcb2009-10-18 18:16:27 +00001838 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1839 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001840 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001841
Duncan Sands83ec4b62008-06-06 12:08:01 +00001842 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1843 unsigned SlotSize = SlotVT.getSizeInBits();
1844 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001845 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1846 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001847
Chris Lattner1401d152008-01-16 07:45:30 +00001848 // Emit a store to the stack slot. Use a truncstore if the input value is
1849 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001851
Chris Lattner1401d152008-01-16 07:45:30 +00001852 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001853 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001854 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001855 else {
1856 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001857 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001858 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001859 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001860
Chris Lattner35481892005-12-23 00:16:34 +00001861 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001862 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001863 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001864 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001865
Chris Lattner1401d152008-01-16 07:45:30 +00001866 assert(SlotSize < DestSize && "Unknown extension!");
Chris Lattnerecf42c42010-09-21 16:36:31 +00001867 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001868 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001869}
1870
Dan Gohman475871a2008-07-27 21:46:04 +00001871SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001872 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001873 // Create a vector sized/aligned stack slot, store the value to element #0,
1874 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001876
Evan Chengff89dcb2009-10-18 18:16:27 +00001877 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1878 int SPFI = StackPtrFI->getIndex();
1879
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001880 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1881 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001882 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001883 Node->getValueType(0).getVectorElementType(),
1884 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001885 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001886 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001887 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001888}
1889
1890
Chris Lattnerce872152006-03-19 06:31:19 +00001891/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001892/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001893SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001894 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001895 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001896 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001897 EVT VT = Node->getValueType(0);
1898 EVT OpVT = Node->getOperand(0).getValueType();
1899 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
1901 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001902 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001903 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001904 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001905 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001906 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001908 if (V.getOpcode() == ISD::UNDEF)
1909 continue;
1910 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001911 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001912 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001913 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001914
1915 if (!Value1.getNode()) {
1916 Value1 = V;
1917 } else if (!Value2.getNode()) {
1918 if (V != Value1)
1919 Value2 = V;
1920 } else if (V != Value1 && V != Value2) {
1921 MoreThanTwoValues = true;
1922 }
Chris Lattnerce872152006-03-19 06:31:19 +00001923 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001924
Eli Friedman7a5e5552009-06-07 06:52:44 +00001925 if (!Value1.getNode())
1926 return DAG.getUNDEF(VT);
1927
1928 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001929 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001930
Chris Lattner2eb86532006-03-24 07:29:17 +00001931 // If all elements are constants, create a load from the constant pool.
1932 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001933 std::vector<Constant*> CV;
1934 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001936 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001937 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001938 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001939 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001940 if (OpVT==EltVT)
1941 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1942 else {
1943 // If OpVT and EltVT don't match, EltVT is not legal and the
1944 // element values have been promoted/truncated earlier. Undo this;
1945 // we don't want a v16i8 to become a v16i32 for example.
1946 const ConstantInt *CI = V->getConstantIntValue();
1947 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1948 CI->getZExtValue()));
1949 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001950 } else {
1951 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001952 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001953 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001954 }
1955 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001956 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001958 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001959 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001960 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001961 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001962 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001963
Eli Friedman7a5e5552009-06-07 06:52:44 +00001964 if (!MoreThanTwoValues) {
1965 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1966 for (unsigned i = 0; i < NumElems; ++i) {
1967 SDValue V = Node->getOperand(i);
1968 if (V.getOpcode() == ISD::UNDEF)
1969 continue;
1970 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1971 }
1972 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001973 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001974 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1975 SDValue Vec2;
1976 if (Value2.getNode())
1977 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1978 else
1979 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001980
Chris Lattner87100e02006-03-20 01:52:29 +00001981 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001982 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001983 }
1984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Eli Friedman7ef3d172009-06-06 07:04:42 +00001986 // Otherwise, we can't handle this case efficiently.
1987 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001988}
1989
Chris Lattner77e77a62005-01-21 06:05:23 +00001990// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1991// does not fit into a register, return the lo part and set the hi part to the
1992// by-reg argument. If it does fit into a single register, return the result
1993// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001994SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001995 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001996 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001997 // Legalizing the call will automatically add the previous call to the
1998 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001999 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Chris Lattner77e77a62005-01-21 06:05:23 +00002001 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002002 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002003 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002004 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00002005 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002006 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002007 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002008 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002009 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002010 }
Bill Wendling056292f2008-09-16 21:48:12 +00002011 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002012 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002013
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002014 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00002015 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002016
2017 // isTailCall may be true since the callee does not reference caller stack
2018 // frame. Check if it's in the right position.
2019 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002020 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002021 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002022 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002023 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002024 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002025
Evan Cheng3d2125c2010-11-30 23:55:39 +00002026 if (!CallInfo.second.getNode())
2027 // It's a tailcall, return the chain (which is the DAG root).
2028 return DAG.getRoot();
2029
Chris Lattner6831a812006-02-13 09:18:02 +00002030 // Legalize the call sequence, starting with the chain. This will advance
2031 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2032 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2033 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002034 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002035}
2036
Jim Grosbache03262f2010-06-18 21:43:38 +00002037// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2038// ExpandLibCall except that the first operand is the in-chain.
2039std::pair<SDValue, SDValue>
2040SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2041 SDNode *Node,
2042 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002043 SDValue InChain = Node->getOperand(0);
2044
2045 TargetLowering::ArgListTy Args;
2046 TargetLowering::ArgListEntry Entry;
2047 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2048 EVT ArgVT = Node->getOperand(i).getValueType();
2049 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2050 Entry.Node = Node->getOperand(i);
2051 Entry.Ty = ArgTy;
2052 Entry.isSExt = isSigned;
2053 Entry.isZExt = !isSigned;
2054 Args.push_back(Entry);
2055 }
2056 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2057 TLI.getPointerTy());
2058
2059 // Splice the libcall in wherever FindInputOutputChains tells us to.
2060 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2061 std::pair<SDValue, SDValue> CallInfo =
2062 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002063 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002064 /*isReturnValueUsed=*/true,
2065 Callee, Args, DAG, Node->getDebugLoc());
2066
2067 // Legalize the call sequence, starting with the chain. This will advance
2068 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2069 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2070 LegalizeOp(CallInfo.second);
2071 return CallInfo;
2072}
2073
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002074SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2075 RTLIB::Libcall Call_F32,
2076 RTLIB::Libcall Call_F64,
2077 RTLIB::Libcall Call_F80,
2078 RTLIB::Libcall Call_PPCF128) {
2079 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002081 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 case MVT::f32: LC = Call_F32; break;
2083 case MVT::f64: LC = Call_F64; break;
2084 case MVT::f80: LC = Call_F80; break;
2085 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002086 }
2087 return ExpandLibCall(LC, Node, false);
2088}
2089
2090SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002091 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002092 RTLIB::Libcall Call_I16,
2093 RTLIB::Libcall Call_I32,
2094 RTLIB::Libcall Call_I64,
2095 RTLIB::Libcall Call_I128) {
2096 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002098 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002099 case MVT::i8: LC = Call_I8; break;
2100 case MVT::i16: LC = Call_I16; break;
2101 case MVT::i32: LC = Call_I32; break;
2102 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002104 }
2105 return ExpandLibCall(LC, Node, isSigned);
2106}
2107
Chris Lattner22cde6a2006-01-28 08:25:58 +00002108/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2109/// INT_TO_FP operation of the specified operand when the target requests that
2110/// we expand it. At this point, we know that the result and operand types are
2111/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002112SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2113 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002114 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002115 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002116 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002117 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002118
Chris Lattner23594d42008-01-16 07:03:22 +00002119 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002120 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Chris Lattner22cde6a2006-01-28 08:25:58 +00002122 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002124 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002125 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002126 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002127 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002128 if (TLI.isLittleEndian())
2129 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002130
Chris Lattner22cde6a2006-01-28 08:25:58 +00002131 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002132 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002133 if (isSigned) {
2134 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2136 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002137 } else {
2138 Op0Mapped = Op0;
2139 }
2140 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002141 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002142 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002143 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002144 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002146 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002147 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2148 MachinePointerInfo(),
2149 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002150 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002151 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2152 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002153 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002154 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002155 BitsToDouble(0x4330000080000000ULL) :
2156 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002157 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002158 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002159 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002160 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002161 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002162 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002164 // do nothing
2165 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002166 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002167 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002168 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002169 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002170 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002171 }
2172 return Result;
2173 }
2174 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002175 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002176
2177 // Implementation of unsigned i64 to f64 following the algorithm in
2178 // __floatundidf in compiler_rt. This implementation has the advantage
2179 // of performing rounding correctly, both in the default rounding mode
2180 // and in all alternate rounding modes.
2181 // TODO: Generalize this for use with other types.
2182 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2183 SDValue TwoP52 =
2184 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2185 SDValue TwoP84PlusTwoP52 =
2186 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2187 SDValue TwoP84 =
2188 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2189
2190 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2191 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2192 DAG.getConstant(32, MVT::i64));
2193 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2194 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002195 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2196 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002197 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2198 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002199 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2200 }
2201
Owen Anderson3a9e7692010-10-05 17:24:05 +00002202 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002203 // TODO: Generalize this for use with other types.
2204 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002205 // For unsigned conversions, convert them to signed conversions using the
2206 // algorithm from the x86_64 __floatundidf in compiler_rt.
2207 if (!isSigned) {
2208 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002209
Owen Anderson3a9e7692010-10-05 17:24:05 +00002210 SDValue ShiftConst = DAG.getConstant(1, TLI.getShiftAmountTy());
2211 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2212 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2213 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2214 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002215
Owen Anderson3a9e7692010-10-05 17:24:05 +00002216 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2217 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002218
Owen Anderson3a9e7692010-10-05 17:24:05 +00002219 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002220 // select. We happen to get lucky and machinesink does the right
2221 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002222 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002223 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002224 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2225 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2226 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002227
Owen Anderson3a9e7692010-10-05 17:24:05 +00002228 // Otherwise, implement the fully general conversion.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002229 EVT SHVT = TLI.getShiftAmountTy();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002230
Jim Grosbach6e992612010-07-02 17:41:59 +00002231 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002232 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2233 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2234 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002235 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002236 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2237 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2238 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2239 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2240 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2241 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002242 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002243 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002244
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002245 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2246 DAG.getConstant(32, SHVT));
2247 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2248 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2249 SDValue TwoP32 =
2250 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2251 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2252 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2253 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2254 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2255 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2256 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002257 }
2258
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002259 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002260
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002261 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2262 Op0, DAG.getConstant(0, Op0.getValueType()),
2263 ISD::SETLT);
2264 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2265 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2266 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002267
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002268 // If the sign bit of the integer is set, the large number will be treated
2269 // as a negative number. To counteract this, the dynamic code adds an
2270 // offset depending on the data type.
2271 uint64_t FF;
2272 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002273 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002274 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2275 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2276 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2277 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2278 }
2279 if (TLI.isLittleEndian()) FF <<= 32;
2280 Constant *FudgeFactor = ConstantInt::get(
2281 Type::getInt64Ty(*DAG.getContext()), FF);
2282
2283 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2284 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2285 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2286 Alignment = std::min(Alignment, 4u);
2287 SDValue FudgeInReg;
2288 if (DestVT == MVT::f32)
2289 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002290 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002291 false, false, Alignment);
2292 else {
2293 FudgeInReg =
Evan Chengbcc80172010-07-07 22:15:37 +00002294 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002295 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002296 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002297 MVT::f32, false, false, Alignment));
2298 }
2299
2300 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002301}
2302
2303/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2304/// *INT_TO_FP 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 UINT_TO_FP or SINT_TO_FP
2307/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002308SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(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 *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002313 EVT NewInTy = LegalOp.getValueType();
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 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002320 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002321
2322 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002323 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2324 OpToUse = ISD::SINT_TO_FP;
2325 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002326 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002327 if (isSigned) continue;
2328
2329 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002330 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2331 OpToUse = ISD::UINT_TO_FP;
2332 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002333 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002334
2335 // Otherwise, try a larger type.
2336 }
2337
2338 // Okay, we found the operation and type to use. Zero extend our input to the
2339 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002340 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002341 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002342 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002343}
2344
2345/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2346/// FP_TO_*INT operation of the specified operand when the target requests that
2347/// we promote it. At this point, we know that the result and operand types are
2348/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2349/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002350SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002351 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002352 bool isSigned,
2353 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002354 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002355 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002356
2357 unsigned OpToUse = 0;
2358
2359 // Scan for the appropriate larger type to use.
2360 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002361 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002362 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002363
Eli Friedman3be2e512009-05-28 03:06:16 +00002364 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002365 OpToUse = ISD::FP_TO_SINT;
2366 break;
2367 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002368
Eli Friedman3be2e512009-05-28 03:06:16 +00002369 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002370 OpToUse = ISD::FP_TO_UINT;
2371 break;
2372 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002373
2374 // Otherwise, try a larger type.
2375 }
2376
Scott Michelfdc40a02009-02-17 22:15:04 +00002377
Chris Lattner27a6c732007-11-24 07:07:01 +00002378 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002379 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002380
Chris Lattner27a6c732007-11-24 07:07:01 +00002381 // Truncate the result of the extended FP_TO_*INT operation to the desired
2382 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002383 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002384}
2385
2386/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2387///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002388SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002389 EVT VT = Op.getValueType();
2390 EVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002391 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002392 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002393 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002394 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002395 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2396 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2397 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002398 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002399 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2400 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2401 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2402 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2403 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2404 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2405 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2406 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2407 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002409 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2410 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2411 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2412 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2413 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2414 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2415 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2416 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2417 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2418 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2419 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2420 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2421 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2422 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2423 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2424 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2425 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2426 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2427 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2428 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2429 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002430 }
2431}
2432
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002433/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002434// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002435static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2436 APInt Val = APInt(NumBits, ByteVal);
2437 unsigned Shift = 8;
2438 for (unsigned i = NumBits; i > 8; i >>= 1) {
2439 Val = (Val << Shift) | Val;
2440 Shift <<= 1;
2441 }
2442 return Val;
2443}
2444
Chris Lattner22cde6a2006-01-28 08:25:58 +00002445/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2446///
Scott Michelfdc40a02009-02-17 22:15:04 +00002447SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002448 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002449 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002450 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002451 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002452 EVT VT = Op.getValueType();
2453 EVT ShVT = TLI.getShiftAmountTy();
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002454 unsigned Len = VT.getSizeInBits();
2455
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002456 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2457 "CTPOP not implemented for this type.");
2458
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002459 // This is the "best" algorithm from
2460 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2461
2462 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2463 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2464 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2465 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2466
2467 // v = v - ((v >> 1) & 0x55555555...)
2468 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2469 DAG.getNode(ISD::AND, dl, VT,
2470 DAG.getNode(ISD::SRL, dl, VT, Op,
2471 DAG.getConstant(1, ShVT)),
2472 Mask55));
2473 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2474 Op = DAG.getNode(ISD::ADD, dl, VT,
2475 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2476 DAG.getNode(ISD::AND, dl, VT,
2477 DAG.getNode(ISD::SRL, dl, VT, Op,
2478 DAG.getConstant(2, ShVT)),
2479 Mask33));
2480 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2481 Op = DAG.getNode(ISD::AND, dl, VT,
2482 DAG.getNode(ISD::ADD, dl, VT, Op,
2483 DAG.getNode(ISD::SRL, dl, VT, Op,
2484 DAG.getConstant(4, ShVT))),
2485 Mask0F);
2486 // v = (v * 0x01010101...) >> (Len - 8)
2487 Op = DAG.getNode(ISD::SRL, dl, VT,
2488 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2489 DAG.getConstant(Len - 8, ShVT));
2490
Chris Lattner22cde6a2006-01-28 08:25:58 +00002491 return Op;
2492 }
2493 case ISD::CTLZ: {
2494 // for now, we do this:
2495 // x = x | (x >> 1);
2496 // x = x | (x >> 2);
2497 // ...
2498 // x = x | (x >>16);
2499 // x = x | (x >>32); // for 64-bit input
2500 // return popcount(~x);
2501 //
2502 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002503 EVT VT = Op.getValueType();
2504 EVT ShVT = TLI.getShiftAmountTy();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002505 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002506 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002507 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002508 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002509 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002510 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002511 Op = DAG.getNOT(dl, Op, VT);
2512 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002513 }
2514 case ISD::CTTZ: {
2515 // for now, we use: { return popcount(~x & (x - 1)); }
2516 // unless the target has ctlz but not ctpop, in which case we use:
2517 // { return 32 - nlz(~x & (x-1)); }
2518 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002519 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002520 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2521 DAG.getNOT(dl, Op, VT),
2522 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002523 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002524 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002525 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2526 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002527 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002528 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002529 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2530 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002531 }
2532 }
2533}
Chris Lattnere34b3962005-01-19 04:19:40 +00002534
Jim Grosbache03262f2010-06-18 21:43:38 +00002535std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2536 unsigned Opc = Node->getOpcode();
2537 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2538 RTLIB::Libcall LC;
2539
2540 switch (Opc) {
2541 default:
2542 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2543 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002544 case ISD::ATOMIC_SWAP:
2545 switch (VT.SimpleTy) {
2546 default: llvm_unreachable("Unexpected value type for atomic!");
2547 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2548 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2549 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2550 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2551 }
2552 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002553 case ISD::ATOMIC_CMP_SWAP:
2554 switch (VT.SimpleTy) {
2555 default: llvm_unreachable("Unexpected value type for atomic!");
2556 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2557 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2558 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2559 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2560 }
2561 break;
2562 case ISD::ATOMIC_LOAD_ADD:
2563 switch (VT.SimpleTy) {
2564 default: llvm_unreachable("Unexpected value type for atomic!");
2565 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2566 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2567 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2568 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2569 }
2570 break;
2571 case ISD::ATOMIC_LOAD_SUB:
2572 switch (VT.SimpleTy) {
2573 default: llvm_unreachable("Unexpected value type for atomic!");
2574 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2575 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2576 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2577 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2578 }
2579 break;
2580 case ISD::ATOMIC_LOAD_AND:
2581 switch (VT.SimpleTy) {
2582 default: llvm_unreachable("Unexpected value type for atomic!");
2583 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2584 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2585 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2586 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2587 }
2588 break;
2589 case ISD::ATOMIC_LOAD_OR:
2590 switch (VT.SimpleTy) {
2591 default: llvm_unreachable("Unexpected value type for atomic!");
2592 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2593 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2594 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2595 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2596 }
2597 break;
2598 case ISD::ATOMIC_LOAD_XOR:
2599 switch (VT.SimpleTy) {
2600 default: llvm_unreachable("Unexpected value type for atomic!");
2601 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2602 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2603 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2604 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2605 }
2606 break;
2607 case ISD::ATOMIC_LOAD_NAND:
2608 switch (VT.SimpleTy) {
2609 default: llvm_unreachable("Unexpected value type for atomic!");
2610 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2611 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2612 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2613 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2614 }
2615 break;
2616 }
2617
2618 return ExpandChainLibCall(LC, Node, false);
2619}
2620
Eli Friedman8c377c72009-05-27 01:25:56 +00002621void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2622 SmallVectorImpl<SDValue> &Results) {
2623 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002624 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002625 switch (Node->getOpcode()) {
2626 case ISD::CTPOP:
2627 case ISD::CTLZ:
2628 case ISD::CTTZ:
2629 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2630 Results.push_back(Tmp1);
2631 break;
2632 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002633 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002634 break;
2635 case ISD::FRAMEADDR:
2636 case ISD::RETURNADDR:
2637 case ISD::FRAME_TO_ARGS_OFFSET:
2638 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2639 break;
2640 case ISD::FLT_ROUNDS_:
2641 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2642 break;
2643 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002644 case ISD::EH_LABEL:
2645 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002646 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002647 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002648 case ISD::EH_SJLJ_DISPATCHSETUP:
2649 // If the target didn't expand these, there's nothing to do, so just
2650 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002651 Results.push_back(Node->getOperand(0));
2652 break;
2653 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002654 // If the target didn't expand this, just return 'zero' and preserve the
2655 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002656 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002657 Results.push_back(Node->getOperand(0));
2658 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002659 case ISD::MEMBARRIER: {
2660 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2661 TargetLowering::ArgListTy Args;
2662 std::pair<SDValue, SDValue> CallResult =
2663 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002664 false, false, false, false, 0, CallingConv::C,
2665 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002666 /*isReturnValueUsed=*/true,
2667 DAG.getExternalSymbol("__sync_synchronize",
2668 TLI.getPointerTy()),
2669 Args, DAG, dl);
2670 Results.push_back(CallResult.second);
2671 break;
2672 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002673 // By default, atomic intrinsics are marked Legal and lowered. Targets
2674 // which don't support them directly, however, may want libcalls, in which
2675 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002676 case ISD::ATOMIC_SWAP:
2677 case ISD::ATOMIC_LOAD_ADD:
2678 case ISD::ATOMIC_LOAD_SUB:
2679 case ISD::ATOMIC_LOAD_AND:
2680 case ISD::ATOMIC_LOAD_OR:
2681 case ISD::ATOMIC_LOAD_XOR:
2682 case ISD::ATOMIC_LOAD_NAND:
2683 case ISD::ATOMIC_LOAD_MIN:
2684 case ISD::ATOMIC_LOAD_MAX:
2685 case ISD::ATOMIC_LOAD_UMIN:
2686 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002687 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002688 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2689 Results.push_back(Tmp.first);
2690 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002691 break;
Evan Chenga8457062010-06-18 22:01:37 +00002692 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002693 case ISD::DYNAMIC_STACKALLOC:
2694 ExpandDYNAMIC_STACKALLOC(Node, Results);
2695 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002696 case ISD::MERGE_VALUES:
2697 for (unsigned i = 0; i < Node->getNumValues(); i++)
2698 Results.push_back(Node->getOperand(i));
2699 break;
2700 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002701 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002702 if (VT.isInteger())
2703 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002704 else {
2705 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002706 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002707 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002708 break;
2709 }
2710 case ISD::TRAP: {
2711 // If this operation is not supported, lower it to 'abort()' call
2712 TargetLowering::ArgListTy Args;
2713 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002714 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002715 false, false, false, false, 0, CallingConv::C,
2716 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002717 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002718 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002719 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002720 Results.push_back(CallResult.second);
2721 break;
2722 }
2723 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002724 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002725 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2726 Node->getValueType(0), dl);
2727 Results.push_back(Tmp1);
2728 break;
2729 case ISD::FP_EXTEND:
2730 Tmp1 = EmitStackConvert(Node->getOperand(0),
2731 Node->getOperand(0).getValueType(),
2732 Node->getValueType(0), dl);
2733 Results.push_back(Tmp1);
2734 break;
2735 case ISD::SIGN_EXTEND_INREG: {
2736 // NOTE: we could fall back on load/store here too for targets without
2737 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002738 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002739 EVT VT = Node->getValueType(0);
2740 EVT ShiftAmountTy = TLI.getShiftAmountTy();
Dan Gohmand1996362010-01-09 02:13:55 +00002741 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002742 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002743 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2744 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002745 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002746 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2747 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002748 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2749 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002750 break;
2751 }
2752 case ISD::FP_ROUND_INREG: {
2753 // The only way we can lower this is to turn it into a TRUNCSTORE,
2754 // EXTLOAD pair, targetting a temporary location (a stack slot).
2755
2756 // NOTE: there is a choice here between constantly creating new stack
2757 // slots and always reusing the same one. We currently always create
2758 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002759 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002760 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2761 Node->getValueType(0), dl);
2762 Results.push_back(Tmp1);
2763 break;
2764 }
2765 case ISD::SINT_TO_FP:
2766 case ISD::UINT_TO_FP:
2767 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2768 Node->getOperand(0), Node->getValueType(0), dl);
2769 Results.push_back(Tmp1);
2770 break;
2771 case ISD::FP_TO_UINT: {
2772 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002773 EVT VT = Node->getOperand(0).getValueType();
2774 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002775 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002776 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2777 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2778 Tmp1 = DAG.getConstantFP(apf, VT);
2779 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2780 Node->getOperand(0),
2781 Tmp1, ISD::SETLT);
2782 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002783 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2784 DAG.getNode(ISD::FSUB, dl, VT,
2785 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002786 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2787 DAG.getConstant(x, NVT));
2788 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2789 Results.push_back(Tmp1);
2790 break;
2791 }
Eli Friedman509150f2009-05-27 07:58:35 +00002792 case ISD::VAARG: {
2793 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002794 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002795 Tmp1 = Node->getOperand(0);
2796 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002797 unsigned Align = Node->getConstantOperandVal(3);
2798
Chris Lattnerecf42c42010-09-21 16:36:31 +00002799 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2800 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002801 SDValue VAList = VAListLoad;
2802
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002803 if (Align > TLI.getMinStackArgumentAlignment()) {
2804 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2805
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002806 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2807 DAG.getConstant(Align - 1,
2808 TLI.getPointerTy()));
2809
2810 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002811 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002812 TLI.getPointerTy()));
2813 }
2814
Eli Friedman509150f2009-05-27 07:58:35 +00002815 // Increment the pointer, VAList, to the next vaarg
2816 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2817 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002818 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002819 TLI.getPointerTy()));
2820 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002821 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2822 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002823 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002824 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002825 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002826 Results.push_back(Results[0].getValue(1));
2827 break;
2828 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002829 case ISD::VACOPY: {
2830 // This defaults to loading a pointer from the input and storing it to the
2831 // output, returning the chain.
2832 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2833 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2834 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002835 Node->getOperand(2), MachinePointerInfo(VS),
2836 false, false, 0);
2837 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2838 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002839 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002840 break;
2841 }
2842 case ISD::EXTRACT_VECTOR_ELT:
2843 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2844 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002845 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002846 Node->getOperand(0));
2847 else
2848 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2849 Results.push_back(Tmp1);
2850 break;
2851 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002852 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002853 break;
David Greenecfe33c42011-01-26 19:13:22 +00002854 case ISD::INSERT_SUBVECTOR:
2855 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2856 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002857 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002858 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002859 break;
2860 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002861 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002862 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002863 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002864 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002865 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2866 Node->getOperand(1),
2867 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002868 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002869 case ISD::VECTOR_SHUFFLE: {
2870 SmallVector<int, 8> Mask;
2871 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2872
Owen Andersone50ed302009-08-10 22:56:29 +00002873 EVT VT = Node->getValueType(0);
2874 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00002875 if (getTypeAction(EltVT) == Promote)
2876 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002877 unsigned NumElems = VT.getVectorNumElements();
2878 SmallVector<SDValue, 8> Ops;
2879 for (unsigned i = 0; i != NumElems; ++i) {
2880 if (Mask[i] < 0) {
2881 Ops.push_back(DAG.getUNDEF(EltVT));
2882 continue;
2883 }
2884 unsigned Idx = Mask[i];
2885 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002886 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2887 Node->getOperand(0),
2888 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002889 else
Bill Wendling775db972009-12-23 00:28:23 +00002890 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2891 Node->getOperand(1),
2892 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002893 }
2894 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2895 Results.push_back(Tmp1);
2896 break;
2897 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002898 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002899 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002900 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2901 // 1 -> Hi
2902 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2903 DAG.getConstant(OpTy.getSizeInBits()/2,
2904 TLI.getShiftAmountTy()));
2905 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2906 } else {
2907 // 0 -> Lo
2908 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2909 Node->getOperand(0));
2910 }
2911 Results.push_back(Tmp1);
2912 break;
2913 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002914 case ISD::STACKSAVE:
2915 // Expand to CopyFromReg if the target set
2916 // StackPointerRegisterToSaveRestore.
2917 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002918 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2919 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002920 Results.push_back(Results[0].getValue(1));
2921 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002922 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002923 Results.push_back(Node->getOperand(0));
2924 }
2925 break;
2926 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002927 // Expand to CopyToReg if the target set
2928 // StackPointerRegisterToSaveRestore.
2929 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2930 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2931 Node->getOperand(1)));
2932 } else {
2933 Results.push_back(Node->getOperand(0));
2934 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002935 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002936 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002937 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002938 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002939 case ISD::FNEG:
2940 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2941 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2942 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2943 Node->getOperand(0));
2944 Results.push_back(Tmp1);
2945 break;
2946 case ISD::FABS: {
2947 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002948 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002949 Tmp1 = Node->getOperand(0);
2950 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002951 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002952 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002953 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2954 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002955 Results.push_back(Tmp1);
2956 break;
2957 }
2958 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002959 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2960 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002961 break;
2962 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002963 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2964 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002965 break;
2966 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002967 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2968 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002969 break;
2970 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002971 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2972 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002973 break;
2974 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002975 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2976 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002977 break;
2978 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002979 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2980 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002981 break;
2982 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002983 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2984 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002985 break;
2986 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002987 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2988 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002989 break;
2990 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002991 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2992 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002993 break;
2994 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002995 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2996 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002997 break;
2998 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002999 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3000 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003001 break;
3002 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003003 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3004 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003005 break;
3006 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003007 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3008 RTLIB::NEARBYINT_F64,
3009 RTLIB::NEARBYINT_F80,
3010 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003011 break;
3012 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003013 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3014 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003015 break;
3016 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003017 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3018 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003019 break;
3020 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003021 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3022 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003023 break;
3024 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003025 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3026 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003027 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003028 case ISD::FP16_TO_FP32:
3029 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3030 break;
3031 case ISD::FP32_TO_FP16:
3032 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3033 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003034 case ISD::ConstantFP: {
3035 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003036 // Check to see if this FP immediate is already legal.
3037 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003038 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003039 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003040 else
Bill Wendling775db972009-12-23 00:28:23 +00003041 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003042 break;
3043 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003044 case ISD::EHSELECTION: {
3045 unsigned Reg = TLI.getExceptionSelectorRegister();
3046 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003047 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3048 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003049 Results.push_back(Results[0].getValue(1));
3050 break;
3051 }
3052 case ISD::EXCEPTIONADDR: {
3053 unsigned Reg = TLI.getExceptionAddressRegister();
3054 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003055 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3056 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003057 Results.push_back(Results[0].getValue(1));
3058 break;
3059 }
3060 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003061 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003062 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3063 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3064 "Don't know how to expand this subtraction!");
3065 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3066 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003067 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3068 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003069 break;
3070 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003071 case ISD::UREM:
3072 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003073 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003074 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003075 bool isSigned = Node->getOpcode() == ISD::SREM;
3076 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3077 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3078 Tmp2 = Node->getOperand(0);
3079 Tmp3 = Node->getOperand(1);
Eli Friedman3be2e512009-05-28 03:06:16 +00003080 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3081 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3082 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003083 // X % Y -> X-X/Y*Y
3084 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3085 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3086 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003087 } else if (isSigned) {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003088 Tmp1 = ExpandIntLibCall(Node, true,
3089 RTLIB::SREM_I8,
3090 RTLIB::SREM_I16, RTLIB::SREM_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003091 RTLIB::SREM_I64, RTLIB::SREM_I128);
3092 } else {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003093 Tmp1 = ExpandIntLibCall(Node, false,
3094 RTLIB::UREM_I8,
3095 RTLIB::UREM_I16, RTLIB::UREM_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003096 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003097 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003098 Results.push_back(Tmp1);
3099 break;
3100 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003101 case ISD::UDIV:
3102 case ISD::SDIV: {
3103 bool isSigned = Node->getOpcode() == ISD::SDIV;
3104 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003105 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003106 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003107 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3108 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3109 Node->getOperand(1));
3110 else if (isSigned)
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003111 Tmp1 = ExpandIntLibCall(Node, true,
3112 RTLIB::SDIV_I8,
3113 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003114 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003115 else
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003116 Tmp1 = ExpandIntLibCall(Node, false,
3117 RTLIB::UDIV_I8,
3118 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003119 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003120 Results.push_back(Tmp1);
3121 break;
3122 }
3123 case ISD::MULHU:
3124 case ISD::MULHS: {
3125 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3126 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003127 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003128 SDVTList VTs = DAG.getVTList(VT, VT);
3129 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3130 "If this wasn't legal, it shouldn't have been created!");
3131 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3132 Node->getOperand(1));
3133 Results.push_back(Tmp1.getValue(1));
3134 break;
3135 }
3136 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003137 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003138 SDVTList VTs = DAG.getVTList(VT, VT);
3139 // See if multiply or divide can be lowered using two-result operations.
3140 // We just need the low half of the multiply; try both the signed
3141 // and unsigned forms. If the target supports both SMUL_LOHI and
3142 // UMUL_LOHI, form a preference by checking which forms of plain
3143 // MULH it supports.
3144 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3145 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3146 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3147 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3148 unsigned OpToUse = 0;
3149 if (HasSMUL_LOHI && !HasMULHS) {
3150 OpToUse = ISD::SMUL_LOHI;
3151 } else if (HasUMUL_LOHI && !HasMULHU) {
3152 OpToUse = ISD::UMUL_LOHI;
3153 } else if (HasSMUL_LOHI) {
3154 OpToUse = ISD::SMUL_LOHI;
3155 } else if (HasUMUL_LOHI) {
3156 OpToUse = ISD::UMUL_LOHI;
3157 }
3158 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003159 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3160 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003161 break;
3162 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003163 Tmp1 = ExpandIntLibCall(Node, false,
3164 RTLIB::MUL_I8,
3165 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003166 RTLIB::MUL_I64, RTLIB::MUL_I128);
3167 Results.push_back(Tmp1);
3168 break;
3169 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003170 case ISD::SADDO:
3171 case ISD::SSUBO: {
3172 SDValue LHS = Node->getOperand(0);
3173 SDValue RHS = Node->getOperand(1);
3174 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3175 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3176 LHS, RHS);
3177 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003178 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003179
Eli Friedman4bc8c712009-05-27 12:20:41 +00003180 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3181
3182 // LHSSign -> LHS >= 0
3183 // RHSSign -> RHS >= 0
3184 // SumSign -> Sum >= 0
3185 //
3186 // Add:
3187 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3188 // Sub:
3189 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3190 //
3191 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3192 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3193 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3194 Node->getOpcode() == ISD::SADDO ?
3195 ISD::SETEQ : ISD::SETNE);
3196
3197 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3198 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3199
3200 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3201 Results.push_back(Cmp);
3202 break;
3203 }
3204 case ISD::UADDO:
3205 case ISD::USUBO: {
3206 SDValue LHS = Node->getOperand(0);
3207 SDValue RHS = Node->getOperand(1);
3208 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3209 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3210 LHS, RHS);
3211 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003212 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3213 Node->getOpcode () == ISD::UADDO ?
3214 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003215 break;
3216 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003217 case ISD::UMULO:
3218 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003219 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003220 SDValue LHS = Node->getOperand(0);
3221 SDValue RHS = Node->getOperand(1);
3222 SDValue BottomHalf;
3223 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003224 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003225 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3226 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3227 bool isSigned = Node->getOpcode() == ISD::SMULO;
3228 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3229 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3230 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3231 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3232 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3233 RHS);
3234 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003235 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3236 VT.getSizeInBits() * 2))) {
Owen Anderson23b9b192009-08-12 00:36:31 +00003237 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003238 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3239 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3240 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3241 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3242 DAG.getIntPtrConstant(0));
3243 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3244 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003245 } else {
3246 // We can fall back to a libcall with an illegal type for the MUL if we
3247 // have a libcall big enough.
3248 // Also, we can fall back to a division in some cases, but that's a big
3249 // performance hit in the general case.
3250 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3251 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3252 if (WideVT == MVT::i16)
3253 LC = RTLIB::MUL_I16;
3254 else if (WideVT == MVT::i32)
3255 LC = RTLIB::MUL_I32;
3256 else if (WideVT == MVT::i64)
3257 LC = RTLIB::MUL_I64;
3258 else if (WideVT == MVT::i128)
3259 LC = RTLIB::MUL_I128;
3260 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3261 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3262 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3263
Eric Christophere3385812011-01-20 01:29:23 +00003264 SDValue Ret = ExpandLibCall(LC, Node, isSigned);
Eric Christopher38a18262011-01-20 00:29:24 +00003265 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Ret);
3266 TopHalf = DAG.getNode(ISD::SRL, dl, Ret.getValueType(), Ret,
3267 DAG.getConstant(VT.getSizeInBits(), TLI.getPointerTy()));
3268 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, TopHalf);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003269 }
3270 if (isSigned) {
3271 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, TLI.getShiftAmountTy());
3272 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3273 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3274 ISD::SETNE);
3275 } else {
3276 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3277 DAG.getConstant(0, VT), ISD::SETNE);
3278 }
3279 Results.push_back(BottomHalf);
3280 Results.push_back(TopHalf);
3281 break;
3282 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003283 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003284 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003285 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3286 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003287 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003288 DAG.getConstant(PairTy.getSizeInBits()/2,
3289 TLI.getShiftAmountTy()));
Bill Wendling775db972009-12-23 00:28:23 +00003290 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003291 break;
3292 }
Eli Friedman509150f2009-05-27 07:58:35 +00003293 case ISD::SELECT:
3294 Tmp1 = Node->getOperand(0);
3295 Tmp2 = Node->getOperand(1);
3296 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003297 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003298 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3299 Tmp2, Tmp3,
3300 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003301 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003302 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3303 DAG.getConstant(0, Tmp1.getValueType()),
3304 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003305 }
Eli Friedman509150f2009-05-27 07:58:35 +00003306 Results.push_back(Tmp1);
3307 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003308 case ISD::BR_JT: {
3309 SDValue Chain = Node->getOperand(0);
3310 SDValue Table = Node->getOperand(1);
3311 SDValue Index = Node->getOperand(2);
3312
Owen Andersone50ed302009-08-10 22:56:29 +00003313 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003314
3315 const TargetData &TD = *TLI.getTargetData();
3316 unsigned EntrySize =
3317 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003318
Chris Lattner071c62f2010-01-25 23:26:13 +00003319 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003320 Index, DAG.getConstant(EntrySize, PTy));
3321 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3322
Owen Anderson23b9b192009-08-12 00:36:31 +00003323 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Evan Chengbcc80172010-07-07 22:15:37 +00003324 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, dl, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003325 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003326 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003327 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003328 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003329 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003330 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003331 // RelocBase can be JumpTable, GOT or some sort of global base.
3332 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3333 TLI.getPICJumpTableRelocBase(Table, DAG));
3334 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003336 Results.push_back(Tmp1);
3337 break;
3338 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003339 case ISD::BRCOND:
3340 // Expand brcond's setcc into its constituent parts and create a BR_CC
3341 // Node.
3342 Tmp1 = Node->getOperand(0);
3343 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003344 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003346 Tmp1, Tmp2.getOperand(2),
3347 Tmp2.getOperand(0), Tmp2.getOperand(1),
3348 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003349 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003351 DAG.getCondCode(ISD::SETNE), Tmp2,
3352 DAG.getConstant(0, Tmp2.getValueType()),
3353 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003354 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003355 Results.push_back(Tmp1);
3356 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003357 case ISD::SETCC: {
3358 Tmp1 = Node->getOperand(0);
3359 Tmp2 = Node->getOperand(1);
3360 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003361 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003362
3363 // If we expanded the SETCC into an AND/OR, return the new node
3364 if (Tmp2.getNode() == 0) {
3365 Results.push_back(Tmp1);
3366 break;
3367 }
3368
3369 // Otherwise, SETCC for the given comparison type must be completely
3370 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003371 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003372 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3373 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3374 Results.push_back(Tmp1);
3375 break;
3376 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003377 case ISD::SELECT_CC: {
3378 Tmp1 = Node->getOperand(0); // LHS
3379 Tmp2 = Node->getOperand(1); // RHS
3380 Tmp3 = Node->getOperand(2); // True
3381 Tmp4 = Node->getOperand(3); // False
3382 SDValue CC = Node->getOperand(4);
3383
3384 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003385 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003386
3387 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3388 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3389 CC = DAG.getCondCode(ISD::SETNE);
3390 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3391 Tmp3, Tmp4, CC);
3392 Results.push_back(Tmp1);
3393 break;
3394 }
3395 case ISD::BR_CC: {
3396 Tmp1 = Node->getOperand(0); // Chain
3397 Tmp2 = Node->getOperand(2); // LHS
3398 Tmp3 = Node->getOperand(3); // RHS
3399 Tmp4 = Node->getOperand(1); // CC
3400
3401 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003402 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003403 LastCALLSEQ_END = DAG.getEntryNode();
3404
3405 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3406 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3407 Tmp4 = DAG.getCondCode(ISD::SETNE);
3408 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3409 Tmp3, Node->getOperand(4));
3410 Results.push_back(Tmp1);
3411 break;
3412 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003413 case ISD::GLOBAL_OFFSET_TABLE:
3414 case ISD::GlobalAddress:
3415 case ISD::GlobalTLSAddress:
3416 case ISD::ExternalSymbol:
3417 case ISD::ConstantPool:
3418 case ISD::JumpTable:
3419 case ISD::INTRINSIC_W_CHAIN:
3420 case ISD::INTRINSIC_WO_CHAIN:
3421 case ISD::INTRINSIC_VOID:
3422 // FIXME: Custom lowering for these operations shouldn't return null!
3423 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3424 Results.push_back(SDValue(Node, i));
3425 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003426 }
3427}
3428void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3429 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003430 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003431 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003432 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003433 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003434 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003435 }
Owen Andersone50ed302009-08-10 22:56:29 +00003436 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003437 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003438 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003439 switch (Node->getOpcode()) {
3440 case ISD::CTTZ:
3441 case ISD::CTLZ:
3442 case ISD::CTPOP:
3443 // Zero extend the argument.
3444 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3445 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003446 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003447 if (Node->getOpcode() == ISD::CTTZ) {
3448 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003449 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003450 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3451 ISD::SETEQ);
3452 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3453 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3454 } else if (Node->getOpcode() == ISD::CTLZ) {
3455 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3456 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3457 DAG.getConstant(NVT.getSizeInBits() -
3458 OVT.getSizeInBits(), NVT));
3459 }
Bill Wendling775db972009-12-23 00:28:23 +00003460 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003461 break;
3462 case ISD::BSWAP: {
3463 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003464 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003465 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3466 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3467 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3468 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003469 break;
3470 }
3471 case ISD::FP_TO_UINT:
3472 case ISD::FP_TO_SINT:
3473 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3474 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3475 Results.push_back(Tmp1);
3476 break;
3477 case ISD::UINT_TO_FP:
3478 case ISD::SINT_TO_FP:
3479 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3480 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3481 Results.push_back(Tmp1);
3482 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003483 case ISD::AND:
3484 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003485 case ISD::XOR: {
3486 unsigned ExtOp, TruncOp;
3487 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003488 ExtOp = ISD::BITCAST;
3489 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003490 } else {
3491 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003492 ExtOp = ISD::ANY_EXTEND;
3493 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003494 }
3495 // Promote each of the values to the new type.
3496 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3497 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3498 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003499 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3500 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003501 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003502 }
3503 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003504 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003505 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003506 ExtOp = ISD::BITCAST;
3507 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003508 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003509 ExtOp = ISD::ANY_EXTEND;
3510 TruncOp = ISD::TRUNCATE;
3511 } else {
3512 ExtOp = ISD::FP_EXTEND;
3513 TruncOp = ISD::FP_ROUND;
3514 }
3515 Tmp1 = Node->getOperand(0);
3516 // Promote each of the values to the new type.
3517 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3518 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3519 // Perform the larger operation, then round down.
3520 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3521 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003522 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003523 else
Bill Wendling775db972009-12-23 00:28:23 +00003524 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003525 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003526 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003527 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003528 }
Eli Friedman509150f2009-05-27 07:58:35 +00003529 case ISD::VECTOR_SHUFFLE: {
3530 SmallVector<int, 8> Mask;
3531 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3532
3533 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003534 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3535 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003536
3537 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003538 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003539 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003540 Results.push_back(Tmp1);
3541 break;
3542 }
Eli Friedmanad754602009-05-28 03:56:57 +00003543 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003544 unsigned ExtOp = ISD::FP_EXTEND;
3545 if (NVT.isInteger()) {
3546 ISD::CondCode CCCode =
3547 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3548 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003549 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003550 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3551 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003552 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3553 Tmp1, Tmp2, Node->getOperand(2)));
3554 break;
3555 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003556 }
3557}
3558
Chris Lattner3e928bb2005-01-07 07:47:09 +00003559// SelectionDAG::Legalize - This is the entry point for the file.
3560//
Dan Gohman714efc62009-12-05 17:51:33 +00003561void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003562 /// run - This is the main entry point to this class.
3563 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003564 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003565}
3566