blob: d06a8b43977e7b44e2a3c019266ecfc1672ba50f [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng3d2125c2010-11-30 23:55:39 +000014#include "llvm/Analysis/DebugInfo.h"
15#include "llvm/CodeGen/Analysis.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000018#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000020#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng3d2125c2010-11-30 23:55:39 +000021#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000022#include "llvm/Target/TargetFrameLowering.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000024#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000027#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000028#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000029#include "llvm/DerivedTypes.h"
Bill Wendling86e6cb92009-02-17 01:04:54 +000030#include "llvm/Function.h"
Devang Patel83489bb2009-01-13 00:35:13 +000031#include "llvm/GlobalVariable.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000032#include "llvm/LLVMContext.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000033#include "llvm/Support/CommandLine.h"
David Greene993aace2010-01-05 01:24:53 +000034#include "llvm/Support/Debug.h"
Jim Grosbache03262f2010-06-18 21:43:38 +000035#include "llvm/Support/ErrorHandling.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000036#include "llvm/Support/MathExtras.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000037#include "llvm/Support/raw_ostream.h"
Chris Lattner79715142007-02-03 01:12:36 +000038#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000039#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000041using namespace llvm;
42
43//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000056class SelectionDAGLegalize {
Dan Gohman55e59c12010-04-19 19:05:59 +000057 const TargetMachine &TM;
Dan Gohmand858e902010-04-17 15:26:15 +000058 const TargetLowering &TLI;
Chris Lattner3e928bb2005-01-07 07:47:09 +000059 SelectionDAG &DAG;
Bill Wendling98a366d2009-04-29 23:29:43 +000060 CodeGenOpt::Level OptLevel;
Chris Lattner3e928bb2005-01-07 07:47:09 +000061
Chris Lattner6831a812006-02-13 09:18:02 +000062 // Libcall insertion helpers.
Scott Michelfdc40a02009-02-17 22:15:04 +000063
Chris Lattner6831a812006-02-13 09:18:02 +000064 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
65 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000067 SDValue LastCALLSEQ_END;
Scott Michelfdc40a02009-02-17 22:15:04 +000068
Chris Lattner3e928bb2005-01-07 07:47:09 +000069 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000070 Legal, // The target natively supports this operation.
71 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000072 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 };
Scott Michelfdc40a02009-02-17 22:15:04 +000074
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 /// ValueTypeActions - This is a bitvector that contains two bits for each
76 /// value type, where the two bits correspond to the LegalizeAction enum.
77 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000078 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000079
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 /// LegalizedNodes - For nodes that are of legal width, and that have more
81 /// than one use, this map indicates what regularized operand to use. This
82 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000083 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000084
Dan Gohman475871a2008-07-27 21:46:04 +000085 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000086 LegalizedNodes.insert(std::make_pair(From, To));
87 // If someone requests legalization of the new node, return itself.
88 if (From != To)
89 LegalizedNodes.insert(std::make_pair(To, To));
Devang Pateld2c97932011-01-26 18:55:05 +000090
91 // Transfer SDDbgValues.
92 DAG.TransferDbgValues(From, To);
Chris Lattner8afc48e2005-01-07 22:28:47 +000093 }
94
Chris Lattner3e928bb2005-01-07 07:47:09 +000095public:
Eli Friedman1fde9c52009-05-24 02:46:31 +000096 SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol);
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattner3e928bb2005-01-07 07:47:09 +000098 /// getTypeAction - Return how we should legalize values of this type, either
99 /// it is already legal or we need to expand it into multiple registers of
100 /// smaller integer type, or we need to promote it to a larger type.
Owen Andersone50ed302009-08-10 22:56:29 +0000101 LegalizeAction getTypeAction(EVT VT) const {
Chris Lattneraafe6262010-08-25 23:00:45 +0000102 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000103 }
104
105 /// isTypeLegal - Return true if this type is legal on this target.
106 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000107 bool isTypeLegal(EVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108 return getTypeAction(VT) == Legal;
109 }
110
Chris Lattner3e928bb2005-01-07 07:47:09 +0000111 void LegalizeDAG();
112
Chris Lattner456a93a2006-01-28 07:39:30 +0000113private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000114 /// LegalizeOp - We know that the specified value has a legal type.
115 /// Recursively ensure that the operands have legal types, then return the
116 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue LegalizeOp(SDValue O);
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Eli Friedman7ef3d172009-06-06 07:04:42 +0000119 SDValue OptimizeFloatStore(StoreSDNode *ST);
120
Nate Begeman68679912008-04-25 18:07:40 +0000121 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
122 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
123 /// is necessary to spill the vector being inserted into to memory, perform
124 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
Eli Friedman3f727d62009-05-27 02:16:40 +0000126 SDValue Idx, DebugLoc dl);
127 SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
128 SDValue Idx, DebugLoc dl);
Dan Gohman82669522007-10-11 23:57:53 +0000129
Nate Begeman5a5ca152009-04-29 05:20:52 +0000130 /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
131 /// performs the same shuffe in terms of order or result bytes, but on a type
132 /// whose vector element type is narrower than the original shuffle type.
133 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Owen Andersone50ed302009-08-10 22:56:29 +0000134 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Jim Grosbach6e992612010-07-02 17:41:59 +0000135 SDValue N1, SDValue N2,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000136 SmallVectorImpl<int> &Mask) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000137
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000138 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000139 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000140
Owen Andersone50ed302009-08-10 22:56:29 +0000141 void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +0000142 DebugLoc dl);
Scott Michelfdc40a02009-02-17 22:15:04 +0000143
Eli Friedman47b41f72009-05-27 02:21:29 +0000144 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
Jim Grosbache03262f2010-06-18 21:43:38 +0000145 std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC,
146 SDNode *Node, bool isSigned);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000147 SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
148 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
149 RTLIB::Libcall Call_PPCF128);
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000150 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
151 RTLIB::Libcall Call_I8,
152 RTLIB::Libcall Call_I16,
153 RTLIB::Libcall Call_I32,
154 RTLIB::Libcall Call_I64,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000155 RTLIB::Libcall Call_I128);
Chris Lattnercad063f2005-07-16 00:19:57 +0000156
Owen Andersone50ed302009-08-10 22:56:29 +0000157 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl);
Dan Gohman475871a2008-07-27 21:46:04 +0000158 SDValue ExpandBUILD_VECTOR(SDNode *Node);
159 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Eli Friedman4bc8c712009-05-27 12:20:41 +0000160 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
161 SmallVectorImpl<SDValue> &Results);
162 SDValue ExpandFCOPYSIGN(SDNode *Node);
Owen Andersone50ed302009-08-10 22:56:29 +0000163 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +0000164 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000165 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000166 DebugLoc dl);
Owen Andersone50ed302009-08-10 22:56:29 +0000167 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned,
Dale Johannesenaf435272009-02-02 19:03:57 +0000168 DebugLoc dl);
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Dale Johannesen8a782a22009-02-02 22:12:50 +0000170 SDValue ExpandBSWAP(SDValue Op, DebugLoc dl);
171 SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000172
Eli Friedman3d43b3f2009-05-23 22:37:25 +0000173 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000174 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
Eli Friedman8c377c72009-05-27 01:25:56 +0000175
Jim Grosbache03262f2010-06-18 21:43:38 +0000176 std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node);
177
Eli Friedman8c377c72009-05-27 01:25:56 +0000178 void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
179 void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000180};
181}
182
Nate Begeman5a5ca152009-04-29 05:20:52 +0000183/// ShuffleWithNarrowerEltType - Return a vector shuffle operation which
184/// performs the same shuffe in terms of order or result bytes, but on a type
185/// whose vector element type is narrower than the original shuffle type.
Nate Begeman9008ca62009-04-27 18:41:29 +0000186/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
Jim Grosbach6e992612010-07-02 17:41:59 +0000187SDValue
188SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl,
Nate Begeman5a5ca152009-04-29 05:20:52 +0000189 SDValue N1, SDValue N2,
Nate Begeman9008ca62009-04-27 18:41:29 +0000190 SmallVectorImpl<int> &Mask) const {
Nate Begeman5a5ca152009-04-29 05:20:52 +0000191 unsigned NumMaskElts = VT.getVectorNumElements();
192 unsigned NumDestElts = NVT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000193 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
Chris Lattner4352cc92006-04-04 17:23:26 +0000194
Nate Begeman9008ca62009-04-27 18:41:29 +0000195 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
196
197 if (NumEltsGrowth == 1)
198 return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]);
Jim Grosbach6e992612010-07-02 17:41:59 +0000199
Nate Begeman9008ca62009-04-27 18:41:29 +0000200 SmallVector<int, 8> NewMask;
Nate Begeman5a5ca152009-04-29 05:20:52 +0000201 for (unsigned i = 0; i != NumMaskElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000202 int Idx = Mask[i];
203 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
Jim Grosbach6e992612010-07-02 17:41:59 +0000204 if (Idx < 0)
Nate Begeman9008ca62009-04-27 18:41:29 +0000205 NewMask.push_back(-1);
206 else
207 NewMask.push_back(Idx * NumEltsGrowth + j);
Chris Lattner4352cc92006-04-04 17:23:26 +0000208 }
Chris Lattner4352cc92006-04-04 17:23:26 +0000209 }
Nate Begeman5a5ca152009-04-29 05:20:52 +0000210 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
Nate Begeman9008ca62009-04-27 18:41:29 +0000211 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
212 return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]);
Chris Lattner4352cc92006-04-04 17:23:26 +0000213}
214
Bill Wendling5aa49772009-02-24 02:35:30 +0000215SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag,
Eli Friedman1fde9c52009-05-24 02:46:31 +0000216 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000217 : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
218 DAG(dag), OptLevel(ol),
Eli Friedman1fde9c52009-05-24 02:46:31 +0000219 ValueTypeActions(TLI.getValueTypeActions()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000221 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000222}
223
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000225 LastCALLSEQ_END = DAG.getEntryNode();
Mon P Wange5ab34e2009-02-04 19:38:14 +0000226
Chris Lattnerab510a72005-10-02 17:49:46 +0000227 // The legalize process is inherently a bottom-up recursive process (users
228 // legalize their uses before themselves). Given infinite stack space, we
229 // could just start legalizing on the root and traverse the whole graph. In
230 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000231 // blocks. To avoid this problem, compute an ordering of the nodes where each
232 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000233 DAG.AssignTopologicalOrder();
234 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
Chris Lattner7896c9f2009-12-03 00:50:42 +0000235 E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I)
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000236 LegalizeOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000237
238 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000239 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000240 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
241 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242
Chris Lattner3e928bb2005-01-07 07:47:09 +0000243 LegalizedNodes.clear();
244
245 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000246 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000247}
248
Chris Lattner6831a812006-02-13 09:18:02 +0000249
250/// FindCallEndFromCallStart - Given a chained node that is part of a call
251/// sequence, find the CALLSEQ_END node that terminates the call sequence.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000252static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000253 // Nested CALLSEQ_START/END constructs aren't yet legal,
254 // but we can DTRT and handle them correctly here.
Stuart Hastingsa304d022010-12-09 21:25:20 +0000255 if (Node->getOpcode() == ISD::CALLSEQ_START)
256 depth++;
Stuart Hastings2965e692010-12-21 17:07:24 +0000257 else if (Node->getOpcode() == ISD::CALLSEQ_END) {
Stuart Hastings56500ed2010-12-21 17:16:58 +0000258 depth--;
259 if (depth == 0)
260 return Node;
261 }
Chris Lattner6831a812006-02-13 09:18:02 +0000262 if (Node->use_empty())
263 return 0; // No CallSeqEnd
Scott Michelfdc40a02009-02-17 22:15:04 +0000264
Chris Lattner6831a812006-02-13 09:18:02 +0000265 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000266 SDValue TheChain(Node, Node->getNumValues()-1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000268 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000269 TheChain = SDValue(Node, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 if (TheChain.getValueType() != MVT::Other) {
Chris Lattner6831a812006-02-13 09:18:02 +0000271 // Otherwise, hunt for it.
272 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000274 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000275 break;
276 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
278 // Otherwise, we walked into a node without a chain.
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 if (TheChain.getValueType() != MVT::Other)
Chris Lattner6831a812006-02-13 09:18:02 +0000280 return 0;
281 }
282 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Chris Lattner6831a812006-02-13 09:18:02 +0000284 for (SDNode::use_iterator UI = Node->use_begin(),
285 E = Node->use_end(); UI != E; ++UI) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000286
Chris Lattner6831a812006-02-13 09:18:02 +0000287 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000288 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000289 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
290 if (User->getOperand(i) == TheChain)
Stuart Hastingsa304d022010-12-09 21:25:20 +0000291 if (SDNode *Result = FindCallEndFromCallStart(User, depth))
Chris Lattner6831a812006-02-13 09:18:02 +0000292 return Result;
293 }
294 return 0;
295}
296
Scott Michelfdc40a02009-02-17 22:15:04 +0000297/// FindCallStartFromCallEnd - Given a chained node that is part of a call
Chris Lattner6831a812006-02-13 09:18:02 +0000298/// sequence, find the CALLSEQ_START node that initiates the call sequence.
299static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
Stuart Hastingsa304d022010-12-09 21:25:20 +0000300 int nested = 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000301 assert(Node && "Didn't find callseq_start for a call??");
Stuart Hastingsa304d022010-12-09 21:25:20 +0000302 while (Node->getOpcode() != ISD::CALLSEQ_START || nested) {
303 Node = Node->getOperand(0).getNode();
304 assert(Node->getOperand(0).getValueType() == MVT::Other &&
305 "Node doesn't have a token chain argument!");
306 switch (Node->getOpcode()) {
307 default:
308 break;
309 case ISD::CALLSEQ_START:
310 if (!nested)
311 return Node;
312 nested--;
313 break;
314 case ISD::CALLSEQ_END:
315 nested++;
316 break;
317 }
318 }
319 return 0;
Chris Lattner6831a812006-02-13 09:18:02 +0000320}
321
322/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
Scott Michelfdc40a02009-02-17 22:15:04 +0000323/// see if any uses can reach Dest. If no dest operands can get to dest,
Chris Lattner6831a812006-02-13 09:18:02 +0000324/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000325///
326/// Keep track of the nodes we fine that actually do lead to Dest in
327/// NodesLeadingTo. This avoids retraversing them exponential number of times.
328///
329bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000330 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000331 if (N == Dest) return true; // N certainly leads to Dest :)
Scott Michelfdc40a02009-02-17 22:15:04 +0000332
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000333 // If we've already processed this node and it does lead to Dest, there is no
334 // need to reprocess it.
335 if (NodesLeadingTo.count(N)) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000336
Chris Lattner6831a812006-02-13 09:18:02 +0000337 // If the first result of this node has been already legalized, then it cannot
338 // reach N.
Eli Friedman74807f22009-05-26 08:55:52 +0000339 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000340
Chris Lattner6831a812006-02-13 09:18:02 +0000341 // Okay, this node has not already been legalized. Check and legalize all
342 // operands. If none lead to Dest, then we can legalize this node.
343 bool OperandsLeadToDest = false;
344 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
345 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Jim Grosbach6e992612010-07-02 17:41:59 +0000346 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest,
347 NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000348
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000349 if (OperandsLeadToDest) {
350 NodesLeadingTo.insert(N);
351 return true;
352 }
Chris Lattner6831a812006-02-13 09:18:02 +0000353
354 // Okay, this node looks safe, legalize it and return false.
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000355 LegalizeOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000356 return false;
357}
358
Evan Cheng9f877882006-12-13 20:57:08 +0000359/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
360/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000361static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Dan Gohman0d137d72009-01-15 16:43:02 +0000362 SelectionDAG &DAG, const TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000363 bool Extend = false;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000364 DebugLoc dl = CFP->getDebugLoc();
Evan Cheng00495212006-12-12 21:32:44 +0000365
366 // If a FP immediate is precise when represented as a float and if the
367 // target can do an extending load from float to double, we put it into
368 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000369 // double. This shrinks FP constants and canonicalizes them for targets where
370 // an FP extending load is the same cost as a normal load (such as on the x87
371 // fp stack or PPC FP unit).
Owen Andersone50ed302009-08-10 22:56:29 +0000372 EVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000373 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000374 if (!UseCP) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000376 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000378 }
379
Owen Andersone50ed302009-08-10 22:56:29 +0000380 EVT OrigVT = VT;
381 EVT SVT = VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 while (SVT != MVT::f32) {
383 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
Dan Gohman7720cb32010-06-18 14:01:07 +0000384 if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) &&
Evan Chengef120572008-03-04 08:05:30 +0000385 // Only do this if the target has a native EXTLOAD instruction from
386 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000387 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000388 TLI.ShouldShrinkFPConstant(OrigVT)) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000389 const Type *SType = SVT.getTypeForEVT(*DAG.getContext());
Owen Andersonbaf3c402009-07-29 18:55:55 +0000390 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
Evan Chengef120572008-03-04 08:05:30 +0000391 VT = SVT;
392 Extend = true;
393 }
Evan Cheng00495212006-12-12 21:32:44 +0000394 }
395
Dan Gohman475871a2008-07-27 21:46:04 +0000396 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +0000397 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000398 if (Extend)
Evan Chengbcc80172010-07-07 22:15:37 +0000399 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, dl,
400 DAG.getEntryNode(),
Chris Lattner85ca1062010-09-21 07:32:19 +0000401 CPIdx, MachinePointerInfo::getConstantPool(),
402 VT, false, false, Alignment);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000403 return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +0000404 MachinePointerInfo::getConstantPool(), false, false,
David Greene1e559442010-02-15 17:00:31 +0000405 Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000406}
407
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000408/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
409static
Dan Gohman475871a2008-07-27 21:46:04 +0000410SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
Dan Gohman0d137d72009-01-15 16:43:02 +0000411 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000412 SDValue Chain = ST->getChain();
413 SDValue Ptr = ST->getBasePtr();
414 SDValue Val = ST->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000415 EVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000416 int Alignment = ST->getAlignment();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000417 DebugLoc dl = ST->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000418 if (ST->getMemoryVT().isFloatingPoint() ||
419 ST->getMemoryVT().isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000420 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000421 if (TLI.isTypeLegal(intVT)) {
422 // Expand to a bitconvert of the value to the integer type of the
423 // same size, then a (misaligned) int store.
424 // FIXME: Does not handle truncating floating point stores!
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000425 SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000426 return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(),
427 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000428 } else {
429 // Do a (aligned) store to a stack slot, then copy from the stack slot
430 // to the final destination using (unaligned) integer loads and stores.
Owen Andersone50ed302009-08-10 22:56:29 +0000431 EVT StoredVT = ST->getMemoryVT();
432 EVT RegVT =
Evan Chengadf97992010-04-15 01:25:27 +0000433 TLI.getRegisterType(*DAG.getContext(),
434 EVT::getIntegerVT(*DAG.getContext(),
435 StoredVT.getSizeInBits()));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000436 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
437 unsigned RegBytes = RegVT.getSizeInBits() / 8;
438 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000439
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000440 // Make sure the stack slot is also aligned for the register type.
441 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
442
443 // Perform the original store, only redirected to the stack slot.
Scott Michelfdc40a02009-02-17 22:15:04 +0000444 SDValue Store = DAG.getTruncStore(Chain, dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000445 Val, StackPtr, MachinePointerInfo(),
446 StoredVT, false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000447 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
448 SmallVector<SDValue, 8> Stores;
449 unsigned Offset = 0;
450
451 // Do all but one copies using the full register width.
452 for (unsigned i = 1; i < NumRegs; i++) {
453 // Load one integer register's worth from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000454 SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr,
455 MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +0000456 false, false, 0);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000457 // Store it to the final location. Remember the store.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000458 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattnerecf42c42010-09-21 16:36:31 +0000459 ST->getPointerInfo().getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +0000460 ST->isVolatile(), ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000461 MinAlign(ST->getAlignment(), Offset)));
462 // Increment the pointers.
463 Offset += RegBytes;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000464 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands05e11fa2008-12-12 21:47:02 +0000465 Increment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000466 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000467 }
468
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000469 // The last store may be partial. Do a truncating store. On big-endian
470 // machines this requires an extending load from the stack slot to ensure
471 // that the bits are in the right place.
Evan Chengadf97992010-04-15 01:25:27 +0000472 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
473 8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000474
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000475 // Load from the stack slot.
Evan Chengbcc80172010-07-07 22:15:37 +0000476 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Store, StackPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000477 MachinePointerInfo(),
478 MemVT, false, false, 0);
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000479
Dale Johannesenbb5da912009-02-02 20:41:04 +0000480 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000481 ST->getPointerInfo()
482 .getWithOffset(Offset),
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000483 MemVT, ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000484 ST->isNonTemporal(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000485 MinAlign(ST->getAlignment(), Offset)));
486 // The order of the stores doesn't matter - say it with a TokenFactor.
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
Duncan Sands05e11fa2008-12-12 21:47:02 +0000488 Stores.size());
489 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000490 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000491 assert(ST->getMemoryVT().isInteger() &&
492 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000493 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000494 // Get the half-size VT
Ken Dyckbceddbd2009-12-17 20:09:43 +0000495 EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000496 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000497 int IncrementSize = NumBits / 8;
498
499 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000500 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
501 SDValue Lo = Val;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000502 SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000503
504 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000505 SDValue Store1, Store2;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000506 Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000507 ST->getPointerInfo(), NewStoredVT,
David Greene1e559442010-02-15 17:00:31 +0000508 ST->isVolatile(), ST->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000509 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000510 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000511 Alignment = MinAlign(Alignment, IncrementSize);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000512 Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000513 ST->getPointerInfo().getWithOffset(IncrementSize),
David Greene1e559442010-02-15 17:00:31 +0000514 NewStoredVT, ST->isVolatile(), ST->isNonTemporal(),
515 Alignment);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000518}
519
520/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
521static
Dan Gohman475871a2008-07-27 21:46:04 +0000522SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
Dan Gohmane9530ec2009-01-15 16:58:17 +0000523 const TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +0000524 SDValue Chain = LD->getChain();
525 SDValue Ptr = LD->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +0000526 EVT VT = LD->getValueType(0);
527 EVT LoadedVT = LD->getMemoryVT();
Dale Johannesenbb5da912009-02-02 20:41:04 +0000528 DebugLoc dl = LD->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000529 if (VT.isFloatingPoint() || VT.isVector()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000530 EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000531 if (TLI.isTypeLegal(intVT)) {
532 // Expand to a (misaligned) integer load of the same size,
533 // then bitconvert to floating point or vector.
Chris Lattnerecf42c42010-09-21 16:36:31 +0000534 SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(),
535 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000536 LD->isNonTemporal(), LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000537 SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000538 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesenbb5da912009-02-02 20:41:04 +0000539 Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000540
Duncan Sands05e11fa2008-12-12 21:47:02 +0000541 SDValue Ops[] = { Result, Chain };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000542 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sands05e11fa2008-12-12 21:47:02 +0000543 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000544
Chris Lattnerecf42c42010-09-21 16:36:31 +0000545 // Copy the value to a (aligned) stack slot using (unaligned) integer
546 // loads and stores, then do a (aligned) load from the stack slot.
547 EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT);
548 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
549 unsigned RegBytes = RegVT.getSizeInBits() / 8;
550 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
551
552 // Make sure the stack slot is also aligned for the register type.
553 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
554
555 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
556 SmallVector<SDValue, 8> Stores;
557 SDValue StackPtr = StackBase;
558 unsigned Offset = 0;
559
560 // Do all but one copies using the full register width.
561 for (unsigned i = 1; i < NumRegs; i++) {
562 // Load one integer register's worth from the original location.
563 SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr,
564 LD->getPointerInfo().getWithOffset(Offset),
565 LD->isVolatile(), LD->isNonTemporal(),
566 MinAlign(LD->getAlignment(), Offset));
567 // Follow the load with a store to the stack slot. Remember the store.
568 Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000569 MachinePointerInfo(), false, false, 0));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000570 // Increment the pointers.
571 Offset += RegBytes;
572 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
573 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
574 Increment);
575 }
576
577 // The last copy may be partial. Do an extending load.
578 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(),
579 8 * (LoadedBytes - Offset));
580 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, dl, Chain, Ptr,
581 LD->getPointerInfo().getWithOffset(Offset),
582 MemVT, LD->isVolatile(),
583 LD->isNonTemporal(),
584 MinAlign(LD->getAlignment(), Offset));
585 // Follow the load with a store to the stack slot. Remember the store.
586 // On big-endian machines this requires a truncating store to ensure
587 // that the bits end up in the right place.
588 Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr,
589 MachinePointerInfo(), MemVT,
590 false, false, 0));
591
592 // The order of the stores doesn't matter - say it with a TokenFactor.
593 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0],
594 Stores.size());
595
596 // Finally, perform the original load only redirected to the stack slot.
597 Load = DAG.getExtLoad(LD->getExtensionType(), VT, dl, TF, StackBase,
598 MachinePointerInfo(), LoadedVT, false, false, 0);
599
600 // Callers expect a MERGE_VALUES node.
601 SDValue Ops[] = { Load, TF };
602 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000603 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000604 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000605 "Unaligned load of unsupported type.");
606
Dale Johannesen8155d642008-02-27 22:36:00 +0000607 // Compute the new VT that is half the size of the old one. This is an
608 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000609 unsigned NumBits = LoadedVT.getSizeInBits();
Owen Andersone50ed302009-08-10 22:56:29 +0000610 EVT NewLoadedVT;
Owen Anderson23b9b192009-08-12 00:36:31 +0000611 NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000612 NumBits >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000613
Chris Lattnere400af82007-11-19 21:38:03 +0000614 unsigned Alignment = LD->getAlignment();
615 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000616 ISD::LoadExtType HiExtType = LD->getExtensionType();
617
618 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
619 if (HiExtType == ISD::NON_EXTLOAD)
620 HiExtType = ISD::ZEXTLOAD;
621
622 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000623 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000624 if (TLI.isLittleEndian()) {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000625 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr, LD->getPointerInfo(),
626 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000627 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000628 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000629 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000630 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr,
631 LD->getPointerInfo().getWithOffset(IncrementSize),
632 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000633 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000634 } else {
Chris Lattnerecf42c42010-09-21 16:36:31 +0000635 Hi = DAG.getExtLoad(HiExtType, VT, dl, Chain, Ptr, LD->getPointerInfo(),
636 NewLoadedVT, LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +0000637 LD->isNonTemporal(), Alignment);
Dale Johannesenbb5da912009-02-02 20:41:04 +0000638 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000639 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Chris Lattnerecf42c42010-09-21 16:36:31 +0000640 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, dl, Chain, Ptr,
641 LD->getPointerInfo().getWithOffset(IncrementSize),
642 NewLoadedVT, LD->isVolatile(),
Jim Grosbach6e992612010-07-02 17:41:59 +0000643 LD->isNonTemporal(), MinAlign(Alignment,IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000644 }
645
646 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000647 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
Dale Johannesenbb5da912009-02-02 20:41:04 +0000648 SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount);
649 Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000652 Hi.getValue(1));
653
Dan Gohman475871a2008-07-27 21:46:04 +0000654 SDValue Ops[] = { Result, TF };
Dale Johannesenbb5da912009-02-02 20:41:04 +0000655 return DAG.getMergeValues(Ops, 2, dl);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000656}
Evan Cheng912095b2007-01-04 21:56:39 +0000657
Nate Begeman68679912008-04-25 18:07:40 +0000658/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
659/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
660/// is necessary to spill the vector being inserted into to memory, perform
661/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000662SDValue SelectionDAGLegalize::
Dale Johannesenbb5da912009-02-02 20:41:04 +0000663PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
664 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +0000665 SDValue Tmp1 = Vec;
666 SDValue Tmp2 = Val;
667 SDValue Tmp3 = Idx;
Scott Michelfdc40a02009-02-17 22:15:04 +0000668
Nate Begeman68679912008-04-25 18:07:40 +0000669 // If the target doesn't support this, we have to spill the input vector
670 // to a temporary stack slot, update the element, then reload it. This is
671 // badness. We could also load the value into a vector register (either
672 // with a "move to register" or "extload into register" instruction, then
673 // permute it into place, if the idx is a constant and if the idx is
674 // supported by the target.
Owen Andersone50ed302009-08-10 22:56:29 +0000675 EVT VT = Tmp1.getValueType();
676 EVT EltVT = VT.getVectorElementType();
677 EVT IdxVT = Tmp3.getValueType();
678 EVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000679 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000680
Evan Chengff89dcb2009-10-18 18:16:27 +0000681 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
682
Nate Begeman68679912008-04-25 18:07:40 +0000683 // Store the vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000684 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000685 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +0000686 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000687
688 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000689 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000690 Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3);
Nate Begeman68679912008-04-25 18:07:40 +0000691 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +0000692 unsigned EltSize = EltVT.getSizeInBits()/8;
Dale Johannesenbb5da912009-02-02 20:41:04 +0000693 Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
694 SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000695 // Store the scalar value.
Chris Lattner85ca1062010-09-21 07:32:19 +0000696 Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT,
David Greene1e559442010-02-15 17:00:31 +0000697 false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000698 // Load the updated vector.
Dale Johannesenbb5da912009-02-02 20:41:04 +0000699 return DAG.getLoad(VT, dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +0000700 MachinePointerInfo::getFixedStack(SPFI), false, false, 0);
Nate Begeman68679912008-04-25 18:07:40 +0000701}
702
Mon P Wange9f10152008-12-09 05:46:39 +0000703
Eli Friedman3f727d62009-05-27 02:16:40 +0000704SDValue SelectionDAGLegalize::
705ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) {
706 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
707 // SCALAR_TO_VECTOR requires that the type of the value being inserted
708 // match the element type of the vector being created, except for
709 // integers in which case the inserted value can be over width.
Owen Andersone50ed302009-08-10 22:56:29 +0000710 EVT EltVT = Vec.getValueType().getVectorElementType();
Eli Friedman3f727d62009-05-27 02:16:40 +0000711 if (Val.getValueType() == EltVT ||
712 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
713 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
714 Vec.getValueType(), Val);
715
716 unsigned NumElts = Vec.getValueType().getVectorNumElements();
717 // We generate a shuffle of InVec and ScVec, so the shuffle mask
718 // should be 0,1,2,3,4,5... with the appropriate element replaced with
719 // elt 0 of the RHS.
720 SmallVector<int, 8> ShufOps;
721 for (unsigned i = 0; i != NumElts; ++i)
722 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
723
724 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec,
725 &ShufOps[0]);
726 }
727 }
728 return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
729}
730
Eli Friedman7ef3d172009-06-06 07:04:42 +0000731SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
732 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
733 // FIXME: We shouldn't do this for TargetConstantFP's.
734 // FIXME: move this to the DAG Combiner! Note that we can't regress due
735 // to phase ordering between legalized code and the dag combiner. This
736 // probably means that we need to integrate dag combiner and legalizer
737 // together.
738 // We generally can't do this one for long doubles.
739 SDValue Tmp1 = ST->getChain();
740 SDValue Tmp2 = ST->getBasePtr();
741 SDValue Tmp3;
Eli Friedman7ef3d172009-06-06 07:04:42 +0000742 unsigned Alignment = ST->getAlignment();
743 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +0000744 bool isNonTemporal = ST->isNonTemporal();
Eli Friedman7ef3d172009-06-06 07:04:42 +0000745 DebugLoc dl = ST->getDebugLoc();
746 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 if (CFP->getValueType(0) == MVT::f32 &&
748 getTypeAction(MVT::i32) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000749 Tmp3 = DAG.getConstant(CFP->getValueAPF().
750 bitcastToAPInt().zextOrTrunc(32),
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 MVT::i32);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000752 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
753 isVolatile, isNonTemporal, Alignment);
754 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000755
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000756 if (CFP->getValueType(0) == MVT::f64) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000757 // If this target supports 64-bit registers, do a single 64-bit store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 if (getTypeAction(MVT::i64) == Legal) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000759 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 zextOrTrunc(64), MVT::i64);
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000761 return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
762 isVolatile, isNonTemporal, Alignment);
763 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000764
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000765 if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +0000766 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
767 // stores. If the target supports neither 32- nor 64-bits, this
768 // xform is certainly not worth it.
769 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Jay Foad40f8f622010-12-07 08:25:19 +0000770 SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000772 if (TLI.isBigEndian()) std::swap(Lo, Hi);
773
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000774 Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile,
775 isNonTemporal, Alignment);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000776 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
777 DAG.getIntPtrConstant(4));
Chris Lattnerda2d8e12010-09-21 17:42:31 +0000778 Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2,
779 ST->getPointerInfo().getWithOffset(4),
David Greene1e559442010-02-15 17:00:31 +0000780 isVolatile, isNonTemporal, MinAlign(Alignment, 4U));
Eli Friedman7ef3d172009-06-06 07:04:42 +0000781
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Eli Friedman7ef3d172009-06-06 07:04:42 +0000783 }
784 }
785 }
786 return SDValue();
787}
788
Dan Gohmana3466152007-07-13 20:14:11 +0000789/// LegalizeOp - We know that the specified value has a legal type, and
790/// that its operands are legal. Now ensure that the operation itself
791/// is legal, recursively ensuring that the operands' operations remain
792/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000793SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000794 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
795 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +0000796
Gabor Greifba36cb52008-08-28 21:40:38 +0000797 SDNode *Node = Op.getNode();
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000798 DebugLoc dl = Node->getDebugLoc();
Chris Lattnere3304a32005-01-08 20:35:13 +0000799
Eli Friedman1fde9c52009-05-24 02:46:31 +0000800 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
801 assert(getTypeAction(Node->getValueType(i)) == Legal &&
802 "Unexpected illegal type!");
803
804 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Jim Grosbach6e992612010-07-02 17:41:59 +0000805 assert((isTypeLegal(Node->getOperand(i).getValueType()) ||
Eli Friedman1fde9c52009-05-24 02:46:31 +0000806 Node->getOperand(i).getOpcode() == ISD::TargetConstant) &&
807 "Unexpected illegal type!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000808
Chris Lattner45982da2005-05-12 16:53:42 +0000809 // Note that LegalizeOp may be reentered even from single-use nodes, which
810 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000811 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000812 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000813
Dan Gohman475871a2008-07-27 21:46:04 +0000814 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
815 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000816 bool isCustom = false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
Eli Friedman8c377c72009-05-27 01:25:56 +0000818 // Figure out the correct action; the way to query this varies by opcode
819 TargetLowering::LegalizeAction Action;
820 bool SimpleFinishLegalizing = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000821 switch (Node->getOpcode()) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000822 case ISD::INTRINSIC_W_CHAIN:
823 case ISD::INTRINSIC_WO_CHAIN:
824 case ISD::INTRINSIC_VOID:
825 case ISD::VAARG:
826 case ISD::STACKSAVE:
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
Eli Friedman8c377c72009-05-27 01:25:56 +0000828 break;
829 case ISD::SINT_TO_FP:
830 case ISD::UINT_TO_FP:
831 case ISD::EXTRACT_VECTOR_ELT:
832 Action = TLI.getOperationAction(Node->getOpcode(),
833 Node->getOperand(0).getValueType());
834 break;
835 case ISD::FP_ROUND_INREG:
836 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +0000837 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +0000838 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
839 break;
840 }
Eli Friedman3be2e512009-05-28 03:06:16 +0000841 case ISD::SELECT_CC:
842 case ISD::SETCC:
843 case ISD::BR_CC: {
844 unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 :
845 Node->getOpcode() == ISD::SETCC ? 2 : 1;
846 unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0;
Owen Andersone50ed302009-08-10 22:56:29 +0000847 EVT OpVT = Node->getOperand(CompareOperand).getValueType();
Eli Friedman3be2e512009-05-28 03:06:16 +0000848 ISD::CondCode CCCode =
849 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
850 Action = TLI.getCondCodeAction(CCCode, OpVT);
851 if (Action == TargetLowering::Legal) {
852 if (Node->getOpcode() == ISD::SELECT_CC)
853 Action = TLI.getOperationAction(Node->getOpcode(),
854 Node->getValueType(0));
855 else
856 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
857 }
858 break;
859 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000860 case ISD::LOAD:
861 case ISD::STORE:
Eli Friedmanad754602009-05-28 03:56:57 +0000862 // FIXME: Model these properly. LOAD and STORE are complicated, and
863 // STORE expects the unlegalized operand in some cases.
864 SimpleFinishLegalizing = false;
865 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000866 case ISD::CALLSEQ_START:
867 case ISD::CALLSEQ_END:
Eli Friedmanad754602009-05-28 03:56:57 +0000868 // FIXME: This shouldn't be necessary. These nodes have special properties
869 // dealing with the recursive nature of legalization. Removing this
870 // special case should be done as part of making LegalizeDAG non-recursive.
871 SimpleFinishLegalizing = false;
872 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000873 case ISD::EXTRACT_ELEMENT:
874 case ISD::FLT_ROUNDS_:
875 case ISD::SADDO:
876 case ISD::SSUBO:
877 case ISD::UADDO:
878 case ISD::USUBO:
879 case ISD::SMULO:
880 case ISD::UMULO:
881 case ISD::FPOWI:
882 case ISD::MERGE_VALUES:
883 case ISD::EH_RETURN:
884 case ISD::FRAME_TO_ARGS_OFFSET:
Jim Grosbachc66e150b2010-07-06 23:44:52 +0000885 case ISD::EH_SJLJ_SETJMP:
886 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +0000887 case ISD::EH_SJLJ_DISPATCHSETUP:
Eli Friedmanf6b23bf2009-05-27 03:33:44 +0000888 // These operations lie about being legal: when they claim to be legal,
889 // they should actually be expanded.
Eli Friedman8c377c72009-05-27 01:25:56 +0000890 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
891 if (Action == TargetLowering::Legal)
892 Action = TargetLowering::Expand;
893 break;
894 case ISD::TRAMPOLINE:
895 case ISD::FRAMEADDR:
896 case ISD::RETURNADDR:
Eli Friedman4bc8c712009-05-27 12:20:41 +0000897 // These operations lie about being legal: when they claim to be legal,
898 // they should actually be custom-lowered.
899 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
900 if (Action == TargetLowering::Legal)
901 Action = TargetLowering::Custom;
Eli Friedman8c377c72009-05-27 01:25:56 +0000902 break;
903 case ISD::BUILD_VECTOR:
Eli Friedmanb5da3f62009-05-27 12:42:55 +0000904 // A weird case: legalization for BUILD_VECTOR never legalizes the
905 // operands!
906 // FIXME: This really sucks... changing it isn't semantically incorrect,
907 // but it massively pessimizes the code for floating-point BUILD_VECTORs
908 // because ConstantFP operands get legalized into constant pool loads
909 // before the BUILD_VECTOR code can see them. It doesn't usually bite,
910 // though, because BUILD_VECTORS usually get lowered into other nodes
911 // which get legalized properly.
Eli Friedman8c377c72009-05-27 01:25:56 +0000912 SimpleFinishLegalizing = false;
Chris Lattner948c1b12006-01-28 08:31:04 +0000913 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000914 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000915 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
Eli Friedman8c377c72009-05-27 01:25:56 +0000916 Action = TargetLowering::Legal;
917 } else {
918 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000919 }
Eli Friedman8c377c72009-05-27 01:25:56 +0000920 break;
921 }
922
923 if (SimpleFinishLegalizing) {
924 SmallVector<SDValue, 8> Ops, ResultVals;
925 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
926 Ops.push_back(LegalizeOp(Node->getOperand(i)));
927 switch (Node->getOpcode()) {
928 default: break;
929 case ISD::BR:
930 case ISD::BRIND:
931 case ISD::BR_JT:
932 case ISD::BR_CC:
933 case ISD::BRCOND:
Eli Friedman8c377c72009-05-27 01:25:56 +0000934 // Branches tweak the chain to include LastCALLSEQ_END
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0],
Eli Friedman8c377c72009-05-27 01:25:56 +0000936 LastCALLSEQ_END);
937 Ops[0] = LegalizeOp(Ops[0]);
938 LastCALLSEQ_END = DAG.getEntryNode();
939 break;
940 case ISD::SHL:
941 case ISD::SRL:
942 case ISD::SRA:
943 case ISD::ROTL:
944 case ISD::ROTR:
945 // Legalizing shifts/rotates requires adjusting the shift amount
946 // to the appropriate width.
947 if (!Ops[1].getValueType().isVector())
948 Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1]));
949 break;
Dan Gohmandb8dc2b2009-08-18 23:36:17 +0000950 case ISD::SRL_PARTS:
951 case ISD::SRA_PARTS:
952 case ISD::SHL_PARTS:
953 // Legalizing shifts/rotates requires adjusting the shift amount
954 // to the appropriate width.
955 if (!Ops[2].getValueType().isVector())
956 Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[2]));
Dan Gohman2c9489d2009-08-18 23:52:48 +0000957 break;
Eli Friedman8c377c72009-05-27 01:25:56 +0000958 }
959
Dan Gohman027657d2010-06-18 15:30:29 +0000960 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(),
961 Ops.size()), 0);
Eli Friedman8c377c72009-05-27 01:25:56 +0000962 switch (Action) {
963 case TargetLowering::Legal:
964 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
965 ResultVals.push_back(Result.getValue(i));
966 break;
967 case TargetLowering::Custom:
968 // FIXME: The handling for custom lowering with multiple results is
969 // a complete mess.
970 Tmp1 = TLI.LowerOperation(Result, DAG);
971 if (Tmp1.getNode()) {
972 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
973 if (e == 1)
974 ResultVals.push_back(Tmp1);
975 else
976 ResultVals.push_back(Tmp1.getValue(i));
977 }
978 break;
979 }
980
981 // FALL THROUGH
982 case TargetLowering::Expand:
983 ExpandNode(Result.getNode(), ResultVals);
984 break;
985 case TargetLowering::Promote:
986 PromoteNode(Result.getNode(), ResultVals);
987 break;
988 }
989 if (!ResultVals.empty()) {
990 for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) {
991 if (ResultVals[i] != SDValue(Node, i))
992 ResultVals[i] = LegalizeOp(ResultVals[i]);
993 AddLegalizedOperand(SDValue(Node, i), ResultVals[i]);
994 }
995 return ResultVals[Op.getResNo()];
996 }
997 }
998
999 switch (Node->getOpcode()) {
1000 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001001#ifndef NDEBUG
David Greene993aace2010-01-05 01:24:53 +00001002 dbgs() << "NODE: ";
1003 Node->dump( &DAG);
1004 dbgs() << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001005#endif
Chris Lattner35a38932010-04-07 23:47:51 +00001006 assert(0 && "Do not know how to legalize this operator!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001007
Chris Lattnerb2827b02006-03-19 00:52:58 +00001008 case ISD::BUILD_VECTOR:
1009 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner35a38932010-04-07 23:47:51 +00001010 default: assert(0 && "This action is not supported yet!");
Chris Lattner2332b9f2006-03-19 01:17:20 +00001011 case TargetLowering::Custom:
1012 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001013 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001014 Result = Tmp3;
1015 break;
1016 }
1017 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001018 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001019 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001020 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001021 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001022 break;
Chris Lattner6831a812006-02-13 09:18:02 +00001023 case ISD::CALLSEQ_START: {
Stuart Hastingsf2243222011-01-18 00:09:27 +00001024 static int depth = 0;
Chris Lattner6831a812006-02-13 09:18:02 +00001025 SDNode *CallEnd = FindCallEndFromCallStart(Node);
Scott Michelfdc40a02009-02-17 22:15:04 +00001026
Chris Lattner6831a812006-02-13 09:18:02 +00001027 // Recursively Legalize all of the inputs of the call end that do not lead
1028 // to this call start. This ensures that any libcalls that need be inserted
1029 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001030 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001031 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001032 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001033 NodesLeadingTo);
1034 }
Chris Lattner6831a812006-02-13 09:18:02 +00001035
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001036 // Now that we have legalized all of the inputs (which may have inserted
1037 // libcalls), create the new CALLSEQ_START node.
Chris Lattner6831a812006-02-13 09:18:02 +00001038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1039
Jim Grosbachee7f8b52010-07-02 17:38:34 +00001040 // Merge in the last call to ensure that this call starts after the last
Chris Lattner6831a812006-02-13 09:18:02 +00001041 // call ended.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001042 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken && depth == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesenca57b842009-02-02 23:46:53 +00001044 Tmp1, LastCALLSEQ_END);
Chris Lattnerb248e162006-05-17 17:55:45 +00001045 Tmp1 = LegalizeOp(Tmp1);
1046 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001047
Chris Lattner6831a812006-02-13 09:18:02 +00001048 // Do not try to legalize the target-specific arguments (#1+).
1049 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001050 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001051 Ops[0] = Tmp1;
Jim Grosbach6e992612010-07-02 17:41:59 +00001052 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0],
1053 Ops.size()), Result.getResNo());
Chris Lattner6831a812006-02-13 09:18:02 +00001054 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001055
Chris Lattner6831a812006-02-13 09:18:02 +00001056 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001057 AddLegalizedOperand(Op.getValue(0), Result);
1058 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1059 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Chris Lattner6831a812006-02-13 09:18:02 +00001061 // Now that the callseq_start and all of the non-call nodes above this call
Scott Michelfdc40a02009-02-17 22:15:04 +00001062 // sequence have been legalized, legalize the call itself. During this
Chris Lattner6831a812006-02-13 09:18:02 +00001063 // process, no libcalls can/will be inserted, guaranteeing that no calls
1064 // can overlap.
Stuart Hastingsf2243222011-01-18 00:09:27 +00001065
1066 SDValue Saved_LastCALLSEQ_END = LastCALLSEQ_END ;
Chris Lattner6831a812006-02-13 09:18:02 +00001067 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001068 LastCALLSEQ_END = SDValue(CallEnd, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001069
Stuart Hastingsf2243222011-01-18 00:09:27 +00001070 depth++;
Chris Lattner6831a812006-02-13 09:18:02 +00001071 // Legalize the call, starting from the CALLSEQ_END.
1072 LegalizeOp(LastCALLSEQ_END);
Stuart Hastingsf2243222011-01-18 00:09:27 +00001073 depth--;
1074 assert(depth >= 0 && "Un-matched CALLSEQ_START?");
1075 if (depth > 0)
1076 LastCALLSEQ_END = Saved_LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +00001077 return Result;
1078 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001079 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001080 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1081 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001083 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1084 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001085 assert(I != LegalizedNodes.end() &&
1086 "Legalizing the call start should have legalized this node!");
1087 return I->second;
1088 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001089
1090 // Otherwise, the call start has been legalized and everything is going
Chris Lattner6831a812006-02-13 09:18:02 +00001091 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001092 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001093 // Do not try to legalize the target-specific arguments (#1+), except for
1094 // an optional flag input.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001095 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){
Chris Lattner70814bc2006-01-29 07:58:15 +00001096 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001097 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001098 Ops[0] = Tmp1;
Dan Gohman027657d2010-06-18 15:30:29 +00001099 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1100 &Ops[0], Ops.size()),
1101 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001102 }
1103 } else {
1104 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1105 if (Tmp1 != Node->getOperand(0) ||
1106 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001107 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001108 Ops[0] = Tmp1;
1109 Ops.back() = Tmp2;
Dan Gohman027657d2010-06-18 15:30:29 +00001110 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1111 &Ops[0], Ops.size()),
1112 Result.getResNo());
Chris Lattner70814bc2006-01-29 07:58:15 +00001113 }
Chris Lattner6a542892006-01-24 05:48:21 +00001114 }
Chris Lattner6831a812006-02-13 09:18:02 +00001115 // This finishes up call legalization.
Chris Lattner4b653a02006-02-14 00:55:02 +00001116 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001117 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001118 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001119 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001120 return Result.getValue(Op.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001121 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001122 LoadSDNode *LD = cast<LoadSDNode>(Node);
1123 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1124 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001125
Evan Cheng466685d2006-10-09 20:57:25 +00001126 ISD::LoadExtType ExtType = LD->getExtensionType();
1127 if (ExtType == ISD::NON_EXTLOAD) {
Owen Andersone50ed302009-08-10 22:56:29 +00001128 EVT VT = Node->getValueType(0);
Dan Gohman027657d2010-06-18 15:30:29 +00001129 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1130 Tmp1, Tmp2, LD->getOffset()),
1131 Result.getResNo());
Evan Cheng466685d2006-10-09 20:57:25 +00001132 Tmp3 = Result.getValue(0);
1133 Tmp4 = Result.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001134
Evan Cheng466685d2006-10-09 20:57:25 +00001135 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001136 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001137 case TargetLowering::Legal:
1138 // If this is an unaligned load and the target doesn't support it,
1139 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001140 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001141 const Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1142 unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001143 if (LD->getAlignment() < ABIAlignment){
Jim Grosbach6e992612010-07-02 17:41:59 +00001144 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
Owen Andersondebcb012009-07-29 22:17:13 +00001145 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001146 Tmp3 = Result.getOperand(0);
1147 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001148 Tmp3 = LegalizeOp(Tmp3);
1149 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001150 }
1151 }
1152 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001153 case TargetLowering::Custom:
1154 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001155 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001156 Tmp3 = LegalizeOp(Tmp1);
1157 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001158 }
Evan Cheng466685d2006-10-09 20:57:25 +00001159 break;
1160 case TargetLowering::Promote: {
1161 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001162 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001163 // Change base type to a different vector type.
Owen Andersone50ed302009-08-10 22:56:29 +00001164 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001165
Chris Lattnerecf42c42010-09-21 16:36:31 +00001166 Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001167 LD->isVolatile(), LD->isNonTemporal(),
1168 LD->getAlignment());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001169 Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1));
Evan Cheng466685d2006-10-09 20:57:25 +00001170 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001171 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001172 }
Evan Cheng466685d2006-10-09 20:57:25 +00001173 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001174 // Since loads produce two values, make sure to remember that we
Evan Cheng466685d2006-10-09 20:57:25 +00001175 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001176 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1177 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001178 return Op.getResNo() ? Tmp4 : Tmp3;
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001179 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001180
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001181 EVT SrcVT = LD->getMemoryVT();
1182 unsigned SrcWidth = SrcVT.getSizeInBits();
1183 unsigned Alignment = LD->getAlignment();
1184 bool isVolatile = LD->isVolatile();
1185 bool isNonTemporal = LD->isNonTemporal();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001186
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001187 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
1188 // Some targets pretend to have an i1 loading operation, and actually
1189 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1190 // bits are guaranteed to be zero; it helps the optimizers understand
1191 // that these bits are zero. It is also useful for EXTLOAD, since it
1192 // tells the optimizers that those bits are undefined. It would be
1193 // nice to have an effective generic way of getting these benefits...
1194 // Until such a way is found, don't insist on promoting i1 here.
1195 (SrcVT != MVT::i1 ||
1196 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1197 // Promote to a byte-sized load if not loading an integral number of
1198 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1199 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1200 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
1201 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001202
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001203 // The extra bits are guaranteed to be zero, since we stored them that
1204 // way. A zext load from NVT thus automatically gives zext from SrcVT.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001205
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001206 ISD::LoadExtType NewExtType =
1207 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001208
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001209 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0), dl,
1210 Tmp1, Tmp2, LD->getPointerInfo(),
1211 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001212
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001213 Ch = Result.getValue(1); // The chain.
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001214
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001215 if (ExtType == ISD::SEXTLOAD)
1216 // Having the top bits zero doesn't help when sign extending.
1217 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1218 Result.getValueType(),
1219 Result, DAG.getValueType(SrcVT));
1220 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1221 // All the top bits are guaranteed to be zero - inform the optimizers.
1222 Result = DAG.getNode(ISD::AssertZext, dl,
1223 Result.getValueType(), Result,
1224 DAG.getValueType(SrcVT));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001225
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001226 Tmp1 = LegalizeOp(Result);
1227 Tmp2 = LegalizeOp(Ch);
1228 } else if (SrcWidth & (SrcWidth - 1)) {
1229 // If not loading a power-of-2 number of bits, expand as two loads.
1230 assert(!SrcVT.isVector() && "Unsupported extload!");
1231 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1232 assert(RoundWidth < SrcWidth);
1233 unsigned ExtraWidth = SrcWidth - RoundWidth;
1234 assert(ExtraWidth < RoundWidth);
1235 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1236 "Load size not an integral number of bytes!");
1237 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1238 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
1239 SDValue Lo, Hi, Ch;
1240 unsigned IncrementSize;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001241
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001242 if (TLI.isLittleEndian()) {
1243 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1244 // Load the bottom RoundWidth bits.
1245 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), dl,
1246 Tmp1, Tmp2,
1247 LD->getPointerInfo(), RoundVT, isVolatile,
1248 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001249
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001250 // Load the remaining ExtraWidth bits.
1251 IncrementSize = RoundWidth / 8;
1252 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1253 DAG.getIntPtrConstant(IncrementSize));
1254 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), dl, Tmp1, Tmp2,
1255 LD->getPointerInfo().getWithOffset(IncrementSize),
1256 ExtraVT, isVolatile, isNonTemporal,
1257 MinAlign(Alignment, IncrementSize));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001258
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001259 // Build a factor node to remember that this load is independent of
1260 // the other one.
1261 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1262 Hi.getValue(1));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001263
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001264 // Move the top bits to the right place.
1265 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1266 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001267
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001268 // Join the hi and lo parts.
1269 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001270 } else {
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001271 // Big endian - avoid unaligned loads.
1272 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1273 // Load the top RoundWidth bits.
1274 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), dl, Tmp1, Tmp2,
1275 LD->getPointerInfo(), RoundVT, isVolatile,
1276 isNonTemporal, Alignment);
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001277
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001278 // Load the remaining ExtraWidth bits.
1279 IncrementSize = RoundWidth / 8;
1280 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
1281 DAG.getIntPtrConstant(IncrementSize));
1282 Lo = DAG.getExtLoad(ISD::ZEXTLOAD,
1283 Node->getValueType(0), dl, Tmp1, Tmp2,
1284 LD->getPointerInfo().getWithOffset(IncrementSize),
1285 ExtraVT, isVolatile, isNonTemporal,
1286 MinAlign(Alignment, IncrementSize));
1287
1288 // Build a factor node to remember that this load is independent of
1289 // the other one.
1290 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1291 Hi.getValue(1));
1292
1293 // Move the top bits to the right place.
1294 Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi,
1295 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1296
1297 // Join the hi and lo parts.
1298 Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
Evan Cheng466685d2006-10-09 20:57:25 +00001299 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001300
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001301 Tmp1 = LegalizeOp(Result);
1302 Tmp2 = LegalizeOp(Ch);
1303 } else {
1304 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
1305 default: assert(0 && "This action is not supported yet!");
1306 case TargetLowering::Custom:
1307 isCustom = true;
1308 // FALLTHROUGH
1309 case TargetLowering::Legal:
1310 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1311 Tmp1, Tmp2, LD->getOffset()),
1312 Result.getResNo());
1313 Tmp1 = Result.getValue(0);
1314 Tmp2 = Result.getValue(1);
1315
1316 if (isCustom) {
1317 Tmp3 = TLI.LowerOperation(Result, DAG);
1318 if (Tmp3.getNode()) {
1319 Tmp1 = LegalizeOp(Tmp3);
1320 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1321 }
1322 } else {
1323 // If this is an unaligned load and the target doesn't support it,
1324 // expand it.
1325 if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) {
1326 const Type *Ty =
1327 LD->getMemoryVT().getTypeForEVT(*DAG.getContext());
1328 unsigned ABIAlignment =
1329 TLI.getTargetData()->getABITypeAlignment(Ty);
1330 if (LD->getAlignment() < ABIAlignment){
1331 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()),
1332 DAG, TLI);
1333 Tmp1 = Result.getOperand(0);
1334 Tmp2 = Result.getOperand(1);
1335 Tmp1 = LegalizeOp(Tmp1);
1336 Tmp2 = LegalizeOp(Tmp2);
1337 }
1338 }
1339 }
1340 break;
1341 case TargetLowering::Expand:
1342 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && isTypeLegal(SrcVT)) {
1343 SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2,
1344 LD->getPointerInfo(),
1345 LD->isVolatile(), LD->isNonTemporal(),
1346 LD->getAlignment());
1347 unsigned ExtendOp;
1348 switch (ExtType) {
1349 case ISD::EXTLOAD:
1350 ExtendOp = (SrcVT.isFloatingPoint() ?
1351 ISD::FP_EXTEND : ISD::ANY_EXTEND);
1352 break;
1353 case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break;
1354 case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break;
1355 default: llvm_unreachable("Unexpected extend load type!");
1356 }
1357 Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
1358 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1359 Tmp2 = LegalizeOp(Load.getValue(1));
1360 break;
1361 }
1362 // FIXME: This does not work for vectors on most targets. Sign- and
1363 // zero-extend operations are currently folded into extending loads,
1364 // whether they are legal or not, and then we end up here without any
1365 // support for legalizing them.
1366 assert(ExtType != ISD::EXTLOAD &&
1367 "EXTLOAD should always be supported!");
1368 // Turn the unsupported load into an EXTLOAD followed by an explicit
1369 // zero/sign extend inreg.
1370 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), dl,
1371 Tmp1, Tmp2, LD->getPointerInfo(), SrcVT,
1372 LD->isVolatile(), LD->isNonTemporal(),
1373 LD->getAlignment());
1374 SDValue ValRes;
1375 if (ExtType == ISD::SEXTLOAD)
1376 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
1377 Result.getValueType(),
1378 Result, DAG.getValueType(SrcVT));
1379 else
Dan Gohmandd11ea42011-01-13 01:06:51 +00001380 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType());
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001381 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1382 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1383 break;
1384 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001385 }
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001386
1387 // Since loads produce two values, make sure to remember that we legalized
1388 // both of them.
1389 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1390 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
1391 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001392 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001393 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001394 StoreSDNode *ST = cast<StoreSDNode>(Node);
1395 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1396 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001397 unsigned Alignment = ST->getAlignment();
1398 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00001399 bool isNonTemporal = ST->isNonTemporal();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001400
Evan Cheng8b2794a2006-10-13 21:14:26 +00001401 if (!ST->isTruncatingStore()) {
Eli Friedman7ef3d172009-06-06 07:04:42 +00001402 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
1403 Result = SDValue(OptStore, 0);
1404 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001406
Eli Friedman957bffa2009-05-24 08:42:01 +00001407 {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001408 Tmp3 = LegalizeOp(ST->getValue());
Dan Gohman027657d2010-06-18 15:30:29 +00001409 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1410 Tmp1, Tmp3, Tmp2,
1411 ST->getOffset()),
1412 Result.getResNo());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001413
Owen Andersone50ed302009-08-10 22:56:29 +00001414 EVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00001415 switch (TLI.getOperationAction(ISD::STORE, VT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001416 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001417 case TargetLowering::Legal:
1418 // If this is an unaligned store and the target doesn't support it,
1419 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001420 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001421 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001422 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001423 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001424 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1425 DAG, TLI);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001426 }
1427 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001428 case TargetLowering::Custom:
1429 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001430 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001431 break;
1432 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001433 assert(VT.isVector() && "Unknown legal promote case!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001434 Tmp3 = DAG.getNode(ISD::BITCAST, dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001435 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
Dale Johannesenca57b842009-02-02 23:46:53 +00001436 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001437 ST->getPointerInfo(), isVolatile,
David Greene1e559442010-02-15 17:00:31 +00001438 isNonTemporal, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001439 break;
1440 }
1441 break;
1442 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00001443 } else {
Eli Friedman957bffa2009-05-24 08:42:01 +00001444 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00001445
Owen Andersone50ed302009-08-10 22:56:29 +00001446 EVT StVT = ST->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001447 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00001448
Duncan Sands83ec4b62008-06-06 12:08:01 +00001449 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00001450 // Promote to a byte-sized store with upper bits zero if not
1451 // storing an integral number of bytes. For example, promote
1452 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Evan Chengadf97992010-04-15 01:25:27 +00001453 EVT NVT = EVT::getIntegerVT(*DAG.getContext(),
1454 StVT.getStoreSizeInBits());
Dale Johannesenca57b842009-02-02 23:46:53 +00001455 Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001456 Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1457 NVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001458 } else if (StWidth & (StWidth - 1)) {
1459 // If not storing a power-of-2 number of bits, expand as two stores.
Ken Dyckbceddbd2009-12-17 20:09:43 +00001460 assert(!StVT.isVector() && "Unsupported truncstore!");
Duncan Sands7e857202008-01-22 07:17:34 +00001461 unsigned RoundWidth = 1 << Log2_32(StWidth);
1462 assert(RoundWidth < StWidth);
1463 unsigned ExtraWidth = StWidth - RoundWidth;
1464 assert(ExtraWidth < RoundWidth);
1465 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1466 "Store size not an integral number of bytes!");
Owen Anderson23b9b192009-08-12 00:36:31 +00001467 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
1468 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001469 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00001470 unsigned IncrementSize;
1471
1472 if (TLI.isLittleEndian()) {
1473 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
1474 // Store the bottom RoundWidth bits.
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001475 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1476 RoundVT,
David Greene1e559442010-02-15 17:00:31 +00001477 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001478
1479 // Store the remaining ExtraWidth bits.
1480 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001481 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001482 DAG.getIntPtrConstant(IncrementSize));
Dale Johannesenca57b842009-02-02 23:46:53 +00001483 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001484 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001485 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2,
1486 ST->getPointerInfo().getWithOffset(IncrementSize),
1487 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001488 MinAlign(Alignment, IncrementSize));
1489 } else {
1490 // Big endian - avoid unaligned stores.
1491 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
1492 // Store the top RoundWidth bits.
Dale Johannesenca57b842009-02-02 23:46:53 +00001493 Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3,
Duncan Sands7e857202008-01-22 07:17:34 +00001494 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001495 Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(),
1496 RoundVT, isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001497
1498 // Store the remaining ExtraWidth bits.
1499 IncrementSize = RoundWidth / 8;
Dale Johannesenca57b842009-02-02 23:46:53 +00001500 Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2,
Duncan Sands7e857202008-01-22 07:17:34 +00001501 DAG.getIntPtrConstant(IncrementSize));
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001502 Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2,
1503 ST->getPointerInfo().getWithOffset(IncrementSize),
1504 ExtraVT, isVolatile, isNonTemporal,
Duncan Sands7e857202008-01-22 07:17:34 +00001505 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001506 }
Duncan Sands7e857202008-01-22 07:17:34 +00001507
1508 // The order of the stores doesn't matter.
Owen Anderson825b72b2009-08-11 20:47:22 +00001509 Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
Duncan Sands7e857202008-01-22 07:17:34 +00001510 } else {
1511 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1512 Tmp2 != ST->getBasePtr())
Dan Gohman027657d2010-06-18 15:30:29 +00001513 Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(),
1514 Tmp1, Tmp3, Tmp2,
1515 ST->getOffset()),
1516 Result.getResNo());
Duncan Sands7e857202008-01-22 07:17:34 +00001517
1518 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001519 default: assert(0 && "This action is not supported yet!");
Duncan Sands7e857202008-01-22 07:17:34 +00001520 case TargetLowering::Legal:
1521 // If this is an unaligned store and the target doesn't support it,
1522 // expand it.
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00001523 if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
Evan Chenge96507c2009-08-15 08:38:52 +00001524 const Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext());
Jim Grosbach6e992612010-07-02 17:41:59 +00001525 unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty);
Duncan Sands7e857202008-01-22 07:17:34 +00001526 if (ST->getAlignment() < ABIAlignment)
Evan Chenge96507c2009-08-15 08:38:52 +00001527 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()),
1528 DAG, TLI);
Duncan Sands7e857202008-01-22 07:17:34 +00001529 }
1530 break;
1531 case TargetLowering::Custom:
1532 Result = TLI.LowerOperation(Result, DAG);
1533 break;
1534 case Expand:
1535 // TRUNCSTORE:i16 i32 -> STORE i16
1536 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
Dale Johannesenca57b842009-02-02 23:46:53 +00001537 Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3);
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001538 Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(),
1539 isVolatile, isNonTemporal, Alignment);
Duncan Sands7e857202008-01-22 07:17:34 +00001540 break;
1541 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001542 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001543 }
1544 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001545 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001546 }
Chris Lattner4ddd2832006-04-08 04:13:17 +00001547 assert(Result.getValueType() == Op.getValueType() &&
1548 "Bad legalization!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Chris Lattner456a93a2006-01-28 07:39:30 +00001550 // Make sure that the generated code is itself legal.
1551 if (Result != Op)
1552 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001553
Chris Lattner45982da2005-05-12 16:53:42 +00001554 // Note that LegalizeOp may be reentered even from single-use nodes, which
1555 // means that we always must cache transformed nodes.
1556 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001557 return Result;
1558}
1559
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001560SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1561 SDValue Vec = Op.getOperand(0);
1562 SDValue Idx = Op.getOperand(1);
1563 DebugLoc dl = Op.getDebugLoc();
1564 // Store the value to a temporary stack slot, then LOAD the returned part.
1565 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Chris Lattner6229d0a2010-09-21 18:41:36 +00001566 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
1567 MachinePointerInfo(), false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001568
1569 // Add the offset to the index.
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001570 unsigned EltSize =
1571 Vec.getValueType().getVectorElementType().getSizeInBits()/8;
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001572 Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx,
1573 DAG.getConstant(EltSize, Idx.getValueType()));
1574
1575 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
1576 Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx);
1577 else
1578 Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx);
1579
1580 StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr);
1581
Eli Friedmanc680ac92009-07-09 22:01:03 +00001582 if (Op.getValueType().isVector())
Chris Lattnerecf42c42010-09-21 16:36:31 +00001583 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001584 false, false, 0);
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00001585 return DAG.getExtLoad(ISD::EXTLOAD, Op.getValueType(), dl, Ch, StackPtr,
1586 MachinePointerInfo(),
1587 Vec.getValueType().getVectorElementType(),
1588 false, false, 0);
Eli Friedman3d43b3f2009-05-23 22:37:25 +00001589}
1590
Eli Friedman7ef3d172009-06-06 07:04:42 +00001591SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1592 // We can't handle this case efficiently. Allocate a sufficiently
1593 // aligned object on the stack, store each element into it, then load
1594 // the result as a vector.
1595 // Create the stack frame object.
Owen Andersone50ed302009-08-10 22:56:29 +00001596 EVT VT = Node->getValueType(0);
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001597 EVT EltVT = VT.getVectorElementType();
Eli Friedman7ef3d172009-06-06 07:04:42 +00001598 DebugLoc dl = Node->getDebugLoc();
1599 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Evan Chengff89dcb2009-10-18 18:16:27 +00001600 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
Chris Lattnerecf42c42010-09-21 16:36:31 +00001601 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001602
1603 // Emit a store of each element to the stack slot.
1604 SmallVector<SDValue, 8> Stores;
Dan Gohmanaa9d8542010-02-25 15:20:39 +00001605 unsigned TypeByteSize = EltVT.getSizeInBits() / 8;
Eli Friedman7ef3d172009-06-06 07:04:42 +00001606 // Store (in the right endianness) the elements to memory.
1607 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1608 // Ignore undef elements.
1609 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
1610
1611 unsigned Offset = TypeByteSize*i;
1612
1613 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
1614 Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx);
1615
Dan Gohman9949dd62010-02-25 20:30:49 +00001616 // If the destination vector element type is narrower than the source
1617 // element type, only store the bits necessary.
1618 if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) {
Dale Johannesen5b8bce12009-11-21 00:53:23 +00001619 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001620 Node->getOperand(i), Idx,
1621 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001622 EltVT, false, false, 0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00001623 } else
Jim Grosbach6e992612010-07-02 17:41:59 +00001624 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001625 Node->getOperand(i), Idx,
1626 PtrInfo.getWithOffset(Offset),
David Greene1e559442010-02-15 17:00:31 +00001627 false, false, 0));
Eli Friedman7ef3d172009-06-06 07:04:42 +00001628 }
1629
1630 SDValue StoreChain;
1631 if (!Stores.empty()) // Not all undef elements?
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Eli Friedman7ef3d172009-06-06 07:04:42 +00001633 &Stores[0], Stores.size());
1634 else
1635 StoreChain = DAG.getEntryNode();
1636
1637 // Result is a load from the stack slot.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001638 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0);
Eli Friedman7ef3d172009-06-06 07:04:42 +00001639}
1640
Eli Friedman4bc8c712009-05-27 12:20:41 +00001641SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) {
1642 DebugLoc dl = Node->getDebugLoc();
1643 SDValue Tmp1 = Node->getOperand(0);
1644 SDValue Tmp2 = Node->getOperand(1);
Duncan Sands5d54b412010-03-12 11:45:06 +00001645
1646 // Get the sign bit of the RHS. First obtain a value that has the same
1647 // sign as the sign bit, i.e. negative if and only if the sign bit is 1.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001648 SDValue SignBit;
Duncan Sands5d54b412010-03-12 11:45:06 +00001649 EVT FloatVT = Tmp2.getValueType();
1650 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits());
Eli Friedman4bc8c712009-05-27 12:20:41 +00001651 if (isTypeLegal(IVT)) {
Duncan Sands5d54b412010-03-12 11:45:06 +00001652 // Convert to an integer with the same sign bit.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001653 SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001654 } else {
Duncan Sands5d54b412010-03-12 11:45:06 +00001655 // Store the float to memory, then load the sign part out as an integer.
1656 MVT LoadTy = TLI.getPointerTy();
1657 // First create a temporary that is aligned for both the load and store.
1658 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1659 // Then store the float to it.
Eli Friedman4bc8c712009-05-27 12:20:41 +00001660 SDValue Ch =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001661 DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00001662 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001663 if (TLI.isBigEndian()) {
1664 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1665 // Load out a legal integer with the same sign bit as the float.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001666 SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(),
1667 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001668 } else { // Little endian
1669 SDValue LoadPtr = StackPtr;
1670 // The float may be wider than the integer we are going to load. Advance
1671 // the pointer so that the loaded integer will contain the sign bit.
1672 unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits();
1673 unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8;
1674 LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(),
1675 LoadPtr, DAG.getIntPtrConstant(ByteOffset));
1676 // Load a legal integer containing the sign bit.
Chris Lattnerecf42c42010-09-21 16:36:31 +00001677 SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(),
1678 false, false, 0);
Duncan Sands5d54b412010-03-12 11:45:06 +00001679 // Move the sign bit to the top bit of the loaded integer.
1680 unsigned BitShift = LoadTy.getSizeInBits() -
1681 (FloatVT.getSizeInBits() - 8 * ByteOffset);
1682 assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?");
1683 if (BitShift)
1684 SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit,
1685 DAG.getConstant(BitShift,TLI.getShiftAmountTy()));
1686 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00001687 }
Duncan Sands5d54b412010-03-12 11:45:06 +00001688 // Now get the sign bit proper, by seeing whether the value is negative.
1689 SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()),
1690 SignBit, DAG.getConstant(0, SignBit.getValueType()),
1691 ISD::SETLT);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001692 // Get the absolute value of the result.
1693 SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1);
1694 // Select between the nabs and abs value based on the sign bit of
1695 // the input.
1696 return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit,
1697 DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal),
1698 AbsVal);
1699}
1700
Eli Friedman4bc8c712009-05-27 12:20:41 +00001701void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1702 SmallVectorImpl<SDValue> &Results) {
1703 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1704 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1705 " not tell us which reg is the stack pointer!");
1706 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001707 EVT VT = Node->getValueType(0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00001708 SDValue Tmp1 = SDValue(Node, 0);
1709 SDValue Tmp2 = SDValue(Node, 1);
1710 SDValue Tmp3 = Node->getOperand(2);
1711 SDValue Chain = Tmp1.getOperand(0);
1712
1713 // Chain the dynamic stack allocation so that it doesn't modify the stack
1714 // pointer when other instructions are using the stack.
1715 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
1716
1717 SDValue Size = Tmp2.getOperand(1);
1718 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1719 Chain = SP.getValue(1);
1720 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001721 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Eli Friedman4bc8c712009-05-27 12:20:41 +00001722 if (Align > StackAlign)
1723 SP = DAG.getNode(ISD::AND, dl, VT, SP,
1724 DAG.getConstant(-(uint64_t)Align, VT));
1725 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
1726 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1727
1728 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1729 DAG.getIntPtrConstant(0, true), SDValue());
1730
1731 Results.push_back(Tmp1);
1732 Results.push_back(Tmp2);
1733}
1734
Evan Cheng7f042682008-10-15 02:05:31 +00001735/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
Dan Gohmanf77fc922009-10-17 01:37:38 +00001736/// condition code CC on the current target. This routine expands SETCC with
Evan Cheng7f042682008-10-15 02:05:31 +00001737/// illegal condition code into AND / OR of multiple SETCC values.
Owen Andersone50ed302009-08-10 22:56:29 +00001738void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT,
Evan Cheng7f042682008-10-15 02:05:31 +00001739 SDValue &LHS, SDValue &RHS,
Dale Johannesenbb5da912009-02-02 20:41:04 +00001740 SDValue &CC,
Bill Wendling775db972009-12-23 00:28:23 +00001741 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001742 EVT OpVT = LHS.getValueType();
Evan Cheng7f042682008-10-15 02:05:31 +00001743 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
1744 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
Chris Lattner35a38932010-04-07 23:47:51 +00001745 default: assert(0 && "Unknown condition code action!");
Evan Cheng7f042682008-10-15 02:05:31 +00001746 case TargetLowering::Legal:
1747 // Nothing to do.
1748 break;
1749 case TargetLowering::Expand: {
1750 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1751 unsigned Opc = 0;
1752 switch (CCCode) {
Chris Lattner35a38932010-04-07 23:47:51 +00001753 default: assert(0 && "Don't know how to expand this condition!");
Dan Gohmane7d238e2008-10-21 03:12:54 +00001754 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
1755 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1756 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1757 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
1758 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1759 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
1760 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1761 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1762 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1763 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1764 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
1765 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00001766 // FIXME: Implement more expansions.
1767 }
1768
Dale Johannesenbb5da912009-02-02 20:41:04 +00001769 SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1);
1770 SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2);
1771 LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2);
Evan Cheng7f042682008-10-15 02:05:31 +00001772 RHS = SDValue();
1773 CC = SDValue();
1774 break;
1775 }
1776 }
1777}
1778
Chris Lattner1401d152008-01-16 07:45:30 +00001779/// EmitStackConvert - Emit a store/load combination to the stack. This stores
1780/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1781/// a load from the stack slot to DestVT, extending it if needed.
1782/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00001783SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
Owen Andersone50ed302009-08-10 22:56:29 +00001784 EVT SlotVT,
1785 EVT DestVT,
Dale Johannesen8a782a22009-02-02 22:12:50 +00001786 DebugLoc dl) {
Chris Lattner35481892005-12-23 00:16:34 +00001787 // Create the stack frame object.
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001788 unsigned SrcAlign =
1789 TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType().
Owen Anderson23b9b192009-08-12 00:36:31 +00001790 getTypeForEVT(*DAG.getContext()));
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001792
Evan Chengff89dcb2009-10-18 18:16:27 +00001793 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1794 int SPFI = StackPtrFI->getIndex();
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001795 MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI);
Evan Chengff89dcb2009-10-18 18:16:27 +00001796
Duncan Sands83ec4b62008-06-06 12:08:01 +00001797 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
1798 unsigned SlotSize = SlotVT.getSizeInBits();
1799 unsigned DestSize = DestVT.getSizeInBits();
Evan Chengadf97992010-04-15 01:25:27 +00001800 const Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1801 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType);
Scott Michelfdc40a02009-02-17 22:15:04 +00001802
Chris Lattner1401d152008-01-16 07:45:30 +00001803 // Emit a store to the stack slot. Use a truncstore if the input value is
1804 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00001805 SDValue Store;
Evan Chengff89dcb2009-10-18 18:16:27 +00001806
Chris Lattner1401d152008-01-16 07:45:30 +00001807 if (SrcSize > SlotSize)
Dale Johannesen8a782a22009-02-02 22:12:50 +00001808 Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001809 PtrInfo, SlotVT, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001810 else {
1811 assert(SrcSize == SlotSize && "Invalid store");
Dale Johannesen8a782a22009-02-02 22:12:50 +00001812 Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001813 PtrInfo, false, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00001814 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001815
Chris Lattner35481892005-12-23 00:16:34 +00001816 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00001817 if (SlotSize == DestSize)
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001818 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo,
Chris Lattnerecf42c42010-09-21 16:36:31 +00001819 false, false, DestAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001820
Chris Lattner1401d152008-01-16 07:45:30 +00001821 assert(SlotSize < DestSize && "Unknown extension!");
Chris Lattnerecf42c42010-09-21 16:36:31 +00001822 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl, Store, FIPtr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001823 PtrInfo, SlotVT, false, false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00001824}
1825
Dan Gohman475871a2008-07-27 21:46:04 +00001826SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Dale Johannesen8a782a22009-02-02 22:12:50 +00001827 DebugLoc dl = Node->getDebugLoc();
Chris Lattner4352cc92006-04-04 17:23:26 +00001828 // Create a vector sized/aligned stack slot, store the value to element #0,
1829 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00001830 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00001831
Evan Chengff89dcb2009-10-18 18:16:27 +00001832 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1833 int SPFI = StackPtrFI->getIndex();
1834
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00001835 SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0),
1836 StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001837 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001838 Node->getValueType(0).getVectorElementType(),
1839 false, false, 0);
Dale Johannesen8a782a22009-02-02 22:12:50 +00001840 return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr,
Chris Lattner85ca1062010-09-21 07:32:19 +00001841 MachinePointerInfo::getFixedStack(SPFI),
David Greene1e559442010-02-15 17:00:31 +00001842 false, false, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001843}
1844
1845
Chris Lattnerce872152006-03-19 06:31:19 +00001846/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00001847/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00001848SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001849 unsigned NumElems = Node->getNumOperands();
Eli Friedman7a5e5552009-06-07 06:52:44 +00001850 SDValue Value1, Value2;
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001851 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001852 EVT VT = Node->getValueType(0);
1853 EVT OpVT = Node->getOperand(0).getValueType();
1854 EVT EltVT = VT.getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
1856 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00001857 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattnerce872152006-03-19 06:31:19 +00001858 bool isOnlyLowElement = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001859 bool MoreThanTwoValues = false;
Chris Lattner2eb86532006-03-24 07:29:17 +00001860 bool isConstant = true;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001861 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001862 SDValue V = Node->getOperand(i);
Eli Friedman7a5e5552009-06-07 06:52:44 +00001863 if (V.getOpcode() == ISD::UNDEF)
1864 continue;
1865 if (i > 0)
Chris Lattnerce872152006-03-19 06:31:19 +00001866 isOnlyLowElement = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001867 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
Chris Lattner2eb86532006-03-24 07:29:17 +00001868 isConstant = false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00001869
1870 if (!Value1.getNode()) {
1871 Value1 = V;
1872 } else if (!Value2.getNode()) {
1873 if (V != Value1)
1874 Value2 = V;
1875 } else if (V != Value1 && V != Value2) {
1876 MoreThanTwoValues = true;
1877 }
Chris Lattnerce872152006-03-19 06:31:19 +00001878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001879
Eli Friedman7a5e5552009-06-07 06:52:44 +00001880 if (!Value1.getNode())
1881 return DAG.getUNDEF(VT);
1882
1883 if (isOnlyLowElement)
Bob Wilson26cbf9e2009-04-13 20:20:30 +00001884 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001885
Chris Lattner2eb86532006-03-24 07:29:17 +00001886 // If all elements are constants, create a load from the constant pool.
1887 if (isConstant) {
Chris Lattner2eb86532006-03-24 07:29:17 +00001888 std::vector<Constant*> CV;
1889 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001890 if (ConstantFPSDNode *V =
Chris Lattner2eb86532006-03-24 07:29:17 +00001891 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00001892 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001893 } else if (ConstantSDNode *V =
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001894 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001895 if (OpVT==EltVT)
1896 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1897 else {
1898 // If OpVT and EltVT don't match, EltVT is not legal and the
1899 // element values have been promoted/truncated earlier. Undo this;
1900 // we don't want a v16i8 to become a v16i32 for example.
1901 const ConstantInt *CI = V->getConstantIntValue();
1902 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1903 CI->getZExtValue()));
1904 }
Chris Lattner2eb86532006-03-24 07:29:17 +00001905 } else {
1906 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Dale Johannesen9a645cd2009-11-10 23:16:41 +00001907 const Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001908 CV.push_back(UndefValue::get(OpNTy));
Chris Lattner2eb86532006-03-24 07:29:17 +00001909 }
1910 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00001911 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng1606e8e2009-03-13 07:51:59 +00001913 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesen8a782a22009-02-02 22:12:50 +00001914 return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00001915 MachinePointerInfo::getConstantPool(),
David Greene1e559442010-02-15 17:00:31 +00001916 false, false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00001917 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001918
Eli Friedman7a5e5552009-06-07 06:52:44 +00001919 if (!MoreThanTwoValues) {
1920 SmallVector<int, 8> ShuffleVec(NumElems, -1);
1921 for (unsigned i = 0; i < NumElems; ++i) {
1922 SDValue V = Node->getOperand(i);
1923 if (V.getOpcode() == ISD::UNDEF)
1924 continue;
1925 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
1926 }
1927 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
Chris Lattner87100e02006-03-20 01:52:29 +00001928 // Get the splatted value into the low element of a vector register.
Eli Friedman7a5e5552009-06-07 06:52:44 +00001929 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
1930 SDValue Vec2;
1931 if (Value2.getNode())
1932 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
1933 else
1934 Vec2 = DAG.getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001935
Chris Lattner87100e02006-03-20 01:52:29 +00001936 // Return shuffle(LowValVec, undef, <0,0,0,0>)
Eli Friedman7a5e5552009-06-07 06:52:44 +00001937 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data());
Evan Cheng033e6812006-03-24 01:17:21 +00001938 }
1939 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001940
Eli Friedman7ef3d172009-06-06 07:04:42 +00001941 // Otherwise, we can't handle this case efficiently.
1942 return ExpandVectorBuildThroughStack(Node);
Chris Lattnerce872152006-03-19 06:31:19 +00001943}
1944
Chris Lattner77e77a62005-01-21 06:05:23 +00001945// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1946// does not fit into a register, return the lo part and set the hi part to the
1947// by-reg argument. If it does fit into a single register, return the result
1948// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00001949SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
Eli Friedman47b41f72009-05-27 02:21:29 +00001950 bool isSigned) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001951 // The input chain to this libcall is the entry node of the function.
Chris Lattner6831a812006-02-13 09:18:02 +00001952 // Legalizing the call will automatically add the previous call to the
1953 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00001954 SDValue InChain = DAG.getEntryNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00001955
Chris Lattner77e77a62005-01-21 06:05:23 +00001956 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00001957 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00001958 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001959 EVT ArgVT = Node->getOperand(i).getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +00001960 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00001961 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001962 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00001963 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00001964 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00001965 }
Bill Wendling056292f2008-09-16 21:48:12 +00001966 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00001967 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001968
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001969 // Splice the libcall in wherever FindInputOutputChains tells us to.
Owen Anderson23b9b192009-08-12 00:36:31 +00001970 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
Evan Cheng3d2125c2010-11-30 23:55:39 +00001971
1972 // isTailCall may be true since the callee does not reference caller stack
1973 // frame. Check if it's in the right position.
1974 bool isTailCall = isInTailCallPosition(DAG, Node, TLI);
Bob Wilsonec15bbf2009-04-10 18:48:47 +00001975 std::pair<SDValue, SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00001976 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977 0, TLI.getLibcallCallingConv(LC), isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001979 Callee, Args, DAG, Node->getDebugLoc());
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00001980
Evan Cheng3d2125c2010-11-30 23:55:39 +00001981 if (!CallInfo.second.getNode())
1982 // It's a tailcall, return the chain (which is the DAG root).
1983 return DAG.getRoot();
1984
Chris Lattner6831a812006-02-13 09:18:02 +00001985 // Legalize the call sequence, starting with the chain. This will advance
1986 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
1987 // was added by LowerCallTo (guaranteeing proper serialization of calls).
1988 LegalizeOp(CallInfo.second);
Eli Friedman74807f22009-05-26 08:55:52 +00001989 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001990}
1991
Jim Grosbache03262f2010-06-18 21:43:38 +00001992// ExpandChainLibCall - Expand a node into a call to a libcall. Similar to
1993// ExpandLibCall except that the first operand is the in-chain.
1994std::pair<SDValue, SDValue>
1995SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC,
1996 SDNode *Node,
1997 bool isSigned) {
Jim Grosbache03262f2010-06-18 21:43:38 +00001998 SDValue InChain = Node->getOperand(0);
1999
2000 TargetLowering::ArgListTy Args;
2001 TargetLowering::ArgListEntry Entry;
2002 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) {
2003 EVT ArgVT = Node->getOperand(i).getValueType();
2004 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2005 Entry.Node = Node->getOperand(i);
2006 Entry.Ty = ArgTy;
2007 Entry.isSExt = isSigned;
2008 Entry.isZExt = !isSigned;
2009 Args.push_back(Entry);
2010 }
2011 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2012 TLI.getPointerTy());
2013
2014 // Splice the libcall in wherever FindInputOutputChains tells us to.
2015 const Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext());
2016 std::pair<SDValue, SDValue> CallInfo =
2017 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
Evan Cheng3d2125c2010-11-30 23:55:39 +00002018 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false,
Jim Grosbache03262f2010-06-18 21:43:38 +00002019 /*isReturnValueUsed=*/true,
2020 Callee, Args, DAG, Node->getDebugLoc());
2021
2022 // Legalize the call sequence, starting with the chain. This will advance
2023 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2024 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2025 LegalizeOp(CallInfo.second);
2026 return CallInfo;
2027}
2028
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002029SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2030 RTLIB::Libcall Call_F32,
2031 RTLIB::Libcall Call_F64,
2032 RTLIB::Libcall Call_F80,
2033 RTLIB::Libcall Call_PPCF128) {
2034 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002036 default: assert(0 && "Unexpected request for libcall!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 case MVT::f32: LC = Call_F32; break;
2038 case MVT::f64: LC = Call_F64; break;
2039 case MVT::f80: LC = Call_F80; break;
2040 case MVT::ppcf128: LC = Call_PPCF128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002041 }
2042 return ExpandLibCall(LC, Node, false);
2043}
2044
2045SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002046 RTLIB::Libcall Call_I8,
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002047 RTLIB::Libcall Call_I16,
2048 RTLIB::Libcall Call_I32,
2049 RTLIB::Libcall Call_I64,
2050 RTLIB::Libcall Call_I128) {
2051 RTLIB::Libcall LC;
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 switch (Node->getValueType(0).getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002053 default: assert(0 && "Unexpected request for libcall!");
Anton Korobeynikov8983da72009-11-07 17:14:39 +00002054 case MVT::i8: LC = Call_I8; break;
2055 case MVT::i16: LC = Call_I16; break;
2056 case MVT::i32: LC = Call_I32; break;
2057 case MVT::i64: LC = Call_I64; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002058 case MVT::i128: LC = Call_I128; break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002059 }
2060 return ExpandLibCall(LC, Node, isSigned);
2061}
2062
Chris Lattner22cde6a2006-01-28 08:25:58 +00002063/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
2064/// INT_TO_FP operation of the specified operand when the target requests that
2065/// we expand it. At this point, we know that the result and operand types are
2066/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00002067SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
2068 SDValue Op0,
Owen Andersone50ed302009-08-10 22:56:29 +00002069 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002070 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002071 if (Op0.getValueType() == MVT::i32) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002072 // simple 32-bit [signed|unsigned] integer to float/double expansion
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
Chris Lattner23594d42008-01-16 07:03:22 +00002074 // Get the stack frame index of a 8 byte buffer.
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Chris Lattner22cde6a2006-01-28 08:25:58 +00002077 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00002078 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00002079 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue Hi = StackSlot;
Scott Michelfdc40a02009-02-17 22:15:04 +00002081 SDValue Lo = DAG.getNode(ISD::ADD, dl,
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002082 TLI.getPointerTy(), StackSlot, WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00002083 if (TLI.isLittleEndian())
2084 std::swap(Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00002085
Chris Lattner22cde6a2006-01-28 08:25:58 +00002086 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00002087 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002088 if (isSigned) {
2089 // constant used to invert sign bit (signed to unsigned mapping)
Owen Anderson825b72b2009-08-11 20:47:22 +00002090 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
2091 Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002092 } else {
2093 Op0Mapped = Op0;
2094 }
2095 // store the lo of the constructed double - based on integer input
Dale Johannesenaf435272009-02-02 19:03:57 +00002096 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002097 Op0Mapped, Lo, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002098 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002099 // initial hi portion of constructed double
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002101 // store the hi of the constructed double - biased exponent
Chris Lattner6229d0a2010-09-21 18:41:36 +00002102 SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi,
2103 MachinePointerInfo(),
2104 false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002105 // load the constructed double
Chris Lattnerecf42c42010-09-21 16:36:31 +00002106 SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot,
2107 MachinePointerInfo(), false, false, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002108 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00002109 SDValue Bias = DAG.getConstantFP(isSigned ?
Bob Wilsonec15bbf2009-04-10 18:48:47 +00002110 BitsToDouble(0x4330000080000000ULL) :
2111 BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00002112 MVT::f64);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002113 // subtract the bias
Owen Anderson825b72b2009-08-11 20:47:22 +00002114 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002115 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00002116 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002117 // handle final rounding
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 if (DestVT == MVT::f64) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002119 // do nothing
2120 Result = Sub;
Owen Anderson825b72b2009-08-11 20:47:22 +00002121 } else if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002122 Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Chris Lattner0bd48932008-01-17 07:00:52 +00002123 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenaf435272009-02-02 19:03:57 +00002125 Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002126 }
2127 return Result;
2128 }
2129 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002130 // Code below here assumes !isSigned without checking again.
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002131
2132 // Implementation of unsigned i64 to f64 following the algorithm in
2133 // __floatundidf in compiler_rt. This implementation has the advantage
2134 // of performing rounding correctly, both in the default rounding mode
2135 // and in all alternate rounding modes.
2136 // TODO: Generalize this for use with other types.
2137 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) {
2138 SDValue TwoP52 =
2139 DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64);
2140 SDValue TwoP84PlusTwoP52 =
2141 DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64);
2142 SDValue TwoP84 =
2143 DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64);
2144
2145 SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32);
2146 SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0,
2147 DAG.getConstant(32, MVT::i64));
2148 SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52);
2149 SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002150 SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr);
2151 SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr);
Jim Grosbach6e992612010-07-02 17:41:59 +00002152 SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt,
2153 TwoP84PlusTwoP52);
Dan Gohman0fa9d1d2010-03-06 00:00:55 +00002154 return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub);
2155 }
2156
Owen Anderson3a9e7692010-10-05 17:24:05 +00002157 // Implementation of unsigned i64 to f32.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002158 // TODO: Generalize this for use with other types.
2159 if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) {
Owen Anderson3a9e7692010-10-05 17:24:05 +00002160 // For unsigned conversions, convert them to signed conversions using the
2161 // algorithm from the x86_64 __floatundidf in compiler_rt.
2162 if (!isSigned) {
2163 SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002164
Owen Anderson3a9e7692010-10-05 17:24:05 +00002165 SDValue ShiftConst = DAG.getConstant(1, TLI.getShiftAmountTy());
2166 SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst);
2167 SDValue AndConst = DAG.getConstant(1, MVT::i64);
2168 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst);
2169 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002170
Owen Anderson3a9e7692010-10-05 17:24:05 +00002171 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or);
2172 SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002173
Owen Anderson3a9e7692010-10-05 17:24:05 +00002174 // TODO: This really should be implemented using a branch rather than a
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002175 // select. We happen to get lucky and machinesink does the right
2176 // thing most of the time. This would be a good candidate for a
Owen Anderson3a9e7692010-10-05 17:24:05 +00002177 //pseudo-op, or, even better, for whole-function isel.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002178 SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002179 Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT);
2180 return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast);
2181 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002182
Owen Anderson3a9e7692010-10-05 17:24:05 +00002183 // Otherwise, implement the fully general conversion.
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002184 EVT SHVT = TLI.getShiftAmountTy();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002185
Jim Grosbach6e992612010-07-02 17:41:59 +00002186 SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002187 DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64));
2188 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And,
2189 DAG.getConstant(UINT64_C(0x800), MVT::i64));
Jim Grosbach6e992612010-07-02 17:41:59 +00002190 SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0,
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002191 DAG.getConstant(UINT64_C(0x7ff), MVT::i64));
2192 SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2193 And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE);
2194 SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0);
2195 SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64),
2196 Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64),
Owen Anderson3a9e7692010-10-05 17:24:05 +00002197 ISD::SETUGE);
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002198 SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002199
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002200 SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2,
2201 DAG.getConstant(32, SHVT));
2202 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh);
2203 SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc);
2204 SDValue TwoP32 =
2205 DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64);
2206 SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt);
2207 SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2);
2208 SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo);
2209 SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2);
2210 return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd,
2211 DAG.getIntPtrConstant(0));
Dale Johannesena5afa1c2010-05-13 23:50:42 +00002212 }
2213
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002214 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002215
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002216 SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()),
2217 Op0, DAG.getConstant(0, Op0.getValueType()),
2218 ISD::SETLT);
2219 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
2220 SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(),
2221 SignSet, Four, Zero);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002222
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002223 // If the sign bit of the integer is set, the large number will be treated
2224 // as a negative number. To counteract this, the dynamic code adds an
2225 // offset depending on the data type.
2226 uint64_t FF;
2227 switch (Op0.getValueType().getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002228 default: assert(0 && "Unsupported integer type!");
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002229 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2230 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2231 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2232 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2233 }
2234 if (TLI.isLittleEndian()) FF <<= 32;
2235 Constant *FudgeFactor = ConstantInt::get(
2236 Type::getInt64Ty(*DAG.getContext()), FF);
2237
2238 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
2239 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
2240 CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset);
2241 Alignment = std::min(Alignment, 4u);
2242 SDValue FudgeInReg;
2243 if (DestVT == MVT::f32)
2244 FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002245 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002246 false, false, Alignment);
2247 else {
2248 FudgeInReg =
Evan Chengbcc80172010-07-07 22:15:37 +00002249 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, dl,
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002250 DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00002251 MachinePointerInfo::getConstantPool(),
Dan Gohmanb6b343d2010-03-05 02:40:23 +00002252 MVT::f32, false, false, Alignment));
2253 }
2254
2255 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002256}
2257
2258/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
2259/// *INT_TO_FP operation of the specified operand when the target requests that
2260/// we promote it. At this point, we know that the result and operand types are
2261/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2262/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00002263SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
Owen Andersone50ed302009-08-10 22:56:29 +00002264 EVT DestVT,
Dale Johannesenaf435272009-02-02 19:03:57 +00002265 bool isSigned,
2266 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002267 // First step, figure out the appropriate *INT_TO_FP operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002268 EVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002269
2270 unsigned OpToUse = 0;
2271
2272 // Scan for the appropriate larger type to use.
2273 while (1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002275 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002276
2277 // If the target supports SINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002278 if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) {
2279 OpToUse = ISD::SINT_TO_FP;
2280 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002281 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002282 if (isSigned) continue;
2283
2284 // If the target supports UINT_TO_FP of this type, use it.
Eli Friedman3be2e512009-05-28 03:06:16 +00002285 if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) {
2286 OpToUse = ISD::UINT_TO_FP;
2287 break;
Chris Lattner22cde6a2006-01-28 08:25:58 +00002288 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002289
2290 // Otherwise, try a larger type.
2291 }
2292
2293 // Okay, we found the operation and type to use. Zero extend our input to the
2294 // desired type then run the operation on it.
Dale Johannesenaf435272009-02-02 19:03:57 +00002295 return DAG.getNode(OpToUse, dl, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00002296 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Dale Johannesenaf435272009-02-02 19:03:57 +00002297 dl, NewInTy, LegalOp));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002298}
2299
2300/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
2301/// FP_TO_*INT 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 FP_TO_UINT or FP_TO_SINT
2304/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00002305SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(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 FP_TO*INT operation to use.
Owen Andersone50ed302009-08-10 22:56:29 +00002310 EVT NewOutTy = DestVT;
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 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002317 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002318
Eli Friedman3be2e512009-05-28 03:06:16 +00002319 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002320 OpToUse = ISD::FP_TO_SINT;
2321 break;
2322 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002323
Eli Friedman3be2e512009-05-28 03:06:16 +00002324 if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002325 OpToUse = ISD::FP_TO_UINT;
2326 break;
2327 }
Chris Lattner22cde6a2006-01-28 08:25:58 +00002328
2329 // Otherwise, try a larger type.
2330 }
2331
Scott Michelfdc40a02009-02-17 22:15:04 +00002332
Chris Lattner27a6c732007-11-24 07:07:01 +00002333 // Okay, we found the operation and type to use.
Dale Johannesenaf435272009-02-02 19:03:57 +00002334 SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00002335
Chris Lattner27a6c732007-11-24 07:07:01 +00002336 // Truncate the result of the extended FP_TO_*INT operation to the desired
2337 // size.
Dale Johannesenaf435272009-02-02 19:03:57 +00002338 return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002339}
2340
2341/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
2342///
Dale Johannesen8a782a22009-02-02 22:12:50 +00002343SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002344 EVT VT = Op.getValueType();
2345 EVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner35a38932010-04-07 23:47:51 +00002348 default: assert(0 && "Unhandled Expand type in BSWAP!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 case MVT::i16:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002350 Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2351 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2352 return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 case MVT::i32:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002354 Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2355 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2356 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2357 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2358 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2359 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2360 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2361 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2362 return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 case MVT::i64:
Dale Johannesen8a782a22009-02-02 22:12:50 +00002364 Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT));
2365 Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT));
2366 Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT));
2367 Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT));
2368 Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT));
2369 Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT));
2370 Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT));
2371 Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT));
2372 Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
2373 Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
2374 Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
2375 Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
2376 Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
2377 Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
2378 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7);
2379 Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5);
2380 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3);
2381 Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1);
2382 Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6);
2383 Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2);
2384 return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002385 }
2386}
2387
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002388/// SplatByte - Distribute ByteVal over NumBits bits.
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002389// FIXME: Move this helper to a common place.
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002390static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2391 APInt Val = APInt(NumBits, ByteVal);
2392 unsigned Shift = 8;
2393 for (unsigned i = NumBits; i > 8; i >>= 1) {
2394 Val = (Val << Shift) | Val;
2395 Shift <<= 1;
2396 }
2397 return Val;
2398}
2399
Chris Lattner22cde6a2006-01-28 08:25:58 +00002400/// ExpandBitCount - Expand the specified bitcount instruction into operations.
2401///
Scott Michelfdc40a02009-02-17 22:15:04 +00002402SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
Dale Johannesen8a782a22009-02-02 22:12:50 +00002403 DebugLoc dl) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00002404 switch (Opc) {
Chris Lattner35a38932010-04-07 23:47:51 +00002405 default: assert(0 && "Cannot expand this yet!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00002406 case ISD::CTPOP: {
Owen Andersone50ed302009-08-10 22:56:29 +00002407 EVT VT = Op.getValueType();
2408 EVT ShVT = TLI.getShiftAmountTy();
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002409 unsigned Len = VT.getSizeInBits();
2410
Benjamin Kramer5df5a222011-01-15 21:19:37 +00002411 assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 &&
2412 "CTPOP not implemented for this type.");
2413
Benjamin Kramerb6516ae2011-01-15 20:30:30 +00002414 // This is the "best" algorithm from
2415 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
2416
2417 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2418 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2419 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2420 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2421
2422 // v = v - ((v >> 1) & 0x55555555...)
2423 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
2424 DAG.getNode(ISD::AND, dl, VT,
2425 DAG.getNode(ISD::SRL, dl, VT, Op,
2426 DAG.getConstant(1, ShVT)),
2427 Mask55));
2428 // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
2429 Op = DAG.getNode(ISD::ADD, dl, VT,
2430 DAG.getNode(ISD::AND, dl, VT, Op, Mask33),
2431 DAG.getNode(ISD::AND, dl, VT,
2432 DAG.getNode(ISD::SRL, dl, VT, Op,
2433 DAG.getConstant(2, ShVT)),
2434 Mask33));
2435 // v = (v + (v >> 4)) & 0x0F0F0F0F...
2436 Op = DAG.getNode(ISD::AND, dl, VT,
2437 DAG.getNode(ISD::ADD, dl, VT, Op,
2438 DAG.getNode(ISD::SRL, dl, VT, Op,
2439 DAG.getConstant(4, ShVT))),
2440 Mask0F);
2441 // v = (v * 0x01010101...) >> (Len - 8)
2442 Op = DAG.getNode(ISD::SRL, dl, VT,
2443 DAG.getNode(ISD::MUL, dl, VT, Op, Mask01),
2444 DAG.getConstant(Len - 8, ShVT));
2445
Chris Lattner22cde6a2006-01-28 08:25:58 +00002446 return Op;
2447 }
2448 case ISD::CTLZ: {
2449 // for now, we do this:
2450 // x = x | (x >> 1);
2451 // x = x | (x >> 2);
2452 // ...
2453 // x = x | (x >>16);
2454 // x = x | (x >>32); // for 64-bit input
2455 // return popcount(~x);
2456 //
2457 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002458 EVT VT = Op.getValueType();
2459 EVT ShVT = TLI.getShiftAmountTy();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002460 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00002461 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002462 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002463 Op = DAG.getNode(ISD::OR, dl, VT, Op,
Dale Johannesene72c5962009-02-06 21:55:48 +00002464 DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002465 }
Dale Johannesen8a782a22009-02-02 22:12:50 +00002466 Op = DAG.getNOT(dl, Op, VT);
2467 return DAG.getNode(ISD::CTPOP, dl, VT, Op);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002468 }
2469 case ISD::CTTZ: {
2470 // for now, we use: { return popcount(~x & (x - 1)); }
2471 // unless the target has ctlz but not ctpop, in which case we use:
2472 // { return 32 - nlz(~x & (x-1)); }
2473 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Owen Andersone50ed302009-08-10 22:56:29 +00002474 EVT VT = Op.getValueType();
Dale Johannesen8a782a22009-02-02 22:12:50 +00002475 SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT,
2476 DAG.getNOT(dl, Op, VT),
2477 DAG.getNode(ISD::SUB, dl, VT, Op,
Bill Wendling7581bfa2009-01-30 23:03:19 +00002478 DAG.getConstant(1, VT)));
Chris Lattner22cde6a2006-01-28 08:25:58 +00002479 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00002480 if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) &&
2481 TLI.isOperationLegalOrCustom(ISD::CTLZ, VT))
Dale Johannesen8a782a22009-02-02 22:12:50 +00002482 return DAG.getNode(ISD::SUB, dl, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002483 DAG.getConstant(VT.getSizeInBits(), VT),
Dale Johannesen8a782a22009-02-02 22:12:50 +00002484 DAG.getNode(ISD::CTLZ, dl, VT, Tmp3));
2485 return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3);
Chris Lattner22cde6a2006-01-28 08:25:58 +00002486 }
2487 }
2488}
Chris Lattnere34b3962005-01-19 04:19:40 +00002489
Jim Grosbache03262f2010-06-18 21:43:38 +00002490std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) {
2491 unsigned Opc = Node->getOpcode();
2492 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2493 RTLIB::Libcall LC;
2494
2495 switch (Opc) {
2496 default:
2497 llvm_unreachable("Unhandled atomic intrinsic Expand!");
2498 break;
Jim Grosbachef6eb9c2010-06-18 23:03:10 +00002499 case ISD::ATOMIC_SWAP:
2500 switch (VT.SimpleTy) {
2501 default: llvm_unreachable("Unexpected value type for atomic!");
2502 case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break;
2503 case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break;
2504 case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break;
2505 case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break;
2506 }
2507 break;
Jim Grosbache03262f2010-06-18 21:43:38 +00002508 case ISD::ATOMIC_CMP_SWAP:
2509 switch (VT.SimpleTy) {
2510 default: llvm_unreachable("Unexpected value type for atomic!");
2511 case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break;
2512 case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break;
2513 case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break;
2514 case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break;
2515 }
2516 break;
2517 case ISD::ATOMIC_LOAD_ADD:
2518 switch (VT.SimpleTy) {
2519 default: llvm_unreachable("Unexpected value type for atomic!");
2520 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break;
2521 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break;
2522 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break;
2523 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break;
2524 }
2525 break;
2526 case ISD::ATOMIC_LOAD_SUB:
2527 switch (VT.SimpleTy) {
2528 default: llvm_unreachable("Unexpected value type for atomic!");
2529 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break;
2530 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break;
2531 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break;
2532 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break;
2533 }
2534 break;
2535 case ISD::ATOMIC_LOAD_AND:
2536 switch (VT.SimpleTy) {
2537 default: llvm_unreachable("Unexpected value type for atomic!");
2538 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break;
2539 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break;
2540 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break;
2541 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break;
2542 }
2543 break;
2544 case ISD::ATOMIC_LOAD_OR:
2545 switch (VT.SimpleTy) {
2546 default: llvm_unreachable("Unexpected value type for atomic!");
2547 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break;
2548 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break;
2549 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break;
2550 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break;
2551 }
2552 break;
2553 case ISD::ATOMIC_LOAD_XOR:
2554 switch (VT.SimpleTy) {
2555 default: llvm_unreachable("Unexpected value type for atomic!");
2556 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break;
2557 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break;
2558 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break;
2559 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break;
2560 }
2561 break;
2562 case ISD::ATOMIC_LOAD_NAND:
2563 switch (VT.SimpleTy) {
2564 default: llvm_unreachable("Unexpected value type for atomic!");
2565 case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break;
2566 case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break;
2567 case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break;
2568 case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break;
2569 }
2570 break;
2571 }
2572
2573 return ExpandChainLibCall(LC, Node, false);
2574}
2575
Eli Friedman8c377c72009-05-27 01:25:56 +00002576void SelectionDAGLegalize::ExpandNode(SDNode *Node,
2577 SmallVectorImpl<SDValue> &Results) {
2578 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanbbdd9032009-05-28 20:40:34 +00002579 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
Eli Friedman8c377c72009-05-27 01:25:56 +00002580 switch (Node->getOpcode()) {
2581 case ISD::CTPOP:
2582 case ISD::CTLZ:
2583 case ISD::CTTZ:
2584 Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl);
2585 Results.push_back(Tmp1);
2586 break;
2587 case ISD::BSWAP:
Bill Wendling775db972009-12-23 00:28:23 +00002588 Results.push_back(ExpandBSWAP(Node->getOperand(0), dl));
Eli Friedman8c377c72009-05-27 01:25:56 +00002589 break;
2590 case ISD::FRAMEADDR:
2591 case ISD::RETURNADDR:
2592 case ISD::FRAME_TO_ARGS_OFFSET:
2593 Results.push_back(DAG.getConstant(0, Node->getValueType(0)));
2594 break;
2595 case ISD::FLT_ROUNDS_:
2596 Results.push_back(DAG.getConstant(1, Node->getValueType(0)));
2597 break;
2598 case ISD::EH_RETURN:
Eli Friedman8c377c72009-05-27 01:25:56 +00002599 case ISD::EH_LABEL:
2600 case ISD::PREFETCH:
Eli Friedman8c377c72009-05-27 01:25:56 +00002601 case ISD::VAEND:
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002602 case ISD::EH_SJLJ_LONGJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002603 case ISD::EH_SJLJ_DISPATCHSETUP:
2604 // If the target didn't expand these, there's nothing to do, so just
2605 // preserve the chain and be done.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002606 Results.push_back(Node->getOperand(0));
2607 break;
2608 case ISD::EH_SJLJ_SETJMP:
Jim Grosbache4ad3872010-10-19 23:27:08 +00002609 // If the target didn't expand this, just return 'zero' and preserve the
2610 // chain.
Jim Grosbachc66e150b2010-07-06 23:44:52 +00002611 Results.push_back(DAG.getConstant(0, MVT::i32));
Eli Friedman8c377c72009-05-27 01:25:56 +00002612 Results.push_back(Node->getOperand(0));
2613 break;
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002614 case ISD::MEMBARRIER: {
2615 // If the target didn't lower this, lower it to '__sync_synchronize()' call
2616 TargetLowering::ArgListTy Args;
2617 std::pair<SDValue, SDValue> CallResult =
2618 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002619 false, false, false, false, 0, CallingConv::C,
2620 /*isTailCall=*/false,
Jim Grosbachbbfc0d22010-06-17 02:00:53 +00002621 /*isReturnValueUsed=*/true,
2622 DAG.getExternalSymbol("__sync_synchronize",
2623 TLI.getPointerTy()),
2624 Args, DAG, dl);
2625 Results.push_back(CallResult.second);
2626 break;
2627 }
Jim Grosbachb56ce812010-06-17 17:50:54 +00002628 // By default, atomic intrinsics are marked Legal and lowered. Targets
2629 // which don't support them directly, however, may want libcalls, in which
2630 // case they mark them Expand, and we get here.
Jim Grosbachb56ce812010-06-17 17:50:54 +00002631 case ISD::ATOMIC_SWAP:
2632 case ISD::ATOMIC_LOAD_ADD:
2633 case ISD::ATOMIC_LOAD_SUB:
2634 case ISD::ATOMIC_LOAD_AND:
2635 case ISD::ATOMIC_LOAD_OR:
2636 case ISD::ATOMIC_LOAD_XOR:
2637 case ISD::ATOMIC_LOAD_NAND:
2638 case ISD::ATOMIC_LOAD_MIN:
2639 case ISD::ATOMIC_LOAD_MAX:
2640 case ISD::ATOMIC_LOAD_UMIN:
2641 case ISD::ATOMIC_LOAD_UMAX:
Evan Chenga8457062010-06-18 22:01:37 +00002642 case ISD::ATOMIC_CMP_SWAP: {
Jim Grosbache03262f2010-06-18 21:43:38 +00002643 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node);
2644 Results.push_back(Tmp.first);
2645 Results.push_back(Tmp.second);
Jim Grosbach59c38f32010-06-17 17:58:54 +00002646 break;
Evan Chenga8457062010-06-18 22:01:37 +00002647 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00002648 case ISD::DYNAMIC_STACKALLOC:
2649 ExpandDYNAMIC_STACKALLOC(Node, Results);
2650 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00002651 case ISD::MERGE_VALUES:
2652 for (unsigned i = 0; i < Node->getNumValues(); i++)
2653 Results.push_back(Node->getOperand(i));
2654 break;
2655 case ISD::UNDEF: {
Owen Andersone50ed302009-08-10 22:56:29 +00002656 EVT VT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00002657 if (VT.isInteger())
2658 Results.push_back(DAG.getConstant(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002659 else {
2660 assert(VT.isFloatingPoint() && "Unknown value type!");
Eli Friedman8c377c72009-05-27 01:25:56 +00002661 Results.push_back(DAG.getConstantFP(0, VT));
Chris Lattner35a38932010-04-07 23:47:51 +00002662 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002663 break;
2664 }
2665 case ISD::TRAP: {
2666 // If this operation is not supported, lower it to 'abort()' call
2667 TargetLowering::ArgListTy Args;
2668 std::pair<SDValue, SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00002669 TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()),
Evan Cheng3d2125c2010-11-30 23:55:39 +00002670 false, false, false, false, 0, CallingConv::C,
2671 /*isTailCall=*/false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002672 /*isReturnValueUsed=*/true,
Eli Friedman8c377c72009-05-27 01:25:56 +00002673 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00002674 Args, DAG, dl);
Eli Friedman8c377c72009-05-27 01:25:56 +00002675 Results.push_back(CallResult.second);
2676 break;
2677 }
2678 case ISD::FP_ROUND:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002679 case ISD::BITCAST:
Eli Friedman8c377c72009-05-27 01:25:56 +00002680 Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
2681 Node->getValueType(0), dl);
2682 Results.push_back(Tmp1);
2683 break;
2684 case ISD::FP_EXTEND:
2685 Tmp1 = EmitStackConvert(Node->getOperand(0),
2686 Node->getOperand(0).getValueType(),
2687 Node->getValueType(0), dl);
2688 Results.push_back(Tmp1);
2689 break;
2690 case ISD::SIGN_EXTEND_INREG: {
2691 // NOTE: we could fall back on load/store here too for targets without
2692 // SAR. However, it is doubtful that any exist.
Owen Andersone50ed302009-08-10 22:56:29 +00002693 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00002694 EVT VT = Node->getValueType(0);
2695 EVT ShiftAmountTy = TLI.getShiftAmountTy();
Dan Gohmand1996362010-01-09 02:13:55 +00002696 if (VT.isVector())
Dan Gohman87862e72009-12-11 21:31:27 +00002697 ShiftAmountTy = VT;
Dan Gohmand1996362010-01-09 02:13:55 +00002698 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
2699 ExtraVT.getScalarType().getSizeInBits();
Dan Gohman87862e72009-12-11 21:31:27 +00002700 SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy);
Eli Friedman8c377c72009-05-27 01:25:56 +00002701 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
2702 Node->getOperand(0), ShiftCst);
Bill Wendling775db972009-12-23 00:28:23 +00002703 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
2704 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002705 break;
2706 }
2707 case ISD::FP_ROUND_INREG: {
2708 // The only way we can lower this is to turn it into a TRUNCSTORE,
2709 // EXTLOAD pair, targetting a temporary location (a stack slot).
2710
2711 // NOTE: there is a choice here between constantly creating new stack
2712 // slots and always reusing the same one. We currently always create
2713 // new ones, as reuse may inhibit scheduling.
Owen Andersone50ed302009-08-10 22:56:29 +00002714 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Eli Friedman8c377c72009-05-27 01:25:56 +00002715 Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT,
2716 Node->getValueType(0), dl);
2717 Results.push_back(Tmp1);
2718 break;
2719 }
2720 case ISD::SINT_TO_FP:
2721 case ISD::UINT_TO_FP:
2722 Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP,
2723 Node->getOperand(0), Node->getValueType(0), dl);
2724 Results.push_back(Tmp1);
2725 break;
2726 case ISD::FP_TO_UINT: {
2727 SDValue True, False;
Owen Andersone50ed302009-08-10 22:56:29 +00002728 EVT VT = Node->getOperand(0).getValueType();
2729 EVT NVT = Node->getValueType(0);
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002730 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Eli Friedman8c377c72009-05-27 01:25:56 +00002731 APInt x = APInt::getSignBit(NVT.getSizeInBits());
2732 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
2733 Tmp1 = DAG.getConstantFP(apf, VT);
2734 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT),
2735 Node->getOperand(0),
2736 Tmp1, ISD::SETLT);
2737 True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00002738 False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT,
2739 DAG.getNode(ISD::FSUB, dl, VT,
2740 Node->getOperand(0), Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00002741 False = DAG.getNode(ISD::XOR, dl, NVT, False,
2742 DAG.getConstant(x, NVT));
2743 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False);
2744 Results.push_back(Tmp1);
2745 break;
2746 }
Eli Friedman509150f2009-05-27 07:58:35 +00002747 case ISD::VAARG: {
2748 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Owen Andersone50ed302009-08-10 22:56:29 +00002749 EVT VT = Node->getValueType(0);
Eli Friedman509150f2009-05-27 07:58:35 +00002750 Tmp1 = Node->getOperand(0);
2751 Tmp2 = Node->getOperand(1);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002752 unsigned Align = Node->getConstantOperandVal(3);
2753
Chris Lattnerecf42c42010-09-21 16:36:31 +00002754 SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2,
2755 MachinePointerInfo(V), false, false, 0);
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002756 SDValue VAList = VAListLoad;
2757
Rafael Espindolacbeeae22010-07-11 04:01:49 +00002758 if (Align > TLI.getMinStackArgumentAlignment()) {
2759 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2760
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002761 VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2762 DAG.getConstant(Align - 1,
2763 TLI.getPointerTy()));
2764
2765 VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList,
Chris Lattner07e3a382010-10-10 18:36:26 +00002766 DAG.getConstant(-(int64_t)Align,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00002767 TLI.getPointerTy()));
2768 }
2769
Eli Friedman509150f2009-05-27 07:58:35 +00002770 // Increment the pointer, VAList, to the next vaarg
2771 Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList,
2772 DAG.getConstant(TLI.getTargetData()->
Evan Chengadf97992010-04-15 01:25:27 +00002773 getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())),
Eli Friedman509150f2009-05-27 07:58:35 +00002774 TLI.getPointerTy()));
2775 // Store the incremented VAList to the legalized pointer
Chris Lattner6229d0a2010-09-21 18:41:36 +00002776 Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2,
2777 MachinePointerInfo(V), false, false, 0);
Eli Friedman509150f2009-05-27 07:58:35 +00002778 // Load the actual argument out of the pointer VAList
Chris Lattnerecf42c42010-09-21 16:36:31 +00002779 Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002780 false, false, 0));
Eli Friedman509150f2009-05-27 07:58:35 +00002781 Results.push_back(Results[0].getValue(1));
2782 break;
2783 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002784 case ISD::VACOPY: {
2785 // This defaults to loading a pointer from the input and storing it to the
2786 // output, returning the chain.
2787 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2788 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2789 Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0),
Chris Lattnerecf42c42010-09-21 16:36:31 +00002790 Node->getOperand(2), MachinePointerInfo(VS),
2791 false, false, 0);
2792 Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2793 MachinePointerInfo(VD), false, false, 0);
Bill Wendling775db972009-12-23 00:28:23 +00002794 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00002795 break;
2796 }
2797 case ISD::EXTRACT_VECTOR_ELT:
2798 if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
2799 // This must be an access of the only element. Return it.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002800 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
Eli Friedman8c377c72009-05-27 01:25:56 +00002801 Node->getOperand(0));
2802 else
2803 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
2804 Results.push_back(Tmp1);
2805 break;
2806 case ISD::EXTRACT_SUBVECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002807 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
Eli Friedman8c377c72009-05-27 01:25:56 +00002808 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002809 case ISD::CONCAT_VECTORS: {
Bill Wendling775db972009-12-23 00:28:23 +00002810 Results.push_back(ExpandVectorBuildThroughStack(Node));
Eli Friedman509150f2009-05-27 07:58:35 +00002811 break;
2812 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002813 case ISD::SCALAR_TO_VECTOR:
Bill Wendling775db972009-12-23 00:28:23 +00002814 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
Eli Friedman8c377c72009-05-27 01:25:56 +00002815 break;
Eli Friedman3f727d62009-05-27 02:16:40 +00002816 case ISD::INSERT_VECTOR_ELT:
Bill Wendling775db972009-12-23 00:28:23 +00002817 Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
2818 Node->getOperand(1),
2819 Node->getOperand(2), dl));
Eli Friedman3f727d62009-05-27 02:16:40 +00002820 break;
Eli Friedman509150f2009-05-27 07:58:35 +00002821 case ISD::VECTOR_SHUFFLE: {
2822 SmallVector<int, 8> Mask;
2823 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
2824
Owen Andersone50ed302009-08-10 22:56:29 +00002825 EVT VT = Node->getValueType(0);
2826 EVT EltVT = VT.getVectorElementType();
Bob Wilson14b21412010-05-19 18:48:32 +00002827 if (getTypeAction(EltVT) == Promote)
2828 EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
Eli Friedman509150f2009-05-27 07:58:35 +00002829 unsigned NumElems = VT.getVectorNumElements();
2830 SmallVector<SDValue, 8> Ops;
2831 for (unsigned i = 0; i != NumElems; ++i) {
2832 if (Mask[i] < 0) {
2833 Ops.push_back(DAG.getUNDEF(EltVT));
2834 continue;
2835 }
2836 unsigned Idx = Mask[i];
2837 if (Idx < NumElems)
Bill Wendling775db972009-12-23 00:28:23 +00002838 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2839 Node->getOperand(0),
2840 DAG.getIntPtrConstant(Idx)));
Eli Friedman509150f2009-05-27 07:58:35 +00002841 else
Bill Wendling775db972009-12-23 00:28:23 +00002842 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
2843 Node->getOperand(1),
2844 DAG.getIntPtrConstant(Idx - NumElems)));
Eli Friedman509150f2009-05-27 07:58:35 +00002845 }
2846 Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size());
2847 Results.push_back(Tmp1);
2848 break;
2849 }
Eli Friedman8c377c72009-05-27 01:25:56 +00002850 case ISD::EXTRACT_ELEMENT: {
Owen Andersone50ed302009-08-10 22:56:29 +00002851 EVT OpTy = Node->getOperand(0).getValueType();
Eli Friedman8c377c72009-05-27 01:25:56 +00002852 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
2853 // 1 -> Hi
2854 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
2855 DAG.getConstant(OpTy.getSizeInBits()/2,
2856 TLI.getShiftAmountTy()));
2857 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
2858 } else {
2859 // 0 -> Lo
2860 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
2861 Node->getOperand(0));
2862 }
2863 Results.push_back(Tmp1);
2864 break;
2865 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002866 case ISD::STACKSAVE:
2867 // Expand to CopyFromReg if the target set
2868 // StackPointerRegisterToSaveRestore.
2869 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Bill Wendling775db972009-12-23 00:28:23 +00002870 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
2871 Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002872 Results.push_back(Results[0].getValue(1));
2873 } else {
Bill Wendling775db972009-12-23 00:28:23 +00002874 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
Eli Friedman3f727d62009-05-27 02:16:40 +00002875 Results.push_back(Node->getOperand(0));
2876 }
2877 break;
2878 case ISD::STACKRESTORE:
Bill Wendling775db972009-12-23 00:28:23 +00002879 // Expand to CopyToReg if the target set
2880 // StackPointerRegisterToSaveRestore.
2881 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2882 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
2883 Node->getOperand(1)));
2884 } else {
2885 Results.push_back(Node->getOperand(0));
2886 }
Eli Friedman3f727d62009-05-27 02:16:40 +00002887 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00002888 case ISD::FCOPYSIGN:
Bill Wendling775db972009-12-23 00:28:23 +00002889 Results.push_back(ExpandFCOPYSIGN(Node));
Eli Friedman4bc8c712009-05-27 12:20:41 +00002890 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002891 case ISD::FNEG:
2892 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2893 Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0));
2894 Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1,
2895 Node->getOperand(0));
2896 Results.push_back(Tmp1);
2897 break;
2898 case ISD::FABS: {
2899 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Owen Andersone50ed302009-08-10 22:56:29 +00002900 EVT VT = Node->getValueType(0);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002901 Tmp1 = Node->getOperand(0);
2902 Tmp2 = DAG.getConstantFP(0.0, VT);
Bill Wendling775db972009-12-23 00:28:23 +00002903 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()),
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002904 Tmp1, Tmp2, ISD::SETUGT);
Bill Wendling775db972009-12-23 00:28:23 +00002905 Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1);
2906 Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3);
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002907 Results.push_back(Tmp1);
2908 break;
2909 }
2910 case ISD::FSQRT:
Bill Wendling775db972009-12-23 00:28:23 +00002911 Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
2912 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002913 break;
2914 case ISD::FSIN:
Bill Wendling775db972009-12-23 00:28:23 +00002915 Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
2916 RTLIB::SIN_F80, RTLIB::SIN_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002917 break;
2918 case ISD::FCOS:
Bill Wendling775db972009-12-23 00:28:23 +00002919 Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
2920 RTLIB::COS_F80, RTLIB::COS_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002921 break;
2922 case ISD::FLOG:
Bill Wendling775db972009-12-23 00:28:23 +00002923 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64,
2924 RTLIB::LOG_F80, RTLIB::LOG_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002925 break;
2926 case ISD::FLOG2:
Bill Wendling775db972009-12-23 00:28:23 +00002927 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
2928 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002929 break;
2930 case ISD::FLOG10:
Bill Wendling775db972009-12-23 00:28:23 +00002931 Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
2932 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002933 break;
2934 case ISD::FEXP:
Bill Wendling775db972009-12-23 00:28:23 +00002935 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64,
2936 RTLIB::EXP_F80, RTLIB::EXP_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002937 break;
2938 case ISD::FEXP2:
Bill Wendling775db972009-12-23 00:28:23 +00002939 Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
2940 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002941 break;
2942 case ISD::FTRUNC:
Bill Wendling775db972009-12-23 00:28:23 +00002943 Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
2944 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002945 break;
2946 case ISD::FFLOOR:
Bill Wendling775db972009-12-23 00:28:23 +00002947 Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
2948 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002949 break;
2950 case ISD::FCEIL:
Bill Wendling775db972009-12-23 00:28:23 +00002951 Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
2952 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002953 break;
2954 case ISD::FRINT:
Bill Wendling775db972009-12-23 00:28:23 +00002955 Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
2956 RTLIB::RINT_F80, RTLIB::RINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002957 break;
2958 case ISD::FNEARBYINT:
Bill Wendling775db972009-12-23 00:28:23 +00002959 Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
2960 RTLIB::NEARBYINT_F64,
2961 RTLIB::NEARBYINT_F80,
2962 RTLIB::NEARBYINT_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002963 break;
2964 case ISD::FPOWI:
Bill Wendling775db972009-12-23 00:28:23 +00002965 Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64,
2966 RTLIB::POWI_F80, RTLIB::POWI_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002967 break;
2968 case ISD::FPOW:
Bill Wendling775db972009-12-23 00:28:23 +00002969 Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64,
2970 RTLIB::POW_F80, RTLIB::POW_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002971 break;
2972 case ISD::FDIV:
Bill Wendling775db972009-12-23 00:28:23 +00002973 Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
2974 RTLIB::DIV_F80, RTLIB::DIV_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002975 break;
2976 case ISD::FREM:
Bill Wendling775db972009-12-23 00:28:23 +00002977 Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
2978 RTLIB::REM_F80, RTLIB::REM_PPCF128));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00002979 break;
Anton Korobeynikov927411b2010-03-14 18:42:24 +00002980 case ISD::FP16_TO_FP32:
2981 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false));
2982 break;
2983 case ISD::FP32_TO_FP16:
2984 Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false));
2985 break;
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002986 case ISD::ConstantFP: {
2987 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Bill Wendling775db972009-12-23 00:28:23 +00002988 // Check to see if this FP immediate is already legal.
2989 // If this is a legal constant, turn it into a TargetConstantFP node.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002990 if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0)))
Bill Wendling775db972009-12-23 00:28:23 +00002991 Results.push_back(SDValue(Node, 0));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002992 else
Bill Wendling775db972009-12-23 00:28:23 +00002993 Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00002994 break;
2995 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00002996 case ISD::EHSELECTION: {
2997 unsigned Reg = TLI.getExceptionSelectorRegister();
2998 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00002999 Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg,
3000 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003001 Results.push_back(Results[0].getValue(1));
3002 break;
3003 }
3004 case ISD::EXCEPTIONADDR: {
3005 unsigned Reg = TLI.getExceptionAddressRegister();
3006 assert(Reg && "Can't expand to unknown register!");
Bill Wendling775db972009-12-23 00:28:23 +00003007 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg,
3008 Node->getValueType(0)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003009 Results.push_back(Results[0].getValue(1));
3010 break;
3011 }
3012 case ISD::SUB: {
Owen Andersone50ed302009-08-10 22:56:29 +00003013 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003014 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3015 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3016 "Don't know how to expand this subtraction!");
3017 Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1),
3018 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT));
Bill Wendling775db972009-12-23 00:28:23 +00003019 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT));
3020 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003021 break;
3022 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003023 case ISD::UREM:
3024 case ISD::SREM: {
Owen Andersone50ed302009-08-10 22:56:29 +00003025 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003026 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003027 bool isSigned = Node->getOpcode() == ISD::SREM;
3028 unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV;
3029 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3030 Tmp2 = Node->getOperand(0);
3031 Tmp3 = Node->getOperand(1);
Eli Friedman3be2e512009-05-28 03:06:16 +00003032 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3033 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1);
3034 } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) {
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003035 // X % Y -> X-X/Y*Y
3036 Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3);
3037 Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3);
3038 Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003039 } else if (isSigned) {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003040 Tmp1 = ExpandIntLibCall(Node, true,
3041 RTLIB::SREM_I8,
3042 RTLIB::SREM_I16, RTLIB::SREM_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003043 RTLIB::SREM_I64, RTLIB::SREM_I128);
3044 } else {
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003045 Tmp1 = ExpandIntLibCall(Node, false,
3046 RTLIB::UREM_I8,
3047 RTLIB::UREM_I16, RTLIB::UREM_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003048 RTLIB::UREM_I64, RTLIB::UREM_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003049 }
Eli Friedman26ea8f92009-05-27 07:05:37 +00003050 Results.push_back(Tmp1);
3051 break;
3052 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003053 case ISD::UDIV:
3054 case ISD::SDIV: {
3055 bool isSigned = Node->getOpcode() == ISD::SDIV;
3056 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
Owen Andersone50ed302009-08-10 22:56:29 +00003057 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003058 SDVTList VTs = DAG.getVTList(VT, VT);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003059 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT))
3060 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3061 Node->getOperand(1));
3062 else if (isSigned)
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003063 Tmp1 = ExpandIntLibCall(Node, true,
3064 RTLIB::SDIV_I8,
3065 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003066 RTLIB::SDIV_I64, RTLIB::SDIV_I128);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003067 else
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003068 Tmp1 = ExpandIntLibCall(Node, false,
3069 RTLIB::UDIV_I8,
3070 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003071 RTLIB::UDIV_I64, RTLIB::UDIV_I128);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003072 Results.push_back(Tmp1);
3073 break;
3074 }
3075 case ISD::MULHU:
3076 case ISD::MULHS: {
3077 unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI :
3078 ISD::SMUL_LOHI;
Owen Andersone50ed302009-08-10 22:56:29 +00003079 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003080 SDVTList VTs = DAG.getVTList(VT, VT);
3081 assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) &&
3082 "If this wasn't legal, it shouldn't have been created!");
3083 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3084 Node->getOperand(1));
3085 Results.push_back(Tmp1.getValue(1));
3086 break;
3087 }
3088 case ISD::MUL: {
Owen Andersone50ed302009-08-10 22:56:29 +00003089 EVT VT = Node->getValueType(0);
Eli Friedman26ea8f92009-05-27 07:05:37 +00003090 SDVTList VTs = DAG.getVTList(VT, VT);
3091 // See if multiply or divide can be lowered using two-result operations.
3092 // We just need the low half of the multiply; try both the signed
3093 // and unsigned forms. If the target supports both SMUL_LOHI and
3094 // UMUL_LOHI, form a preference by checking which forms of plain
3095 // MULH it supports.
3096 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3097 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3098 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3099 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3100 unsigned OpToUse = 0;
3101 if (HasSMUL_LOHI && !HasMULHS) {
3102 OpToUse = ISD::SMUL_LOHI;
3103 } else if (HasUMUL_LOHI && !HasMULHU) {
3104 OpToUse = ISD::UMUL_LOHI;
3105 } else if (HasSMUL_LOHI) {
3106 OpToUse = ISD::SMUL_LOHI;
3107 } else if (HasUMUL_LOHI) {
3108 OpToUse = ISD::UMUL_LOHI;
3109 }
3110 if (OpToUse) {
Bill Wendling775db972009-12-23 00:28:23 +00003111 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3112 Node->getOperand(1)));
Eli Friedman26ea8f92009-05-27 07:05:37 +00003113 break;
3114 }
Anton Korobeynikov8983da72009-11-07 17:14:39 +00003115 Tmp1 = ExpandIntLibCall(Node, false,
3116 RTLIB::MUL_I8,
3117 RTLIB::MUL_I16, RTLIB::MUL_I32,
Eli Friedman26ea8f92009-05-27 07:05:37 +00003118 RTLIB::MUL_I64, RTLIB::MUL_I128);
3119 Results.push_back(Tmp1);
3120 break;
3121 }
Eli Friedman4bc8c712009-05-27 12:20:41 +00003122 case ISD::SADDO:
3123 case ISD::SSUBO: {
3124 SDValue LHS = Node->getOperand(0);
3125 SDValue RHS = Node->getOperand(1);
3126 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3127 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3128 LHS, RHS);
3129 Results.push_back(Sum);
Bill Wendling122d06d2009-12-23 00:05:09 +00003130 EVT OType = Node->getValueType(1);
Bill Wendling775db972009-12-23 00:28:23 +00003131
Eli Friedman4bc8c712009-05-27 12:20:41 +00003132 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
3133
3134 // LHSSign -> LHS >= 0
3135 // RHSSign -> RHS >= 0
3136 // SumSign -> Sum >= 0
3137 //
3138 // Add:
3139 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3140 // Sub:
3141 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3142 //
3143 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3144 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3145 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3146 Node->getOpcode() == ISD::SADDO ?
3147 ISD::SETEQ : ISD::SETNE);
3148
3149 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3150 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3151
3152 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3153 Results.push_back(Cmp);
3154 break;
3155 }
3156 case ISD::UADDO:
3157 case ISD::USUBO: {
3158 SDValue LHS = Node->getOperand(0);
3159 SDValue RHS = Node->getOperand(1);
3160 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
3161 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3162 LHS, RHS);
3163 Results.push_back(Sum);
Bill Wendling775db972009-12-23 00:28:23 +00003164 Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS,
3165 Node->getOpcode () == ISD::UADDO ?
3166 ISD::SETULT : ISD::SETUGT));
Eli Friedman4bc8c712009-05-27 12:20:41 +00003167 break;
3168 }
Eli Friedmandb3c1692009-06-16 06:58:29 +00003169 case ISD::UMULO:
3170 case ISD::SMULO: {
Owen Andersone50ed302009-08-10 22:56:29 +00003171 EVT VT = Node->getValueType(0);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003172 SDValue LHS = Node->getOperand(0);
3173 SDValue RHS = Node->getOperand(1);
3174 SDValue BottomHalf;
3175 SDValue TopHalf;
Nuno Lopesec9d8b02009-12-23 17:48:10 +00003176 static const unsigned Ops[2][3] =
Eli Friedmandb3c1692009-06-16 06:58:29 +00003177 { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
3178 { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};
3179 bool isSigned = Node->getOpcode() == ISD::SMULO;
3180 if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) {
3181 BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3182 TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS);
3183 } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) {
3184 BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS,
3185 RHS);
3186 TopHalf = BottomHalf.getValue(1);
Eric Christopher38a18262011-01-20 00:29:24 +00003187 } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(),
3188 VT.getSizeInBits() * 2))) {
Owen Anderson23b9b192009-08-12 00:36:31 +00003189 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003190 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3191 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3192 Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
3193 BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3194 DAG.getIntPtrConstant(0));
3195 TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1,
3196 DAG.getIntPtrConstant(1));
Eric Christopher38a18262011-01-20 00:29:24 +00003197 } else {
3198 // We can fall back to a libcall with an illegal type for the MUL if we
3199 // have a libcall big enough.
3200 // Also, we can fall back to a division in some cases, but that's a big
3201 // performance hit in the general case.
3202 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2);
3203 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3204 if (WideVT == MVT::i16)
3205 LC = RTLIB::MUL_I16;
3206 else if (WideVT == MVT::i32)
3207 LC = RTLIB::MUL_I32;
3208 else if (WideVT == MVT::i64)
3209 LC = RTLIB::MUL_I64;
3210 else if (WideVT == MVT::i128)
3211 LC = RTLIB::MUL_I128;
3212 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!");
3213 LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS);
3214 RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS);
3215
Eric Christophere3385812011-01-20 01:29:23 +00003216 SDValue Ret = ExpandLibCall(LC, Node, isSigned);
Eric Christopher38a18262011-01-20 00:29:24 +00003217 BottomHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, Ret);
3218 TopHalf = DAG.getNode(ISD::SRL, dl, Ret.getValueType(), Ret,
3219 DAG.getConstant(VT.getSizeInBits(), TLI.getPointerTy()));
3220 TopHalf = DAG.getNode(ISD::TRUNCATE, dl, VT, TopHalf);
Eli Friedmandb3c1692009-06-16 06:58:29 +00003221 }
3222 if (isSigned) {
3223 Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, TLI.getShiftAmountTy());
3224 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1);
3225 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1,
3226 ISD::SETNE);
3227 } else {
3228 TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf,
3229 DAG.getConstant(0, VT), ISD::SETNE);
3230 }
3231 Results.push_back(BottomHalf);
3232 Results.push_back(TopHalf);
3233 break;
3234 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003235 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +00003236 EVT PairTy = Node->getValueType(0);
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003237 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3238 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
Bill Wendling775db972009-12-23 00:28:23 +00003239 Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003240 DAG.getConstant(PairTy.getSizeInBits()/2,
3241 TLI.getShiftAmountTy()));
Bill Wendling775db972009-12-23 00:28:23 +00003242 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003243 break;
3244 }
Eli Friedman509150f2009-05-27 07:58:35 +00003245 case ISD::SELECT:
3246 Tmp1 = Node->getOperand(0);
3247 Tmp2 = Node->getOperand(1);
3248 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003249 if (Tmp1.getOpcode() == ISD::SETCC) {
Eli Friedman509150f2009-05-27 07:58:35 +00003250 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3251 Tmp2, Tmp3,
3252 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
Bill Wendling775db972009-12-23 00:28:23 +00003253 } else {
Eli Friedman509150f2009-05-27 07:58:35 +00003254 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3255 DAG.getConstant(0, Tmp1.getValueType()),
3256 Tmp2, Tmp3, ISD::SETNE);
Bill Wendling775db972009-12-23 00:28:23 +00003257 }
Eli Friedman509150f2009-05-27 07:58:35 +00003258 Results.push_back(Tmp1);
3259 break;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003260 case ISD::BR_JT: {
3261 SDValue Chain = Node->getOperand(0);
3262 SDValue Table = Node->getOperand(1);
3263 SDValue Index = Node->getOperand(2);
3264
Owen Andersone50ed302009-08-10 22:56:29 +00003265 EVT PTy = TLI.getPointerTy();
Chris Lattner071c62f2010-01-25 23:26:13 +00003266
3267 const TargetData &TD = *TLI.getTargetData();
3268 unsigned EntrySize =
3269 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Jim Grosbach6e992612010-07-02 17:41:59 +00003270
Chris Lattner071c62f2010-01-25 23:26:13 +00003271 Index = DAG.getNode(ISD::MUL, dl, PTy,
Eli Friedman4bc8c712009-05-27 12:20:41 +00003272 Index, DAG.getConstant(EntrySize, PTy));
3273 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3274
Owen Anderson23b9b192009-08-12 00:36:31 +00003275 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Evan Chengbcc80172010-07-07 22:15:37 +00003276 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, dl, Chain, Addr,
Chris Lattner85ca1062010-09-21 07:32:19 +00003277 MachinePointerInfo::getJumpTable(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00003278 false, false, 0);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003279 Addr = LD;
Dan Gohman55e59c12010-04-19 19:05:59 +00003280 if (TM.getRelocationModel() == Reloc::PIC_) {
Eli Friedman4bc8c712009-05-27 12:20:41 +00003281 // For PIC, the sequence is:
Bill Wendling775db972009-12-23 00:28:23 +00003282 // BRIND(load(Jumptable + index) + RelocBase)
Eli Friedman4bc8c712009-05-27 12:20:41 +00003283 // RelocBase can be JumpTable, GOT or some sort of global base.
3284 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3285 TLI.getPICJumpTableRelocBase(Table, DAG));
3286 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr);
Eli Friedman4bc8c712009-05-27 12:20:41 +00003288 Results.push_back(Tmp1);
3289 break;
3290 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003291 case ISD::BRCOND:
3292 // Expand brcond's setcc into its constituent parts and create a BR_CC
3293 // Node.
3294 Tmp1 = Node->getOperand(0);
3295 Tmp2 = Node->getOperand(1);
Bill Wendling775db972009-12-23 00:28:23 +00003296 if (Tmp2.getOpcode() == ISD::SETCC) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003298 Tmp1, Tmp2.getOperand(2),
3299 Tmp2.getOperand(0), Tmp2.getOperand(1),
3300 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003301 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003303 DAG.getCondCode(ISD::SETNE), Tmp2,
3304 DAG.getConstant(0, Tmp2.getValueType()),
3305 Node->getOperand(2));
Bill Wendling775db972009-12-23 00:28:23 +00003306 }
Eli Friedmanf6f20a72009-05-27 07:32:27 +00003307 Results.push_back(Tmp1);
3308 break;
Eli Friedmanad754602009-05-28 03:56:57 +00003309 case ISD::SETCC: {
3310 Tmp1 = Node->getOperand(0);
3311 Tmp2 = Node->getOperand(1);
3312 Tmp3 = Node->getOperand(2);
Bill Wendling775db972009-12-23 00:28:23 +00003313 LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl);
Eli Friedmanad754602009-05-28 03:56:57 +00003314
3315 // If we expanded the SETCC into an AND/OR, return the new node
3316 if (Tmp2.getNode() == 0) {
3317 Results.push_back(Tmp1);
3318 break;
3319 }
3320
3321 // Otherwise, SETCC for the given comparison type must be completely
3322 // illegal; expand it into a SELECT_CC.
Owen Andersone50ed302009-08-10 22:56:29 +00003323 EVT VT = Node->getValueType(0);
Eli Friedmanad754602009-05-28 03:56:57 +00003324 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
3325 DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3);
3326 Results.push_back(Tmp1);
3327 break;
3328 }
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003329 case ISD::SELECT_CC: {
3330 Tmp1 = Node->getOperand(0); // LHS
3331 Tmp2 = Node->getOperand(1); // RHS
3332 Tmp3 = Node->getOperand(2); // True
3333 Tmp4 = Node->getOperand(3); // False
3334 SDValue CC = Node->getOperand(4);
3335
3336 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003337 Tmp1, Tmp2, CC, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003338
3339 assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!");
3340 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3341 CC = DAG.getCondCode(ISD::SETNE);
3342 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2,
3343 Tmp3, Tmp4, CC);
3344 Results.push_back(Tmp1);
3345 break;
3346 }
3347 case ISD::BR_CC: {
3348 Tmp1 = Node->getOperand(0); // Chain
3349 Tmp2 = Node->getOperand(2); // LHS
3350 Tmp3 = Node->getOperand(3); // RHS
3351 Tmp4 = Node->getOperand(1); // CC
3352
3353 LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()),
Bill Wendling775db972009-12-23 00:28:23 +00003354 Tmp2, Tmp3, Tmp4, dl);
Eli Friedmanbbdd9032009-05-28 20:40:34 +00003355 LastCALLSEQ_END = DAG.getEntryNode();
3356
3357 assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!");
3358 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
3359 Tmp4 = DAG.getCondCode(ISD::SETNE);
3360 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2,
3361 Tmp3, Node->getOperand(4));
3362 Results.push_back(Tmp1);
3363 break;
3364 }
Eli Friedman3f727d62009-05-27 02:16:40 +00003365 case ISD::GLOBAL_OFFSET_TABLE:
3366 case ISD::GlobalAddress:
3367 case ISD::GlobalTLSAddress:
3368 case ISD::ExternalSymbol:
3369 case ISD::ConstantPool:
3370 case ISD::JumpTable:
3371 case ISD::INTRINSIC_W_CHAIN:
3372 case ISD::INTRINSIC_WO_CHAIN:
3373 case ISD::INTRINSIC_VOID:
3374 // FIXME: Custom lowering for these operations shouldn't return null!
3375 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
3376 Results.push_back(SDValue(Node, i));
3377 break;
Eli Friedman8c377c72009-05-27 01:25:56 +00003378 }
3379}
3380void SelectionDAGLegalize::PromoteNode(SDNode *Node,
3381 SmallVectorImpl<SDValue> &Results) {
Owen Andersone50ed302009-08-10 22:56:29 +00003382 EVT OVT = Node->getValueType(0);
Eli Friedman8c377c72009-05-27 01:25:56 +00003383 if (Node->getOpcode() == ISD::UINT_TO_FP ||
Eli Friedmana64eb922009-07-17 05:16:04 +00003384 Node->getOpcode() == ISD::SINT_TO_FP ||
Bill Wendling775db972009-12-23 00:28:23 +00003385 Node->getOpcode() == ISD::SETCC) {
Eli Friedman8c377c72009-05-27 01:25:56 +00003386 OVT = Node->getOperand(0).getValueType();
Bill Wendling775db972009-12-23 00:28:23 +00003387 }
Owen Andersone50ed302009-08-10 22:56:29 +00003388 EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Eli Friedman8c377c72009-05-27 01:25:56 +00003389 DebugLoc dl = Node->getDebugLoc();
Eli Friedman509150f2009-05-27 07:58:35 +00003390 SDValue Tmp1, Tmp2, Tmp3;
Eli Friedman8c377c72009-05-27 01:25:56 +00003391 switch (Node->getOpcode()) {
3392 case ISD::CTTZ:
3393 case ISD::CTLZ:
3394 case ISD::CTPOP:
3395 // Zero extend the argument.
3396 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
3397 // Perform the larger operation.
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003398 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003399 if (Node->getOpcode() == ISD::CTTZ) {
3400 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jakob Stoklund Olesen9a4ba452009-07-12 17:43:20 +00003401 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT),
Eli Friedman8c377c72009-05-27 01:25:56 +00003402 Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT),
3403 ISD::SETEQ);
3404 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2,
3405 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
3406 } else if (Node->getOpcode() == ISD::CTLZ) {
3407 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
3408 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
3409 DAG.getConstant(NVT.getSizeInBits() -
3410 OVT.getSizeInBits(), NVT));
3411 }
Bill Wendling775db972009-12-23 00:28:23 +00003412 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
Eli Friedman8c377c72009-05-27 01:25:56 +00003413 break;
3414 case ISD::BSWAP: {
3415 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Bill Wendling167bea72009-12-22 22:53:39 +00003416 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
Bill Wendling775db972009-12-23 00:28:23 +00003417 Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1);
3418 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
3419 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3420 Results.push_back(Tmp1);
Eli Friedman8c377c72009-05-27 01:25:56 +00003421 break;
3422 }
3423 case ISD::FP_TO_UINT:
3424 case ISD::FP_TO_SINT:
3425 Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0),
3426 Node->getOpcode() == ISD::FP_TO_SINT, dl);
3427 Results.push_back(Tmp1);
3428 break;
3429 case ISD::UINT_TO_FP:
3430 case ISD::SINT_TO_FP:
3431 Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0),
3432 Node->getOpcode() == ISD::SINT_TO_FP, dl);
3433 Results.push_back(Tmp1);
3434 break;
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003435 case ISD::AND:
3436 case ISD::OR:
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003437 case ISD::XOR: {
3438 unsigned ExtOp, TruncOp;
3439 if (OVT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003440 ExtOp = ISD::BITCAST;
3441 TruncOp = ISD::BITCAST;
Chris Lattner35a38932010-04-07 23:47:51 +00003442 } else {
3443 assert(OVT.isInteger() && "Cannot promote logic operation");
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003444 ExtOp = ISD::ANY_EXTEND;
3445 TruncOp = ISD::TRUNCATE;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003446 }
3447 // Promote each of the values to the new type.
3448 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3449 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3450 // Perform the larger operation, then convert back
Bill Wendling775db972009-12-23 00:28:23 +00003451 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
3452 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
Eli Friedmanf6b23bf2009-05-27 03:33:44 +00003453 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003454 }
3455 case ISD::SELECT: {
Eli Friedman509150f2009-05-27 07:58:35 +00003456 unsigned ExtOp, TruncOp;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003457 if (Node->getValueType(0).isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003458 ExtOp = ISD::BITCAST;
3459 TruncOp = ISD::BITCAST;
Eli Friedman4bc8c712009-05-27 12:20:41 +00003460 } else if (Node->getValueType(0).isInteger()) {
Eli Friedman509150f2009-05-27 07:58:35 +00003461 ExtOp = ISD::ANY_EXTEND;
3462 TruncOp = ISD::TRUNCATE;
3463 } else {
3464 ExtOp = ISD::FP_EXTEND;
3465 TruncOp = ISD::FP_ROUND;
3466 }
3467 Tmp1 = Node->getOperand(0);
3468 // Promote each of the values to the new type.
3469 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
3470 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
3471 // Perform the larger operation, then round down.
3472 Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3);
3473 if (TruncOp != ISD::FP_ROUND)
Bill Wendling775db972009-12-23 00:28:23 +00003474 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003475 else
Bill Wendling775db972009-12-23 00:28:23 +00003476 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
Eli Friedman509150f2009-05-27 07:58:35 +00003477 DAG.getIntPtrConstant(0));
Bill Wendling775db972009-12-23 00:28:23 +00003478 Results.push_back(Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003479 break;
Jakob Stoklund Olesenc8ca3ae2009-07-12 18:10:18 +00003480 }
Eli Friedman509150f2009-05-27 07:58:35 +00003481 case ISD::VECTOR_SHUFFLE: {
3482 SmallVector<int, 8> Mask;
3483 cast<ShuffleVectorSDNode>(Node)->getMask(Mask);
3484
3485 // Cast the two input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003486 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
3487 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
Eli Friedman509150f2009-05-27 07:58:35 +00003488
3489 // Convert the shuffle mask to the right # elements.
Bill Wendling775db972009-12-23 00:28:23 +00003490 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003491 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
Eli Friedman509150f2009-05-27 07:58:35 +00003492 Results.push_back(Tmp1);
3493 break;
3494 }
Eli Friedmanad754602009-05-28 03:56:57 +00003495 case ISD::SETCC: {
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003496 unsigned ExtOp = ISD::FP_EXTEND;
3497 if (NVT.isInteger()) {
3498 ISD::CondCode CCCode =
3499 cast<CondCodeSDNode>(Node->getOperand(2))->get();
3500 ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Eli Friedmanad754602009-05-28 03:56:57 +00003501 }
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00003502 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
3503 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
Eli Friedmanad754602009-05-28 03:56:57 +00003504 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0),
3505 Tmp1, Tmp2, Node->getOperand(2)));
3506 break;
3507 }
Eli Friedman8c377c72009-05-27 01:25:56 +00003508 }
3509}
3510
Chris Lattner3e928bb2005-01-07 07:47:09 +00003511// SelectionDAG::Legalize - This is the entry point for the file.
3512//
Dan Gohman714efc62009-12-05 17:51:33 +00003513void SelectionDAG::Legalize(CodeGenOpt::Level OptLevel) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003514 /// run - This is the main entry point to this class.
3515 ///
Eli Friedman1fde9c52009-05-24 02:46:31 +00003516 SelectionDAGLegalize(*this, OptLevel).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003517}
3518