blob: 94080a04e39efb70587413b50bc91271b5eb17ca [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));
Owen Anderson95771af2011-02-25 21:41:48 +000090
Devang Patela778f5c2011-02-18 22:43:42 +000091 // 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)
Stuart Hastingsa9011292011-02-16 16:23:55 +0000400 return DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT,
Evan Chengbcc80172010-07-07 22:15:37 +0000401 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.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000477 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, 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.
Owen Anderson95771af2011-02-25 21:41:48 +0000501 SDValue ShiftAmount = DAG.getConstant(NumBits,
502 TLI.getShiftAmountTy(Val.getValueType()));
Dan Gohman475871a2008-07-27 21:46:04 +0000503 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000504 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000505
506 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000507 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000508 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000509 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000510 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000511 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000512 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000513 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000514 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000515 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000516 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
517 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000518
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000520}
521
522/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
523static
Dan Gohman475871a2008-07-27 21:46:04 +0000524SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000525 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000526 SDValue Chain = LD->getChain();
527 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000528 EVT VT = LD->getValueType(0);
529 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000530 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000531 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000532 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000533 if (TLI.isTypeLegal(intVT)) {
534 // Expand to a (misaligned) integer load of the same size,
535 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000536 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
537 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000538 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000539 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000540 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000541 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000542
Duncan Sands05e11fa2008-12-12 21:47:02 +0000543 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000544 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000545 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000546
Chris Lattnerecf42c42010-09-21 16:36:31 +0000547 // Copy the value to a (aligned) stack slot using (unaligned) integer
548 // loads and stores, then do a (aligned) load from the stack slot.
549 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
550 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
551 unsigned RegBytes = RegVT.getSizeInBits() / 8;
552 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
553
554 // Make sure the stack slot is also aligned for the register type.
555 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
556
557 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
558 SmallVector<SDValue, 8> Stores;
559 SDValue StackPtr = StackBase;
560 unsigned Offset = 0;
561
562 // Do all but one copies using the full register width.
563 for (unsigned i = 1; i < NumRegs; i++) {
564 // Load one integer register's worth from the original location.
565 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
566 LD->getPointerInfo().getWithOffset(Offset),
567 LD->isVolatile(), LD->isNonTemporal(),
568 MinAlign(LD->getAlignment(), Offset));
569 // Follow the load with a store to the stack slot. Remember the store.
570 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000571 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000572 // Increment the pointers.
573 Offset += RegBytes;
574 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
575 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
576 Increment);
577 }
578
579 // The last copy may be partial. Do an extending load.
580 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
581 8 * (LoadedBytes - Offset));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000582 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000583 LD->getPointerInfo().getWithOffset(Offset),
584 MemVT, LD->isVolatile(),
585 LD->isNonTemporal(),
586 MinAlign(LD->getAlignment(), Offset));
587 // Follow the load with a store to the stack slot. Remember the store.
588 // On big-endian machines this requires a truncating store to ensure
589 // that the bits end up in the right place.
590 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
591 MachinePointerInfo(), MemVT,
592 false, false, 0));
593
594 // The order of the stores doesn't matter - say it with a TokenFactor.
595 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
596 Stores.size());
597
598 // Finally, perform the original load only redirected to the stack slot.
Stuart Hastingsa9011292011-02-16 16:23:55 +0000599 Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000600 MachinePointerInfo(), LoadedVT, false, false, 0);
601
602 // Callers expect a MERGE_VALUES node.
603 SDValue Ops[] = { Load, TF };
604 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000605 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000606 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000607 "Unaligned load of unsupported type.");
608
Dale Johannesen8155d642008-02-27 22:36:00 +0000609 // Compute the new VT that is half the size of the old one. This is an
610 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000611 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000612 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000613 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000614 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000615
Chris Lattnere400af82007-11-19 21:38:03 +0000616 unsigned Alignment = LD->getAlignment();
617 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 ISD::LoadExtType HiExtType = LD->getExtensionType();
619
620 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
621 if (HiExtType == ISD::NON_EXTLOAD)
622 HiExtType = ISD::ZEXTLOAD;
623
624 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000625 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000626 if (TLI.isLittleEndian()) {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000627 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000628 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000629 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000630 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000631 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000632 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000633 LD->getPointerInfo().getWithOffset(IncrementSize),
634 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000635 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000636 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +0000637 Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(),
Chris Lattnerecf42c42010-09-21 16:36:31 +0000638 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000639 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000640 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000641 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000642 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000643 LD->getPointerInfo().getWithOffset(IncrementSize),
644 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000645 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000646 }
647
648 // aggregate the two parts
Owen Anderson95771af2011-02-25 21:41:48 +0000649 SDValue ShiftAmount = DAG.getConstant(NumBits,
650 TLI.getShiftAmountTy(Hi.getValueType()));
Dale Johannesenbb5da912009-02-02 20:41:04 +0000651 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
652 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000655 Hi.getValue(1));
656
Dan Gohman475871a2008-07-27 21:46:04 +0000657 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000658 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000659}
Evan Cheng912095b2007-01-04 21:56:39 +0000660
Nate Begeman68679912008-04-25 18:07:40 +0000661/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
662/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
663/// is necessary to spill the vector being inserted into to memory, perform
664/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000665SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000666PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
667 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000668 SDValue Tmp1 = Vec;
669 SDValue Tmp2 = Val;
670 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000671
Nate Begeman68679912008-04-25 18:07:40 +0000672 // If the target doesn't support this, we have to spill the input vector
673 // to a temporary stack slot, update the element, then reload it. This is
674 // badness. We could also load the value into a vector register (either
675 // with a "move to register" or "extload into register" instruction, then
676 // permute it into place, if the idx is a constant and if the idx is
677 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000678 EVT VT = Tmp1.getValueType();
679 EVT EltVT = VT.getVectorElementType();
680 EVT IdxVT = Tmp3.getValueType();
681 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000682 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000683
Evan Chengff89dcb2009-10-18 18:16:27 +0000684 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
685
Nate Begeman68679912008-04-25 18:07:40 +0000686 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000687 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000688 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000689 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000690
691 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000692 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000693 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000694 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000695 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000696 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
697 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000698 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000699 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000700 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000701 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000702 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000703 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000704}
705
Mon P Wange9f10152008-12-09 05:46:39 +0000706
Eli Friedman3f727d62009-05-27 02:16:40 +0000707SDValue SelectionDAGLegalize::
708ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
709 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
710 // SCALAR_TO_VECTOR requires that the type of the value being inserted
711 // match the element type of the vector being created, except for
712 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000713 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000714 if (Val.getValueType() == EltVT ||
715 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
716 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
717 Vec.getValueType(), Val);
718
719 unsigned NumElts = Vec.getValueType().getVectorNumElements();
720 // We generate a shuffle of InVec and ScVec, so the shuffle mask
721 // should be 0,1,2,3,4,5... with the appropriate element replaced with
722 // elt 0 of the RHS.
723 SmallVector<int, 8> ShufOps;
724 for (unsigned i = 0; i != NumElts; ++i)
725 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
726
727 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
728 &ShufOps[0]);
729 }
730 }
731 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
732}
733
Eli Friedman7ef3d172009-06-06 07:04:42 +0000734SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
735 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
736 // FIXME: We shouldn't do this for TargetConstantFP's.
737 // FIXME: move this to the DAG Combiner! Note that we can't regress due
738 // to phase ordering between legalized code and the dag combiner. This
739 // probably means that we need to integrate dag combiner and legalizer
740 // together.
741 // We generally can't do this one for long doubles.
742 SDValue Tmp1 = ST->getChain();
743 SDValue Tmp2 = ST->getBasePtr();
744 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000745 unsigned Alignment = ST->getAlignment();
746 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000747 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000748 DebugLoc dl = ST->getDebugLoc();
749 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 if (CFP->getValueType(0) == MVT::f32 &&
751 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000752 Tmp3 = DAG.getConstant(CFP->getValueAPF().
753 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000755 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
756 isVolatile, isNonTemporal, Alignment);
757 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000759 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000760 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000762 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000763 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000764 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
765 isVolatile, isNonTemporal, Alignment);
766 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000768 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000769 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
770 // stores. If the target supports neither 32- nor 64-bits, this
771 // xform is certainly not worth it.
772 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000773 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000775 if (TLI.isBigEndian()) std::swap(Lo, Hi);
776
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000777 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
778 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000779 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
780 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000781 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
782 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000783 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000784
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000786 }
787 }
788 }
789 return SDValue();
790}
791
Dan Gohmana3466152007-07-13 20:14:11 +0000792/// LegalizeOp - We know that the specified value has a legal type, and
793/// that its operands are legal. Now ensure that the operation itself
794/// is legal, recursively ensuring that the operands' operations remain
795/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000796SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000797 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
798 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000799
Gabor Greifba36cb52008-08-28 21:40:38 +0000800 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000801 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000802
Eli Friedman1fde9c52009-05-24 02:46:31 +0000803 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
804 assert(getTypeAction(Node->getValueType(i)) == Legal &&
805 "Unexpected illegal type!");
806
807 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000808 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000809 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
810 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000811
Chris Lattner45982da2005-05-12 16:53:42 +0000812 // Note that LegalizeOp may be reentered even from single-use nodes, which
813 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000814 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000815 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000816
Dan Gohman475871a2008-07-27 21:46:04 +0000817 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
818 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000819 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000820
Eli Friedman8c377c72009-05-27 01:25:56 +0000821 // Figure out the correct action; the way to query this varies by opcode
Bill Wendling6b9a2932011-01-26 22:21:35 +0000822 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
Eli Friedman8c377c72009-05-27 01:25:56 +0000823 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000824 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000825 case ISD::INTRINSIC_W_CHAIN:
826 case ISD::INTRINSIC_WO_CHAIN:
827 case ISD::INTRINSIC_VOID:
828 case ISD::VAARG:
829 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000831 break;
832 case ISD::SINT_TO_FP:
833 case ISD::UINT_TO_FP:
834 case ISD::EXTRACT_VECTOR_ELT:
835 Action = TLI.getOperationAction(Node->getOpcode(),
836 Node->getOperand(0).getValueType());
837 break;
838 case ISD::FP_ROUND_INREG:
839 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000840 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000841 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
842 break;
843 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000844 case ISD::SELECT_CC:
845 case ISD::SETCC:
846 case ISD::BR_CC: {
847 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
848 Node->getOpcode() == ISD::SETCC ? 2 : 1;
849 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000850 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000851 ISD::CondCode CCCode =
852 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
853 Action = TLI.getCondCodeAction(CCCode, OpVT);
854 if (Action == TargetLowering::Legal) {
855 if (Node->getOpcode() == ISD::SELECT_CC)
856 Action = TLI.getOperationAction(Node->getOpcode(),
857 Node->getValueType(0));
858 else
859 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
860 }
861 break;
862 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000863 case ISD::LOAD:
864 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000865 // FIXME: Model these properly. LOAD and STORE are complicated, and
866 // STORE expects the unlegalized operand in some cases.
867 SimpleFinishLegalizing = false;
868 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000869 case ISD::CALLSEQ_START:
870 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000871 // FIXME: This shouldn't be necessary. These nodes have special properties
872 // dealing with the recursive nature of legalization. Removing this
873 // special case should be done as part of making LegalizeDAG non-recursive.
874 SimpleFinishLegalizing = false;
875 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000876 case ISD::EXTRACT_ELEMENT:
877 case ISD::FLT_ROUNDS_:
878 case ISD::SADDO:
879 case ISD::SSUBO:
880 case ISD::UADDO:
881 case ISD::USUBO:
882 case ISD::SMULO:
883 case ISD::UMULO:
884 case ISD::FPOWI:
885 case ISD::MERGE_VALUES:
886 case ISD::EH_RETURN:
887 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000888 case ISD::EH_SJLJ_SETJMP:
889 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000890 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000891 // These operations lie about being legal: when they claim to be legal,
892 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000893 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
894 if (Action == TargetLowering::Legal)
895 Action = TargetLowering::Expand;
896 break;
897 case ISD::TRAMPOLINE:
898 case ISD::FRAMEADDR:
899 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000900 // These operations lie about being legal: when they claim to be legal,
901 // they should actually be custom-lowered.
902 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
903 if (Action == TargetLowering::Legal)
904 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000905 break;
906 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000907 // A weird case: legalization for BUILD_VECTOR never legalizes the
908 // operands!
909 // FIXME: This really sucks... changing it isn't semantically incorrect,
910 // but it massively pessimizes the code for floating-point BUILD_VECTORs
911 // because ConstantFP operands get legalized into constant pool loads
912 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
913 // though, because BUILD_VECTORS usually get lowered into other nodes
914 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000915 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000916 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000917 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000918 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000919 Action = TargetLowering::Legal;
920 } else {
921 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000922 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000923 break;
924 }
925
926 if (SimpleFinishLegalizing) {
927 SmallVector<SDValue, 8> Ops, ResultVals;
928 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
929 Ops.push_back(LegalizeOp(Node->getOperand(i)));
930 switch (Node->getOpcode()) {
931 default: break;
932 case ISD::BR:
933 case ISD::BRIND:
934 case ISD::BR_JT:
935 case ISD::BR_CC:
936 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000937 // Branches tweak the chain to include LastCALLSEQ_END
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Eli Friedman8c377c72009-05-27 01:25:56 +0000939 LastCALLSEQ_END);
940 Ops[0] = LegalizeOp(Ops[0]);
941 LastCALLSEQ_END = DAG.getEntryNode();
942 break;
943 case ISD::SHL:
944 case ISD::SRL:
945 case ISD::SRA:
946 case ISD::ROTL:
947 case ISD::ROTR:
948 // Legalizing shifts/rotates requires adjusting the shift amount
949 // to the appropriate width.
950 if (!Ops[1].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000951 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
952 Ops[1]));
Eli Friedman8c377c72009-05-27 01:25:56 +0000953 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000954 case ISD::SRL_PARTS:
955 case ISD::SRA_PARTS:
956 case ISD::SHL_PARTS:
957 // Legalizing shifts/rotates requires adjusting the shift amount
958 // to the appropriate width.
959 if (!Ops[2].getValueType().isVector())
Owen Anderson6154f6c2011-03-07 18:29:47 +0000960 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(),
961 Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000962 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000963 }
964
Dan Gohman027657d2010-06-18 15:30:29 +0000965 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
966 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000967 switch (Action) {
968 case TargetLowering::Legal:
969 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
970 ResultVals.push_back(Result.getValue(i));
971 break;
972 case TargetLowering::Custom:
973 // FIXME: The handling for custom lowering with multiple results is
974 // a complete mess.
975 Tmp1 = TLI.LowerOperation(Result, DAG);
976 if (Tmp1.getNode()) {
977 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
978 if (e == 1)
979 ResultVals.push_back(Tmp1);
980 else
981 ResultVals.push_back(Tmp1.getValue(i));
982 }
983 break;
984 }
985
986 // FALL THROUGH
987 case TargetLowering::Expand:
988 ExpandNode(Result.getNode(), ResultVals);
989 break;
990 case TargetLowering::Promote:
991 PromoteNode(Result.getNode(), ResultVals);
992 break;
993 }
994 if (!ResultVals.empty()) {
995 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
996 if (ResultVals[i] != SDValue(Node, i))
997 ResultVals[i] = LegalizeOp(ResultVals[i]);
998 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
999 }
1000 return ResultVals[Op.getResNo()];
1001 }
1002 }
1003
1004 switch (Node->getOpcode()) {
1005 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001006#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001007 dbgs() << "NODE: ";
1008 Node->dump( &DAG);
1009 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001010#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001011 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001012
Chris Lattnerb2827b02006-03-19 00:52:58 +00001013 case ISD::BUILD_VECTOR:
1014 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001015 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001016 case TargetLowering::Custom:
1017 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001018 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001019 Result = Tmp3;
1020 break;
1021 }
1022 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001023 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001024 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001025 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001026 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001027 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001028 case ISD::CALLSEQ_START: {
Stuart Hastingsf2243222011-01-18 00:09:27 +00001029 static int depth = 0;
Chris Lattner6831a812006-02-13 09:18:02 +00001030 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Chris Lattner6831a812006-02-13 09:18:02 +00001032 // Recursively Legalize all of the inputs of the call end that do not lead
1033 // to this call start. This ensures that any libcalls that need be inserted
1034 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001035 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001036 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001037 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001038 NodesLeadingTo);
1039 }
Chris Lattner6831a812006-02-13 09:18:02 +00001040
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001041 // Now that we have legalized all of the inputs (which may have inserted
1042 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1044
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001045 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001046 // call ended.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001047 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken && depth == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001048 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001049 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001050 Tmp1 = LegalizeOp(Tmp1);
1051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Chris Lattner6831a812006-02-13 09:18:02 +00001053 // Do not try to legalize the target-specific arguments (#1+).
1054 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001055 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001056 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001057 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1058 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001059 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Chris Lattner6831a812006-02-13 09:18:02 +00001061 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001062 AddLegalizedOperand(Op.getValue(0), Result);
1063 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1064 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Chris Lattner6831a812006-02-13 09:18:02 +00001066 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001067 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001068 // process, no libcalls can/will be inserted, guaranteeing that no calls
1069 // can overlap.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001070
1071 SDValue Saved_LastCALLSEQ_END = LastCALLSEQ_END ;
Chris Lattner6831a812006-02-13 09:18:02 +00001072 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001073 LastCALLSEQ_END = SDValue(CallEnd, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Stuart Hastingsf2243222011-01-18 00:09:27 +00001075 depth++;
Chris Lattner6831a812006-02-13 09:18:02 +00001076 // Legalize the call, starting from the CALLSEQ_END.
1077 LegalizeOp(LastCALLSEQ_END);
Stuart Hastingsf2243222011-01-18 00:09:27 +00001078 depth--;
1079 assert(depth >= 0 && "Un-matched CALLSEQ_START?");
1080 if (depth > 0)
1081 LastCALLSEQ_END = Saved_LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +00001082 return Result;
1083 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001084 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001085 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1086 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001087 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001088 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1089 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001090 assert(I != LegalizedNodes.end() &&
1091 "Legalizing the call start should have legalized this node!");
1092 return I->second;
1093 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001094
1095 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001096 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001098 // Do not try to legalize the target-specific arguments (#1+), except for
1099 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001100 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001101 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001102 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001103 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001104 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1105 &Ops[0], Ops.size()),
1106 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001107 }
1108 } else {
1109 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1110 if (Tmp1 != Node->getOperand(0) ||
1111 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001112 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001113 Ops[0] = Tmp1;
1114 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001115 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1116 &Ops[0], Ops.size()),
1117 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001118 }
Chris Lattner6a542892006-01-24 05:48:21 +00001119 }
Chris Lattner6831a812006-02-13 09:18:02 +00001120 // This finishes up call legalization.
Chris Lattner4b653a02006-02-14 00:55:02 +00001121 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001122 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001123 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001124 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001125 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001126 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001127 LoadSDNode *LD = cast<LoadSDNode>(Node);
1128 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1129 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001130
Evan Cheng466685d2006-10-09 20:57:25 +00001131 ISD::LoadExtType ExtType = LD->getExtensionType();
1132 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001133 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001134 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1135 Tmp1, Tmp2, LD->getOffset()),
1136 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001137 Tmp3 = Result.getValue(0);
1138 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001139
Evan Cheng466685d2006-10-09 20:57:25 +00001140 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001141 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001142 case TargetLowering::Legal:
1143 // If this is an unaligned load and the target doesn't support it,
1144 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001145 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001146 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1147 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001148 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001149 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001150 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001151 Tmp3 = Result.getOperand(0);
1152 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001153 Tmp3 = LegalizeOp(Tmp3);
1154 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001155 }
1156 }
1157 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001158 case TargetLowering::Custom:
1159 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001160 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001161 Tmp3 = LegalizeOp(Tmp1);
1162 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001163 }
Evan Cheng466685d2006-10-09 20:57:25 +00001164 break;
1165 case TargetLowering::Promote: {
1166 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001167 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001168 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001169 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001170
Chris Lattnerecf42c42010-09-21 16:36:31 +00001171 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001172 LD->isVolatile(), LD->isNonTemporal(),
1173 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001174 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001175 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001176 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001177 }
Evan Cheng466685d2006-10-09 20:57:25 +00001178 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001179 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001180 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001181 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1182 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001183 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001184 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001185
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001186 EVT SrcVT = LD->getMemoryVT();
1187 unsigned SrcWidth = SrcVT.getSizeInBits();
1188 unsigned Alignment = LD->getAlignment();
1189 bool isVolatile = LD->isVolatile();
1190 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001191
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001192 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1193 // Some targets pretend to have an i1 loading operation, and actually
1194 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1195 // bits are guaranteed to be zero; it helps the optimizers understand
1196 // that these bits are zero. It is also useful for EXTLOAD, since it
1197 // tells the optimizers that those bits are undefined. It would be
1198 // nice to have an effective generic way of getting these benefits...
1199 // Until such a way is found, don't insist on promoting i1 here.
1200 (SrcVT != MVT::i1 ||
1201 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1202 // Promote to a byte-sized load if not loading an integral number of
1203 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1204 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1205 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1206 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001207
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001208 // The extra bits are guaranteed to be zero, since we stored them that
1209 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001210
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001211 ISD::LoadExtType NewExtType =
1212 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001213
Stuart Hastingsa9011292011-02-16 16:23:55 +00001214 Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001215 Tmp1, Tmp2, LD->getPointerInfo(),
1216 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001217
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001218 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001219
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001220 if (ExtType == ISD::SEXTLOAD)
1221 // Having the top bits zero doesn't help when sign extending.
1222 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1223 Result.getValueType(),
1224 Result, DAG.getValueType(SrcVT));
1225 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1226 // All the top bits are guaranteed to be zero - inform the optimizers.
1227 Result = DAG.getNode(ISD::AssertZext, dl,
1228 Result.getValueType(), Result,
1229 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001230
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001231 Tmp1 = LegalizeOp(Result);
1232 Tmp2 = LegalizeOp(Ch);
1233 } else if (SrcWidth & (SrcWidth - 1)) {
1234 // If not loading a power-of-2 number of bits, expand as two loads.
1235 assert(!SrcVT.isVector() && "Unsupported extload!");
1236 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1237 assert(RoundWidth < SrcWidth);
1238 unsigned ExtraWidth = SrcWidth - RoundWidth;
1239 assert(ExtraWidth < RoundWidth);
1240 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1241 "Load size not an integral number of bytes!");
1242 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1243 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1244 SDValue Lo, Hi, Ch;
1245 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001246
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001247 if (TLI.isLittleEndian()) {
1248 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1249 // Load the bottom RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001250 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001251 Tmp1, Tmp2,
1252 LD->getPointerInfo(), RoundVT, isVolatile,
1253 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001254
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001255 // Load the remaining ExtraWidth bits.
1256 IncrementSize = RoundWidth / 8;
1257 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1258 DAG.getIntPtrConstant(IncrementSize));
Stuart Hastingsa9011292011-02-16 16:23:55 +00001259 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001260 LD->getPointerInfo().getWithOffset(IncrementSize),
1261 ExtraVT, isVolatile, isNonTemporal,
1262 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001263
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001264 // Build a factor node to remember that this load is independent of
1265 // the other one.
1266 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1267 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001268
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001269 // Move the top bits to the right place.
1270 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001271 DAG.getConstant(RoundWidth,
1272 TLI.getShiftAmountTy(Hi.getValueType())));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001273
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001274 // Join the hi and lo parts.
1275 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001276 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001277 // Big endian - avoid unaligned loads.
1278 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1279 // Load the top RoundWidth bits.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001280 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001281 LD->getPointerInfo(), RoundVT, isVolatile,
1282 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001283
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001284 // Load the remaining ExtraWidth bits.
1285 IncrementSize = RoundWidth / 8;
1286 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1287 DAG.getIntPtrConstant(IncrementSize));
1288 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
Stuart Hastingsa9011292011-02-16 16:23:55 +00001289 dl, Node->getValueType(0), Tmp1, Tmp2,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001290 LD->getPointerInfo().getWithOffset(IncrementSize),
1291 ExtraVT, isVolatile, isNonTemporal,
1292 MinAlign(Alignment, IncrementSize));
1293
1294 // Build a factor node to remember that this load is independent of
1295 // the other one.
1296 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1297 Hi.getValue(1));
1298
1299 // Move the top bits to the right place.
1300 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
Owen Anderson95771af2011-02-25 21:41:48 +00001301 DAG.getConstant(ExtraWidth,
1302 TLI.getShiftAmountTy(Hi.getValueType())));
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001303
1304 // Join the hi and lo parts.
1305 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001306 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001307
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001308 Tmp1 = LegalizeOp(Result);
1309 Tmp2 = LegalizeOp(Ch);
1310 } else {
1311 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1312 default: assert(0 && "This action is not supported yet!");
1313 case TargetLowering::Custom:
1314 isCustom = true;
1315 // FALLTHROUGH
1316 case TargetLowering::Legal:
1317 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1318 Tmp1, Tmp2, LD->getOffset()),
1319 Result.getResNo());
1320 Tmp1 = Result.getValue(0);
1321 Tmp2 = Result.getValue(1);
1322
1323 if (isCustom) {
1324 Tmp3 = TLI.LowerOperation(Result, DAG);
1325 if (Tmp3.getNode()) {
1326 Tmp1 = LegalizeOp(Tmp3);
1327 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1328 }
1329 } else {
1330 // If this is an unaligned load and the target doesn't support it,
1331 // expand it.
1332 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1333 const Type *Ty =
1334 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1335 unsigned ABIAlignment =
1336 TLI.getTargetData()->getABITypeAlignment(Ty);
1337 if (LD->getAlignment() < ABIAlignment){
1338 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1339 DAG, TLI);
1340 Tmp1 = Result.getOperand(0);
1341 Tmp2 = Result.getOperand(1);
1342 Tmp1 = LegalizeOp(Tmp1);
1343 Tmp2 = LegalizeOp(Tmp2);
1344 }
1345 }
1346 }
1347 break;
1348 case TargetLowering::Expand:
1349 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1350 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1351 LD->getPointerInfo(),
1352 LD->isVolatile(), LD->isNonTemporal(),
1353 LD->getAlignment());
1354 unsigned ExtendOp;
1355 switch (ExtType) {
1356 case ISD::EXTLOAD:
1357 ExtendOp = (SrcVT.isFloatingPoint() ?
1358 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1359 break;
1360 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1361 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1362 default: llvm_unreachable("Unexpected extend load type!");
1363 }
1364 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1365 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1366 Tmp2 = LegalizeOp(Load.getValue(1));
1367 break;
1368 }
1369 // FIXME: This does not work for vectors on most targets. Sign- and
1370 // zero-extend operations are currently folded into extending loads,
1371 // whether they are legal or not, and then we end up here without any
1372 // support for legalizing them.
1373 assert(ExtType != ISD::EXTLOAD &&
1374 "EXTLOAD should always be supported!");
1375 // Turn the unsupported load into an EXTLOAD followed by an explicit
1376 // zero/sign extend inreg.
Stuart Hastingsa9011292011-02-16 16:23:55 +00001377 Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001378 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1379 LD->isVolatile(), LD->isNonTemporal(),
1380 LD->getAlignment());
1381 SDValue ValRes;
1382 if (ExtType == ISD::SEXTLOAD)
1383 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1384 Result.getValueType(),
1385 Result, DAG.getValueType(SrcVT));
1386 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001387 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001388 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1389 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1390 break;
1391 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001392 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001393
1394 // Since loads produce two values, make sure to remember that we legalized
1395 // both of them.
1396 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1397 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1398 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001399 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001400 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001401 StoreSDNode *ST = cast<StoreSDNode>(Node);
1402 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1403 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001404 unsigned Alignment = ST->getAlignment();
1405 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001406 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001407
Evan Cheng8b2794a2006-10-13 21:14:26 +00001408 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001409 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1410 Result = SDValue(OptStore, 0);
1411 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001412 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001413
Eli Friedman957bffa2009-05-24 08:42:01 +00001414 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001415 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001416 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1417 Tmp1, Tmp3, Tmp2,
1418 ST->getOffset()),
1419 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001420
Owen Andersone50ed302009-08-10 22:56:29 +00001421 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001422 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001423 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001424 case TargetLowering::Legal:
1425 // If this is an unaligned store and the target doesn't support it,
1426 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001427 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001428 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001429 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001430 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001431 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1432 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001433 }
1434 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001435 case TargetLowering::Custom:
1436 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001437 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001438 break;
1439 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001440 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001441 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001442 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001443 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001444 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001445 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001446 break;
1447 }
1448 break;
1449 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001450 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001451 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001452
Owen Andersone50ed302009-08-10 22:56:29 +00001453 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001454 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001455
Duncan Sands83ec4b62008-06-06 12:08:01 +00001456 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001457 // Promote to a byte-sized store with upper bits zero if not
1458 // storing an integral number of bytes. For example, promote
1459 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001460 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1461 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001462 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001463 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1464 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001465 } else if (StWidth & (StWidth - 1)) {
1466 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001467 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001468 unsigned RoundWidth = 1 << Log2_32(StWidth);
1469 assert(RoundWidth < StWidth);
1470 unsigned ExtraWidth = StWidth - RoundWidth;
1471 assert(ExtraWidth < RoundWidth);
1472 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1473 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001474 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1475 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001476 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001477 unsigned IncrementSize;
1478
1479 if (TLI.isLittleEndian()) {
1480 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1481 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001482 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1483 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001484 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001485
1486 // Store the remaining ExtraWidth bits.
1487 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001488 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001489 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001490 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001491 DAG.getConstant(RoundWidth,
1492 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001493 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1494 ST->getPointerInfo().getWithOffset(IncrementSize),
1495 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001496 MinAlign(Alignment, IncrementSize));
1497 } else {
1498 // Big endian - avoid unaligned stores.
1499 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1500 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001501 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Owen Anderson95771af2011-02-25 21:41:48 +00001502 DAG.getConstant(ExtraWidth,
1503 TLI.getShiftAmountTy(Tmp3.getValueType())));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001504 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1505 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001506
1507 // Store the remaining ExtraWidth bits.
1508 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001509 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001510 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001511 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1512 ST->getPointerInfo().getWithOffset(IncrementSize),
1513 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001514 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001515 }
Duncan Sands7e857202008-01-22 07:17:34 +00001516
1517 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001518 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001519 } else {
1520 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1521 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001522 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1523 Tmp1, Tmp3, Tmp2,
1524 ST->getOffset()),
1525 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001526
1527 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001528 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001529 case TargetLowering::Legal:
1530 // If this is an unaligned store and the target doesn't support it,
1531 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001532 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001533 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001534 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001535 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001536 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1537 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001538 }
1539 break;
1540 case TargetLowering::Custom:
1541 Result = TLI.LowerOperation(Result, DAG);
1542 break;
1543 case Expand:
1544 // TRUNCSTORE:i16 i32 -> STORE i16
1545 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001546 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001547 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1548 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001549 break;
1550 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001551 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001552 }
1553 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001554 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001555 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001556 assert(Result.getValueType() == Op.getValueType() &&
1557 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattner456a93a2006-01-28 07:39:30 +00001559 // Make sure that the generated code is itself legal.
1560 if (Result != Op)
1561 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001562
Chris Lattner45982da2005-05-12 16:53:42 +00001563 // Note that LegalizeOp may be reentered even from single-use nodes, which
1564 // means that we always must cache transformed nodes.
1565 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001566 return Result;
1567}
1568
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001569SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1570 SDValue Vec = Op.getOperand(0);
1571 SDValue Idx = Op.getOperand(1);
1572 DebugLoc dl = Op.getDebugLoc();
1573 // Store the value to a temporary stack slot, then LOAD the returned part.
1574 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001575 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1576 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001577
1578 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001579 unsigned EltSize =
1580 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001581 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1582 DAG.getConstant(EltSize, Idx.getValueType()));
1583
1584 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1585 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1586 else
1587 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1588
1589 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1590
Eli Friedmanc680ac92009-07-09 22:01:03 +00001591 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001592 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001593 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00001594 return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001595 MachinePointerInfo(),
1596 Vec.getValueType().getVectorElementType(),
1597 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001598}
1599
David Greenecfe33c42011-01-26 19:13:22 +00001600SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1601 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1602
1603 SDValue Vec = Op.getOperand(0);
1604 SDValue Part = Op.getOperand(1);
1605 SDValue Idx = Op.getOperand(2);
1606 DebugLoc dl = Op.getDebugLoc();
1607
1608 // Store the value to a temporary stack slot, then LOAD the returned part.
1609
1610 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1611 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1612 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1613
1614 // First store the whole vector.
1615 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1616 false, false, 0);
1617
1618 // Then store the inserted part.
1619
1620 // Add the offset to the index.
1621 unsigned EltSize =
1622 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1623
1624 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1625 DAG.getConstant(EltSize, Idx.getValueType()));
1626
1627 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1628 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1629 else
1630 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1631
1632 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1633 StackPtr);
1634
1635 // Store the subvector.
1636 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1637 MachinePointerInfo(), false, false, 0);
1638
1639 // Finally, load the updated vector.
1640 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1641 false, false, 0);
1642}
1643
Eli Friedman7ef3d172009-06-06 07:04:42 +00001644SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1645 // We can't handle this case efficiently. Allocate a sufficiently
1646 // aligned object on the stack, store each element into it, then load
1647 // the result as a vector.
1648 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001649 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001650 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001651 DebugLoc dl = Node->getDebugLoc();
1652 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001653 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001654 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001655
1656 // Emit a store of each element to the stack slot.
1657 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001658 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001659 // Store (in the right endianness) the elements to memory.
1660 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1661 // Ignore undef elements.
1662 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1663
1664 unsigned Offset = TypeByteSize*i;
1665
1666 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1667 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1668
Dan Gohman9949dd62010-02-25 20:30:49 +00001669 // If the destination vector element type is narrower than the source
1670 // element type, only store the bits necessary.
1671 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001672 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001673 Node->getOperand(i), Idx,
1674 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001675 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001676 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001677 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001678 Node->getOperand(i), Idx,
1679 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001680 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001681 }
1682
1683 SDValue StoreChain;
1684 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001685 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001686 &Stores[0], Stores.size());
1687 else
1688 StoreChain = DAG.getEntryNode();
1689
1690 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001691 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001692}
1693
Eli Friedman4bc8c712009-05-27 12:20:41 +00001694SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1695 DebugLoc dl = Node->getDebugLoc();
1696 SDValue Tmp1 = Node->getOperand(0);
1697 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001698
1699 // Get the sign bit of the RHS. First obtain a value that has the same
1700 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001701 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001702 EVT FloatVT = Tmp2.getValueType();
1703 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001704 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001705 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001706 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001707 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001708 // Store the float to memory, then load the sign part out as an integer.
1709 MVT LoadTy = TLI.getPointerTy();
1710 // First create a temporary that is aligned for both the load and store.
1711 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1712 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001713 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001714 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001715 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001716 if (TLI.isBigEndian()) {
1717 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1718 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001719 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1720 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001721 } else { // Little endian
1722 SDValue LoadPtr = StackPtr;
1723 // The float may be wider than the integer we are going to load. Advance
1724 // the pointer so that the loaded integer will contain the sign bit.
1725 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1726 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1727 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1728 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1729 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001730 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1731 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001732 // Move the sign bit to the top bit of the loaded integer.
1733 unsigned BitShift = LoadTy.getSizeInBits() -
1734 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1735 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1736 if (BitShift)
1737 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
Owen Anderson95771af2011-02-25 21:41:48 +00001738 DAG.getConstant(BitShift,
1739 TLI.getShiftAmountTy(SignBit.getValueType())));
Duncan Sands5d54b412010-03-12 11:45:06 +00001740 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001741 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001742 // Now get the sign bit proper, by seeing whether the value is negative.
1743 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1744 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1745 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001746 // Get the absolute value of the result.
1747 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1748 // Select between the nabs and abs value based on the sign bit of
1749 // the input.
1750 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1751 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1752 AbsVal);
1753}
1754
Eli Friedman4bc8c712009-05-27 12:20:41 +00001755void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1756 SmallVectorImpl<SDValue> &Results) {
1757 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1758 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1759 " not tell us which reg is the stack pointer!");
1760 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001761 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001762 SDValue Tmp1 = SDValue(Node, 0);
1763 SDValue Tmp2 = SDValue(Node, 1);
1764 SDValue Tmp3 = Node->getOperand(2);
1765 SDValue Chain = Tmp1.getOperand(0);
1766
1767 // Chain the dynamic stack allocation so that it doesn't modify the stack
1768 // pointer when other instructions are using the stack.
1769 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1770
1771 SDValue Size = Tmp2.getOperand(1);
1772 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1773 Chain = SP.getValue(1);
1774 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001775 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001776 if (Align > StackAlign)
1777 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1778 DAG.getConstant(-(uint64_t)Align, VT));
1779 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1780 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1781
1782 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1783 DAG.getIntPtrConstant(0, true), SDValue());
1784
1785 Results.push_back(Tmp1);
1786 Results.push_back(Tmp2);
1787}
1788
Evan Cheng7f042682008-10-15 02:05:31 +00001789/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001790/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001791/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001792void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001793 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001794 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001795 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001796 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001797 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1798 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001799 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001800 case TargetLowering::Legal:
1801 // Nothing to do.
1802 break;
1803 case TargetLowering::Expand: {
1804 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1805 unsigned Opc = 0;
1806 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001807 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001808 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1809 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1810 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1811 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1812 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1813 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1814 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1815 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1816 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1817 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1818 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1819 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001820 // FIXME: Implement more expansions.
1821 }
1822
Dale Johannesenbb5da912009-02-02 20:41:04 +00001823 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1824 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1825 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001826 RHS = SDValue();
1827 CC = SDValue();
1828 break;
1829 }
1830 }
1831}
1832
Chris Lattner1401d152008-01-16 07:45:30 +00001833/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1834/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1835/// a load from the stack slot to DestVT, extending it if needed.
1836/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001837SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001838 EVT SlotVT,
1839 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001840 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001841 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001842 unsigned SrcAlign =
1843 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001844 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001845 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001846
Evan Chengff89dcb2009-10-18 18:16:27 +00001847 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1848 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001849 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001850
Duncan Sands83ec4b62008-06-06 12:08:01 +00001851 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1852 unsigned SlotSize = SlotVT.getSizeInBits();
1853 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001854 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1855 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001856
Chris Lattner1401d152008-01-16 07:45:30 +00001857 // Emit a store to the stack slot. Use a truncstore if the input value is
1858 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001860
Chris Lattner1401d152008-01-16 07:45:30 +00001861 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001862 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001863 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001864 else {
1865 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001866 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001867 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001869
Chris Lattner35481892005-12-23 00:16:34 +00001870 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001871 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001872 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001873 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Chris Lattner1401d152008-01-16 07:45:30 +00001875 assert(SlotSize < DestSize && "Unknown extension!");
Stuart Hastingsa9011292011-02-16 16:23:55 +00001876 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001877 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001878}
1879
Dan Gohman475871a2008-07-27 21:46:04 +00001880SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001881 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001882 // Create a vector sized/aligned stack slot, store the value to element #0,
1883 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001884 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001885
Evan Chengff89dcb2009-10-18 18:16:27 +00001886 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1887 int SPFI = StackPtrFI->getIndex();
1888
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001889 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1890 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001891 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001892 Node->getValueType(0).getVectorElementType(),
1893 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001894 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001895 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001896 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001897}
1898
1899
Chris Lattnerce872152006-03-19 06:31:19 +00001900/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001901/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001902SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001903 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001904 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001905 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001906 EVT VT = Node->getValueType(0);
1907 EVT OpVT = Node->getOperand(0).getValueType();
1908 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001909
1910 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001911 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001912 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001913 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001914 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001915 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001916 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001917 if (V.getOpcode() == ISD::UNDEF)
1918 continue;
1919 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001920 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001921 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001922 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001923
1924 if (!Value1.getNode()) {
1925 Value1 = V;
1926 } else if (!Value2.getNode()) {
1927 if (V != Value1)
1928 Value2 = V;
1929 } else if (V != Value1 && V != Value2) {
1930 MoreThanTwoValues = true;
1931 }
Chris Lattnerce872152006-03-19 06:31:19 +00001932 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001933
Eli Friedman7a5e5552009-06-07 06:52:44 +00001934 if (!Value1.getNode())
1935 return DAG.getUNDEF(VT);
1936
1937 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001938 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001939
Chris Lattner2eb86532006-03-24 07:29:17 +00001940 // If all elements are constants, create a load from the constant pool.
1941 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001942 std::vector<Constant*> CV;
1943 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001944 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001945 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001946 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001947 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001948 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001949 if (OpVT==EltVT)
1950 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1951 else {
1952 // If OpVT and EltVT don't match, EltVT is not legal and the
1953 // element values have been promoted/truncated earlier. Undo this;
1954 // we don't want a v16i8 to become a v16i32 for example.
1955 const ConstantInt *CI = V->getConstantIntValue();
1956 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1957 CI->getZExtValue()));
1958 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001959 } else {
1960 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001961 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001962 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001963 }
1964 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001965 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001966 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001967 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001968 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001969 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001970 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001971 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001972
Eli Friedman7a5e5552009-06-07 06:52:44 +00001973 if (!MoreThanTwoValues) {
1974 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1975 for (unsigned i = 0; i < NumElems; ++i) {
1976 SDValue V = Node->getOperand(i);
1977 if (V.getOpcode() == ISD::UNDEF)
1978 continue;
1979 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1980 }
1981 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001982 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001983 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1984 SDValue Vec2;
1985 if (Value2.getNode())
1986 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1987 else
1988 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001989
Chris Lattner87100e02006-03-20 01:52:29 +00001990 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001991 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001992 }
1993 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Eli Friedman7ef3d172009-06-06 07:04:42 +00001995 // Otherwise, we can't handle this case efficiently.
1996 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001997}
1998
Chris Lattner77e77a62005-01-21 06:05:23 +00001999// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2000// does not fit into a register, return the lo part and set the hi part to the
2001// by-reg argument. If it does fit into a single register, return the result
2002// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00002003SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00002004 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002005 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00002006 // Legalizing the call will automatically add the previous call to the
2007 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Chris Lattner77e77a62005-01-21 06:05:23 +00002010 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00002011 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002012 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002013 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00002014 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002015 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002016 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002017 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002018 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002019 }
Bill Wendling056292f2008-09-16 21:48:12 +00002020 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002021 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002022
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002023 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00002024 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002025
2026 // isTailCall may be true since the callee does not reference caller stack
2027 // frame. Check if it's in the right position.
2028 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002029 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002030 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002031 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002032 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002033 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002034
Evan Cheng3d2125c2010-11-30 23:55:39 +00002035 if (!CallInfo.second.getNode())
2036 // It's a tailcall, return the chain (which is the DAG root).
2037 return DAG.getRoot();
2038
Chris Lattner6831a812006-02-13 09:18:02 +00002039 // Legalize the call sequence, starting with the chain. This will advance
2040 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2041 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2042 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002043 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002044}
2045
Jim Grosbache03262f2010-06-18 21:43:38 +00002046// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2047// ExpandLibCall except that the first operand is the in-chain.
2048std::pair<SDValue, SDValue>
2049SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2050 SDNode *Node,
2051 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002052 SDValue InChain = Node->getOperand(0);
2053
2054 TargetLowering::ArgListTy Args;
2055 TargetLowering::ArgListEntry Entry;
2056 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2057 EVT ArgVT = Node->getOperand(i).getValueType();
2058 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2059 Entry.Node = Node->getOperand(i);
2060 Entry.Ty = ArgTy;
2061 Entry.isSExt = isSigned;
2062 Entry.isZExt = !isSigned;
2063 Args.push_back(Entry);
2064 }
2065 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2066 TLI.getPointerTy());
2067
2068 // Splice the libcall in wherever FindInputOutputChains tells us to.
2069 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2070 std::pair<SDValue, SDValue> CallInfo =
2071 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002072 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002073 /*isReturnValueUsed=*/true,
2074 Callee, Args, DAG, Node->getDebugLoc());
2075
2076 // Legalize the call sequence, starting with the chain. This will advance
2077 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2078 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2079 LegalizeOp(CallInfo.second);
2080 return CallInfo;
2081}
2082
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002083SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2084 RTLIB::Libcall Call_F32,
2085 RTLIB::Libcall Call_F64,
2086 RTLIB::Libcall Call_F80,
2087 RTLIB::Libcall Call_PPCF128) {
2088 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002090 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002091 case MVT::f32: LC = Call_F32; break;
2092 case MVT::f64: LC = Call_F64; break;
2093 case MVT::f80: LC = Call_F80; break;
2094 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002095 }
2096 return ExpandLibCall(LC, Node, false);
2097}
2098
2099SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002100 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002101 RTLIB::Libcall Call_I16,
2102 RTLIB::Libcall Call_I32,
2103 RTLIB::Libcall Call_I64,
2104 RTLIB::Libcall Call_I128) {
2105 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002106 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002107 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002108 case MVT::i8: LC = Call_I8; break;
2109 case MVT::i16: LC = Call_I16; break;
2110 case MVT::i32: LC = Call_I32; break;
2111 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002112 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002113 }
2114 return ExpandLibCall(LC, Node, isSigned);
2115}
2116
Chris Lattner22cde6a2006-01-28 08:25:58 +00002117/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2118/// INT_TO_FP operation of the specified operand when the target requests that
2119/// we expand it. At this point, we know that the result and operand types are
2120/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002121SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2122 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002123 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002124 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002125 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002126 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002127
Chris Lattner23594d42008-01-16 07:03:22 +00002128 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002129 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002130
Chris Lattner22cde6a2006-01-28 08:25:58 +00002131 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002132 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002133 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002134 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002135 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002136 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002137 if (TLI.isLittleEndian())
2138 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002139
Chris Lattner22cde6a2006-01-28 08:25:58 +00002140 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002141 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002142 if (isSigned) {
2143 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002144 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2145 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002146 } else {
2147 Op0Mapped = Op0;
2148 }
2149 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002150 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002151 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002152 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002153 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002154 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002155 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002156 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2157 MachinePointerInfo(),
2158 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002159 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002160 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2161 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002162 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002163 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002164 BitsToDouble(0x4330000080000000ULL) :
2165 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002166 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002167 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002168 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002169 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002170 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002171 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002172 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002173 // do nothing
2174 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002175 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002176 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002177 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002178 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002179 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002180 }
2181 return Result;
2182 }
2183 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002184 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002185
2186 // Implementation of unsigned i64 to f64 following the algorithm in
2187 // __floatundidf in compiler_rt. This implementation has the advantage
2188 // of performing rounding correctly, both in the default rounding mode
2189 // and in all alternate rounding modes.
2190 // TODO: Generalize this for use with other types.
2191 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2192 SDValue TwoP52 =
2193 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2194 SDValue TwoP84PlusTwoP52 =
2195 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2196 SDValue TwoP84 =
2197 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2198
2199 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2200 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2201 DAG.getConstant(32, MVT::i64));
2202 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2203 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002204 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2205 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002206 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2207 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002208 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2209 }
2210
Owen Anderson3a9e7692010-10-05 17:24:05 +00002211 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002212 // TODO: Generalize this for use with other types.
2213 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002214 // For unsigned conversions, convert them to signed conversions using the
2215 // algorithm from the x86_64 __floatundidf in compiler_rt.
2216 if (!isSigned) {
2217 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002218
Owen Anderson95771af2011-02-25 21:41:48 +00002219 SDValue ShiftConst =
2220 DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType()));
Owen Anderson3a9e7692010-10-05 17:24:05 +00002221 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2222 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2223 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2224 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002225
Owen Anderson3a9e7692010-10-05 17:24:05 +00002226 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2227 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002228
Owen Anderson3a9e7692010-10-05 17:24:05 +00002229 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002230 // select. We happen to get lucky and machinesink does the right
2231 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002232 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002233 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002234 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2235 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2236 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002237
Owen Anderson3a9e7692010-10-05 17:24:05 +00002238 // Otherwise, implement the fully general conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002239
Jim Grosbach6e992612010-07-02 17:41:59 +00002240 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002241 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2242 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2243 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002244 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002245 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2246 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2247 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2248 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2249 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2250 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002251 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002252 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Owen Anderson95771af2011-02-25 21:41:48 +00002253 EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002254
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002255 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2256 DAG.getConstant(32, SHVT));
2257 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2258 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2259 SDValue TwoP32 =
2260 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2261 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2262 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2263 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2264 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2265 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2266 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002267 }
2268
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002269 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002270
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002271 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2272 Op0, DAG.getConstant(0, Op0.getValueType()),
2273 ISD::SETLT);
2274 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2275 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2276 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002277
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002278 // If the sign bit of the integer is set, the large number will be treated
2279 // as a negative number. To counteract this, the dynamic code adds an
2280 // offset depending on the data type.
2281 uint64_t FF;
2282 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002283 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002284 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2285 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2286 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2287 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2288 }
2289 if (TLI.isLittleEndian()) FF <<= 32;
2290 Constant *FudgeFactor = ConstantInt::get(
2291 Type::getInt64Ty(*DAG.getContext()), FF);
2292
2293 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2294 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2295 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2296 Alignment = std::min(Alignment, 4u);
2297 SDValue FudgeInReg;
2298 if (DestVT == MVT::f32)
2299 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002300 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002301 false, false, Alignment);
2302 else {
2303 FudgeInReg =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002304 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002305 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002306 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002307 MVT::f32, false, false, Alignment));
2308 }
2309
2310 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002311}
2312
2313/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2314/// *INT_TO_FP operation of the specified operand when the target requests that
2315/// we promote it. At this point, we know that the result and operand types are
2316/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2317/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002318SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002319 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002320 bool isSigned,
2321 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002322 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002323 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002324
2325 unsigned OpToUse = 0;
2326
2327 // Scan for the appropriate larger type to use.
2328 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002329 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002330 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002331
2332 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002333 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2334 OpToUse = ISD::SINT_TO_FP;
2335 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002336 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002337 if (isSigned) continue;
2338
2339 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002340 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2341 OpToUse = ISD::UINT_TO_FP;
2342 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002343 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002344
2345 // Otherwise, try a larger type.
2346 }
2347
2348 // Okay, we found the operation and type to use. Zero extend our input to the
2349 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002350 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002351 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002352 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002353}
2354
2355/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2356/// FP_TO_*INT operation of the specified operand when the target requests that
2357/// we promote it. At this point, we know that the result and operand types are
2358/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2359/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002360SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002362 bool isSigned,
2363 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002364 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002365 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002366
2367 unsigned OpToUse = 0;
2368
2369 // Scan for the appropriate larger type to use.
2370 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002371 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002372 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002373
Eli Friedman3be2e512009-05-28 03:06:16 +00002374 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002375 OpToUse = ISD::FP_TO_SINT;
2376 break;
2377 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002378
Eli Friedman3be2e512009-05-28 03:06:16 +00002379 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002380 OpToUse = ISD::FP_TO_UINT;
2381 break;
2382 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002383
2384 // Otherwise, try a larger type.
2385 }
2386
Scott Michelfdc40a02009-02-17 22:15:04 +00002387
Chris Lattner27a6c732007-11-24 07:07:01 +00002388 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002389 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002390
Chris Lattner27a6c732007-11-24 07:07:01 +00002391 // Truncate the result of the extended FP_TO_*INT operation to the desired
2392 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002393 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002394}
2395
2396/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2397///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002398SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002400 EVT SHVT = TLI.getShiftAmountTy(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002401 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002403 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002404 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002405 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2406 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2407 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002409 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2410 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2411 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2412 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2413 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2414 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2415 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2416 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2417 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002418 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002419 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2420 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2421 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2422 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2423 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2424 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2425 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2426 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2427 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2428 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2429 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2430 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2431 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2432 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2433 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2434 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2435 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2436 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2437 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2438 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2439 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002440 }
2441}
2442
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002443/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002444// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002445static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2446 APInt Val = APInt(NumBits, ByteVal);
2447 unsigned Shift = 8;
2448 for (unsigned i = NumBits; i > 8; i >>= 1) {
2449 Val = (Val << Shift) | Val;
2450 Shift <<= 1;
2451 }
2452 return Val;
2453}
2454
Chris Lattner22cde6a2006-01-28 08:25:58 +00002455/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2456///
Scott Michelfdc40a02009-02-17 22:15:04 +00002457SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002458 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002459 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002460 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002461 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002462 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002463 EVT ShVT = TLI.getShiftAmountTy(VT);
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002464 unsigned Len = VT.getSizeInBits();
2465
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002466 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2467 "CTPOP not implemented for this type.");
2468
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002469 // This is the "best" algorithm from
2470 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2471
2472 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2473 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2474 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2475 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2476
2477 // v = v - ((v >> 1) & 0x55555555...)
2478 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2479 DAG.getNode(ISD::AND, dl, VT,
2480 DAG.getNode(ISD::SRL, dl, VT, Op,
2481 DAG.getConstant(1, ShVT)),
2482 Mask55));
2483 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2484 Op = DAG.getNode(ISD::ADD, dl, VT,
2485 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2486 DAG.getNode(ISD::AND, dl, VT,
2487 DAG.getNode(ISD::SRL, dl, VT, Op,
2488 DAG.getConstant(2, ShVT)),
2489 Mask33));
2490 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2491 Op = DAG.getNode(ISD::AND, dl, VT,
2492 DAG.getNode(ISD::ADD, dl, VT, Op,
2493 DAG.getNode(ISD::SRL, dl, VT, Op,
2494 DAG.getConstant(4, ShVT))),
2495 Mask0F);
2496 // v = (v * 0x01010101...) >> (Len - 8)
2497 Op = DAG.getNode(ISD::SRL, dl, VT,
2498 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2499 DAG.getConstant(Len - 8, ShVT));
Owen Anderson95771af2011-02-25 21:41:48 +00002500
Chris Lattner22cde6a2006-01-28 08:25:58 +00002501 return Op;
2502 }
2503 case ISD::CTLZ: {
2504 // for now, we do this:
2505 // x = x | (x >> 1);
2506 // x = x | (x >> 2);
2507 // ...
2508 // x = x | (x >>16);
2509 // x = x | (x >>32); // for 64-bit input
2510 // return popcount(~x);
2511 //
2512 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002513 EVT VT = Op.getValueType();
Owen Anderson95771af2011-02-25 21:41:48 +00002514 EVT ShVT = TLI.getShiftAmountTy(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002515 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002516 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002517 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002518 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002519 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002520 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002521 Op = DAG.getNOT(dl, Op, VT);
2522 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002523 }
2524 case ISD::CTTZ: {
2525 // for now, we use: { return popcount(~x & (x - 1)); }
2526 // unless the target has ctlz but not ctpop, in which case we use:
2527 // { return 32 - nlz(~x & (x-1)); }
2528 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002529 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002530 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2531 DAG.getNOT(dl, Op, VT),
2532 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002533 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002534 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002535 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2536 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002537 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002538 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002539 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2540 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002541 }
2542 }
2543}
Chris Lattnere34b3962005-01-19 04:19:40 +00002544
Jim Grosbache03262f2010-06-18 21:43:38 +00002545std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2546 unsigned Opc = Node->getOpcode();
2547 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2548 RTLIB::Libcall LC;
2549
2550 switch (Opc) {
2551 default:
2552 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2553 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002554 case ISD::ATOMIC_SWAP:
2555 switch (VT.SimpleTy) {
2556 default: llvm_unreachable("Unexpected value type for atomic!");
2557 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2558 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2559 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2560 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2561 }
2562 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002563 case ISD::ATOMIC_CMP_SWAP:
2564 switch (VT.SimpleTy) {
2565 default: llvm_unreachable("Unexpected value type for atomic!");
2566 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2567 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2568 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2569 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2570 }
2571 break;
2572 case ISD::ATOMIC_LOAD_ADD:
2573 switch (VT.SimpleTy) {
2574 default: llvm_unreachable("Unexpected value type for atomic!");
2575 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2576 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2577 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2578 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2579 }
2580 break;
2581 case ISD::ATOMIC_LOAD_SUB:
2582 switch (VT.SimpleTy) {
2583 default: llvm_unreachable("Unexpected value type for atomic!");
2584 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2585 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2586 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2587 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2588 }
2589 break;
2590 case ISD::ATOMIC_LOAD_AND:
2591 switch (VT.SimpleTy) {
2592 default: llvm_unreachable("Unexpected value type for atomic!");
2593 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2594 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2595 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2596 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2597 }
2598 break;
2599 case ISD::ATOMIC_LOAD_OR:
2600 switch (VT.SimpleTy) {
2601 default: llvm_unreachable("Unexpected value type for atomic!");
2602 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2603 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2604 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2605 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2606 }
2607 break;
2608 case ISD::ATOMIC_LOAD_XOR:
2609 switch (VT.SimpleTy) {
2610 default: llvm_unreachable("Unexpected value type for atomic!");
2611 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2612 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2613 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2614 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2615 }
2616 break;
2617 case ISD::ATOMIC_LOAD_NAND:
2618 switch (VT.SimpleTy) {
2619 default: llvm_unreachable("Unexpected value type for atomic!");
2620 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2621 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2622 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2623 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2624 }
2625 break;
2626 }
2627
2628 return ExpandChainLibCall(LC, Node, false);
2629}
2630
Eli Friedman8c377c72009-05-27 01:25:56 +00002631void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2632 SmallVectorImpl<SDValue> &Results) {
2633 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002634 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002635 switch (Node->getOpcode()) {
2636 case ISD::CTPOP:
2637 case ISD::CTLZ:
2638 case ISD::CTTZ:
2639 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2640 Results.push_back(Tmp1);
2641 break;
2642 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002643 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002644 break;
2645 case ISD::FRAMEADDR:
2646 case ISD::RETURNADDR:
2647 case ISD::FRAME_TO_ARGS_OFFSET:
2648 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2649 break;
2650 case ISD::FLT_ROUNDS_:
2651 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2652 break;
2653 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002654 case ISD::EH_LABEL:
2655 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002656 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002657 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002658 case ISD::EH_SJLJ_DISPATCHSETUP:
2659 // If the target didn't expand these, there's nothing to do, so just
2660 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002661 Results.push_back(Node->getOperand(0));
2662 break;
2663 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002664 // If the target didn't expand this, just return 'zero' and preserve the
2665 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002666 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002667 Results.push_back(Node->getOperand(0));
2668 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002669 case ISD::MEMBARRIER: {
2670 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2671 TargetLowering::ArgListTy Args;
2672 std::pair<SDValue, SDValue> CallResult =
2673 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002674 false, false, false, false, 0, CallingConv::C,
2675 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002676 /*isReturnValueUsed=*/true,
2677 DAG.getExternalSymbol("__sync_synchronize",
2678 TLI.getPointerTy()),
2679 Args, DAG, dl);
2680 Results.push_back(CallResult.second);
2681 break;
2682 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002683 // By default, atomic intrinsics are marked Legal and lowered. Targets
2684 // which don't support them directly, however, may want libcalls, in which
2685 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002686 case ISD::ATOMIC_SWAP:
2687 case ISD::ATOMIC_LOAD_ADD:
2688 case ISD::ATOMIC_LOAD_SUB:
2689 case ISD::ATOMIC_LOAD_AND:
2690 case ISD::ATOMIC_LOAD_OR:
2691 case ISD::ATOMIC_LOAD_XOR:
2692 case ISD::ATOMIC_LOAD_NAND:
2693 case ISD::ATOMIC_LOAD_MIN:
2694 case ISD::ATOMIC_LOAD_MAX:
2695 case ISD::ATOMIC_LOAD_UMIN:
2696 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002697 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002698 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2699 Results.push_back(Tmp.first);
2700 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002701 break;
Evan Chenga8457062010-06-18 22:01:37 +00002702 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002703 case ISD::DYNAMIC_STACKALLOC:
2704 ExpandDYNAMIC_STACKALLOC(Node, Results);
2705 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002706 case ISD::MERGE_VALUES:
2707 for (unsigned i = 0; i < Node->getNumValues(); i++)
2708 Results.push_back(Node->getOperand(i));
2709 break;
2710 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002711 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002712 if (VT.isInteger())
2713 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002714 else {
2715 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002716 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002717 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002718 break;
2719 }
2720 case ISD::TRAP: {
2721 // If this operation is not supported, lower it to 'abort()' call
2722 TargetLowering::ArgListTy Args;
2723 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002724 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002725 false, false, false, false, 0, CallingConv::C,
2726 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002727 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002728 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002729 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002730 Results.push_back(CallResult.second);
2731 break;
2732 }
2733 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002734 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002735 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2736 Node->getValueType(0), dl);
2737 Results.push_back(Tmp1);
2738 break;
2739 case ISD::FP_EXTEND:
2740 Tmp1 = EmitStackConvert(Node->getOperand(0),
2741 Node->getOperand(0).getValueType(),
2742 Node->getValueType(0), dl);
2743 Results.push_back(Tmp1);
2744 break;
2745 case ISD::SIGN_EXTEND_INREG: {
2746 // NOTE: we could fall back on load/store here too for targets without
2747 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002748 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002749 EVT VT = Node->getValueType(0);
Owen Anderson95771af2011-02-25 21:41:48 +00002750 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT);
Dan Gohmand1996362010-01-09 02:13:55 +00002751 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002752 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002753 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2754 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002755 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002756 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2757 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002758 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2759 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002760 break;
2761 }
2762 case ISD::FP_ROUND_INREG: {
2763 // The only way we can lower this is to turn it into a TRUNCSTORE,
2764 // EXTLOAD pair, targetting a temporary location (a stack slot).
2765
2766 // NOTE: there is a choice here between constantly creating new stack
2767 // slots and always reusing the same one. We currently always create
2768 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002769 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002770 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2771 Node->getValueType(0), dl);
2772 Results.push_back(Tmp1);
2773 break;
2774 }
2775 case ISD::SINT_TO_FP:
2776 case ISD::UINT_TO_FP:
2777 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2778 Node->getOperand(0), Node->getValueType(0), dl);
2779 Results.push_back(Tmp1);
2780 break;
2781 case ISD::FP_TO_UINT: {
2782 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002783 EVT VT = Node->getOperand(0).getValueType();
2784 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002785 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002786 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2787 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2788 Tmp1 = DAG.getConstantFP(apf, VT);
2789 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2790 Node->getOperand(0),
2791 Tmp1, ISD::SETLT);
2792 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002793 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2794 DAG.getNode(ISD::FSUB, dl, VT,
2795 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002796 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2797 DAG.getConstant(x, NVT));
2798 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2799 Results.push_back(Tmp1);
2800 break;
2801 }
Eli Friedman509150f2009-05-27 07:58:35 +00002802 case ISD::VAARG: {
2803 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002804 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002805 Tmp1 = Node->getOperand(0);
2806 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002807 unsigned Align = Node->getConstantOperandVal(3);
2808
Chris Lattnerecf42c42010-09-21 16:36:31 +00002809 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2810 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002811 SDValue VAList = VAListLoad;
2812
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002813 if (Align > TLI.getMinStackArgumentAlignment()) {
2814 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2815
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002816 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2817 DAG.getConstant(Align - 1,
2818 TLI.getPointerTy()));
2819
2820 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002821 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002822 TLI.getPointerTy()));
2823 }
2824
Eli Friedman509150f2009-05-27 07:58:35 +00002825 // Increment the pointer, VAList, to the next vaarg
2826 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2827 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002828 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002829 TLI.getPointerTy()));
2830 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002831 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2832 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002833 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002834 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002835 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002836 Results.push_back(Results[0].getValue(1));
2837 break;
2838 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002839 case ISD::VACOPY: {
2840 // This defaults to loading a pointer from the input and storing it to the
2841 // output, returning the chain.
2842 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2843 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2844 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002845 Node->getOperand(2), MachinePointerInfo(VS),
2846 false, false, 0);
2847 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2848 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002849 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002850 break;
2851 }
2852 case ISD::EXTRACT_VECTOR_ELT:
2853 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2854 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002855 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002856 Node->getOperand(0));
2857 else
2858 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2859 Results.push_back(Tmp1);
2860 break;
2861 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002862 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002863 break;
David Greenecfe33c42011-01-26 19:13:22 +00002864 case ISD::INSERT_SUBVECTOR:
2865 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2866 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002867 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002868 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002869 break;
2870 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002871 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002872 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002873 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002874 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002875 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2876 Node->getOperand(1),
2877 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002878 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002879 case ISD::VECTOR_SHUFFLE: {
2880 SmallVector<int, 8> Mask;
2881 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2882
Owen Andersone50ed302009-08-10 22:56:29 +00002883 EVT VT = Node->getValueType(0);
2884 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00002885 if (getTypeAction(EltVT) == Promote)
2886 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002887 unsigned NumElems = VT.getVectorNumElements();
2888 SmallVector<SDValue, 8> Ops;
2889 for (unsigned i = 0; i != NumElems; ++i) {
2890 if (Mask[i] < 0) {
2891 Ops.push_back(DAG.getUNDEF(EltVT));
2892 continue;
2893 }
2894 unsigned Idx = Mask[i];
2895 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002896 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2897 Node->getOperand(0),
2898 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002899 else
Bill Wendling775db972009-12-23 00:28:23 +00002900 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2901 Node->getOperand(1),
2902 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002903 }
2904 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2905 Results.push_back(Tmp1);
2906 break;
2907 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002908 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002909 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002910 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2911 // 1 -> Hi
2912 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2913 DAG.getConstant(OpTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00002914 TLI.getShiftAmountTy(Node->getOperand(0).getValueType())));
Eli Friedman8c377c72009-05-27 01:25:56 +00002915 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2916 } else {
2917 // 0 -> Lo
2918 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2919 Node->getOperand(0));
2920 }
2921 Results.push_back(Tmp1);
2922 break;
2923 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002924 case ISD::STACKSAVE:
2925 // Expand to CopyFromReg if the target set
2926 // StackPointerRegisterToSaveRestore.
2927 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002928 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2929 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002930 Results.push_back(Results[0].getValue(1));
2931 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002932 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002933 Results.push_back(Node->getOperand(0));
2934 }
2935 break;
2936 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002937 // Expand to CopyToReg if the target set
2938 // StackPointerRegisterToSaveRestore.
2939 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2940 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2941 Node->getOperand(1)));
2942 } else {
2943 Results.push_back(Node->getOperand(0));
2944 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002945 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002946 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002947 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002948 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002949 case ISD::FNEG:
2950 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2951 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2952 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2953 Node->getOperand(0));
2954 Results.push_back(Tmp1);
2955 break;
2956 case ISD::FABS: {
2957 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002958 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002959 Tmp1 = Node->getOperand(0);
2960 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002961 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002962 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002963 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2964 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002965 Results.push_back(Tmp1);
2966 break;
2967 }
2968 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002969 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2970 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002971 break;
2972 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002973 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2974 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002975 break;
2976 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002977 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2978 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002979 break;
2980 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002981 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2982 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002983 break;
2984 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002985 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2986 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002987 break;
2988 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002989 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2990 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002991 break;
2992 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002993 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2994 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002995 break;
2996 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002997 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2998 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002999 break;
3000 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00003001 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3002 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003003 break;
3004 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00003005 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3006 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003007 break;
3008 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00003009 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3010 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003011 break;
3012 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003013 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3014 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003015 break;
3016 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003017 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3018 RTLIB::NEARBYINT_F64,
3019 RTLIB::NEARBYINT_F80,
3020 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003021 break;
3022 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003023 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3024 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003025 break;
3026 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003027 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3028 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003029 break;
3030 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003031 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3032 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003033 break;
3034 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003035 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3036 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003037 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003038 case ISD::FP16_TO_FP32:
3039 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3040 break;
3041 case ISD::FP32_TO_FP16:
3042 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3043 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003044 case ISD::ConstantFP: {
3045 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003046 // Check to see if this FP immediate is already legal.
3047 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003048 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003049 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003050 else
Bill Wendling775db972009-12-23 00:28:23 +00003051 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003052 break;
3053 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003054 case ISD::EHSELECTION: {
3055 unsigned Reg = TLI.getExceptionSelectorRegister();
3056 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003057 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3058 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003059 Results.push_back(Results[0].getValue(1));
3060 break;
3061 }
3062 case ISD::EXCEPTIONADDR: {
3063 unsigned Reg = TLI.getExceptionAddressRegister();
3064 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003065 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3066 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003067 Results.push_back(Results[0].getValue(1));
3068 break;
3069 }
3070 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003071 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003072 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3073 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3074 "Don't know how to expand this subtraction!");
3075 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3076 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003077 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3078 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003079 break;
3080 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003081 case ISD::UREM:
3082 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003083 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003084 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003085 bool isSigned = Node->getOpcode() == ISD::SREM;
3086 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3087 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3088 Tmp2 = Node->getOperand(0);
3089 Tmp3 = Node->getOperand(1);
Eli Friedman3be2e512009-05-28 03:06:16 +00003090 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3091 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3092 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003093 // X % Y -> X-X/Y*Y
3094 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3095 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3096 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003097 } else if (isSigned) {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003098 Tmp1 = ExpandIntLibCall(Node, true,
3099 RTLIB::SREM_I8,
3100 RTLIB::SREM_I16, RTLIB::SREM_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003101 RTLIB::SREM_I64, RTLIB::SREM_I128);
3102 } else {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003103 Tmp1 = ExpandIntLibCall(Node, false,
3104 RTLIB::UREM_I8,
3105 RTLIB::UREM_I16, RTLIB::UREM_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003106 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003107 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003108 Results.push_back(Tmp1);
3109 break;
3110 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003111 case ISD::UDIV:
3112 case ISD::SDIV: {
3113 bool isSigned = Node->getOpcode() == ISD::SDIV;
3114 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003115 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003116 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003117 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3118 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3119 Node->getOperand(1));
3120 else if (isSigned)
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003121 Tmp1 = ExpandIntLibCall(Node, true,
3122 RTLIB::SDIV_I8,
3123 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003124 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003125 else
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003126 Tmp1 = ExpandIntLibCall(Node, false,
3127 RTLIB::UDIV_I8,
3128 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003129 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003130 Results.push_back(Tmp1);
3131 break;
3132 }
3133 case ISD::MULHU:
3134 case ISD::MULHS: {
3135 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3136 ISD::SMUL_LOHI;
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 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3140 "If this wasn't legal, it shouldn't have been created!");
3141 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3142 Node->getOperand(1));
3143 Results.push_back(Tmp1.getValue(1));
3144 break;
3145 }
3146 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003147 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003148 SDVTList VTs = DAG.getVTList(VT, VT);
3149 // See if multiply or divide can be lowered using two-result operations.
3150 // We just need the low half of the multiply; try both the signed
3151 // and unsigned forms. If the target supports both SMUL_LOHI and
3152 // UMUL_LOHI, form a preference by checking which forms of plain
3153 // MULH it supports.
3154 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3155 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3156 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3157 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3158 unsigned OpToUse = 0;
3159 if (HasSMUL_LOHI && !HasMULHS) {
3160 OpToUse = ISD::SMUL_LOHI;
3161 } else if (HasUMUL_LOHI && !HasMULHU) {
3162 OpToUse = ISD::UMUL_LOHI;
3163 } else if (HasSMUL_LOHI) {
3164 OpToUse = ISD::SMUL_LOHI;
3165 } else if (HasUMUL_LOHI) {
3166 OpToUse = ISD::UMUL_LOHI;
3167 }
3168 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003169 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3170 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003171 break;
3172 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003173 Tmp1 = ExpandIntLibCall(Node, false,
3174 RTLIB::MUL_I8,
3175 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003176 RTLIB::MUL_I64, RTLIB::MUL_I128);
3177 Results.push_back(Tmp1);
3178 break;
3179 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003180 case ISD::SADDO:
3181 case ISD::SSUBO: {
3182 SDValue LHS = Node->getOperand(0);
3183 SDValue RHS = Node->getOperand(1);
3184 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3185 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3186 LHS, RHS);
3187 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003188 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003189
Eli Friedman4bc8c712009-05-27 12:20:41 +00003190 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3191
3192 // LHSSign -> LHS >= 0
3193 // RHSSign -> RHS >= 0
3194 // SumSign -> Sum >= 0
3195 //
3196 // Add:
3197 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3198 // Sub:
3199 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3200 //
3201 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3202 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3203 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3204 Node->getOpcode() == ISD::SADDO ?
3205 ISD::SETEQ : ISD::SETNE);
3206
3207 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3208 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3209
3210 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3211 Results.push_back(Cmp);
3212 break;
3213 }
3214 case ISD::UADDO:
3215 case ISD::USUBO: {
3216 SDValue LHS = Node->getOperand(0);
3217 SDValue RHS = Node->getOperand(1);
3218 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3219 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3220 LHS, RHS);
3221 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003222 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3223 Node->getOpcode () == ISD::UADDO ?
3224 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003225 break;
3226 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003227 case ISD::UMULO:
3228 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003229 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003230 SDValue LHS = Node->getOperand(0);
3231 SDValue RHS = Node->getOperand(1);
3232 SDValue BottomHalf;
3233 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003234 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003235 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3236 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3237 bool isSigned = Node->getOpcode() == ISD::SMULO;
3238 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3239 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3240 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3241 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3242 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3243 RHS);
3244 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003245 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3246 VT.getSizeInBits() * 2))) {
Owen Anderson23b9b192009-08-12 00:36:31 +00003247 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003248 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3249 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3250 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3251 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3252 DAG.getIntPtrConstant(0));
3253 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3254 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003255 } else {
3256 // We can fall back to a libcall with an illegal type for the MUL if we
3257 // have a libcall big enough.
3258 // Also, we can fall back to a division in some cases, but that's a big
3259 // performance hit in the general case.
3260 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3261 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3262 if (WideVT == MVT::i16)
3263 LC = RTLIB::MUL_I16;
3264 else if (WideVT == MVT::i32)
3265 LC = RTLIB::MUL_I32;
3266 else if (WideVT == MVT::i64)
3267 LC = RTLIB::MUL_I64;
3268 else if (WideVT == MVT::i128)
3269 LC = RTLIB::MUL_I128;
3270 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3271 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3272 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
Owen Anderson95771af2011-02-25 21:41:48 +00003273
Eric Christophere3385812011-01-20 01:29:23 +00003274 SDValue Ret = ExpandLibCall(LC, Node, isSigned);
Eric Christopher38a18262011-01-20 00:29:24 +00003275 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Ret);
3276 TopHalf = DAG.getNode(ISD::SRL, dl, Ret.getValueType(), Ret,
3277 DAG.getConstant(VT.getSizeInBits(), TLI.getPointerTy()));
3278 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, TopHalf);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003279 }
3280 if (isSigned) {
Owen Anderson95771af2011-02-25 21:41:48 +00003281 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1,
3282 TLI.getShiftAmountTy(BottomHalf.getValueType()));
Eli Friedmandb3c1692009-06-16 06:58:29 +00003283 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3284 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3285 ISD::SETNE);
3286 } else {
3287 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3288 DAG.getConstant(0, VT), ISD::SETNE);
3289 }
3290 Results.push_back(BottomHalf);
3291 Results.push_back(TopHalf);
3292 break;
3293 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003294 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003295 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003296 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3297 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003298 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003299 DAG.getConstant(PairTy.getSizeInBits()/2,
Owen Anderson95771af2011-02-25 21:41:48 +00003300 TLI.getShiftAmountTy(PairTy)));
Bill Wendling775db972009-12-23 00:28:23 +00003301 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003302 break;
3303 }
Eli Friedman509150f2009-05-27 07:58:35 +00003304 case ISD::SELECT:
3305 Tmp1 = Node->getOperand(0);
3306 Tmp2 = Node->getOperand(1);
3307 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003308 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003309 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3310 Tmp2, Tmp3,
3311 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003312 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003313 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3314 DAG.getConstant(0, Tmp1.getValueType()),
3315 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003316 }
Eli Friedman509150f2009-05-27 07:58:35 +00003317 Results.push_back(Tmp1);
3318 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003319 case ISD::BR_JT: {
3320 SDValue Chain = Node->getOperand(0);
3321 SDValue Table = Node->getOperand(1);
3322 SDValue Index = Node->getOperand(2);
3323
Owen Andersone50ed302009-08-10 22:56:29 +00003324 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003325
3326 const TargetData &TD = *TLI.getTargetData();
3327 unsigned EntrySize =
3328 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003329
Chris Lattner071c62f2010-01-25 23:26:13 +00003330 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003331 Index, DAG.getConstant(EntrySize, PTy));
3332 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3333
Owen Anderson23b9b192009-08-12 00:36:31 +00003334 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Stuart Hastingsa9011292011-02-16 16:23:55 +00003335 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003336 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003337 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003338 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003339 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003340 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003341 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003342 // RelocBase can be JumpTable, GOT or some sort of global base.
3343 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3344 TLI.getPICJumpTableRelocBase(Table, DAG));
3345 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003347 Results.push_back(Tmp1);
3348 break;
3349 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003350 case ISD::BRCOND:
3351 // Expand brcond's setcc into its constituent parts and create a BR_CC
3352 // Node.
3353 Tmp1 = Node->getOperand(0);
3354 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003355 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003357 Tmp1, Tmp2.getOperand(2),
3358 Tmp2.getOperand(0), Tmp2.getOperand(1),
3359 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003360 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003362 DAG.getCondCode(ISD::SETNE), Tmp2,
3363 DAG.getConstant(0, Tmp2.getValueType()),
3364 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003365 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003366 Results.push_back(Tmp1);
3367 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003368 case ISD::SETCC: {
3369 Tmp1 = Node->getOperand(0);
3370 Tmp2 = Node->getOperand(1);
3371 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003372 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003373
3374 // If we expanded the SETCC into an AND/OR, return the new node
3375 if (Tmp2.getNode() == 0) {
3376 Results.push_back(Tmp1);
3377 break;
3378 }
3379
3380 // Otherwise, SETCC for the given comparison type must be completely
3381 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003382 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003383 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3384 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3385 Results.push_back(Tmp1);
3386 break;
3387 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003388 case ISD::SELECT_CC: {
3389 Tmp1 = Node->getOperand(0); // LHS
3390 Tmp2 = Node->getOperand(1); // RHS
3391 Tmp3 = Node->getOperand(2); // True
3392 Tmp4 = Node->getOperand(3); // False
3393 SDValue CC = Node->getOperand(4);
3394
3395 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003396 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003397
3398 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3399 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3400 CC = DAG.getCondCode(ISD::SETNE);
3401 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3402 Tmp3, Tmp4, CC);
3403 Results.push_back(Tmp1);
3404 break;
3405 }
3406 case ISD::BR_CC: {
3407 Tmp1 = Node->getOperand(0); // Chain
3408 Tmp2 = Node->getOperand(2); // LHS
3409 Tmp3 = Node->getOperand(3); // RHS
3410 Tmp4 = Node->getOperand(1); // CC
3411
3412 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003413 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003414 LastCALLSEQ_END = DAG.getEntryNode();
3415
3416 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3417 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3418 Tmp4 = DAG.getCondCode(ISD::SETNE);
3419 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3420 Tmp3, Node->getOperand(4));
3421 Results.push_back(Tmp1);
3422 break;
3423 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003424 case ISD::GLOBAL_OFFSET_TABLE:
3425 case ISD::GlobalAddress:
3426 case ISD::GlobalTLSAddress:
3427 case ISD::ExternalSymbol:
3428 case ISD::ConstantPool:
3429 case ISD::JumpTable:
3430 case ISD::INTRINSIC_W_CHAIN:
3431 case ISD::INTRINSIC_WO_CHAIN:
3432 case ISD::INTRINSIC_VOID:
3433 // FIXME: Custom lowering for these operations shouldn't return null!
3434 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3435 Results.push_back(SDValue(Node, i));
3436 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003437 }
3438}
3439void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3440 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003441 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003442 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003443 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003444 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003445 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003446 }
Owen Andersone50ed302009-08-10 22:56:29 +00003447 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003448 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003449 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003450 switch (Node->getOpcode()) {
3451 case ISD::CTTZ:
3452 case ISD::CTLZ:
3453 case ISD::CTPOP:
3454 // Zero extend the argument.
3455 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3456 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003457 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003458 if (Node->getOpcode() == ISD::CTTZ) {
3459 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003460 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003461 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3462 ISD::SETEQ);
3463 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3464 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3465 } else if (Node->getOpcode() == ISD::CTLZ) {
3466 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3467 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3468 DAG.getConstant(NVT.getSizeInBits() -
3469 OVT.getSizeInBits(), NVT));
3470 }
Bill Wendling775db972009-12-23 00:28:23 +00003471 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003472 break;
3473 case ISD::BSWAP: {
3474 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003475 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003476 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3477 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
Owen Anderson95771af2011-02-25 21:41:48 +00003478 DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT)));
Bill Wendling775db972009-12-23 00:28:23 +00003479 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003480 break;
3481 }
3482 case ISD::FP_TO_UINT:
3483 case ISD::FP_TO_SINT:
3484 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3485 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3486 Results.push_back(Tmp1);
3487 break;
3488 case ISD::UINT_TO_FP:
3489 case ISD::SINT_TO_FP:
3490 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3491 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3492 Results.push_back(Tmp1);
3493 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003494 case ISD::AND:
3495 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003496 case ISD::XOR: {
3497 unsigned ExtOp, TruncOp;
3498 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003499 ExtOp = ISD::BITCAST;
3500 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003501 } else {
3502 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003503 ExtOp = ISD::ANY_EXTEND;
3504 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003505 }
3506 // Promote each of the values to the new type.
3507 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3508 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3509 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003510 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3511 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003512 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003513 }
3514 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003515 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003516 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003517 ExtOp = ISD::BITCAST;
3518 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003519 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003520 ExtOp = ISD::ANY_EXTEND;
3521 TruncOp = ISD::TRUNCATE;
3522 } else {
3523 ExtOp = ISD::FP_EXTEND;
3524 TruncOp = ISD::FP_ROUND;
3525 }
3526 Tmp1 = Node->getOperand(0);
3527 // Promote each of the values to the new type.
3528 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3529 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3530 // Perform the larger operation, then round down.
3531 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3532 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003533 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003534 else
Bill Wendling775db972009-12-23 00:28:23 +00003535 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003536 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003537 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003538 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003539 }
Eli Friedman509150f2009-05-27 07:58:35 +00003540 case ISD::VECTOR_SHUFFLE: {
3541 SmallVector<int, 8> Mask;
3542 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3543
3544 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003545 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3546 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003547
3548 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003549 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003550 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003551 Results.push_back(Tmp1);
3552 break;
3553 }
Eli Friedmanad754602009-05-28 03:56:57 +00003554 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003555 unsigned ExtOp = ISD::FP_EXTEND;
3556 if (NVT.isInteger()) {
3557 ISD::CondCode CCCode =
3558 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3559 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003560 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003561 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3562 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003563 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3564 Tmp1, Tmp2, Node->getOperand(2)));
3565 break;
3566 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003567 }
3568}
3569
Chris Lattner3e928bb2005-01-07 07:47:09 +00003570// SelectionDAG::Legalize - This is the entry point for the file.
3571//
Dan Gohman714efc62009-12-05 17:51:33 +00003572void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003573 /// run - This is the main entry point to this class.
3574 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003575 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003576}
3577