blob: 355758ac5dd3ba53099d3dec3ae1b16474ca4e6a [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));
Chris Lattner8afc48e2005-01-07 22:28:47 +000090 }
91
Chris Lattner3e928bb2005-01-07 07:47:09 +000092public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000093 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000094
Chris Lattner3e928bb2005-01-07 07:47:09 +000095 /// getTypeAction - Return how we should legalize values of this type, either
96 /// it is already legal or we need to expand it into multiple registers of
97 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +000098 LegalizeAction getTypeAction(EVT VT) const {
Chris Lattneraafe6262010-08-25 23:00:45 +000099 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000100 }
101
102 /// isTypeLegal - Return true if this type is legal on this target.
103 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000104 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105 return getTypeAction(VT) == Legal;
106 }
107
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 void LegalizeDAG();
109
Chris Lattner456a93a2006-01-28 07:39:30 +0000110private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000111 /// LegalizeOp - We know that the specified value has a legal type.
112 /// Recursively ensure that the operands have legal types, then return the
113 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
Eli Friedman7ef3d172009-06-06 07:04:42 +0000116 SDValue OptimizeFloatStore(StoreSDNode *ST);
117
Nate Begeman68679912008-04-25 18:07:40 +0000118 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
119 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
120 /// is necessary to spill the vector being inserted into to memory, perform
121 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000123 SDValue Idx, DebugLoc dl);
124 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
125 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000126
Nate Begeman5a5ca152009-04-29 05:20:52 +0000127 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
128 /// performs the same shuffe in terms of order or result bytes, but on a type
129 /// whose vector element type is narrower than the original shuffle type.
130 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000131 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000132 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000133 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000134
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000135 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000136 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000137
Owen Andersone50ed302009-08-10 22:56:29 +0000138 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000139 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000140
Eli Friedman47b41f72009-05-27 02:21:29 +0000141 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Jim Grosbache03262f2010-06-18 21:43:38 +0000142 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
143 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000144 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
145 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
146 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000147 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
148 RTLIB::Libcall Call_I8,
149 RTLIB::Libcall Call_I16,
150 RTLIB::Libcall Call_I32,
151 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000152 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000153
Owen Andersone50ed302009-08-10 22:56:29 +0000154 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue ExpandBUILD_VECTOR(SDNode *Node);
156 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000157 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
158 SmallVectorImpl<SDValue> &Results);
159 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000160 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000161 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000162 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000163 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000164 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000165 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000166
Dale Johannesen8a782a22009-02-02 22:12:50 +0000167 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
168 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000169
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000170 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
David Greenecfe33c42011-01-26 19:13:22 +0000171 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000172 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000173
Jim Grosbache03262f2010-06-18 21:43:38 +0000174 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
175
Eli Friedman8c377c72009-05-27 01:25:56 +0000176 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
177 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000178};
179}
180
Nate Begeman5a5ca152009-04-29 05:20:52 +0000181/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
182/// performs the same shuffe in terms of order or result bytes, but on a type
183/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000184/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000185SDValue
186SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000187 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000188 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000189 unsigned NumMaskElts = VT.getVectorNumElements();
190 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000191 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000192
Nate Begeman9008ca62009-04-27 18:41:29 +0000193 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
194
195 if (NumEltsGrowth == 1)
196 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000197
Nate Begeman9008ca62009-04-27 18:41:29 +0000198 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000199 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000200 int Idx = Mask[i];
201 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000202 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000203 NewMask.push_back(-1);
204 else
205 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000206 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000207 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000208 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000209 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
210 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000211}
212
Bill Wendling5aa49772009-02-24 02:35:30 +0000213SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000214 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000215 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
216 DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000217 ValueTypeActions(TLI.getValueTypeActions()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000219 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000220}
221
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000223 LastCALLSEQ_END = DAG.getEntryNode();
Mon P Wange5ab34e2009-02-04 19:38:14 +0000224
Chris Lattnerab510a72005-10-02 17:49:46 +0000225 // The legalize process is inherently a bottom-up recursive process (users
226 // legalize their uses before themselves). Given infinite stack space, we
227 // could just start legalizing on the root and traverse the whole graph. In
228 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000229 // blocks. To avoid this problem, compute an ordering of the nodes where each
230 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000231 DAG.AssignTopologicalOrder();
232 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000233 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000234 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000235
236 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000237 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000238 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
239 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000240
Chris Lattner3e928bb2005-01-07 07:47:09 +0000241 LegalizedNodes.clear();
242
243 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000244 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245}
246
Chris Lattner6831a812006-02-13 09:18:02 +0000247
248/// FindCallEndFromCallStart - Given a chained node that is part of a call
249/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000250static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000251 // Nested CALLSEQ_START/END constructs aren't yet legal,
252 // but we can DTRT and handle them correctly here.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000253 if (Node->getOpcode() == ISD::CALLSEQ_START)
254 depth++;
Stuart Hastings2965e692010-12-21 17:07:24 +0000255 else if (Node->getOpcode() == ISD::CALLSEQ_END) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000256 depth--;
257 if (depth == 0)
258 return Node;
259 }
Chris Lattner6831a812006-02-13 09:18:02 +0000260 if (Node->use_empty())
261 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000262
Chris Lattner6831a812006-02-13 09:18:02 +0000263 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000264 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000266 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000269 // Otherwise, hunt for it.
270 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000271 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000272 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000273 break;
274 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
276 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000278 return 0;
279 }
280 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Chris Lattner6831a812006-02-13 09:18:02 +0000282 for (SDNode::use_iterator UI = Node->use_begin(),
283 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Chris Lattner6831a812006-02-13 09:18:02 +0000285 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000286 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000287 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
288 if (User->getOperand(i) == TheChain)
Stuart Hastingsa304d022010-12-09 21:25:20 +0000289 if (SDNode *Result = FindCallEndFromCallStart(User, depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000290 return Result;
291 }
292 return 0;
293}
294
Scott Michelfdc40a02009-02-17 22:15:04 +0000295/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000296/// sequence, find the CALLSEQ_START node that initiates the call sequence.
297static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000298 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000299 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000300 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
301 Node = Node->getOperand(0).getNode();
302 assert(Node->getOperand(0).getValueType() == MVT::Other &&
303 "Node doesn't have a token chain argument!");
304 switch (Node->getOpcode()) {
305 default:
306 break;
307 case ISD::CALLSEQ_START:
308 if (!nested)
309 return Node;
310 nested--;
311 break;
312 case ISD::CALLSEQ_END:
313 nested++;
314 break;
315 }
316 }
317 return 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000318}
319
320/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000321/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000322/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000323///
324/// Keep track of the nodes we fine that actually do lead to Dest in
325/// NodesLeadingTo. This avoids retraversing them exponential number of times.
326///
327bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000328 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000329 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000330
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000331 // If we've already processed this node and it does lead to Dest, there is no
332 // need to reprocess it.
333 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000334
Chris Lattner6831a812006-02-13 09:18:02 +0000335 // If the first result of this node has been already legalized, then it cannot
336 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000337 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000338
Chris Lattner6831a812006-02-13 09:18:02 +0000339 // Okay, this node has not already been legalized. Check and legalize all
340 // operands. If none lead to Dest, then we can legalize this node.
341 bool OperandsLeadToDest = false;
342 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
343 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000344 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
345 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000346
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000347 if (OperandsLeadToDest) {
348 NodesLeadingTo.insert(N);
349 return true;
350 }
Chris Lattner6831a812006-02-13 09:18:02 +0000351
352 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000353 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000354 return false;
355}
356
Evan Cheng9f877882006-12-13 20:57:08 +0000357/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
358/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000359static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000360 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000361 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000362 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000363
364 // If a FP immediate is precise when represented as a float and if the
365 // target can do an extending load from float to double, we put it into
366 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000367 // double. This shrinks FP constants and canonicalizes them for targets where
368 // an FP extending load is the same cost as a normal load (such as on the x87
369 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000370 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000371 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000372 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000374 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000376 }
377
Owen Andersone50ed302009-08-10 22:56:29 +0000378 EVT OrigVT = VT;
379 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 while (SVT != MVT::f32) {
381 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000382 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000383 // Only do this if the target has a native EXTLOAD instruction from
384 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000385 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000386 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000387 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000388 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000389 VT = SVT;
390 Extend = true;
391 }
Evan Cheng00495212006-12-12 21:32:44 +0000392 }
393
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000395 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000396 if (Extend)
Evan Chengbcc80172010-07-07 22:15:37 +0000397 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, dl,
398 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000399 CPIdx, MachinePointerInfo::getConstantPool(),
400 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000401 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000402 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000403 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000404}
405
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000406/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
407static
Dan Gohman475871a2008-07-27 21:46:04 +0000408SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000409 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000410 SDValue Chain = ST->getChain();
411 SDValue Ptr = ST->getBasePtr();
412 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000413 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000414 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000415 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000416 if (ST->getMemoryVT().isFloatingPoint() ||
417 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000418 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000419 if (TLI.isTypeLegal(intVT)) {
420 // Expand to a bitconvert of the value to the integer type of the
421 // same size, then a (misaligned) int store.
422 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000423 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000424 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
425 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000426 } else {
427 // Do a (aligned) store to a stack slot, then copy from the stack slot
428 // to the final destination using (unaligned) integer loads and stores.
Owen Andersone50ed302009-08-10 22:56:29 +0000429 EVT StoredVT = ST->getMemoryVT();
430 EVT RegVT =
Evan Chengadf97992010-04-15 01:25:27 +0000431 TLI.getRegisterType(*DAG.getContext(),
432 EVT::getIntegerVT(*DAG.getContext(),
433 StoredVT.getSizeInBits()));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000434 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
435 unsigned RegBytes = RegVT.getSizeInBits() / 8;
436 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000437
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000438 // Make sure the stack slot is also aligned for the register type.
439 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
440
441 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000442 SDValue Store = DAG.getTruncStore(Chain, dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000443 Val, StackPtr, MachinePointerInfo(),
444 StoredVT, false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000445 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
446 SmallVector<SDValue, 8> Stores;
447 unsigned Offset = 0;
448
449 // Do all but one copies using the full register width.
450 for (unsigned i = 1; i < NumRegs; i++) {
451 // Load one integer register's worth from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000452 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
453 MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000454 false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000455 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000456 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000457 ST->getPointerInfo().getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +0000458 ST->isVolatile(), ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000459 MinAlign(ST->getAlignment(), Offset)));
460 // Increment the pointers.
461 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000462 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000463 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000464 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000465 }
466
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000467 // The last store may be partial. Do a truncating store. On big-endian
468 // machines this requires an extending load from the stack slot to ensure
469 // that the bits are in the right place.
Evan Chengadf97992010-04-15 01:25:27 +0000470 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
471 8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000472
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000473 // Load from the stack slot.
Evan Chengbcc80172010-07-07 22:15:37 +0000474 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Store, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000475 MachinePointerInfo(),
476 MemVT, false, false, 0);
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000477
Dale Johannesenbb5da912009-02-02 20:41:04 +0000478 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000479 ST->getPointerInfo()
480 .getWithOffset(Offset),
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000481 MemVT, ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000482 ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000483 MinAlign(ST->getAlignment(), Offset)));
484 // The order of the stores doesn't matter - say it with a TokenFactor.
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000486 Stores.size());
487 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000488 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000489 assert(ST->getMemoryVT().isInteger() &&
490 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000491 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000492 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000493 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000494 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000495 int IncrementSize = NumBits / 8;
496
497 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000498 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
499 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000500 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000501
502 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000503 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000504 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000505 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000506 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000507 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000508 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000509 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000510 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000511 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000512 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
513 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000514
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000516}
517
518/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
519static
Dan Gohman475871a2008-07-27 21:46:04 +0000520SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000521 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000522 SDValue Chain = LD->getChain();
523 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000524 EVT VT = LD->getValueType(0);
525 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000526 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000527 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000528 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000529 if (TLI.isTypeLegal(intVT)) {
530 // Expand to a (misaligned) integer load of the same size,
531 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000532 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
533 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000534 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000535 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000536 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000537 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000538
Duncan Sands05e11fa2008-12-12 21:47:02 +0000539 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000540 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000541 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000542
Chris Lattnerecf42c42010-09-21 16:36:31 +0000543 // Copy the value to a (aligned) stack slot using (unaligned) integer
544 // loads and stores, then do a (aligned) load from the stack slot.
545 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
546 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
547 unsigned RegBytes = RegVT.getSizeInBits() / 8;
548 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
549
550 // Make sure the stack slot is also aligned for the register type.
551 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
552
553 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
554 SmallVector<SDValue, 8> Stores;
555 SDValue StackPtr = StackBase;
556 unsigned Offset = 0;
557
558 // Do all but one copies using the full register width.
559 for (unsigned i = 1; i < NumRegs; i++) {
560 // Load one integer register's worth from the original location.
561 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
562 LD->getPointerInfo().getWithOffset(Offset),
563 LD->isVolatile(), LD->isNonTemporal(),
564 MinAlign(LD->getAlignment(), Offset));
565 // Follow the load with a store to the stack slot. Remember the store.
566 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000567 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000568 // Increment the pointers.
569 Offset += RegBytes;
570 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
571 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
572 Increment);
573 }
574
575 // The last copy may be partial. Do an extending load.
576 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
577 8 * (LoadedBytes - Offset));
578 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Chain, Ptr,
579 LD->getPointerInfo().getWithOffset(Offset),
580 MemVT, LD->isVolatile(),
581 LD->isNonTemporal(),
582 MinAlign(LD->getAlignment(), Offset));
583 // Follow the load with a store to the stack slot. Remember the store.
584 // On big-endian machines this requires a truncating store to ensure
585 // that the bits end up in the right place.
586 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
587 MachinePointerInfo(), MemVT,
588 false, false, 0));
589
590 // The order of the stores doesn't matter - say it with a TokenFactor.
591 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
592 Stores.size());
593
594 // Finally, perform the original load only redirected to the stack slot.
595 Load = DAG.getExtLoad(LD->getExtensionType(), VT, dl, TF, StackBase,
596 MachinePointerInfo(), LoadedVT, false, false, 0);
597
598 // Callers expect a MERGE_VALUES node.
599 SDValue Ops[] = { Load, TF };
600 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000601 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000602 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000603 "Unaligned load of unsupported type.");
604
Dale Johannesen8155d642008-02-27 22:36:00 +0000605 // Compute the new VT that is half the size of the old one. This is an
606 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000607 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000608 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000609 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000610 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000611
Chris Lattnere400af82007-11-19 21:38:03 +0000612 unsigned Alignment = LD->getAlignment();
613 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000614 ISD::LoadExtType HiExtType = LD->getExtensionType();
615
616 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
617 if (HiExtType == ISD::NON_EXTLOAD)
618 HiExtType = ISD::ZEXTLOAD;
619
620 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000621 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000622 if (TLI.isLittleEndian()) {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000623 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr, LD->getPointerInfo(),
624 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000625 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000626 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000627 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000628 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr,
629 LD->getPointerInfo().getWithOffset(IncrementSize),
630 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000631 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000632 } else {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000633 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr, LD->getPointerInfo(),
634 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000635 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000636 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000637 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000638 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr,
639 LD->getPointerInfo().getWithOffset(IncrementSize),
640 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000641 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000642 }
643
644 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000645 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000646 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
647 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000648
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000650 Hi.getValue(1));
651
Dan Gohman475871a2008-07-27 21:46:04 +0000652 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000653 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000654}
Evan Cheng912095b2007-01-04 21:56:39 +0000655
Nate Begeman68679912008-04-25 18:07:40 +0000656/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
657/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
658/// is necessary to spill the vector being inserted into to memory, perform
659/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000660SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000661PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
662 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000663 SDValue Tmp1 = Vec;
664 SDValue Tmp2 = Val;
665 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000666
Nate Begeman68679912008-04-25 18:07:40 +0000667 // If the target doesn't support this, we have to spill the input vector
668 // to a temporary stack slot, update the element, then reload it. This is
669 // badness. We could also load the value into a vector register (either
670 // with a "move to register" or "extload into register" instruction, then
671 // permute it into place, if the idx is a constant and if the idx is
672 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000673 EVT VT = Tmp1.getValueType();
674 EVT EltVT = VT.getVectorElementType();
675 EVT IdxVT = Tmp3.getValueType();
676 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000677 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000678
Evan Chengff89dcb2009-10-18 18:16:27 +0000679 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
680
Nate Begeman68679912008-04-25 18:07:40 +0000681 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000682 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000683 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000684 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000685
686 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000687 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000688 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000689 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000690 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000691 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
692 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000693 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000694 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000695 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000696 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000697 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000698 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000699}
700
Mon P Wange9f10152008-12-09 05:46:39 +0000701
Eli Friedman3f727d62009-05-27 02:16:40 +0000702SDValue SelectionDAGLegalize::
703ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
704 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
705 // SCALAR_TO_VECTOR requires that the type of the value being inserted
706 // match the element type of the vector being created, except for
707 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000708 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000709 if (Val.getValueType() == EltVT ||
710 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
711 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
712 Vec.getValueType(), Val);
713
714 unsigned NumElts = Vec.getValueType().getVectorNumElements();
715 // We generate a shuffle of InVec and ScVec, so the shuffle mask
716 // should be 0,1,2,3,4,5... with the appropriate element replaced with
717 // elt 0 of the RHS.
718 SmallVector<int, 8> ShufOps;
719 for (unsigned i = 0; i != NumElts; ++i)
720 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
721
722 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
723 &ShufOps[0]);
724 }
725 }
726 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
727}
728
Eli Friedman7ef3d172009-06-06 07:04:42 +0000729SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
730 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
731 // FIXME: We shouldn't do this for TargetConstantFP's.
732 // FIXME: move this to the DAG Combiner! Note that we can't regress due
733 // to phase ordering between legalized code and the dag combiner. This
734 // probably means that we need to integrate dag combiner and legalizer
735 // together.
736 // We generally can't do this one for long doubles.
737 SDValue Tmp1 = ST->getChain();
738 SDValue Tmp2 = ST->getBasePtr();
739 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000740 unsigned Alignment = ST->getAlignment();
741 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000742 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000743 DebugLoc dl = ST->getDebugLoc();
744 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 if (CFP->getValueType(0) == MVT::f32 &&
746 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000747 Tmp3 = DAG.getConstant(CFP->getValueAPF().
748 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000750 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
751 isVolatile, isNonTemporal, Alignment);
752 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000753
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000754 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000755 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000757 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000759 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
760 isVolatile, isNonTemporal, Alignment);
761 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000763 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000764 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
765 // stores. If the target supports neither 32- nor 64-bits, this
766 // xform is certainly not worth it.
767 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000768 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000770 if (TLI.isBigEndian()) std::swap(Lo, Hi);
771
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000772 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
773 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000774 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
775 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000776 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
777 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000778 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000779
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000781 }
782 }
783 }
784 return SDValue();
785}
786
Dan Gohmana3466152007-07-13 20:14:11 +0000787/// LegalizeOp - We know that the specified value has a legal type, and
788/// that its operands are legal. Now ensure that the operation itself
789/// is legal, recursively ensuring that the operands' operations remain
790/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000791SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000792 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
793 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000794
Gabor Greifba36cb52008-08-28 21:40:38 +0000795 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000796 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000797
Eli Friedman1fde9c52009-05-24 02:46:31 +0000798 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
799 assert(getTypeAction(Node->getValueType(i)) == Legal &&
800 "Unexpected illegal type!");
801
802 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000803 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000804 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
805 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000806
Chris Lattner45982da2005-05-12 16:53:42 +0000807 // Note that LegalizeOp may be reentered even from single-use nodes, which
808 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000809 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000810 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000811
Dan Gohman475871a2008-07-27 21:46:04 +0000812 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
813 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000814 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000815
Eli Friedman8c377c72009-05-27 01:25:56 +0000816 // Figure out the correct action; the way to query this varies by opcode
817 TargetLowering::LegalizeAction Action;
818 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000819 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000820 case ISD::INTRINSIC_W_CHAIN:
821 case ISD::INTRINSIC_WO_CHAIN:
822 case ISD::INTRINSIC_VOID:
823 case ISD::VAARG:
824 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000826 break;
827 case ISD::SINT_TO_FP:
828 case ISD::UINT_TO_FP:
829 case ISD::EXTRACT_VECTOR_ELT:
830 Action = TLI.getOperationAction(Node->getOpcode(),
831 Node->getOperand(0).getValueType());
832 break;
833 case ISD::FP_ROUND_INREG:
834 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000835 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000836 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
837 break;
838 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000839 case ISD::SELECT_CC:
840 case ISD::SETCC:
841 case ISD::BR_CC: {
842 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
843 Node->getOpcode() == ISD::SETCC ? 2 : 1;
844 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000845 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000846 ISD::CondCode CCCode =
847 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
848 Action = TLI.getCondCodeAction(CCCode, OpVT);
849 if (Action == TargetLowering::Legal) {
850 if (Node->getOpcode() == ISD::SELECT_CC)
851 Action = TLI.getOperationAction(Node->getOpcode(),
852 Node->getValueType(0));
853 else
854 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
855 }
856 break;
857 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000858 case ISD::LOAD:
859 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000860 // FIXME: Model these properly. LOAD and STORE are complicated, and
861 // STORE expects the unlegalized operand in some cases.
862 SimpleFinishLegalizing = false;
863 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000864 case ISD::CALLSEQ_START:
865 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000866 // FIXME: This shouldn't be necessary. These nodes have special properties
867 // dealing with the recursive nature of legalization. Removing this
868 // special case should be done as part of making LegalizeDAG non-recursive.
869 SimpleFinishLegalizing = false;
870 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000871 case ISD::EXTRACT_ELEMENT:
872 case ISD::FLT_ROUNDS_:
873 case ISD::SADDO:
874 case ISD::SSUBO:
875 case ISD::UADDO:
876 case ISD::USUBO:
877 case ISD::SMULO:
878 case ISD::UMULO:
879 case ISD::FPOWI:
880 case ISD::MERGE_VALUES:
881 case ISD::EH_RETURN:
882 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000883 case ISD::EH_SJLJ_SETJMP:
884 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000885 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000886 // These operations lie about being legal: when they claim to be legal,
887 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000888 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
889 if (Action == TargetLowering::Legal)
890 Action = TargetLowering::Expand;
891 break;
892 case ISD::TRAMPOLINE:
893 case ISD::FRAMEADDR:
894 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000895 // These operations lie about being legal: when they claim to be legal,
896 // they should actually be custom-lowered.
897 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
898 if (Action == TargetLowering::Legal)
899 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000900 break;
901 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000902 // A weird case: legalization for BUILD_VECTOR never legalizes the
903 // operands!
904 // FIXME: This really sucks... changing it isn't semantically incorrect,
905 // but it massively pessimizes the code for floating-point BUILD_VECTORs
906 // because ConstantFP operands get legalized into constant pool loads
907 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
908 // though, because BUILD_VECTORS usually get lowered into other nodes
909 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000910 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000911 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000912 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000913 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000914 Action = TargetLowering::Legal;
915 } else {
916 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000917 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000918 break;
919 }
920
921 if (SimpleFinishLegalizing) {
922 SmallVector<SDValue, 8> Ops, ResultVals;
923 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
924 Ops.push_back(LegalizeOp(Node->getOperand(i)));
925 switch (Node->getOpcode()) {
926 default: break;
927 case ISD::BR:
928 case ISD::BRIND:
929 case ISD::BR_JT:
930 case ISD::BR_CC:
931 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000932 // Branches tweak the chain to include LastCALLSEQ_END
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Eli Friedman8c377c72009-05-27 01:25:56 +0000934 LastCALLSEQ_END);
935 Ops[0] = LegalizeOp(Ops[0]);
936 LastCALLSEQ_END = DAG.getEntryNode();
937 break;
938 case ISD::SHL:
939 case ISD::SRL:
940 case ISD::SRA:
941 case ISD::ROTL:
942 case ISD::ROTR:
943 // Legalizing shifts/rotates requires adjusting the shift amount
944 // to the appropriate width.
945 if (!Ops[1].getValueType().isVector())
946 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
947 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000948 case ISD::SRL_PARTS:
949 case ISD::SRA_PARTS:
950 case ISD::SHL_PARTS:
951 // Legalizing shifts/rotates requires adjusting the shift amount
952 // to the appropriate width.
953 if (!Ops[2].getValueType().isVector())
954 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000955 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000956 }
957
Dan Gohman027657d2010-06-18 15:30:29 +0000958 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
959 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000960 switch (Action) {
961 case TargetLowering::Legal:
962 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
963 ResultVals.push_back(Result.getValue(i));
964 break;
965 case TargetLowering::Custom:
966 // FIXME: The handling for custom lowering with multiple results is
967 // a complete mess.
968 Tmp1 = TLI.LowerOperation(Result, DAG);
969 if (Tmp1.getNode()) {
970 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
971 if (e == 1)
972 ResultVals.push_back(Tmp1);
973 else
974 ResultVals.push_back(Tmp1.getValue(i));
975 }
976 break;
977 }
978
979 // FALL THROUGH
980 case TargetLowering::Expand:
981 ExpandNode(Result.getNode(), ResultVals);
982 break;
983 case TargetLowering::Promote:
984 PromoteNode(Result.getNode(), ResultVals);
985 break;
986 }
987 if (!ResultVals.empty()) {
988 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
989 if (ResultVals[i] != SDValue(Node, i))
990 ResultVals[i] = LegalizeOp(ResultVals[i]);
991 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
992 }
993 return ResultVals[Op.getResNo()];
994 }
995 }
996
997 switch (Node->getOpcode()) {
998 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000999#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001000 dbgs() << "NODE: ";
1001 Node->dump( &DAG);
1002 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001003#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001004 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001005
Chris Lattnerb2827b02006-03-19 00:52:58 +00001006 case ISD::BUILD_VECTOR:
1007 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001008 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001009 case TargetLowering::Custom:
1010 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001011 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001012 Result = Tmp3;
1013 break;
1014 }
1015 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001016 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001017 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001018 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001019 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001020 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001021 case ISD::CALLSEQ_START: {
Stuart Hastingsf2243222011-01-18 00:09:27 +00001022 static int depth = 0;
Chris Lattner6831a812006-02-13 09:18:02 +00001023 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001024
Chris Lattner6831a812006-02-13 09:18:02 +00001025 // Recursively Legalize all of the inputs of the call end that do not lead
1026 // to this call start. This ensures that any libcalls that need be inserted
1027 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001028 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001029 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001030 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001031 NodesLeadingTo);
1032 }
Chris Lattner6831a812006-02-13 09:18:02 +00001033
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001034 // Now that we have legalized all of the inputs (which may have inserted
1035 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1037
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001038 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001039 // call ended.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001040 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken && depth == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001042 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001043 Tmp1 = LegalizeOp(Tmp1);
1044 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Chris Lattner6831a812006-02-13 09:18:02 +00001046 // Do not try to legalize the target-specific arguments (#1+).
1047 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001048 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001049 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001050 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1051 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001052 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001053
Chris Lattner6831a812006-02-13 09:18:02 +00001054 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001055 AddLegalizedOperand(Op.getValue(0), Result);
1056 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1057 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001058
Chris Lattner6831a812006-02-13 09:18:02 +00001059 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001060 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001061 // process, no libcalls can/will be inserted, guaranteeing that no calls
1062 // can overlap.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001063
1064 SDValue Saved_LastCALLSEQ_END = LastCALLSEQ_END ;
Chris Lattner6831a812006-02-13 09:18:02 +00001065 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001066 LastCALLSEQ_END = SDValue(CallEnd, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001067
Stuart Hastingsf2243222011-01-18 00:09:27 +00001068 depth++;
Chris Lattner6831a812006-02-13 09:18:02 +00001069 // Legalize the call, starting from the CALLSEQ_END.
1070 LegalizeOp(LastCALLSEQ_END);
Stuart Hastingsf2243222011-01-18 00:09:27 +00001071 depth--;
1072 assert(depth >= 0 && "Un-matched CALLSEQ_START?");
1073 if (depth > 0)
1074 LastCALLSEQ_END = Saved_LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +00001075 return Result;
1076 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001077 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001078 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1079 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001081 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1082 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001083 assert(I != LegalizedNodes.end() &&
1084 "Legalizing the call start should have legalized this node!");
1085 return I->second;
1086 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001087
1088 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001089 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001091 // Do not try to legalize the target-specific arguments (#1+), except for
1092 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001093 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001094 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001095 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001096 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001097 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1098 &Ops[0], Ops.size()),
1099 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001100 }
1101 } else {
1102 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1103 if (Tmp1 != Node->getOperand(0) ||
1104 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001105 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001106 Ops[0] = Tmp1;
1107 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001108 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1109 &Ops[0], Ops.size()),
1110 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001111 }
Chris Lattner6a542892006-01-24 05:48:21 +00001112 }
Chris Lattner6831a812006-02-13 09:18:02 +00001113 // This finishes up call legalization.
Chris Lattner4b653a02006-02-14 00:55:02 +00001114 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001115 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001116 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001117 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001118 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001119 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001120 LoadSDNode *LD = cast<LoadSDNode>(Node);
1121 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1122 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001123
Evan Cheng466685d2006-10-09 20:57:25 +00001124 ISD::LoadExtType ExtType = LD->getExtensionType();
1125 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001126 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001127 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1128 Tmp1, Tmp2, LD->getOffset()),
1129 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001130 Tmp3 = Result.getValue(0);
1131 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001132
Evan Cheng466685d2006-10-09 20:57:25 +00001133 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001134 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001135 case TargetLowering::Legal:
1136 // If this is an unaligned load and the target doesn't support it,
1137 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001138 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001139 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1140 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001141 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001142 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001143 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001144 Tmp3 = Result.getOperand(0);
1145 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001146 Tmp3 = LegalizeOp(Tmp3);
1147 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001148 }
1149 }
1150 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001151 case TargetLowering::Custom:
1152 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001153 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001154 Tmp3 = LegalizeOp(Tmp1);
1155 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001156 }
Evan Cheng466685d2006-10-09 20:57:25 +00001157 break;
1158 case TargetLowering::Promote: {
1159 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001160 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001161 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001162 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001163
Chris Lattnerecf42c42010-09-21 16:36:31 +00001164 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001165 LD->isVolatile(), LD->isNonTemporal(),
1166 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001167 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001168 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001169 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001170 }
Evan Cheng466685d2006-10-09 20:57:25 +00001171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001172 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001173 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001174 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1175 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001176 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001177 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001178
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001179 EVT SrcVT = LD->getMemoryVT();
1180 unsigned SrcWidth = SrcVT.getSizeInBits();
1181 unsigned Alignment = LD->getAlignment();
1182 bool isVolatile = LD->isVolatile();
1183 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001184
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001185 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1186 // Some targets pretend to have an i1 loading operation, and actually
1187 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1188 // bits are guaranteed to be zero; it helps the optimizers understand
1189 // that these bits are zero. It is also useful for EXTLOAD, since it
1190 // tells the optimizers that those bits are undefined. It would be
1191 // nice to have an effective generic way of getting these benefits...
1192 // Until such a way is found, don't insist on promoting i1 here.
1193 (SrcVT != MVT::i1 ||
1194 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1195 // Promote to a byte-sized load if not loading an integral number of
1196 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1197 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1198 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1199 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001200
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001201 // The extra bits are guaranteed to be zero, since we stored them that
1202 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001203
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001204 ISD::LoadExtType NewExtType =
1205 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001206
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001207 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0), dl,
1208 Tmp1, Tmp2, LD->getPointerInfo(),
1209 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001210
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001211 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001212
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001213 if (ExtType == ISD::SEXTLOAD)
1214 // Having the top bits zero doesn't help when sign extending.
1215 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1216 Result.getValueType(),
1217 Result, DAG.getValueType(SrcVT));
1218 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1219 // All the top bits are guaranteed to be zero - inform the optimizers.
1220 Result = DAG.getNode(ISD::AssertZext, dl,
1221 Result.getValueType(), Result,
1222 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001223
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001224 Tmp1 = LegalizeOp(Result);
1225 Tmp2 = LegalizeOp(Ch);
1226 } else if (SrcWidth & (SrcWidth - 1)) {
1227 // If not loading a power-of-2 number of bits, expand as two loads.
1228 assert(!SrcVT.isVector() && "Unsupported extload!");
1229 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1230 assert(RoundWidth < SrcWidth);
1231 unsigned ExtraWidth = SrcWidth - RoundWidth;
1232 assert(ExtraWidth < RoundWidth);
1233 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1234 "Load size not an integral number of bytes!");
1235 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1236 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1237 SDValue Lo, Hi, Ch;
1238 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001239
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001240 if (TLI.isLittleEndian()) {
1241 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1242 // Load the bottom RoundWidth bits.
1243 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), dl,
1244 Tmp1, Tmp2,
1245 LD->getPointerInfo(), RoundVT, isVolatile,
1246 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001247
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001248 // Load the remaining ExtraWidth bits.
1249 IncrementSize = RoundWidth / 8;
1250 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1251 DAG.getIntPtrConstant(IncrementSize));
1252 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), dl, Tmp1, Tmp2,
1253 LD->getPointerInfo().getWithOffset(IncrementSize),
1254 ExtraVT, isVolatile, isNonTemporal,
1255 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001256
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001257 // Build a factor node to remember that this load is independent of
1258 // the other one.
1259 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1260 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001261
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001262 // Move the top bits to the right place.
1263 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1264 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001265
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001266 // Join the hi and lo parts.
1267 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001268 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001269 // Big endian - avoid unaligned loads.
1270 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1271 // Load the top RoundWidth bits.
1272 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), dl, Tmp1, Tmp2,
1273 LD->getPointerInfo(), RoundVT, isVolatile,
1274 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001275
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001276 // Load the remaining ExtraWidth bits.
1277 IncrementSize = RoundWidth / 8;
1278 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1279 DAG.getIntPtrConstant(IncrementSize));
1280 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
1281 Node->getValueType(0), dl, Tmp1, Tmp2,
1282 LD->getPointerInfo().getWithOffset(IncrementSize),
1283 ExtraVT, isVolatile, isNonTemporal,
1284 MinAlign(Alignment, IncrementSize));
1285
1286 // Build a factor node to remember that this load is independent of
1287 // the other one.
1288 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1289 Hi.getValue(1));
1290
1291 // Move the top bits to the right place.
1292 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1293 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1294
1295 // Join the hi and lo parts.
1296 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001297 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001298
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001299 Tmp1 = LegalizeOp(Result);
1300 Tmp2 = LegalizeOp(Ch);
1301 } else {
1302 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1303 default: assert(0 && "This action is not supported yet!");
1304 case TargetLowering::Custom:
1305 isCustom = true;
1306 // FALLTHROUGH
1307 case TargetLowering::Legal:
1308 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1309 Tmp1, Tmp2, LD->getOffset()),
1310 Result.getResNo());
1311 Tmp1 = Result.getValue(0);
1312 Tmp2 = Result.getValue(1);
1313
1314 if (isCustom) {
1315 Tmp3 = TLI.LowerOperation(Result, DAG);
1316 if (Tmp3.getNode()) {
1317 Tmp1 = LegalizeOp(Tmp3);
1318 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1319 }
1320 } else {
1321 // If this is an unaligned load and the target doesn't support it,
1322 // expand it.
1323 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1324 const Type *Ty =
1325 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1326 unsigned ABIAlignment =
1327 TLI.getTargetData()->getABITypeAlignment(Ty);
1328 if (LD->getAlignment() < ABIAlignment){
1329 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1330 DAG, TLI);
1331 Tmp1 = Result.getOperand(0);
1332 Tmp2 = Result.getOperand(1);
1333 Tmp1 = LegalizeOp(Tmp1);
1334 Tmp2 = LegalizeOp(Tmp2);
1335 }
1336 }
1337 }
1338 break;
1339 case TargetLowering::Expand:
1340 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1341 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1342 LD->getPointerInfo(),
1343 LD->isVolatile(), LD->isNonTemporal(),
1344 LD->getAlignment());
1345 unsigned ExtendOp;
1346 switch (ExtType) {
1347 case ISD::EXTLOAD:
1348 ExtendOp = (SrcVT.isFloatingPoint() ?
1349 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1350 break;
1351 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1352 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1353 default: llvm_unreachable("Unexpected extend load type!");
1354 }
1355 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1356 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1357 Tmp2 = LegalizeOp(Load.getValue(1));
1358 break;
1359 }
1360 // FIXME: This does not work for vectors on most targets. Sign- and
1361 // zero-extend operations are currently folded into extending loads,
1362 // whether they are legal or not, and then we end up here without any
1363 // support for legalizing them.
1364 assert(ExtType != ISD::EXTLOAD &&
1365 "EXTLOAD should always be supported!");
1366 // Turn the unsupported load into an EXTLOAD followed by an explicit
1367 // zero/sign extend inreg.
1368 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), dl,
1369 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1370 LD->isVolatile(), LD->isNonTemporal(),
1371 LD->getAlignment());
1372 SDValue ValRes;
1373 if (ExtType == ISD::SEXTLOAD)
1374 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1375 Result.getValueType(),
1376 Result, DAG.getValueType(SrcVT));
1377 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001378 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001379 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1380 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1381 break;
1382 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001383 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001384
1385 // Since loads produce two values, make sure to remember that we legalized
1386 // both of them.
1387 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1388 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1389 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001390 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001391 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001392 StoreSDNode *ST = cast<StoreSDNode>(Node);
1393 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1394 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001395 unsigned Alignment = ST->getAlignment();
1396 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001397 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001398
Evan Cheng8b2794a2006-10-13 21:14:26 +00001399 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001400 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1401 Result = SDValue(OptStore, 0);
1402 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001403 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001404
Eli Friedman957bffa2009-05-24 08:42:01 +00001405 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001406 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001407 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1408 Tmp1, Tmp3, Tmp2,
1409 ST->getOffset()),
1410 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001411
Owen Andersone50ed302009-08-10 22:56:29 +00001412 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001413 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001414 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001415 case TargetLowering::Legal:
1416 // If this is an unaligned store and the target doesn't support it,
1417 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001418 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001419 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001420 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001421 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001422 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1423 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001424 }
1425 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001426 case TargetLowering::Custom:
1427 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001428 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001429 break;
1430 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001431 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001432 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001433 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001434 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001435 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001436 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001437 break;
1438 }
1439 break;
1440 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001441 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001442 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001443
Owen Andersone50ed302009-08-10 22:56:29 +00001444 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001445 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001446
Duncan Sands83ec4b62008-06-06 12:08:01 +00001447 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001448 // Promote to a byte-sized store with upper bits zero if not
1449 // storing an integral number of bytes. For example, promote
1450 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001451 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1452 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001453 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001454 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1455 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001456 } else if (StWidth & (StWidth - 1)) {
1457 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001458 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001459 unsigned RoundWidth = 1 << Log2_32(StWidth);
1460 assert(RoundWidth < StWidth);
1461 unsigned ExtraWidth = StWidth - RoundWidth;
1462 assert(ExtraWidth < RoundWidth);
1463 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1464 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001465 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1466 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001467 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001468 unsigned IncrementSize;
1469
1470 if (TLI.isLittleEndian()) {
1471 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1472 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001473 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1474 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001475 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001476
1477 // Store the remaining ExtraWidth bits.
1478 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001479 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001480 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001481 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001482 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001483 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1484 ST->getPointerInfo().getWithOffset(IncrementSize),
1485 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001486 MinAlign(Alignment, IncrementSize));
1487 } else {
1488 // Big endian - avoid unaligned stores.
1489 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1490 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001491 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001492 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001493 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1494 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001495
1496 // Store the remaining ExtraWidth bits.
1497 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001498 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001499 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001500 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1501 ST->getPointerInfo().getWithOffset(IncrementSize),
1502 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001503 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001504 }
Duncan Sands7e857202008-01-22 07:17:34 +00001505
1506 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001507 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001508 } else {
1509 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1510 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001511 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1512 Tmp1, Tmp3, Tmp2,
1513 ST->getOffset()),
1514 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001515
1516 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001517 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001518 case TargetLowering::Legal:
1519 // If this is an unaligned store and the target doesn't support it,
1520 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001521 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001522 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001523 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001524 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001525 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1526 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001527 }
1528 break;
1529 case TargetLowering::Custom:
1530 Result = TLI.LowerOperation(Result, DAG);
1531 break;
1532 case Expand:
1533 // TRUNCSTORE:i16 i32 -> STORE i16
1534 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001535 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001536 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1537 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001538 break;
1539 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001540 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001541 }
1542 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001543 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001544 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001545 assert(Result.getValueType() == Op.getValueType() &&
1546 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001547
Chris Lattner456a93a2006-01-28 07:39:30 +00001548 // Make sure that the generated code is itself legal.
1549 if (Result != Op)
1550 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001551
Chris Lattner45982da2005-05-12 16:53:42 +00001552 // Note that LegalizeOp may be reentered even from single-use nodes, which
1553 // means that we always must cache transformed nodes.
1554 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001555 return Result;
1556}
1557
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001558SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1559 SDValue Vec = Op.getOperand(0);
1560 SDValue Idx = Op.getOperand(1);
1561 DebugLoc dl = Op.getDebugLoc();
1562 // Store the value to a temporary stack slot, then LOAD the returned part.
1563 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001564 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1565 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001566
1567 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001568 unsigned EltSize =
1569 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001570 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1571 DAG.getConstant(EltSize, Idx.getValueType()));
1572
1573 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1574 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1575 else
1576 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1577
1578 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1579
Eli Friedmanc680ac92009-07-09 22:01:03 +00001580 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001581 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001582 false, false, 0);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001583 return DAG.getExtLoad(ISD::EXTLOAD, Op.getValueType(), dl, Ch, StackPtr,
1584 MachinePointerInfo(),
1585 Vec.getValueType().getVectorElementType(),
1586 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001587}
1588
David Greenecfe33c42011-01-26 19:13:22 +00001589SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1590 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1591
1592 SDValue Vec = Op.getOperand(0);
1593 SDValue Part = Op.getOperand(1);
1594 SDValue Idx = Op.getOperand(2);
1595 DebugLoc dl = Op.getDebugLoc();
1596
1597 // Store the value to a temporary stack slot, then LOAD the returned part.
1598
1599 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
1600 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1601 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
1602
1603 // First store the whole vector.
1604 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1605 false, false, 0);
1606
1607 // Then store the inserted part.
1608
1609 // Add the offset to the index.
1610 unsigned EltSize =
1611 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
1612
1613 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1614 DAG.getConstant(EltSize, Idx.getValueType()));
1615
1616 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1617 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1618 else
1619 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1620
1621 SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx,
1622 StackPtr);
1623
1624 // Store the subvector.
1625 Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr,
1626 MachinePointerInfo(), false, false, 0);
1627
1628 // Finally, load the updated vector.
1629 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1630 false, false, 0);
1631}
1632
Eli Friedman7ef3d172009-06-06 07:04:42 +00001633SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1634 // We can't handle this case efficiently. Allocate a sufficiently
1635 // aligned object on the stack, store each element into it, then load
1636 // the result as a vector.
1637 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001638 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001639 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001640 DebugLoc dl = Node->getDebugLoc();
1641 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001642 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001643 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001644
1645 // Emit a store of each element to the stack slot.
1646 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001647 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001648 // Store (in the right endianness) the elements to memory.
1649 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1650 // Ignore undef elements.
1651 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1652
1653 unsigned Offset = TypeByteSize*i;
1654
1655 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1656 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1657
Dan Gohman9949dd62010-02-25 20:30:49 +00001658 // If the destination vector element type is narrower than the source
1659 // element type, only store the bits necessary.
1660 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001661 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001662 Node->getOperand(i), Idx,
1663 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001664 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001665 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001666 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001667 Node->getOperand(i), Idx,
1668 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001669 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001670 }
1671
1672 SDValue StoreChain;
1673 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001674 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001675 &Stores[0], Stores.size());
1676 else
1677 StoreChain = DAG.getEntryNode();
1678
1679 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001680 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001681}
1682
Eli Friedman4bc8c712009-05-27 12:20:41 +00001683SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1684 DebugLoc dl = Node->getDebugLoc();
1685 SDValue Tmp1 = Node->getOperand(0);
1686 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001687
1688 // Get the sign bit of the RHS. First obtain a value that has the same
1689 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001690 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001691 EVT FloatVT = Tmp2.getValueType();
1692 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001693 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001694 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001695 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001696 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001697 // Store the float to memory, then load the sign part out as an integer.
1698 MVT LoadTy = TLI.getPointerTy();
1699 // First create a temporary that is aligned for both the load and store.
1700 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1701 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001702 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001703 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001704 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001705 if (TLI.isBigEndian()) {
1706 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1707 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001708 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1709 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001710 } else { // Little endian
1711 SDValue LoadPtr = StackPtr;
1712 // The float may be wider than the integer we are going to load. Advance
1713 // the pointer so that the loaded integer will contain the sign bit.
1714 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1715 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1716 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1717 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1718 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001719 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1720 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001721 // Move the sign bit to the top bit of the loaded integer.
1722 unsigned BitShift = LoadTy.getSizeInBits() -
1723 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1724 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1725 if (BitShift)
1726 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
1727 DAG.getConstant(BitShift,TLI.getShiftAmountTy()));
1728 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001729 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001730 // Now get the sign bit proper, by seeing whether the value is negative.
1731 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1732 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1733 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001734 // Get the absolute value of the result.
1735 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1736 // Select between the nabs and abs value based on the sign bit of
1737 // the input.
1738 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1739 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1740 AbsVal);
1741}
1742
Eli Friedman4bc8c712009-05-27 12:20:41 +00001743void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1744 SmallVectorImpl<SDValue> &Results) {
1745 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1746 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1747 " not tell us which reg is the stack pointer!");
1748 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001749 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001750 SDValue Tmp1 = SDValue(Node, 0);
1751 SDValue Tmp2 = SDValue(Node, 1);
1752 SDValue Tmp3 = Node->getOperand(2);
1753 SDValue Chain = Tmp1.getOperand(0);
1754
1755 // Chain the dynamic stack allocation so that it doesn't modify the stack
1756 // pointer when other instructions are using the stack.
1757 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1758
1759 SDValue Size = Tmp2.getOperand(1);
1760 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1761 Chain = SP.getValue(1);
1762 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001763 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001764 if (Align > StackAlign)
1765 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1766 DAG.getConstant(-(uint64_t)Align, VT));
1767 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1768 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1769
1770 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1771 DAG.getIntPtrConstant(0, true), SDValue());
1772
1773 Results.push_back(Tmp1);
1774 Results.push_back(Tmp2);
1775}
1776
Evan Cheng7f042682008-10-15 02:05:31 +00001777/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001778/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001779/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001780void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001781 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001782 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001783 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001784 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001785 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1786 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001787 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001788 case TargetLowering::Legal:
1789 // Nothing to do.
1790 break;
1791 case TargetLowering::Expand: {
1792 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1793 unsigned Opc = 0;
1794 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001795 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001796 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1797 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1798 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1799 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1800 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1801 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1802 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1803 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1804 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1805 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1806 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1807 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001808 // FIXME: Implement more expansions.
1809 }
1810
Dale Johannesenbb5da912009-02-02 20:41:04 +00001811 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1812 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1813 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001814 RHS = SDValue();
1815 CC = SDValue();
1816 break;
1817 }
1818 }
1819}
1820
Chris Lattner1401d152008-01-16 07:45:30 +00001821/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1822/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1823/// a load from the stack slot to DestVT, extending it if needed.
1824/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001825SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001826 EVT SlotVT,
1827 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001828 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001829 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001830 unsigned SrcAlign =
1831 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001832 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001833 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001834
Evan Chengff89dcb2009-10-18 18:16:27 +00001835 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1836 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001837 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001838
Duncan Sands83ec4b62008-06-06 12:08:01 +00001839 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1840 unsigned SlotSize = SlotVT.getSizeInBits();
1841 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001842 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1843 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001844
Chris Lattner1401d152008-01-16 07:45:30 +00001845 // Emit a store to the stack slot. Use a truncstore if the input value is
1846 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001847 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001848
Chris Lattner1401d152008-01-16 07:45:30 +00001849 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001850 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001851 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001852 else {
1853 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001854 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001855 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001856 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001857
Chris Lattner35481892005-12-23 00:16:34 +00001858 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001859 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001860 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001861 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Chris Lattner1401d152008-01-16 07:45:30 +00001863 assert(SlotSize < DestSize && "Unknown extension!");
Chris Lattnerecf42c42010-09-21 16:36:31 +00001864 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001865 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001866}
1867
Dan Gohman475871a2008-07-27 21:46:04 +00001868SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001869 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001870 // Create a vector sized/aligned stack slot, store the value to element #0,
1871 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001873
Evan Chengff89dcb2009-10-18 18:16:27 +00001874 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1875 int SPFI = StackPtrFI->getIndex();
1876
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001877 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1878 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001879 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001880 Node->getValueType(0).getVectorElementType(),
1881 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001882 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001883 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001884 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001885}
1886
1887
Chris Lattnerce872152006-03-19 06:31:19 +00001888/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001889/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001890SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001891 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001892 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001893 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001894 EVT VT = Node->getValueType(0);
1895 EVT OpVT = Node->getOperand(0).getValueType();
1896 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001897
1898 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001899 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001900 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001901 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001902 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001903 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001904 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001905 if (V.getOpcode() == ISD::UNDEF)
1906 continue;
1907 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001908 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001909 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001910 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001911
1912 if (!Value1.getNode()) {
1913 Value1 = V;
1914 } else if (!Value2.getNode()) {
1915 if (V != Value1)
1916 Value2 = V;
1917 } else if (V != Value1 && V != Value2) {
1918 MoreThanTwoValues = true;
1919 }
Chris Lattnerce872152006-03-19 06:31:19 +00001920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Eli Friedman7a5e5552009-06-07 06:52:44 +00001922 if (!Value1.getNode())
1923 return DAG.getUNDEF(VT);
1924
1925 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001926 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Chris Lattner2eb86532006-03-24 07:29:17 +00001928 // If all elements are constants, create a load from the constant pool.
1929 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001930 std::vector<Constant*> CV;
1931 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001932 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001933 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001934 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001936 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001937 if (OpVT==EltVT)
1938 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1939 else {
1940 // If OpVT and EltVT don't match, EltVT is not legal and the
1941 // element values have been promoted/truncated earlier. Undo this;
1942 // we don't want a v16i8 to become a v16i32 for example.
1943 const ConstantInt *CI = V->getConstantIntValue();
1944 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1945 CI->getZExtValue()));
1946 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001947 } else {
1948 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001949 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001950 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001951 }
1952 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001953 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001955 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001956 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001957 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001958 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001959 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001960
Eli Friedman7a5e5552009-06-07 06:52:44 +00001961 if (!MoreThanTwoValues) {
1962 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1963 for (unsigned i = 0; i < NumElems; ++i) {
1964 SDValue V = Node->getOperand(i);
1965 if (V.getOpcode() == ISD::UNDEF)
1966 continue;
1967 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1968 }
1969 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001970 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001971 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1972 SDValue Vec2;
1973 if (Value2.getNode())
1974 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1975 else
1976 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001977
Chris Lattner87100e02006-03-20 01:52:29 +00001978 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001979 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001980 }
1981 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001982
Eli Friedman7ef3d172009-06-06 07:04:42 +00001983 // Otherwise, we can't handle this case efficiently.
1984 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001985}
1986
Chris Lattner77e77a62005-01-21 06:05:23 +00001987// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1988// does not fit into a register, return the lo part and set the hi part to the
1989// by-reg argument. If it does fit into a single register, return the result
1990// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001991SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001992 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001993 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001994 // Legalizing the call will automatically add the previous call to the
1995 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001996 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001997
Chris Lattner77e77a62005-01-21 06:05:23 +00001998 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001999 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00002000 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002001 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00002002 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00002003 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002004 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00002005 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00002006 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00002007 }
Bill Wendling056292f2008-09-16 21:48:12 +00002008 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00002009 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002010
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002011 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00002012 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00002013
2014 // isTailCall may be true since the callee does not reference caller stack
2015 // frame. Check if it's in the right position.
2016 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002017 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00002018 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002019 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002021 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002022
Evan Cheng3d2125c2010-11-30 23:55:39 +00002023 if (!CallInfo.second.getNode())
2024 // It's a tailcall, return the chain (which is the DAG root).
2025 return DAG.getRoot();
2026
Chris Lattner6831a812006-02-13 09:18:02 +00002027 // Legalize the call sequence, starting with the chain. This will advance
2028 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2029 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2030 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00002031 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002032}
2033
Jim Grosbache03262f2010-06-18 21:43:38 +00002034// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
2035// ExpandLibCall except that the first operand is the in-chain.
2036std::pair<SDValue, SDValue>
2037SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
2038 SDNode *Node,
2039 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00002040 SDValue InChain = Node->getOperand(0);
2041
2042 TargetLowering::ArgListTy Args;
2043 TargetLowering::ArgListEntry Entry;
2044 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2045 EVT ArgVT = Node->getOperand(i).getValueType();
2046 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2047 Entry.Node = Node->getOperand(i);
2048 Entry.Ty = ArgTy;
2049 Entry.isSExt = isSigned;
2050 Entry.isZExt = !isSigned;
2051 Args.push_back(Entry);
2052 }
2053 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2054 TLI.getPointerTy());
2055
2056 // Splice the libcall in wherever FindInputOutputChains tells us to.
2057 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2058 std::pair<SDValue, SDValue> CallInfo =
2059 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002060 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002061 /*isReturnValueUsed=*/true,
2062 Callee, Args, DAG, Node->getDebugLoc());
2063
2064 // Legalize the call sequence, starting with the chain. This will advance
2065 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2066 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2067 LegalizeOp(CallInfo.second);
2068 return CallInfo;
2069}
2070
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002071SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2072 RTLIB::Libcall Call_F32,
2073 RTLIB::Libcall Call_F64,
2074 RTLIB::Libcall Call_F80,
2075 RTLIB::Libcall Call_PPCF128) {
2076 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002077 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002078 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002079 case MVT::f32: LC = Call_F32; break;
2080 case MVT::f64: LC = Call_F64; break;
2081 case MVT::f80: LC = Call_F80; break;
2082 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002083 }
2084 return ExpandLibCall(LC, Node, false);
2085}
2086
2087SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002088 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002089 RTLIB::Libcall Call_I16,
2090 RTLIB::Libcall Call_I32,
2091 RTLIB::Libcall Call_I64,
2092 RTLIB::Libcall Call_I128) {
2093 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002095 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002096 case MVT::i8: LC = Call_I8; break;
2097 case MVT::i16: LC = Call_I16; break;
2098 case MVT::i32: LC = Call_I32; break;
2099 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002101 }
2102 return ExpandLibCall(LC, Node, isSigned);
2103}
2104
Chris Lattner22cde6a2006-01-28 08:25:58 +00002105/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2106/// INT_TO_FP operation of the specified operand when the target requests that
2107/// we expand it. At this point, we know that the result and operand types are
2108/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002109SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2110 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002111 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002112 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002113 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002114 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002115
Chris Lattner23594d42008-01-16 07:03:22 +00002116 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002117 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002118
Chris Lattner22cde6a2006-01-28 08:25:58 +00002119 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002121 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002122 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002123 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002124 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002125 if (TLI.isLittleEndian())
2126 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002127
Chris Lattner22cde6a2006-01-28 08:25:58 +00002128 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002129 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002130 if (isSigned) {
2131 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2133 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002134 } else {
2135 Op0Mapped = Op0;
2136 }
2137 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002138 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002139 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002140 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002141 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002142 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002143 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002144 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2145 MachinePointerInfo(),
2146 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002147 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002148 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2149 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002150 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002151 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002152 BitsToDouble(0x4330000080000000ULL) :
2153 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002154 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002155 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002156 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002157 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002158 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002159 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002160 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002161 // do nothing
2162 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002164 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002165 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002166 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002167 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002168 }
2169 return Result;
2170 }
2171 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002172 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002173
2174 // Implementation of unsigned i64 to f64 following the algorithm in
2175 // __floatundidf in compiler_rt. This implementation has the advantage
2176 // of performing rounding correctly, both in the default rounding mode
2177 // and in all alternate rounding modes.
2178 // TODO: Generalize this for use with other types.
2179 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2180 SDValue TwoP52 =
2181 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2182 SDValue TwoP84PlusTwoP52 =
2183 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2184 SDValue TwoP84 =
2185 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2186
2187 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2188 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2189 DAG.getConstant(32, MVT::i64));
2190 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2191 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002192 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2193 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002194 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2195 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002196 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2197 }
2198
Owen Anderson3a9e7692010-10-05 17:24:05 +00002199 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002200 // TODO: Generalize this for use with other types.
2201 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002202 // For unsigned conversions, convert them to signed conversions using the
2203 // algorithm from the x86_64 __floatundidf in compiler_rt.
2204 if (!isSigned) {
2205 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002206
Owen Anderson3a9e7692010-10-05 17:24:05 +00002207 SDValue ShiftConst = DAG.getConstant(1, TLI.getShiftAmountTy());
2208 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2209 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2210 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2211 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002212
Owen Anderson3a9e7692010-10-05 17:24:05 +00002213 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2214 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002215
Owen Anderson3a9e7692010-10-05 17:24:05 +00002216 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002217 // select. We happen to get lucky and machinesink does the right
2218 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002219 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002220 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002221 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2222 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2223 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002224
Owen Anderson3a9e7692010-10-05 17:24:05 +00002225 // Otherwise, implement the fully general conversion.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002226 EVT SHVT = TLI.getShiftAmountTy();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002227
Jim Grosbach6e992612010-07-02 17:41:59 +00002228 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002229 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2230 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2231 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002232 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002233 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2234 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2235 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2236 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2237 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2238 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002239 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002240 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002241
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002242 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2243 DAG.getConstant(32, SHVT));
2244 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2245 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2246 SDValue TwoP32 =
2247 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2248 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2249 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2250 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2251 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2252 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2253 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002254 }
2255
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002256 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002257
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002258 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2259 Op0, DAG.getConstant(0, Op0.getValueType()),
2260 ISD::SETLT);
2261 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2262 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2263 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002264
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002265 // If the sign bit of the integer is set, the large number will be treated
2266 // as a negative number. To counteract this, the dynamic code adds an
2267 // offset depending on the data type.
2268 uint64_t FF;
2269 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002270 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002271 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2272 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2273 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2274 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2275 }
2276 if (TLI.isLittleEndian()) FF <<= 32;
2277 Constant *FudgeFactor = ConstantInt::get(
2278 Type::getInt64Ty(*DAG.getContext()), FF);
2279
2280 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2281 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2282 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2283 Alignment = std::min(Alignment, 4u);
2284 SDValue FudgeInReg;
2285 if (DestVT == MVT::f32)
2286 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002287 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002288 false, false, Alignment);
2289 else {
2290 FudgeInReg =
Evan Chengbcc80172010-07-07 22:15:37 +00002291 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002292 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002293 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002294 MVT::f32, false, false, Alignment));
2295 }
2296
2297 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002298}
2299
2300/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2301/// *INT_TO_FP operation of the specified operand when the target requests that
2302/// we promote it. At this point, we know that the result and operand types are
2303/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2304/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002305SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002306 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002307 bool isSigned,
2308 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002309 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002310 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002311
2312 unsigned OpToUse = 0;
2313
2314 // Scan for the appropriate larger type to use.
2315 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002317 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002318
2319 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002320 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2321 OpToUse = ISD::SINT_TO_FP;
2322 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002323 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002324 if (isSigned) continue;
2325
2326 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002327 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2328 OpToUse = ISD::UINT_TO_FP;
2329 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002330 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002331
2332 // Otherwise, try a larger type.
2333 }
2334
2335 // Okay, we found the operation and type to use. Zero extend our input to the
2336 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002337 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002338 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002339 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002340}
2341
2342/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2343/// FP_TO_*INT operation of the specified operand when the target requests that
2344/// we promote it. At this point, we know that the result and operand types are
2345/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2346/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002347SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002348 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002349 bool isSigned,
2350 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002351 // First step, figure out the appropriate FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002352 EVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002353
2354 unsigned OpToUse = 0;
2355
2356 // Scan for the appropriate larger type to use.
2357 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002358 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002359 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002360
Eli Friedman3be2e512009-05-28 03:06:16 +00002361 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002362 OpToUse = ISD::FP_TO_SINT;
2363 break;
2364 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002365
Eli Friedman3be2e512009-05-28 03:06:16 +00002366 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002367 OpToUse = ISD::FP_TO_UINT;
2368 break;
2369 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002370
2371 // Otherwise, try a larger type.
2372 }
2373
Scott Michelfdc40a02009-02-17 22:15:04 +00002374
Chris Lattner27a6c732007-11-24 07:07:01 +00002375 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002376 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002377
Chris Lattner27a6c732007-11-24 07:07:01 +00002378 // Truncate the result of the extended FP_TO_*INT operation to the desired
2379 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002380 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002381}
2382
2383/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2384///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002385SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002386 EVT VT = Op.getValueType();
2387 EVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002388 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002389 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002390 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002391 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002392 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2393 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2394 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002395 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002396 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2397 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2398 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2399 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2400 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2401 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2402 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2403 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2404 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002405 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002406 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2407 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2408 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2409 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2410 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2411 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2412 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2413 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2414 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2415 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2416 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2417 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2418 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2419 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2420 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2421 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2422 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2423 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2424 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2425 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2426 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002427 }
2428}
2429
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002430/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002431// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002432static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2433 APInt Val = APInt(NumBits, ByteVal);
2434 unsigned Shift = 8;
2435 for (unsigned i = NumBits; i > 8; i >>= 1) {
2436 Val = (Val << Shift) | Val;
2437 Shift <<= 1;
2438 }
2439 return Val;
2440}
2441
Chris Lattner22cde6a2006-01-28 08:25:58 +00002442/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2443///
Scott Michelfdc40a02009-02-17 22:15:04 +00002444SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002445 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002446 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002447 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002448 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002449 EVT VT = Op.getValueType();
2450 EVT ShVT = TLI.getShiftAmountTy();
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002451 unsigned Len = VT.getSizeInBits();
2452
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002453 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2454 "CTPOP not implemented for this type.");
2455
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002456 // This is the "best" algorithm from
2457 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2458
2459 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2460 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2461 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2462 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2463
2464 // v = v - ((v >> 1) & 0x55555555...)
2465 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2466 DAG.getNode(ISD::AND, dl, VT,
2467 DAG.getNode(ISD::SRL, dl, VT, Op,
2468 DAG.getConstant(1, ShVT)),
2469 Mask55));
2470 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2471 Op = DAG.getNode(ISD::ADD, dl, VT,
2472 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2473 DAG.getNode(ISD::AND, dl, VT,
2474 DAG.getNode(ISD::SRL, dl, VT, Op,
2475 DAG.getConstant(2, ShVT)),
2476 Mask33));
2477 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2478 Op = DAG.getNode(ISD::AND, dl, VT,
2479 DAG.getNode(ISD::ADD, dl, VT, Op,
2480 DAG.getNode(ISD::SRL, dl, VT, Op,
2481 DAG.getConstant(4, ShVT))),
2482 Mask0F);
2483 // v = (v * 0x01010101...) >> (Len - 8)
2484 Op = DAG.getNode(ISD::SRL, dl, VT,
2485 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2486 DAG.getConstant(Len - 8, ShVT));
2487
Chris Lattner22cde6a2006-01-28 08:25:58 +00002488 return Op;
2489 }
2490 case ISD::CTLZ: {
2491 // for now, we do this:
2492 // x = x | (x >> 1);
2493 // x = x | (x >> 2);
2494 // ...
2495 // x = x | (x >>16);
2496 // x = x | (x >>32); // for 64-bit input
2497 // return popcount(~x);
2498 //
2499 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002500 EVT VT = Op.getValueType();
2501 EVT ShVT = TLI.getShiftAmountTy();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002502 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002503 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002504 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002505 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002506 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002507 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002508 Op = DAG.getNOT(dl, Op, VT);
2509 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002510 }
2511 case ISD::CTTZ: {
2512 // for now, we use: { return popcount(~x & (x - 1)); }
2513 // unless the target has ctlz but not ctpop, in which case we use:
2514 // { return 32 - nlz(~x & (x-1)); }
2515 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002516 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002517 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2518 DAG.getNOT(dl, Op, VT),
2519 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002520 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002521 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002522 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2523 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002524 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002525 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002526 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2527 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002528 }
2529 }
2530}
Chris Lattnere34b3962005-01-19 04:19:40 +00002531
Jim Grosbache03262f2010-06-18 21:43:38 +00002532std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2533 unsigned Opc = Node->getOpcode();
2534 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2535 RTLIB::Libcall LC;
2536
2537 switch (Opc) {
2538 default:
2539 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2540 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002541 case ISD::ATOMIC_SWAP:
2542 switch (VT.SimpleTy) {
2543 default: llvm_unreachable("Unexpected value type for atomic!");
2544 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2545 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2546 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2547 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2548 }
2549 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002550 case ISD::ATOMIC_CMP_SWAP:
2551 switch (VT.SimpleTy) {
2552 default: llvm_unreachable("Unexpected value type for atomic!");
2553 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2554 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2555 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2556 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2557 }
2558 break;
2559 case ISD::ATOMIC_LOAD_ADD:
2560 switch (VT.SimpleTy) {
2561 default: llvm_unreachable("Unexpected value type for atomic!");
2562 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2563 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2564 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2565 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2566 }
2567 break;
2568 case ISD::ATOMIC_LOAD_SUB:
2569 switch (VT.SimpleTy) {
2570 default: llvm_unreachable("Unexpected value type for atomic!");
2571 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2572 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2573 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2574 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2575 }
2576 break;
2577 case ISD::ATOMIC_LOAD_AND:
2578 switch (VT.SimpleTy) {
2579 default: llvm_unreachable("Unexpected value type for atomic!");
2580 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2581 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2582 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2583 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2584 }
2585 break;
2586 case ISD::ATOMIC_LOAD_OR:
2587 switch (VT.SimpleTy) {
2588 default: llvm_unreachable("Unexpected value type for atomic!");
2589 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2590 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2591 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2592 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2593 }
2594 break;
2595 case ISD::ATOMIC_LOAD_XOR:
2596 switch (VT.SimpleTy) {
2597 default: llvm_unreachable("Unexpected value type for atomic!");
2598 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2599 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2600 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2601 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2602 }
2603 break;
2604 case ISD::ATOMIC_LOAD_NAND:
2605 switch (VT.SimpleTy) {
2606 default: llvm_unreachable("Unexpected value type for atomic!");
2607 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2608 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2609 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2610 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2611 }
2612 break;
2613 }
2614
2615 return ExpandChainLibCall(LC, Node, false);
2616}
2617
Eli Friedman8c377c72009-05-27 01:25:56 +00002618void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2619 SmallVectorImpl<SDValue> &Results) {
2620 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002621 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002622 switch (Node->getOpcode()) {
2623 case ISD::CTPOP:
2624 case ISD::CTLZ:
2625 case ISD::CTTZ:
2626 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2627 Results.push_back(Tmp1);
2628 break;
2629 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002630 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002631 break;
2632 case ISD::FRAMEADDR:
2633 case ISD::RETURNADDR:
2634 case ISD::FRAME_TO_ARGS_OFFSET:
2635 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2636 break;
2637 case ISD::FLT_ROUNDS_:
2638 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2639 break;
2640 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002641 case ISD::EH_LABEL:
2642 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002643 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002644 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002645 case ISD::EH_SJLJ_DISPATCHSETUP:
2646 // If the target didn't expand these, there's nothing to do, so just
2647 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002648 Results.push_back(Node->getOperand(0));
2649 break;
2650 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002651 // If the target didn't expand this, just return 'zero' and preserve the
2652 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002653 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002654 Results.push_back(Node->getOperand(0));
2655 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002656 case ISD::MEMBARRIER: {
2657 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2658 TargetLowering::ArgListTy Args;
2659 std::pair<SDValue, SDValue> CallResult =
2660 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002661 false, false, false, false, 0, CallingConv::C,
2662 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002663 /*isReturnValueUsed=*/true,
2664 DAG.getExternalSymbol("__sync_synchronize",
2665 TLI.getPointerTy()),
2666 Args, DAG, dl);
2667 Results.push_back(CallResult.second);
2668 break;
2669 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002670 // By default, atomic intrinsics are marked Legal and lowered. Targets
2671 // which don't support them directly, however, may want libcalls, in which
2672 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002673 case ISD::ATOMIC_SWAP:
2674 case ISD::ATOMIC_LOAD_ADD:
2675 case ISD::ATOMIC_LOAD_SUB:
2676 case ISD::ATOMIC_LOAD_AND:
2677 case ISD::ATOMIC_LOAD_OR:
2678 case ISD::ATOMIC_LOAD_XOR:
2679 case ISD::ATOMIC_LOAD_NAND:
2680 case ISD::ATOMIC_LOAD_MIN:
2681 case ISD::ATOMIC_LOAD_MAX:
2682 case ISD::ATOMIC_LOAD_UMIN:
2683 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002684 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002685 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2686 Results.push_back(Tmp.first);
2687 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002688 break;
Evan Chenga8457062010-06-18 22:01:37 +00002689 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002690 case ISD::DYNAMIC_STACKALLOC:
2691 ExpandDYNAMIC_STACKALLOC(Node, Results);
2692 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002693 case ISD::MERGE_VALUES:
2694 for (unsigned i = 0; i < Node->getNumValues(); i++)
2695 Results.push_back(Node->getOperand(i));
2696 break;
2697 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002698 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002699 if (VT.isInteger())
2700 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002701 else {
2702 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002703 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002704 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002705 break;
2706 }
2707 case ISD::TRAP: {
2708 // If this operation is not supported, lower it to 'abort()' call
2709 TargetLowering::ArgListTy Args;
2710 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002711 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002712 false, false, false, false, 0, CallingConv::C,
2713 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002714 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002715 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002716 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002717 Results.push_back(CallResult.second);
2718 break;
2719 }
2720 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002721 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002722 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2723 Node->getValueType(0), dl);
2724 Results.push_back(Tmp1);
2725 break;
2726 case ISD::FP_EXTEND:
2727 Tmp1 = EmitStackConvert(Node->getOperand(0),
2728 Node->getOperand(0).getValueType(),
2729 Node->getValueType(0), dl);
2730 Results.push_back(Tmp1);
2731 break;
2732 case ISD::SIGN_EXTEND_INREG: {
2733 // NOTE: we could fall back on load/store here too for targets without
2734 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002735 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002736 EVT VT = Node->getValueType(0);
2737 EVT ShiftAmountTy = TLI.getShiftAmountTy();
Dan Gohmand1996362010-01-09 02:13:55 +00002738 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002739 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002740 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2741 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002742 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002743 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2744 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002745 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2746 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002747 break;
2748 }
2749 case ISD::FP_ROUND_INREG: {
2750 // The only way we can lower this is to turn it into a TRUNCSTORE,
2751 // EXTLOAD pair, targetting a temporary location (a stack slot).
2752
2753 // NOTE: there is a choice here between constantly creating new stack
2754 // slots and always reusing the same one. We currently always create
2755 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002756 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002757 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2758 Node->getValueType(0), dl);
2759 Results.push_back(Tmp1);
2760 break;
2761 }
2762 case ISD::SINT_TO_FP:
2763 case ISD::UINT_TO_FP:
2764 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2765 Node->getOperand(0), Node->getValueType(0), dl);
2766 Results.push_back(Tmp1);
2767 break;
2768 case ISD::FP_TO_UINT: {
2769 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002770 EVT VT = Node->getOperand(0).getValueType();
2771 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002772 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002773 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2774 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2775 Tmp1 = DAG.getConstantFP(apf, VT);
2776 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2777 Node->getOperand(0),
2778 Tmp1, ISD::SETLT);
2779 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002780 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2781 DAG.getNode(ISD::FSUB, dl, VT,
2782 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002783 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2784 DAG.getConstant(x, NVT));
2785 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2786 Results.push_back(Tmp1);
2787 break;
2788 }
Eli Friedman509150f2009-05-27 07:58:35 +00002789 case ISD::VAARG: {
2790 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002791 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002792 Tmp1 = Node->getOperand(0);
2793 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002794 unsigned Align = Node->getConstantOperandVal(3);
2795
Chris Lattnerecf42c42010-09-21 16:36:31 +00002796 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2797 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002798 SDValue VAList = VAListLoad;
2799
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002800 if (Align > TLI.getMinStackArgumentAlignment()) {
2801 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2802
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002803 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2804 DAG.getConstant(Align - 1,
2805 TLI.getPointerTy()));
2806
2807 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002808 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002809 TLI.getPointerTy()));
2810 }
2811
Eli Friedman509150f2009-05-27 07:58:35 +00002812 // Increment the pointer, VAList, to the next vaarg
2813 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2814 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002815 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002816 TLI.getPointerTy()));
2817 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002818 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2819 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002820 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002821 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002822 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002823 Results.push_back(Results[0].getValue(1));
2824 break;
2825 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002826 case ISD::VACOPY: {
2827 // This defaults to loading a pointer from the input and storing it to the
2828 // output, returning the chain.
2829 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2830 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2831 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002832 Node->getOperand(2), MachinePointerInfo(VS),
2833 false, false, 0);
2834 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2835 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002836 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002837 break;
2838 }
2839 case ISD::EXTRACT_VECTOR_ELT:
2840 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2841 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002842 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002843 Node->getOperand(0));
2844 else
2845 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2846 Results.push_back(Tmp1);
2847 break;
2848 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002849 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002850 break;
David Greenecfe33c42011-01-26 19:13:22 +00002851 case ISD::INSERT_SUBVECTOR:
2852 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
2853 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002854 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002855 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002856 break;
2857 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002858 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002859 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002860 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002861 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002862 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2863 Node->getOperand(1),
2864 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002865 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002866 case ISD::VECTOR_SHUFFLE: {
2867 SmallVector<int, 8> Mask;
2868 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2869
Owen Andersone50ed302009-08-10 22:56:29 +00002870 EVT VT = Node->getValueType(0);
2871 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00002872 if (getTypeAction(EltVT) == Promote)
2873 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002874 unsigned NumElems = VT.getVectorNumElements();
2875 SmallVector<SDValue, 8> Ops;
2876 for (unsigned i = 0; i != NumElems; ++i) {
2877 if (Mask[i] < 0) {
2878 Ops.push_back(DAG.getUNDEF(EltVT));
2879 continue;
2880 }
2881 unsigned Idx = Mask[i];
2882 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002883 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2884 Node->getOperand(0),
2885 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002886 else
Bill Wendling775db972009-12-23 00:28:23 +00002887 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2888 Node->getOperand(1),
2889 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002890 }
2891 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2892 Results.push_back(Tmp1);
2893 break;
2894 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002895 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002896 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002897 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2898 // 1 -> Hi
2899 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2900 DAG.getConstant(OpTy.getSizeInBits()/2,
2901 TLI.getShiftAmountTy()));
2902 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2903 } else {
2904 // 0 -> Lo
2905 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2906 Node->getOperand(0));
2907 }
2908 Results.push_back(Tmp1);
2909 break;
2910 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002911 case ISD::STACKSAVE:
2912 // Expand to CopyFromReg if the target set
2913 // StackPointerRegisterToSaveRestore.
2914 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002915 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2916 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002917 Results.push_back(Results[0].getValue(1));
2918 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002919 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002920 Results.push_back(Node->getOperand(0));
2921 }
2922 break;
2923 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002924 // Expand to CopyToReg if the target set
2925 // StackPointerRegisterToSaveRestore.
2926 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2927 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2928 Node->getOperand(1)));
2929 } else {
2930 Results.push_back(Node->getOperand(0));
2931 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002932 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002933 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002934 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002935 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002936 case ISD::FNEG:
2937 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2938 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2939 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2940 Node->getOperand(0));
2941 Results.push_back(Tmp1);
2942 break;
2943 case ISD::FABS: {
2944 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002945 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002946 Tmp1 = Node->getOperand(0);
2947 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002948 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002949 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002950 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2951 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002952 Results.push_back(Tmp1);
2953 break;
2954 }
2955 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002956 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2957 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002958 break;
2959 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002960 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2961 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002962 break;
2963 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002964 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2965 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002966 break;
2967 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002968 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2969 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002970 break;
2971 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002972 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2973 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002974 break;
2975 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002976 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2977 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002978 break;
2979 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002980 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2981 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002982 break;
2983 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002984 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2985 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002986 break;
2987 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002988 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2989 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002990 break;
2991 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002992 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2993 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002994 break;
2995 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002996 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2997 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002998 break;
2999 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00003000 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
3001 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003002 break;
3003 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00003004 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
3005 RTLIB::NEARBYINT_F64,
3006 RTLIB::NEARBYINT_F80,
3007 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003008 break;
3009 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00003010 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
3011 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003012 break;
3013 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00003014 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
3015 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003016 break;
3017 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00003018 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
3019 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003020 break;
3021 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00003022 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
3023 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003024 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00003025 case ISD::FP16_TO_FP32:
3026 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
3027 break;
3028 case ISD::FP32_TO_FP16:
3029 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
3030 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003031 case ISD::ConstantFP: {
3032 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00003033 // Check to see if this FP immediate is already legal.
3034 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003035 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00003036 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003037 else
Bill Wendling775db972009-12-23 00:28:23 +00003038 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003039 break;
3040 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003041 case ISD::EHSELECTION: {
3042 unsigned Reg = TLI.getExceptionSelectorRegister();
3043 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003044 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3045 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003046 Results.push_back(Results[0].getValue(1));
3047 break;
3048 }
3049 case ISD::EXCEPTIONADDR: {
3050 unsigned Reg = TLI.getExceptionAddressRegister();
3051 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003052 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3053 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003054 Results.push_back(Results[0].getValue(1));
3055 break;
3056 }
3057 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003058 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003059 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3060 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3061 "Don't know how to expand this subtraction!");
3062 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3063 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003064 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3065 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003066 break;
3067 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003068 case ISD::UREM:
3069 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003070 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003071 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003072 bool isSigned = Node->getOpcode() == ISD::SREM;
3073 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3074 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3075 Tmp2 = Node->getOperand(0);
3076 Tmp3 = Node->getOperand(1);
Eli Friedman3be2e512009-05-28 03:06:16 +00003077 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3078 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3079 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003080 // X % Y -> X-X/Y*Y
3081 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3082 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3083 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003084 } else if (isSigned) {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003085 Tmp1 = ExpandIntLibCall(Node, true,
3086 RTLIB::SREM_I8,
3087 RTLIB::SREM_I16, RTLIB::SREM_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003088 RTLIB::SREM_I64, RTLIB::SREM_I128);
3089 } else {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003090 Tmp1 = ExpandIntLibCall(Node, false,
3091 RTLIB::UREM_I8,
3092 RTLIB::UREM_I16, RTLIB::UREM_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003093 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003094 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003095 Results.push_back(Tmp1);
3096 break;
3097 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003098 case ISD::UDIV:
3099 case ISD::SDIV: {
3100 bool isSigned = Node->getOpcode() == ISD::SDIV;
3101 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003102 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003103 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003104 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3105 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3106 Node->getOperand(1));
3107 else if (isSigned)
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003108 Tmp1 = ExpandIntLibCall(Node, true,
3109 RTLIB::SDIV_I8,
3110 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003111 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003112 else
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003113 Tmp1 = ExpandIntLibCall(Node, false,
3114 RTLIB::UDIV_I8,
3115 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003116 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003117 Results.push_back(Tmp1);
3118 break;
3119 }
3120 case ISD::MULHU:
3121 case ISD::MULHS: {
3122 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3123 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003124 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003125 SDVTList VTs = DAG.getVTList(VT, VT);
3126 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3127 "If this wasn't legal, it shouldn't have been created!");
3128 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3129 Node->getOperand(1));
3130 Results.push_back(Tmp1.getValue(1));
3131 break;
3132 }
3133 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003134 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003135 SDVTList VTs = DAG.getVTList(VT, VT);
3136 // See if multiply or divide can be lowered using two-result operations.
3137 // We just need the low half of the multiply; try both the signed
3138 // and unsigned forms. If the target supports both SMUL_LOHI and
3139 // UMUL_LOHI, form a preference by checking which forms of plain
3140 // MULH it supports.
3141 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3142 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3143 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3144 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3145 unsigned OpToUse = 0;
3146 if (HasSMUL_LOHI && !HasMULHS) {
3147 OpToUse = ISD::SMUL_LOHI;
3148 } else if (HasUMUL_LOHI && !HasMULHU) {
3149 OpToUse = ISD::UMUL_LOHI;
3150 } else if (HasSMUL_LOHI) {
3151 OpToUse = ISD::SMUL_LOHI;
3152 } else if (HasUMUL_LOHI) {
3153 OpToUse = ISD::UMUL_LOHI;
3154 }
3155 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003156 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3157 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003158 break;
3159 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003160 Tmp1 = ExpandIntLibCall(Node, false,
3161 RTLIB::MUL_I8,
3162 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003163 RTLIB::MUL_I64, RTLIB::MUL_I128);
3164 Results.push_back(Tmp1);
3165 break;
3166 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003167 case ISD::SADDO:
3168 case ISD::SSUBO: {
3169 SDValue LHS = Node->getOperand(0);
3170 SDValue RHS = Node->getOperand(1);
3171 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3172 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3173 LHS, RHS);
3174 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003175 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003176
Eli Friedman4bc8c712009-05-27 12:20:41 +00003177 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3178
3179 // LHSSign -> LHS >= 0
3180 // RHSSign -> RHS >= 0
3181 // SumSign -> Sum >= 0
3182 //
3183 // Add:
3184 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3185 // Sub:
3186 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3187 //
3188 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3189 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3190 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3191 Node->getOpcode() == ISD::SADDO ?
3192 ISD::SETEQ : ISD::SETNE);
3193
3194 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3195 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3196
3197 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3198 Results.push_back(Cmp);
3199 break;
3200 }
3201 case ISD::UADDO:
3202 case ISD::USUBO: {
3203 SDValue LHS = Node->getOperand(0);
3204 SDValue RHS = Node->getOperand(1);
3205 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3206 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3207 LHS, RHS);
3208 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003209 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3210 Node->getOpcode () == ISD::UADDO ?
3211 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003212 break;
3213 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003214 case ISD::UMULO:
3215 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003216 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003217 SDValue LHS = Node->getOperand(0);
3218 SDValue RHS = Node->getOperand(1);
3219 SDValue BottomHalf;
3220 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003221 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003222 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3223 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3224 bool isSigned = Node->getOpcode() == ISD::SMULO;
3225 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3226 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3227 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3228 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3229 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3230 RHS);
3231 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003232 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3233 VT.getSizeInBits() * 2))) {
Owen Anderson23b9b192009-08-12 00:36:31 +00003234 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003235 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3236 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3237 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3238 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3239 DAG.getIntPtrConstant(0));
3240 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3241 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003242 } else {
3243 // We can fall back to a libcall with an illegal type for the MUL if we
3244 // have a libcall big enough.
3245 // Also, we can fall back to a division in some cases, but that's a big
3246 // performance hit in the general case.
3247 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3248 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3249 if (WideVT == MVT::i16)
3250 LC = RTLIB::MUL_I16;
3251 else if (WideVT == MVT::i32)
3252 LC = RTLIB::MUL_I32;
3253 else if (WideVT == MVT::i64)
3254 LC = RTLIB::MUL_I64;
3255 else if (WideVT == MVT::i128)
3256 LC = RTLIB::MUL_I128;
3257 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3258 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3259 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3260
Eric Christophere3385812011-01-20 01:29:23 +00003261 SDValue Ret = ExpandLibCall(LC, Node, isSigned);
Eric Christopher38a18262011-01-20 00:29:24 +00003262 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Ret);
3263 TopHalf = DAG.getNode(ISD::SRL, dl, Ret.getValueType(), Ret,
3264 DAG.getConstant(VT.getSizeInBits(), TLI.getPointerTy()));
3265 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, TopHalf);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003266 }
3267 if (isSigned) {
3268 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, TLI.getShiftAmountTy());
3269 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3270 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3271 ISD::SETNE);
3272 } else {
3273 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3274 DAG.getConstant(0, VT), ISD::SETNE);
3275 }
3276 Results.push_back(BottomHalf);
3277 Results.push_back(TopHalf);
3278 break;
3279 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003280 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003281 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003282 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3283 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003284 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003285 DAG.getConstant(PairTy.getSizeInBits()/2,
3286 TLI.getShiftAmountTy()));
Bill Wendling775db972009-12-23 00:28:23 +00003287 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003288 break;
3289 }
Eli Friedman509150f2009-05-27 07:58:35 +00003290 case ISD::SELECT:
3291 Tmp1 = Node->getOperand(0);
3292 Tmp2 = Node->getOperand(1);
3293 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003294 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003295 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3296 Tmp2, Tmp3,
3297 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003298 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003299 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3300 DAG.getConstant(0, Tmp1.getValueType()),
3301 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003302 }
Eli Friedman509150f2009-05-27 07:58:35 +00003303 Results.push_back(Tmp1);
3304 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003305 case ISD::BR_JT: {
3306 SDValue Chain = Node->getOperand(0);
3307 SDValue Table = Node->getOperand(1);
3308 SDValue Index = Node->getOperand(2);
3309
Owen Andersone50ed302009-08-10 22:56:29 +00003310 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003311
3312 const TargetData &TD = *TLI.getTargetData();
3313 unsigned EntrySize =
3314 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003315
Chris Lattner071c62f2010-01-25 23:26:13 +00003316 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003317 Index, DAG.getConstant(EntrySize, PTy));
3318 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3319
Owen Anderson23b9b192009-08-12 00:36:31 +00003320 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Evan Chengbcc80172010-07-07 22:15:37 +00003321 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, dl, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003322 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003323 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003324 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003325 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003326 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003327 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003328 // RelocBase can be JumpTable, GOT or some sort of global base.
3329 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3330 TLI.getPICJumpTableRelocBase(Table, DAG));
3331 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003333 Results.push_back(Tmp1);
3334 break;
3335 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003336 case ISD::BRCOND:
3337 // Expand brcond's setcc into its constituent parts and create a BR_CC
3338 // Node.
3339 Tmp1 = Node->getOperand(0);
3340 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003341 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003343 Tmp1, Tmp2.getOperand(2),
3344 Tmp2.getOperand(0), Tmp2.getOperand(1),
3345 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003346 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003348 DAG.getCondCode(ISD::SETNE), Tmp2,
3349 DAG.getConstant(0, Tmp2.getValueType()),
3350 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003351 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003352 Results.push_back(Tmp1);
3353 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003354 case ISD::SETCC: {
3355 Tmp1 = Node->getOperand(0);
3356 Tmp2 = Node->getOperand(1);
3357 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003358 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003359
3360 // If we expanded the SETCC into an AND/OR, return the new node
3361 if (Tmp2.getNode() == 0) {
3362 Results.push_back(Tmp1);
3363 break;
3364 }
3365
3366 // Otherwise, SETCC for the given comparison type must be completely
3367 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003368 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003369 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3370 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3371 Results.push_back(Tmp1);
3372 break;
3373 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003374 case ISD::SELECT_CC: {
3375 Tmp1 = Node->getOperand(0); // LHS
3376 Tmp2 = Node->getOperand(1); // RHS
3377 Tmp3 = Node->getOperand(2); // True
3378 Tmp4 = Node->getOperand(3); // False
3379 SDValue CC = Node->getOperand(4);
3380
3381 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003382 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003383
3384 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3385 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3386 CC = DAG.getCondCode(ISD::SETNE);
3387 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3388 Tmp3, Tmp4, CC);
3389 Results.push_back(Tmp1);
3390 break;
3391 }
3392 case ISD::BR_CC: {
3393 Tmp1 = Node->getOperand(0); // Chain
3394 Tmp2 = Node->getOperand(2); // LHS
3395 Tmp3 = Node->getOperand(3); // RHS
3396 Tmp4 = Node->getOperand(1); // CC
3397
3398 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003399 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003400 LastCALLSEQ_END = DAG.getEntryNode();
3401
3402 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3403 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3404 Tmp4 = DAG.getCondCode(ISD::SETNE);
3405 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3406 Tmp3, Node->getOperand(4));
3407 Results.push_back(Tmp1);
3408 break;
3409 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003410 case ISD::GLOBAL_OFFSET_TABLE:
3411 case ISD::GlobalAddress:
3412 case ISD::GlobalTLSAddress:
3413 case ISD::ExternalSymbol:
3414 case ISD::ConstantPool:
3415 case ISD::JumpTable:
3416 case ISD::INTRINSIC_W_CHAIN:
3417 case ISD::INTRINSIC_WO_CHAIN:
3418 case ISD::INTRINSIC_VOID:
3419 // FIXME: Custom lowering for these operations shouldn't return null!
3420 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3421 Results.push_back(SDValue(Node, i));
3422 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003423 }
3424}
3425void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3426 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003427 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003428 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003429 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003430 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003431 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003432 }
Owen Andersone50ed302009-08-10 22:56:29 +00003433 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003434 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003435 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003436 switch (Node->getOpcode()) {
3437 case ISD::CTTZ:
3438 case ISD::CTLZ:
3439 case ISD::CTPOP:
3440 // Zero extend the argument.
3441 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3442 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003443 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003444 if (Node->getOpcode() == ISD::CTTZ) {
3445 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003446 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003447 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3448 ISD::SETEQ);
3449 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3450 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3451 } else if (Node->getOpcode() == ISD::CTLZ) {
3452 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3453 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3454 DAG.getConstant(NVT.getSizeInBits() -
3455 OVT.getSizeInBits(), NVT));
3456 }
Bill Wendling775db972009-12-23 00:28:23 +00003457 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003458 break;
3459 case ISD::BSWAP: {
3460 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003461 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003462 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3463 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3464 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3465 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003466 break;
3467 }
3468 case ISD::FP_TO_UINT:
3469 case ISD::FP_TO_SINT:
3470 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3471 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3472 Results.push_back(Tmp1);
3473 break;
3474 case ISD::UINT_TO_FP:
3475 case ISD::SINT_TO_FP:
3476 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3477 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3478 Results.push_back(Tmp1);
3479 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003480 case ISD::AND:
3481 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003482 case ISD::XOR: {
3483 unsigned ExtOp, TruncOp;
3484 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003485 ExtOp = ISD::BITCAST;
3486 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003487 } else {
3488 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003489 ExtOp = ISD::ANY_EXTEND;
3490 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003491 }
3492 // Promote each of the values to the new type.
3493 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3494 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3495 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003496 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3497 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003498 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003499 }
3500 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003501 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003502 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003503 ExtOp = ISD::BITCAST;
3504 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003505 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003506 ExtOp = ISD::ANY_EXTEND;
3507 TruncOp = ISD::TRUNCATE;
3508 } else {
3509 ExtOp = ISD::FP_EXTEND;
3510 TruncOp = ISD::FP_ROUND;
3511 }
3512 Tmp1 = Node->getOperand(0);
3513 // Promote each of the values to the new type.
3514 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3515 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3516 // Perform the larger operation, then round down.
3517 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3518 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003519 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003520 else
Bill Wendling775db972009-12-23 00:28:23 +00003521 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003522 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003523 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003524 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003525 }
Eli Friedman509150f2009-05-27 07:58:35 +00003526 case ISD::VECTOR_SHUFFLE: {
3527 SmallVector<int, 8> Mask;
3528 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3529
3530 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003531 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3532 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003533
3534 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003535 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003536 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003537 Results.push_back(Tmp1);
3538 break;
3539 }
Eli Friedmanad754602009-05-28 03:56:57 +00003540 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003541 unsigned ExtOp = ISD::FP_EXTEND;
3542 if (NVT.isInteger()) {
3543 ISD::CondCode CCCode =
3544 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3545 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003546 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003547 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3548 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003549 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3550 Tmp1, Tmp2, Node->getOperand(2)));
3551 break;
3552 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003553 }
3554}
3555
Chris Lattner3e928bb2005-01-07 07:47:09 +00003556// SelectionDAG::Legalize - This is the entry point for the file.
3557//
Dan Gohman714efc62009-12-05 17:51:33 +00003558void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003559 /// run - This is the main entry point to this class.
3560 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003561 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003562}
3563