blob: 73109004d10796f808bd14977a4794208ab857c1 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman6a648612005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner03c85462005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattner8afc48e2005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000080 LegalizedNodes.insert(std::make_pair(From, To));
81 // If someone requests legalization of the new node, return itself.
82 if (From != To)
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000084 }
Chris Lattner03c85462005-01-15 05:21:40 +000085 void AddPromotedOperand(SDOperand From, SDOperand To) {
86 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
87 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +000088 // If someone requests legalization of the new node, return itself.
89 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +000090 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000091
Chris Lattner3e928bb2005-01-07 07:47:09 +000092public:
93
Chris Lattner9c32d3b2005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000095
96 /// Run - While there is still lowering to do, perform a pass over the DAG.
97 /// Most regularization can be done in a single pass, but targets that require
98 /// large values to be split into registers multiple times (e.g. i64 -> 4x
99 /// i16) require iteration for these values (the first iteration will demote
100 /// to i32, the second will demote to i16).
101 void Run() {
102 do {
103 NeedsAnotherIteration = false;
104 LegalizeDAG();
105 } while (NeedsAnotherIteration);
106 }
107
108 /// getTypeAction - Return how we should legalize values of this type, either
109 /// it is already legal or we need to expand it into multiple registers of
110 /// smaller integer type, or we need to promote it to a larger type.
111 LegalizeAction getTypeAction(MVT::ValueType VT) const {
112 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
113 }
114
115 /// isTypeLegal - Return true if this type is legal on this target.
116 ///
117 bool isTypeLegal(MVT::ValueType VT) const {
118 return getTypeAction(VT) == Legal;
119 }
120
121private:
122 void LegalizeDAG();
123
124 SDOperand LegalizeOp(SDOperand O);
125 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127
Chris Lattner77e77a62005-01-21 06:05:23 +0000128 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
129 SDOperand &Hi);
130 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
131 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000132
Chris Lattner35481892005-12-23 00:16:34 +0000133 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000134 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
135 SDOperand LegalOp,
136 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000137 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
138 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000139 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
140 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000141
Chris Lattnere34b3962005-01-19 04:19:40 +0000142 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000144 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
145 SDOperand &Lo, SDOperand &Hi);
146 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000147 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000148
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000149 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
150
Chris Lattner3e928bb2005-01-07 07:47:09 +0000151 SDOperand getIntPtrConstant(uint64_t Val) {
152 return DAG.getConstant(Val, TLI.getPointerTy());
153 }
154};
155}
156
Chris Lattnerb89175f2005-11-19 05:51:46 +0000157static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000158 switch (VecOp) {
159 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000160 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
161 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
162 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
163 }
164}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000165
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000171}
172
Jim Laskey6269ed12005-08-17 00:39:29 +0000173/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
174/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000175/// we expand it. At this point, we know that the result and operand types are
176/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000177SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
178 SDOperand Op0,
179 MVT::ValueType DestVT) {
180 if (Op0.getValueType() == MVT::i32) {
181 // simple 32-bit [signed|unsigned] integer to float/double expansion
182
183 // get the stack frame index of a 8 byte buffer
184 MachineFunction &MF = DAG.getMachineFunction();
185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
186 // get address of 8 byte buffer
187 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
188 // word offset constant for Hi/Lo address computation
189 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
190 // set up Hi and Lo (into buffer) address based on endian
191 SDOperand Hi, Lo;
192 if (TLI.isLittleEndian()) {
193 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
194 Lo = StackSlot;
195 } else {
196 Hi = StackSlot;
197 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
198 }
199 // if signed map to unsigned space
200 SDOperand Op0Mapped;
201 if (isSigned) {
202 // constant used to invert sign bit (signed to unsigned mapping)
203 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
204 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
205 } else {
206 Op0Mapped = Op0;
207 }
208 // store the lo of the constructed double - based on integer input
209 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
210 Op0Mapped, Lo, DAG.getSrcValue(NULL));
211 // initial hi portion of constructed double
212 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
213 // store the hi of the constructed double - biased exponent
214 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
215 InitialHi, Hi, DAG.getSrcValue(NULL));
216 // load the constructed double
217 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
218 DAG.getSrcValue(NULL));
219 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000220 SDOperand Bias = DAG.getConstantFP(isSigned ?
221 BitsToDouble(0x4330000080000000ULL)
222 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000223 MVT::f64);
224 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000226 // final result
227 SDOperand Result;
228 // handle final rounding
229 if (DestVT == MVT::f64) {
230 // do nothing
231 Result = Sub;
232 } else {
233 // if f32 then cast to f32
234 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
235 }
Chris Lattner69a889e2005-12-20 00:53:54 +0000236 return LegalizeOp(Result);
Jim Laskey6269ed12005-08-17 00:39:29 +0000237 }
238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
242 DAG.getConstant(0, Op0.getValueType()),
243 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000244 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
245 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
246 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Jim Laskey6269ed12005-08-17 00:39:29 +0000248 // If the sign bit of the integer is set, the large number will be treated
249 // as a negative number. To counteract this, the dynamic code adds an
250 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000251 uint64_t FF;
252 switch (Op0.getValueType()) {
253 default: assert(0 && "Unsupported integer type!");
254 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
255 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
256 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
257 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
258 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000259 if (TLI.isLittleEndian()) FF <<= 32;
260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000261
Chris Lattner5839bf22005-08-26 17:15:30 +0000262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000263 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
264 SDOperand FudgeInReg;
265 if (DestVT == MVT::f32)
266 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
267 DAG.getSrcValue(NULL));
268 else {
269 assert(DestVT == MVT::f64 && "Unexpected conversion");
270 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
271 DAG.getEntryNode(), CPIdx,
272 DAG.getSrcValue(NULL), MVT::f32));
273 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000274
Chris Lattner69a889e2005-12-20 00:53:54 +0000275 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnercad063f2005-07-16 00:19:57 +0000276}
277
Chris Lattner149c58c2005-08-16 18:17:10 +0000278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000279/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000280/// we promote it. At this point, we know that the result and operand types are
281/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
282/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
284 MVT::ValueType DestVT,
285 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000286 // First step, figure out the appropriate *INT_TO_FP operation to use.
287 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000288
Chris Lattnercad063f2005-07-16 00:19:57 +0000289 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000290
Chris Lattnercad063f2005-07-16 00:19:57 +0000291 // Scan for the appropriate larger type to use.
292 while (1) {
293 NewInTy = (MVT::ValueType)(NewInTy+1);
294 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000295
Chris Lattnercad063f2005-07-16 00:19:57 +0000296 // If the target supports SINT_TO_FP of this type, use it.
297 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
298 default: break;
299 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000300 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000301 break; // Can't use this datatype.
302 // FALL THROUGH.
303 case TargetLowering::Custom:
304 OpToUse = ISD::SINT_TO_FP;
305 break;
306 }
307 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000308 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000309
Chris Lattnercad063f2005-07-16 00:19:57 +0000310 // If the target supports UINT_TO_FP of this type, use it.
311 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
312 default: break;
313 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000314 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000315 break; // Can't use this datatype.
316 // FALL THROUGH.
317 case TargetLowering::Custom:
318 OpToUse = ISD::UINT_TO_FP;
319 break;
320 }
321 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000322
Chris Lattnercad063f2005-07-16 00:19:57 +0000323 // Otherwise, try a larger type.
324 }
325
Chris Lattnercad063f2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
Chris Lattner69a889e2005-12-20 00:53:54 +0000328 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattner69a889e2005-12-20 00:53:54 +0000331 // Make sure to legalize any nodes we create here.
332 return LegalizeOp(N);
Chris Lattnercad063f2005-07-16 00:19:57 +0000333}
334
Chris Lattner1618beb2005-07-29 00:11:56 +0000335/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
336/// FP_TO_*INT operation of the specified operand when the target requests that
337/// we promote it. At this point, we know that the result and operand types are
338/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
339/// operation that returns a larger result.
340SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
341 MVT::ValueType DestVT,
342 bool isSigned) {
343 // First step, figure out the appropriate FP_TO*INT operation to use.
344 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000345
Chris Lattner1618beb2005-07-29 00:11:56 +0000346 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000347
Chris Lattner1618beb2005-07-29 00:11:56 +0000348 // Scan for the appropriate larger type to use.
349 while (1) {
350 NewOutTy = (MVT::ValueType)(NewOutTy+1);
351 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000352
Chris Lattner1618beb2005-07-29 00:11:56 +0000353 // If the target supports FP_TO_SINT returning this type, use it.
354 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
355 default: break;
356 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000357 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000358 break; // Can't use this datatype.
359 // FALL THROUGH.
360 case TargetLowering::Custom:
361 OpToUse = ISD::FP_TO_SINT;
362 break;
363 }
364 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000365
Chris Lattner1618beb2005-07-29 00:11:56 +0000366 // If the target supports FP_TO_UINT of this type, use it.
367 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
368 default: break;
369 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000370 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000371 break; // Can't use this datatype.
372 // FALL THROUGH.
373 case TargetLowering::Custom:
374 OpToUse = ISD::FP_TO_UINT;
375 break;
376 }
377 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000378
Chris Lattner1618beb2005-07-29 00:11:56 +0000379 // Otherwise, try a larger type.
380 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000381
Chris Lattner1618beb2005-07-29 00:11:56 +0000382 // Okay, we found the operation and type to use. Truncate the result of the
383 // extended FP_TO_*INT operation to the desired size.
Chris Lattner69a889e2005-12-20 00:53:54 +0000384 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
385 DAG.getNode(OpToUse, NewOutTy, LegalOp));
386 // Make sure to legalize any nodes we create here in the next pass.
387 return LegalizeOp(N);
Chris Lattner1618beb2005-07-29 00:11:56 +0000388}
389
Chris Lattner32fca002005-10-06 01:20:27 +0000390/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
391/// not been visited yet and if all of its operands have already been visited.
392static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
393 std::map<SDNode*, unsigned> &Visited) {
394 if (++Visited[N] != N->getNumOperands())
395 return; // Haven't visited all operands yet
396
397 Order.push_back(N);
398
399 if (N->hasOneUse()) { // Tail recurse in common case.
400 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
401 return;
402 }
403
404 // Now that we have N in, add anything that uses it if all of their operands
405 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000406 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
407 ComputeTopDownOrdering(*UI, Order, Visited);
408}
409
Chris Lattner1618beb2005-07-29 00:11:56 +0000410
Chris Lattner3e928bb2005-01-07 07:47:09 +0000411void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000412 // The legalize process is inherently a bottom-up recursive process (users
413 // legalize their uses before themselves). Given infinite stack space, we
414 // could just start legalizing on the root and traverse the whole graph. In
415 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000416 // blocks. To avoid this problem, compute an ordering of the nodes where each
417 // node is only legalized after all of its operands are legalized.
418 std::map<SDNode*, unsigned> Visited;
419 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000420
Chris Lattner32fca002005-10-06 01:20:27 +0000421 // Compute ordering from all of the leaves in the graphs, those (like the
422 // entry node) that have no operands.
423 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
424 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000425 if (I->getNumOperands() == 0) {
426 Visited[I] = 0 - 1U;
427 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000428 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000429 }
430
Chris Lattnerde202b32005-11-09 23:47:37 +0000431 assert(Order.size() == Visited.size() &&
432 Order.size() ==
433 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000434 "Error: DAG is cyclic!");
435 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000436
Chris Lattner32fca002005-10-06 01:20:27 +0000437 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
438 SDNode *N = Order[i];
439 switch (getTypeAction(N->getValueType(0))) {
440 default: assert(0 && "Bad type action!");
441 case Legal:
442 LegalizeOp(SDOperand(N, 0));
443 break;
444 case Promote:
445 PromoteOp(SDOperand(N, 0));
446 break;
447 case Expand: {
448 SDOperand X, Y;
449 ExpandOp(SDOperand(N, 0), X, Y);
450 break;
451 }
452 }
453 }
454
455 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000456 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000457 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
458 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000459
460 ExpandedNodes.clear();
461 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000462 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463
464 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000465 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466}
467
468SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000469 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000470 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000471 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000472
Chris Lattner3e928bb2005-01-07 07:47:09 +0000473 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000474 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000475 if (Node->getNumValues() > 1) {
476 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
477 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000478 case Legal: break; // Nothing to do.
479 case Expand: {
480 SDOperand T1, T2;
481 ExpandOp(Op.getValue(i), T1, T2);
482 assert(LegalizedNodes.count(Op) &&
483 "Expansion didn't add legal operands!");
484 return LegalizedNodes[Op];
485 }
486 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000487 PromoteOp(Op.getValue(i));
488 assert(LegalizedNodes.count(Op) &&
489 "Expansion didn't add legal operands!");
490 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000491 }
492 }
493
Chris Lattner45982da2005-05-12 16:53:42 +0000494 // Note that LegalizeOp may be reentered even from single-use nodes, which
495 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000496 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
497 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000498
Nate Begeman9373a812005-08-10 20:51:12 +0000499 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000500
501 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000502
503 switch (Node->getOpcode()) {
504 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000505 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
506 // If this is a target node, legalize it by legalizing the operands then
507 // passing it through.
508 std::vector<SDOperand> Ops;
509 bool Changed = false;
510 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
511 Ops.push_back(LegalizeOp(Node->getOperand(i)));
512 Changed = Changed || Node->getOperand(i) != Ops.back();
513 }
514 if (Changed)
515 if (Node->getNumValues() == 1)
516 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
517 else {
518 std::vector<MVT::ValueType> VTs(Node->value_begin(),
519 Node->value_end());
520 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
521 }
522
523 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
524 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
525 return Result.getValue(Op.ResNo);
526 }
527 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000528 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
529 assert(0 && "Do not know how to legalize this operator!");
530 abort();
531 case ISD::EntryToken:
532 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000533 case ISD::TargetFrameIndex:
534 case ISD::Register:
535 case ISD::TargetConstant:
Nate Begeman28a6b022005-12-10 02:36:00 +0000536 case ISD::TargetConstantPool:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000537 case ISD::GlobalAddress:
Chris Lattnerb9debbf2005-11-17 05:52:24 +0000538 case ISD::TargetGlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000539 case ISD::ExternalSymbol:
Andrew Lenharthe8f65f12005-12-24 23:42:32 +0000540 case ISD::TargetExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000541 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000542 case ISD::BasicBlock:
543 case ISD::CONDCODE:
544 case ISD::VALUETYPE:
545 case ISD::SRCVALUE:
Chris Lattner36ce6912005-11-29 06:21:05 +0000546 case ISD::STRING:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000547 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
548 default: assert(0 && "This action is not supported yet!");
549 case TargetLowering::Custom: {
550 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
551 if (Tmp.Val) {
552 Result = LegalizeOp(Tmp);
553 break;
554 }
555 } // FALLTHROUGH if the target doesn't want to lower this op after all.
556 case TargetLowering::Legal:
557 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
558 break;
559 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000560 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000561 case ISD::AssertSext:
562 case ISD::AssertZext:
563 Tmp1 = LegalizeOp(Node->getOperand(0));
564 if (Tmp1 != Node->getOperand(0))
565 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
566 Node->getOperand(1));
567 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000568 case ISD::MERGE_VALUES:
569 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner69a52152005-01-14 22:38:01 +0000570 case ISD::CopyFromReg:
571 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000572 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000573 if (Node->getNumValues() == 2) {
Chris Lattner7310fb12005-12-18 15:27:43 +0000574 if (Tmp1 != Node->getOperand(0))
575 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000576 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattner7310fb12005-12-18 15:27:43 +0000577 Node->getValueType(0));
578 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000579 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
580 if (Node->getNumOperands() == 3)
581 Tmp2 = LegalizeOp(Node->getOperand(2));
582 if (Tmp1 != Node->getOperand(0) ||
583 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattner7310fb12005-12-18 15:27:43 +0000584 Result = DAG.getCopyFromReg(Tmp1,
585 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
586 Node->getValueType(0), Tmp2);
587 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
588 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000589 // Since CopyFromReg produces two values, make sure to remember that we
590 // legalized both of them.
591 AddLegalizedOperand(Op.getValue(0), Result);
592 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
593 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000594 case ISD::UNDEF: {
595 MVT::ValueType VT = Op.getValueType();
596 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000597 default: assert(0 && "This action is not supported yet!");
598 case TargetLowering::Expand:
599 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000600 if (MVT::isInteger(VT))
601 Result = DAG.getConstant(0, VT);
602 else if (MVT::isFloatingPoint(VT))
603 Result = DAG.getConstantFP(0, VT);
604 else
605 assert(0 && "Unknown value type!");
606 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000607 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000608 break;
609 }
610 break;
611 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000612
613 case ISD::LOCATION:
614 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
616
617 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
618 case TargetLowering::Promote:
619 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000620 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000621 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000622 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
623 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
624
625 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
626 const std::string &FName =
627 cast<StringSDNode>(Node->getOperand(3))->getValue();
628 const std::string &DirName =
629 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000630 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000631
Jim Laskeye81aecb2005-12-21 20:51:37 +0000632 std::vector<SDOperand> Ops;
633 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000634 SDOperand LineOp = Node->getOperand(1);
635 SDOperand ColOp = Node->getOperand(2);
636
637 if (useDEBUG_LOC) {
638 Ops.push_back(LineOp); // line #
639 Ops.push_back(ColOp); // col #
640 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
641 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
642 } else {
643 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
644 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
645 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
646 Ops.push_back(DAG.getConstant(ID, MVT::i32));
647 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
648 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000649 } else {
650 Result = Tmp1; // chain
651 }
Chris Lattnere7736732005-12-18 23:54:29 +0000652 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner36ce6912005-11-29 06:21:05 +0000653 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000654 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000655 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000656 if (Tmp1 != Node->getOperand(0) ||
657 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000658 std::vector<SDOperand> Ops;
659 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000660 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
661 Ops.push_back(Node->getOperand(1)); // line # must be legal.
662 Ops.push_back(Node->getOperand(2)); // col # must be legal.
663 } else {
664 // Otherwise promote them.
665 Ops.push_back(PromoteOp(Node->getOperand(1)));
666 Ops.push_back(PromoteOp(Node->getOperand(2)));
667 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000668 Ops.push_back(Node->getOperand(3)); // filename must be legal.
669 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
670 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
671 }
672 break;
673 }
674 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000675
676 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000677 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000678 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
679 case TargetLowering::Promote:
680 case TargetLowering::Expand:
681 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000682 case TargetLowering::Legal:
683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
684 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
685 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
686 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
687
688 if (Tmp1 != Node->getOperand(0) ||
689 Tmp2 != Node->getOperand(1) ||
690 Tmp3 != Node->getOperand(2) ||
691 Tmp4 != Node->getOperand(3)) {
692 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
693 }
694 break;
695 }
696 break;
697
698 case ISD::DEBUG_LABEL:
699 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
700 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
701 case TargetLowering::Promote:
702 case TargetLowering::Expand:
703 default: assert(0 && "This action is not supported yet!");
704 case TargetLowering::Legal:
705 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
706 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
707
708 if (Tmp1 != Node->getOperand(0) ||
709 Tmp2 != Node->getOperand(1)) {
710 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000711 }
712 break;
713 }
714 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000715
Chris Lattner3e928bb2005-01-07 07:47:09 +0000716 case ISD::Constant:
717 // We know we don't need to expand constants here, constants only have one
718 // value and we check that it is fine above.
719
720 // FIXME: Maybe we should handle things like targets that don't support full
721 // 32-bit immediates?
722 break;
723 case ISD::ConstantFP: {
724 // Spill FP immediates to the constant pool if the target cannot directly
725 // codegen them. Targets often have some immediate values that can be
726 // efficiently generated into an FP register without a load. We explicitly
727 // leave these constants as ConstantFP nodes for the target to deal with.
728
729 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
730
731 // Check to see if this FP immediate is already legal.
732 bool isLegal = false;
733 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
734 E = TLI.legal_fpimm_end(); I != E; ++I)
735 if (CFP->isExactlyValue(*I)) {
736 isLegal = true;
737 break;
738 }
739
740 if (!isLegal) {
741 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000742 bool Extend = false;
743
744 // If a FP immediate is precise when represented as a float, we put it
745 // into the constant pool as a float, even if it's is statically typed
746 // as a double.
747 MVT::ValueType VT = CFP->getValueType(0);
748 bool isDouble = VT == MVT::f64;
749 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
750 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000751 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
752 // Only do this if the target has a native EXTLOAD instruction from
753 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000754 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000755 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
756 VT = MVT::f32;
757 Extend = true;
758 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000759
Nate Begeman28a6b022005-12-10 02:36:00 +0000760 SDOperand CPIdx =
761 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000762 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000763 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
764 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000765 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000766 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
767 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000768 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 }
770 break;
771 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000772 case ISD::ConstantVec: {
773 // We assume that vector constants are not legal, and will be immediately
774 // spilled to the constant pool.
775 //
776 // FIXME: revisit this when we have some kind of mechanism by which targets
777 // can decided legality of vector constants, of which there may be very
778 // many.
779 //
780 // Create a ConstantPacked, and put it in the constant pool.
781 std::vector<Constant*> CV;
782 MVT::ValueType VT = Node->getValueType(0);
783 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
784 SDOperand OpN = Node->getOperand(I);
785 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
786 if (MVT::isFloatingPoint(VT))
787 CV.push_back(ConstantFP::get(OpNTy,
788 cast<ConstantFPSDNode>(OpN)->getValue()));
789 else
790 CV.push_back(ConstantUInt::get(OpNTy,
791 cast<ConstantSDNode>(OpN)->getValue()));
792 }
793 Constant *CP = ConstantPacked::get(CV);
Nate Begemand7d746f2005-12-13 03:03:23 +0000794 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000795 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
796 break;
797 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000798 case ISD::TokenFactor:
799 if (Node->getNumOperands() == 2) {
800 bool Changed = false;
801 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
802 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
803 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
804 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
805 } else {
806 std::vector<SDOperand> Ops;
807 bool Changed = false;
808 // Legalize the operands.
809 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
810 SDOperand Op = Node->getOperand(i);
811 Ops.push_back(LegalizeOp(Op));
812 Changed |= Ops[i] != Op;
813 }
814 if (Changed)
815 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000816 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000817 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000818
Chris Lattner16cd04d2005-05-12 23:24:06 +0000819 case ISD::CALLSEQ_START:
820 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000822 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000823 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000824 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000825 Node->setAdjCallChain(Tmp1);
Chris Lattner6a542892006-01-24 05:48:21 +0000826
827 // If this has a flag input, do legalize it.
828 if (Node->getOperand(Node->getNumOperands()-1).getValueType() == MVT::Flag){
829 Tmp1 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
830 if (Tmp1 != Node->getOperand(Node->getNumOperands()-1))
831 Node->setAdjCallFlag(Tmp1);
832 }
Nate Begeman27d404c2005-10-04 00:37:37 +0000833
Chris Lattner16cd04d2005-05-12 23:24:06 +0000834 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000835 // nodes are treated specially and are mutated in place. This makes the dag
836 // legalization process more efficient and also makes libcall insertion
837 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000838 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000839 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
841 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
842 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
843 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000844 Tmp3 != Node->getOperand(2)) {
845 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
846 std::vector<SDOperand> Ops;
847 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
848 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
849 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000850 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000851
Chris Lattner5f652292006-01-15 08:43:08 +0000852 Tmp1 = Result;
853 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000854 switch (TLI.getOperationAction(Node->getOpcode(),
855 Node->getValueType(0))) {
856 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000857 case TargetLowering::Expand: {
858 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
859 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
860 " not tell us which reg is the stack pointer!");
861 SDOperand Chain = Tmp1.getOperand(0);
862 SDOperand Size = Tmp2.getOperand(1);
863 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
864 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
865 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
866 break;
867 }
868 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000869 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
870 if (Tmp3.Val) {
871 Tmp1 = LegalizeOp(Tmp3);
872 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000873 }
Chris Lattner903d2782006-01-15 08:54:32 +0000874 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000875 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000876 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000877 }
Chris Lattner903d2782006-01-15 08:54:32 +0000878 // Since this op produce two values, make sure to remember that we
879 // legalized both of them.
880 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
881 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
882 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000883 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000884 case ISD::INLINEASM:
885 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
886 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
887 if (Tmp2.getValueType() != MVT::Flag) // Legalize Flag if it exists.
888 Tmp2 = Tmp3 = SDOperand(0, 0);
889 else
890 Tmp3 = LegalizeOp(Tmp2);
891
892 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
893 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
894 Ops[0] = Tmp1;
895 Ops.back() = Tmp3;
896 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
897 Result = DAG.getNode(ISD::INLINEASM, VTs, Ops);
898 } else {
899 Result = SDOperand(Node, 0);
900 }
901
902 // INLINE asm returns a chain and flag, make sure to add both to the map.
903 AddLegalizedOperand(SDOperand(Node, 0), Result);
904 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
905 return Result.getValue(Op.ResNo);
Chris Lattnerd71c0412005-05-13 18:43:43 +0000906 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000907 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000910
911 bool Changed = false;
912 std::vector<SDOperand> Ops;
913 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
914 Ops.push_back(LegalizeOp(Node->getOperand(i)));
915 Changed |= Ops.back() != Node->getOperand(i);
916 }
917
918 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000919 std::vector<MVT::ValueType> RetTyVTs;
920 RetTyVTs.reserve(Node->getNumValues());
921 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000922 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000923 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
924 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000925 } else {
926 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000927 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000928 // Since calls produce multiple values, make sure to remember that we
929 // legalized all of them.
930 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
931 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
932 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000933 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000934 case ISD::BR:
935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
936 if (Tmp1 != Node->getOperand(0))
937 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
938 break;
939
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000940 case ISD::BRCOND:
941 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000942
Chris Lattner47e92232005-01-18 19:27:06 +0000943 switch (getTypeAction(Node->getOperand(1).getValueType())) {
944 case Expand: assert(0 && "It's impossible to expand bools");
945 case Legal:
946 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
947 break;
948 case Promote:
949 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
950 break;
951 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000952
953 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
954 default: assert(0 && "This action is not supported yet!");
955 case TargetLowering::Expand:
956 // Expand brcond's setcc into its constituent parts and create a BR_CC
957 // Node.
958 if (Tmp2.getOpcode() == ISD::SETCC) {
959 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
960 Tmp2.getOperand(0), Tmp2.getOperand(1),
961 Node->getOperand(2));
962 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000963 // Make sure the condition is either zero or one. It may have been
964 // promoted from something else.
965 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
966
Nate Begeman7cbd5252005-08-16 19:49:35 +0000967 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
968 DAG.getCondCode(ISD::SETNE), Tmp2,
969 DAG.getConstant(0, Tmp2.getValueType()),
970 Node->getOperand(2));
971 }
Chris Lattnere7736732005-12-18 23:54:29 +0000972 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000973 break;
Evan Cheng898101c2005-12-19 23:12:38 +0000974 case TargetLowering::Custom: {
975 SDOperand Tmp =
976 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
977 Tmp1, Tmp2, Node->getOperand(2)), DAG);
978 if (Tmp.Val) {
979 Result = LegalizeOp(Tmp);
980 break;
981 }
982 // FALLTHROUGH if the target thinks it is legal.
983 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000984 case TargetLowering::Legal:
985 // Basic block destination (Op#2) is always legal.
986 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
987 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
988 Node->getOperand(2));
989 break;
990 }
991 break;
992 case ISD::BR_CC:
993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000994 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000995 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
996 Node->getOperand(2), // LHS
997 Node->getOperand(3), // RHS
998 Node->getOperand(1)));
999 // If we get a SETCC back from legalizing the SETCC node we just
1000 // created, then use its LHS, RHS, and CC directly in creating a new
1001 // node. Otherwise, select between the true and false value based on
1002 // comparing the result of the legalized with zero.
1003 if (Tmp2.getOpcode() == ISD::SETCC) {
1004 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1005 Tmp2.getOperand(0), Tmp2.getOperand(1),
1006 Node->getOperand(4));
1007 } else {
1008 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1009 DAG.getCondCode(ISD::SETNE),
1010 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
1011 Node->getOperand(4));
1012 }
Chris Lattner181b7a32005-12-17 23:46:46 +00001013 break;
1014 }
1015
1016 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1017 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1018
1019 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1020 default: assert(0 && "Unexpected action for BR_CC!");
1021 case TargetLowering::Custom: {
1022 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
1023 Tmp2, Tmp3, Node->getOperand(4));
1024 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
1025 if (Tmp4.Val) {
1026 Result = LegalizeOp(Tmp4);
1027 break;
1028 }
1029 } // FALLTHROUGH if the target doesn't want to lower this op after all.
1030 case TargetLowering::Legal:
1031 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1032 Tmp3 != Node->getOperand(3)) {
1033 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
1034 Tmp2, Tmp3, Node->getOperand(4));
1035 }
1036 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001037 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001038 break;
Chris Lattner411e8882005-04-09 03:30:19 +00001039 case ISD::BRCONDTWOWAY:
1040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1041 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1042 case Expand: assert(0 && "It's impossible to expand bools");
1043 case Legal:
1044 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1045 break;
1046 case Promote:
1047 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1048 break;
1049 }
1050 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
1051 // pair.
1052 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
1053 case TargetLowering::Promote:
1054 default: assert(0 && "This action is not supported yet!");
1055 case TargetLowering::Legal:
1056 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1057 std::vector<SDOperand> Ops;
1058 Ops.push_back(Tmp1);
1059 Ops.push_back(Tmp2);
1060 Ops.push_back(Node->getOperand(2));
1061 Ops.push_back(Node->getOperand(3));
1062 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
1063 }
1064 break;
1065 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +00001066 // If BRTWOWAY_CC is legal for this target, then simply expand this node
1067 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
1068 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001069 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001070 if (Tmp2.getOpcode() == ISD::SETCC) {
1071 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1072 Tmp2.getOperand(0), Tmp2.getOperand(1),
1073 Node->getOperand(2), Node->getOperand(3));
1074 } else {
1075 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1076 DAG.getConstant(0, Tmp2.getValueType()),
1077 Node->getOperand(2), Node->getOperand(3));
1078 }
1079 } else {
1080 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +00001081 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001082 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1083 }
Chris Lattnere7736732005-12-18 23:54:29 +00001084 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner411e8882005-04-09 03:30:19 +00001085 break;
1086 }
1087 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001088 case ISD::BRTWOWAY_CC:
1089 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001090 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001091 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1092 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1093 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1094 Tmp3 != Node->getOperand(3)) {
1095 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1096 Node->getOperand(4), Node->getOperand(5));
1097 }
1098 break;
1099 } else {
1100 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1101 Node->getOperand(2), // LHS
1102 Node->getOperand(3), // RHS
1103 Node->getOperand(1)));
1104 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1105 // pair.
1106 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1107 default: assert(0 && "This action is not supported yet!");
1108 case TargetLowering::Legal:
1109 // If we get a SETCC back from legalizing the SETCC node we just
1110 // created, then use its LHS, RHS, and CC directly in creating a new
1111 // node. Otherwise, select between the true and false value based on
1112 // comparing the result of the legalized with zero.
1113 if (Tmp2.getOpcode() == ISD::SETCC) {
1114 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1115 Tmp2.getOperand(0), Tmp2.getOperand(1),
1116 Node->getOperand(4), Node->getOperand(5));
1117 } else {
1118 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1119 DAG.getConstant(0, Tmp2.getValueType()),
1120 Node->getOperand(4), Node->getOperand(5));
1121 }
1122 break;
1123 case TargetLowering::Expand:
1124 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1125 Node->getOperand(4));
1126 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1127 break;
1128 }
Chris Lattnerf9dee6a2005-12-21 19:40:42 +00001129 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001130 }
1131 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001132 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1134 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001135
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001136 MVT::ValueType VT = Node->getValueType(0);
1137 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1138 default: assert(0 && "This action is not supported yet!");
1139 case TargetLowering::Custom: {
Nate Begemanacc398c2006-01-25 18:21:52 +00001140 SDOperand Op = DAG.getLoad(VT, Tmp1, Tmp2, Node->getOperand(2));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001141 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1142 if (Tmp.Val) {
1143 Result = LegalizeOp(Tmp);
Nate Begemanacc398c2006-01-25 18:21:52 +00001144 // Since loads produce two values, make sure to remember that we
1145 // legalized both of them.
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001146 AddLegalizedOperand(SDOperand(Node, 0), Result);
1147 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1148 return Result.getValue(Op.ResNo);
1149 }
1150 // FALLTHROUGH if the target thinks it is legal.
1151 }
1152 case TargetLowering::Legal:
1153 if (Tmp1 != Node->getOperand(0) ||
1154 Tmp2 != Node->getOperand(1))
Nate Begemanacc398c2006-01-25 18:21:52 +00001155 Result = DAG.getLoad(VT, Tmp1, Tmp2, Node->getOperand(2));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001156 else
1157 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001158
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001159 // Since loads produce two values, make sure to remember that we legalized
1160 // both of them.
1161 AddLegalizedOperand(SDOperand(Node, 0), Result);
1162 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1163 return Result.getValue(Op.ResNo);
1164 }
1165 assert(0 && "Unreachable");
1166 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001167 case ISD::EXTLOAD:
1168 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001169 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001170 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1171 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001172
Chris Lattner5f056bf2005-07-10 01:55:33 +00001173 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001174 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001175 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001176 case TargetLowering::Promote:
1177 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001178 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1179 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001180 // Since loads produce two values, make sure to remember that we legalized
1181 // both of them.
1182 AddLegalizedOperand(SDOperand(Node, 0), Result);
1183 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1184 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001185
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001186 case TargetLowering::Custom: {
1187 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1188 Tmp1, Tmp2, Node->getOperand(2),
1189 SrcVT);
1190 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1191 if (Tmp.Val) {
1192 Result = LegalizeOp(Tmp);
1193 // Since loads produce two values, make sure to remember that we legalized
1194 // both of them.
1195 AddLegalizedOperand(SDOperand(Node, 0), Result);
1196 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1197 return Result.getValue(Op.ResNo);
1198 }
1199 // FALLTHROUGH if the target thinks it is legal.
1200 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001201 case TargetLowering::Legal:
1202 if (Tmp1 != Node->getOperand(0) ||
1203 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001204 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1205 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001206 else
1207 Result = SDOperand(Node, 0);
1208
1209 // Since loads produce two values, make sure to remember that we legalized
1210 // both of them.
1211 AddLegalizedOperand(SDOperand(Node, 0), Result);
1212 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1213 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +00001214 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001215 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001216 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1217 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001218 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001219 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001220 Load = LegalizeOp(Load);
1221 AddLegalizedOperand(SDOperand(Node, 0), Result);
1222 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001223 if (Op.ResNo)
1224 return Load.getValue(1);
1225 return Result;
1226 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001227 assert(Node->getOpcode() != ISD::EXTLOAD &&
1228 "EXTLOAD should always be supported!");
1229 // Turn the unsupported load into an EXTLOAD followed by an explicit
1230 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001231 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1232 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001233 SDOperand ValRes;
1234 if (Node->getOpcode() == ISD::SEXTLOAD)
1235 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001236 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001237 else
1238 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001239 Result = LegalizeOp(Result); // Relegalize new nodes.
1240 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001241 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1242 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner01ff7212005-04-10 22:54:25 +00001243 if (Op.ResNo)
1244 return Result.getValue(1);
1245 return ValRes;
1246 }
1247 assert(0 && "Unreachable");
1248 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001249 case ISD::EXTRACT_ELEMENT: {
1250 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1251 switch (getTypeAction(OpTy)) {
1252 default:
1253 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1254 break;
1255 case Legal:
1256 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1257 // 1 -> Hi
1258 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1259 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1260 TLI.getShiftAmountTy()));
1261 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1262 } else {
1263 // 0 -> Lo
1264 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1265 Node->getOperand(0));
1266 }
1267 Result = LegalizeOp(Result);
1268 break;
1269 case Expand:
1270 // Get both the low and high parts.
1271 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1272 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1273 Result = Tmp2; // 1 -> Hi
1274 else
1275 Result = Tmp1; // 0 -> Lo
1276 break;
1277 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001278 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001279 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001280
1281 case ISD::CopyToReg:
1282 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001283
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001284 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001285 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001286 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001287 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001288 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001289 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1290 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1291 Node->getOperand(1), Tmp2);
1292 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001293 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1294 if (Node->getNumOperands() == 4)
1295 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001296 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001297 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001298 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1299 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1300 }
1301
1302 // Since this produces two values, make sure to remember that we legalized
1303 // both of them.
1304 AddLegalizedOperand(SDOperand(Node, 0), Result);
1305 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1306 return Result.getValue(Op.ResNo);
1307 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001308 break;
1309
1310 case ISD::RET:
1311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1312 switch (Node->getNumOperands()) {
1313 case 2: // ret val
1314 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1315 case Legal:
1316 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001317 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001318 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1319 break;
1320 case Expand: {
1321 SDOperand Lo, Hi;
1322 ExpandOp(Node->getOperand(1), Lo, Hi);
1323 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001324 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001325 }
1326 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001327 Tmp2 = PromoteOp(Node->getOperand(1));
1328 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1329 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001330 }
1331 break;
1332 case 1: // ret void
1333 if (Tmp1 != Node->getOperand(0))
1334 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1335 break;
1336 default: { // ret <values>
1337 std::vector<SDOperand> NewValues;
1338 NewValues.push_back(Tmp1);
1339 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1340 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1341 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001342 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001343 break;
1344 case Expand: {
1345 SDOperand Lo, Hi;
1346 ExpandOp(Node->getOperand(i), Lo, Hi);
1347 NewValues.push_back(Lo);
1348 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001349 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001350 }
1351 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001352 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001353 }
1354 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1355 break;
1356 }
1357 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001358
Chris Lattner47f5bea2006-01-06 05:47:48 +00001359 switch (TLI.getOperationAction(Node->getOpcode(),
1360 Node->getValueType(0))) {
Evan Cheng17c428e2006-01-06 00:41:43 +00001361 default: assert(0 && "This action is not supported yet!");
1362 case TargetLowering::Custom: {
1363 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1364 if (Tmp.Val) {
1365 Result = LegalizeOp(Tmp);
1366 break;
1367 }
1368 // FALLTHROUGH if the target thinks it is legal.
1369 }
1370 case TargetLowering::Legal:
1371 // Nothing to do.
1372 break;
1373 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001374 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001375 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001376 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1377 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1378
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001379 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001380 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001381 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001382 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001383 DAG.getConstant(FloatToBits(CFP->getValue()),
1384 MVT::i32),
1385 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001386 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001387 } else {
1388 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001389 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001390 DAG.getConstant(DoubleToBits(CFP->getValue()),
1391 MVT::i64),
1392 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001393 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001394 }
Chris Lattner6a542892006-01-24 05:48:21 +00001395 Result = LegalizeOp(Result);
1396 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001397 }
1398
Chris Lattner3e928bb2005-01-07 07:47:09 +00001399 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1400 case Legal: {
1401 SDOperand Val = LegalizeOp(Node->getOperand(1));
1402 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1403 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001404 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1405 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001406
1407 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1408 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1409 default: assert(0 && "This action is not supported yet!");
1410 case TargetLowering::Custom: {
1411 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1412 if (Tmp.Val) {
1413 Result = LegalizeOp(Tmp);
1414 break;
1415 }
1416 // FALLTHROUGH if the target thinks it is legal.
1417 }
1418 case TargetLowering::Legal:
1419 // Nothing to do.
1420 break;
1421 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001422 break;
1423 }
1424 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001425 // Truncate the value and store the result.
1426 Tmp3 = PromoteOp(Node->getOperand(1));
1427 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001428 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001429 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001430 break;
1431
Chris Lattner3e928bb2005-01-07 07:47:09 +00001432 case Expand:
1433 SDOperand Lo, Hi;
Nate Begemanab48be32005-11-22 18:16:00 +00001434 unsigned IncrementSize;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001435 ExpandOp(Node->getOperand(1), Lo, Hi);
1436
1437 if (!TLI.isLittleEndian())
1438 std::swap(Lo, Hi);
1439
Chris Lattneredb1add2005-05-11 04:51:16 +00001440 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1441 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001442 // If this is a vector type, then we have to calculate the increment as
1443 // the product of the element size in bytes, and the number of elements
1444 // in the high half of the vector.
1445 if (MVT::Vector == Hi.getValueType()) {
1446 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1447 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1448 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1449 } else {
1450 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1451 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001452 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1453 getIntPtrConstant(IncrementSize));
1454 assert(isTypeLegal(Tmp2.getValueType()) &&
1455 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001456 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001457 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1458 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001459 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1460 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001461 }
1462 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001463 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001464 case ISD::PCMARKER:
1465 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001466 if (Tmp1 != Node->getOperand(0))
1467 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001468 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001469 case ISD::STACKSAVE:
1470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1471 if (Tmp1 != Node->getOperand(0)) {
1472 std::vector<MVT::ValueType> VTs;
1473 VTs.push_back(Node->getValueType(0));
1474 VTs.push_back(MVT::Other);
1475 std::vector<SDOperand> Ops;
1476 Ops.push_back(Tmp1);
1477 Result = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1478 }
1479
1480 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1481 default: assert(0 && "This action is not supported yet!");
1482 case TargetLowering::Custom: {
1483 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1484 if (Tmp.Val) {
1485 Result = LegalizeOp(Tmp);
1486 break;
1487 }
1488 // FALLTHROUGH if the target thinks it is legal.
1489 }
1490 case TargetLowering::Legal:
1491 // Since stacksave produce two values, make sure to remember that we
1492 // legalized both of them.
1493 AddLegalizedOperand(SDOperand(Node, 0), Result);
1494 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1495 return Result.getValue(Op.ResNo);
1496 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001497 // Expand to CopyFromReg if the target set
1498 // StackPointerRegisterToSaveRestore.
1499 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Andrew Lenharth8ff318b2006-01-18 23:19:08 +00001500 Tmp1 = DAG.getCopyFromReg(Tmp1, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001501 Node->getValueType(0));
1502 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1503 AddLegalizedOperand(SDOperand(Node, 1), Tmp1.getValue(1));
1504 return Tmp1.getValue(Op.ResNo);
1505 } else {
1506 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1507 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1508 AddLegalizedOperand(SDOperand(Node, 1), Node->getOperand(0));
1509 return Op.ResNo ? Node->getOperand(0) : Tmp1;
1510 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001511 }
1512
1513 case ISD::STACKRESTORE:
1514 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1515 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1516 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1517 Result = DAG.getNode(ISD::STACKRESTORE, MVT::Other, Tmp1, Tmp2);
1518
1519 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1520 default: assert(0 && "This action is not supported yet!");
1521 case TargetLowering::Custom: {
1522 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1523 if (Tmp.Val) {
1524 Result = LegalizeOp(Tmp);
1525 break;
1526 }
1527 // FALLTHROUGH if the target thinks it is legal.
1528 }
1529 case TargetLowering::Legal:
1530 break;
1531 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001532 // Expand to CopyToReg if the target set
1533 // StackPointerRegisterToSaveRestore.
1534 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1535 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1536 } else {
1537 Result = Tmp1;
1538 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001539 break;
1540 }
1541 break;
1542
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001543 case ISD::READCYCLECOUNTER:
1544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001545 if (Tmp1 != Node->getOperand(0)) {
1546 std::vector<MVT::ValueType> rtypes;
1547 std::vector<SDOperand> rvals;
1548 rtypes.push_back(MVT::i64);
1549 rtypes.push_back(MVT::Other);
1550 rvals.push_back(Tmp1);
1551 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1552 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001553
1554 // Since rdcc produce two values, make sure to remember that we legalized
1555 // both of them.
1556 AddLegalizedOperand(SDOperand(Node, 0), Result);
1557 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1558 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001559
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001560 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1562 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1563
1564 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattnerc26f7a02005-12-23 16:12:20 +00001565 case Promote:
1566 case Expand:
1567 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
Chris Lattner0f69b292005-01-15 06:18:18 +00001568 case Legal:
1569 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001570
1571 // The only promote case we handle is TRUNCSTORE:i1 X into
1572 // -> TRUNCSTORE:i8 (and X, 1)
1573 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1574 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1575 TargetLowering::Promote) {
1576 // Promote the bool to a mask then store.
1577 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1578 DAG.getConstant(1, Tmp2.getValueType()));
1579 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1580 Node->getOperand(3), DAG.getValueType(MVT::i8));
1581
1582 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1583 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001584 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001585 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001586 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001587
1588 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1589 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1590 default: assert(0 && "This action is not supported yet!");
1591 case TargetLowering::Custom: {
1592 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1593 if (Tmp.Val) {
1594 Result = LegalizeOp(Tmp);
1595 break;
1596 }
1597 // FALLTHROUGH if the target thinks it is legal.
1598 }
1599 case TargetLowering::Legal:
1600 // Nothing to do.
1601 break;
1602 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001603 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001604 }
1605 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001606 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001607 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001608 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1609 case Expand: assert(0 && "It's impossible to expand bools");
1610 case Legal:
1611 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1612 break;
1613 case Promote:
1614 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1615 break;
1616 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001617 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001618 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001619
Nate Begemanb942a3d2005-08-23 04:29:48 +00001620 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001621 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001622 case TargetLowering::Expand:
1623 if (Tmp1.getOpcode() == ISD::SETCC) {
1624 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1625 Tmp2, Tmp3,
1626 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1627 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001628 // Make sure the condition is either zero or one. It may have been
1629 // promoted from something else.
1630 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001631 Result = DAG.getSelectCC(Tmp1,
1632 DAG.getConstant(0, Tmp1.getValueType()),
1633 Tmp2, Tmp3, ISD::SETNE);
1634 }
Chris Lattnere7736732005-12-18 23:54:29 +00001635 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman9373a812005-08-10 20:51:12 +00001636 break;
Evan Cheng7df96d62005-12-17 01:21:05 +00001637 case TargetLowering::Custom: {
1638 SDOperand Tmp =
1639 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1640 Tmp1, Tmp2, Tmp3), DAG);
1641 if (Tmp.Val) {
1642 Result = LegalizeOp(Tmp);
1643 break;
1644 }
1645 // FALLTHROUGH if the target thinks it is legal.
1646 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001647 case TargetLowering::Legal:
1648 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1649 Tmp3 != Node->getOperand(2))
1650 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1651 Tmp1, Tmp2, Tmp3);
1652 break;
1653 case TargetLowering::Promote: {
1654 MVT::ValueType NVT =
1655 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1656 unsigned ExtOp, TruncOp;
1657 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001658 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001659 TruncOp = ISD::TRUNCATE;
1660 } else {
1661 ExtOp = ISD::FP_EXTEND;
1662 TruncOp = ISD::FP_ROUND;
1663 }
1664 // Promote each of the values to the new type.
1665 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1666 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1667 // Perform the larger operation, then round down.
1668 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1669 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
Evan Cheng433f8ac2006-01-17 19:47:13 +00001670 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001671 break;
1672 }
1673 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001674 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001675 case ISD::SELECT_CC:
1676 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1677 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1678
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001679 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001680 // Everything is legal, see if we should expand this op or something.
1681 switch (TLI.getOperationAction(ISD::SELECT_CC,
1682 Node->getOperand(0).getValueType())) {
1683 default: assert(0 && "This action is not supported yet!");
1684 case TargetLowering::Custom: {
1685 SDOperand Tmp =
1686 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1687 Node->getOperand(0),
1688 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001689 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001690 if (Tmp.Val) {
1691 Result = LegalizeOp(Tmp);
1692 break;
1693 }
1694 } // FALLTHROUGH if the target can't lower this operation after all.
1695 case TargetLowering::Legal:
1696 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1697 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1698 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1699 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001700 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner23004e52005-08-26 00:23:59 +00001701 Tmp3, Tmp4, Node->getOperand(4));
1702 }
1703 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001704 }
1705 break;
1706 } else {
1707 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1708 Node->getOperand(0), // LHS
1709 Node->getOperand(1), // RHS
1710 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001711 // If we get a SETCC back from legalizing the SETCC node we just
1712 // created, then use its LHS, RHS, and CC directly in creating a new
1713 // node. Otherwise, select between the true and false value based on
1714 // comparing the result of the legalized with zero.
1715 if (Tmp1.getOpcode() == ISD::SETCC) {
1716 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1717 Tmp1.getOperand(0), Tmp1.getOperand(1),
1718 Tmp3, Tmp4, Tmp1.getOperand(2));
1719 } else {
1720 Result = DAG.getSelectCC(Tmp1,
1721 DAG.getConstant(0, Tmp1.getValueType()),
1722 Tmp3, Tmp4, ISD::SETNE);
1723 }
Nate Begeman9373a812005-08-10 20:51:12 +00001724 }
1725 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001726 case ISD::SETCC:
1727 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1728 case Legal:
1729 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1730 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001731 break;
1732 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001733 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1734 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1735
1736 // If this is an FP compare, the operands have already been extended.
1737 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1738 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001739 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001740
1741 // Otherwise, we have to insert explicit sign or zero extends. Note
1742 // that we could insert sign extends for ALL conditions, but zero extend
1743 // is cheaper on many machines (an AND instead of two shifts), so prefer
1744 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001745 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001746 default: assert(0 && "Unknown integer comparison!");
1747 case ISD::SETEQ:
1748 case ISD::SETNE:
1749 case ISD::SETUGE:
1750 case ISD::SETUGT:
1751 case ISD::SETULE:
1752 case ISD::SETULT:
1753 // ALL of these operations will work if we either sign or zero extend
1754 // the operands (including the unsigned comparisons!). Zero extend is
1755 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001756 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1757 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001758 break;
1759 case ISD::SETGE:
1760 case ISD::SETGT:
1761 case ISD::SETLT:
1762 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001763 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1764 DAG.getValueType(VT));
1765 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1766 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001767 break;
1768 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001769 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001770 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001771 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001772 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1773 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1774 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001775 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001776 case ISD::SETEQ:
1777 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001778 if (RHSLo == RHSHi)
1779 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1780 if (RHSCST->isAllOnesValue()) {
1781 // Comparison to -1.
1782 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001783 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001784 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001785 }
1786
Chris Lattner3e928bb2005-01-07 07:47:09 +00001787 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1788 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1789 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001790 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001791 break;
1792 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001793 // If this is a comparison of the sign bit, just look at the top part.
1794 // X > -1, x < 0
1795 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001796 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001797 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001798 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001799 (CST->isAllOnesValue()))) { // X > -1
1800 Tmp1 = LHSHi;
1801 Tmp2 = RHSHi;
1802 break;
1803 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001804
Chris Lattner3e928bb2005-01-07 07:47:09 +00001805 // FIXME: This generated code sucks.
1806 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001807 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001808 default: assert(0 && "Unknown integer setcc!");
1809 case ISD::SETLT:
1810 case ISD::SETULT: LowCC = ISD::SETULT; break;
1811 case ISD::SETGT:
1812 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1813 case ISD::SETLE:
1814 case ISD::SETULE: LowCC = ISD::SETULE; break;
1815 case ISD::SETGE:
1816 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1817 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001818
Chris Lattner3e928bb2005-01-07 07:47:09 +00001819 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1820 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1821 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1822
1823 // NOTE: on targets without efficient SELECT of bools, we can always use
1824 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001825 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1826 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1827 Node->getOperand(2));
1828 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001829 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1830 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001831 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001832 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001833 }
1834 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001835
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001836 switch(TLI.getOperationAction(ISD::SETCC,
1837 Node->getOperand(0).getValueType())) {
Nate Begemanb942a3d2005-08-23 04:29:48 +00001838 default:
1839 assert(0 && "Cannot handle this action for SETCC yet!");
1840 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001841 case TargetLowering::Promote: {
1842 // First step, figure out the appropriate operation to use.
1843 // Allow SETCC to not be supported for all legal data types
1844 // Mostly this targets FP
1845 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1846 MVT::ValueType OldVT = NewInTy;
1847
1848 // Scan for the appropriate larger type to use.
1849 while (1) {
1850 NewInTy = (MVT::ValueType)(NewInTy+1);
1851
1852 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1853 "Fell off of the edge of the integer world");
1854 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1855 "Fell off of the edge of the floating point world");
1856
1857 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001858 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001859 break;
1860 }
1861 if (MVT::isInteger(NewInTy))
1862 assert(0 && "Cannot promote Legal Integer SETCC yet");
1863 else {
1864 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1865 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1866 }
1867
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001868 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1869 Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001870 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001871 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001872 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001873 case TargetLowering::Custom: {
1874 SDOperand Tmp =
1875 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1876 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1877 if (Tmp.Val) {
1878 Result = LegalizeOp(Tmp);
1879 break;
1880 }
1881 // FALLTHROUGH if the target thinks it is legal.
1882 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001883 case TargetLowering::Legal:
1884 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1885 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1886 Node->getOperand(2));
1887 break;
1888 case TargetLowering::Expand:
1889 // Expand a setcc node into a select_cc of the same condition, lhs, and
1890 // rhs that selects between const 1 (true) and const 0 (false).
1891 MVT::ValueType VT = Node->getValueType(0);
1892 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1893 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1894 Node->getOperand(2));
1895 Result = LegalizeOp(Result);
1896 break;
1897 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001898 break;
1899
Chris Lattnere1bd8222005-01-11 05:57:22 +00001900 case ISD::MEMSET:
1901 case ISD::MEMCPY:
1902 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001904 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1905
1906 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1907 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1908 case Expand: assert(0 && "Cannot expand a byte!");
1909 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001910 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001911 break;
1912 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001913 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001914 break;
1915 }
1916 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001917 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001918 }
Chris Lattner272455b2005-02-02 03:44:41 +00001919
1920 SDOperand Tmp4;
1921 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001922 case Expand: {
1923 // Length is too big, just take the lo-part of the length.
1924 SDOperand HiPart;
1925 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1926 break;
1927 }
Chris Lattnere5605212005-01-28 22:29:18 +00001928 case Legal:
1929 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001930 break;
1931 case Promote:
1932 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001933 break;
1934 }
1935
1936 SDOperand Tmp5;
1937 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1938 case Expand: assert(0 && "Cannot expand this yet!");
1939 case Legal:
1940 Tmp5 = LegalizeOp(Node->getOperand(4));
1941 break;
1942 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001943 Tmp5 = PromoteOp(Node->getOperand(4));
1944 break;
1945 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001946
1947 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1948 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001949 case TargetLowering::Custom: {
1950 SDOperand Tmp =
1951 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1952 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1953 if (Tmp.Val) {
1954 Result = LegalizeOp(Tmp);
1955 break;
1956 }
1957 // FALLTHROUGH if the target thinks it is legal.
1958 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001959 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001960 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1961 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1962 Tmp5 != Node->getOperand(4)) {
1963 std::vector<SDOperand> Ops;
1964 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1965 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1966 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1967 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001968 break;
1969 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001970 // Otherwise, the target does not support this operation. Lower the
1971 // operation to an explicit libcall as appropriate.
1972 MVT::ValueType IntPtr = TLI.getPointerTy();
1973 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1974 std::vector<std::pair<SDOperand, const Type*> > Args;
1975
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001976 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001977 if (Node->getOpcode() == ISD::MEMSET) {
1978 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1979 // Extend the ubyte argument to be an int value for the call.
1980 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1981 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1982 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1983
1984 FnName = "memset";
1985 } else if (Node->getOpcode() == ISD::MEMCPY ||
1986 Node->getOpcode() == ISD::MEMMOVE) {
1987 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1988 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1989 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1990 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1991 } else {
1992 assert(0 && "Unknown op!");
1993 }
Chris Lattner45982da2005-05-12 16:53:42 +00001994
Chris Lattnere1bd8222005-01-11 05:57:22 +00001995 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001996 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001997 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner69a889e2005-12-20 00:53:54 +00001998 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001999 break;
2000 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002001 }
2002 break;
2003 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002004
2005 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00002006 Tmp1 = LegalizeOp(Node->getOperand(0));
2007 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002008
Chris Lattner3e011362005-05-14 07:45:46 +00002009 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
2010 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2011 std::vector<SDOperand> Ops;
2012 Ops.push_back(Tmp1);
2013 Ops.push_back(Tmp2);
2014 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
2015 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00002016 Result = SDOperand(Node, 0);
2017 // Since these produce two values, make sure to remember that we legalized
2018 // both of them.
2019 AddLegalizedOperand(SDOperand(Node, 0), Result);
2020 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2021 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00002022 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00002023 Tmp1 = LegalizeOp(Node->getOperand(0));
2024 Tmp2 = LegalizeOp(Node->getOperand(1));
2025 Tmp3 = LegalizeOp(Node->getOperand(2));
2026 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2027 Tmp3 != Node->getOperand(2))
2028 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2029 break;
2030
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002031 case ISD::READIO:
2032 Tmp1 = LegalizeOp(Node->getOperand(0));
2033 Tmp2 = LegalizeOp(Node->getOperand(1));
2034
2035 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2036 case TargetLowering::Custom:
2037 default: assert(0 && "This action not implemented for this operation!");
2038 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00002039 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
2040 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2041 std::vector<SDOperand> Ops;
2042 Ops.push_back(Tmp1);
2043 Ops.push_back(Tmp2);
2044 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
2045 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002046 Result = SDOperand(Node, 0);
2047 break;
2048 case TargetLowering::Expand:
2049 // Replace this with a load from memory.
2050 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
2051 Node->getOperand(1), DAG.getSrcValue(NULL));
2052 Result = LegalizeOp(Result);
2053 break;
2054 }
2055
2056 // Since these produce two values, make sure to remember that we legalized
2057 // both of them.
2058 AddLegalizedOperand(SDOperand(Node, 0), Result);
2059 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2060 return Result.getValue(Op.ResNo);
2061
2062 case ISD::WRITEIO:
2063 Tmp1 = LegalizeOp(Node->getOperand(0));
2064 Tmp2 = LegalizeOp(Node->getOperand(1));
2065 Tmp3 = LegalizeOp(Node->getOperand(2));
2066
2067 switch (TLI.getOperationAction(Node->getOpcode(),
2068 Node->getOperand(1).getValueType())) {
2069 case TargetLowering::Custom:
2070 default: assert(0 && "This action not implemented for this operation!");
2071 case TargetLowering::Legal:
2072 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2073 Tmp3 != Node->getOperand(2))
2074 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2075 break;
2076 case TargetLowering::Expand:
2077 // Replace this with a store to memory.
2078 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
2079 Node->getOperand(1), Node->getOperand(2),
2080 DAG.getSrcValue(NULL));
2081 Result = LegalizeOp(Result);
2082 break;
2083 }
2084 break;
2085
Chris Lattner84f67882005-01-20 18:52:28 +00002086 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00002087 case ISD::SUB_PARTS:
2088 case ISD::SHL_PARTS:
2089 case ISD::SRA_PARTS:
2090 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002091 std::vector<SDOperand> Ops;
2092 bool Changed = false;
2093 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2094 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2095 Changed |= Ops.back() != Node->getOperand(i);
2096 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002097 if (Changed) {
2098 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2099 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
2100 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002101
Evan Cheng05a2d562006-01-09 18:31:59 +00002102 switch (TLI.getOperationAction(Node->getOpcode(),
2103 Node->getValueType(0))) {
2104 default: assert(0 && "This action is not supported yet!");
2105 case TargetLowering::Custom: {
2106 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2107 if (Tmp.Val) {
Chris Lattner269f8c02006-01-10 19:43:26 +00002108 SDOperand Tmp2, RetVal(0,0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002109 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2110 Tmp2 = LegalizeOp(Tmp.getValue(i));
2111 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2112 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002113 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002114 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002115 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002116 return RetVal;
2117 }
2118 // FALLTHROUGH if the target thinks it is legal.
2119 }
2120 case TargetLowering::Legal:
2121 // Nothing to do.
2122 break;
2123 }
2124
Chris Lattner2c8086f2005-04-02 05:00:07 +00002125 // Since these produce multiple values, make sure to remember that we
2126 // legalized all of them.
2127 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2128 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2129 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002130 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002131
2132 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002133 case ISD::ADD:
2134 case ISD::SUB:
2135 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002136 case ISD::MULHS:
2137 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002138 case ISD::UDIV:
2139 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002140 case ISD::AND:
2141 case ISD::OR:
2142 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002143 case ISD::SHL:
2144 case ISD::SRL:
2145 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002146 case ISD::FADD:
2147 case ISD::FSUB:
2148 case ISD::FMUL:
2149 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002150 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002151 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2152 case Expand: assert(0 && "Not possible");
2153 case Legal:
2154 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2155 break;
2156 case Promote:
2157 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2158 break;
2159 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002160 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002161 case TargetLowering::Custom: {
2162 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2163 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2164 if (Tmp.Val) {
2165 Tmp = LegalizeOp(Tmp); // Relegalize input.
2166 AddLegalizedOperand(Op, Tmp);
2167 return Tmp;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002168 } //else it was considered legal and we fall through
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002169 }
Andrew Lenharth57030e32005-12-25 01:07:37 +00002170 case TargetLowering::Legal:
2171 if (Tmp1 != Node->getOperand(0) ||
2172 Tmp2 != Node->getOperand(1))
2173 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
2174 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002175 default:
2176 assert(0 && "Operation not supported");
2177 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002178 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002179
Nate Begeman419f8b62005-10-18 00:27:41 +00002180 case ISD::BUILD_PAIR: {
2181 MVT::ValueType PairTy = Node->getValueType(0);
2182 // TODO: handle the case where the Lo and Hi operands are not of legal type
2183 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2184 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2185 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2186 case TargetLowering::Legal:
2187 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2188 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2189 break;
2190 case TargetLowering::Promote:
2191 case TargetLowering::Custom:
2192 assert(0 && "Cannot promote/custom this yet!");
2193 case TargetLowering::Expand:
2194 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2195 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2196 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2197 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2198 TLI.getShiftAmountTy()));
2199 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
2200 break;
2201 }
2202 break;
2203 }
2204
Nate Begemanc105e192005-04-06 00:23:54 +00002205 case ISD::UREM:
2206 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002207 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002208 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2209 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2210 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth57030e32005-12-25 01:07:37 +00002211 case TargetLowering::Custom: {
Nate Begemanc02d98e2006-01-16 07:59:13 +00002212 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
Andrew Lenharth57030e32005-12-25 01:07:37 +00002213 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2214 if (Tmp.Val) {
Nate Begemanc02d98e2006-01-16 07:59:13 +00002215 Tmp = LegalizeOp(Tmp); // Relegalize input.
2216 AddLegalizedOperand(Op, Tmp);
2217 return Tmp;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002218 } //else it was considered legal and we fall through
2219 }
Nate Begemanc105e192005-04-06 00:23:54 +00002220 case TargetLowering::Legal:
2221 if (Tmp1 != Node->getOperand(0) ||
2222 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00002223 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00002224 Tmp2);
2225 break;
2226 case TargetLowering::Promote:
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002227 assert(0 && "Cannot promote handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00002228 case TargetLowering::Expand:
2229 if (MVT::isInteger(Node->getValueType(0))) {
2230 MVT::ValueType VT = Node->getValueType(0);
2231 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2232 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2233 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2234 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2235 } else {
2236 // Floating point mod -> fmod libcall.
2237 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2238 SDOperand Dummy;
2239 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002240 }
2241 break;
2242 }
2243 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002244
Nate Begemanacc398c2006-01-25 18:21:52 +00002245 case ISD::VAARG: {
2246 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2247 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2248
2249 MVT::ValueType VT = Node->getValueType(0);
2250 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2251 default: assert(0 && "This action is not supported yet!");
2252 case TargetLowering::Custom: {
2253 SDOperand Op = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2254 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
2255 if (Tmp.Val) {
2256 Result = LegalizeOp(Tmp);
2257 break;
2258 }
2259 // FALLTHROUGH if the target thinks it is legal.
2260 }
2261 case TargetLowering::Legal:
2262 if (Tmp1 != Node->getOperand(0) ||
2263 Tmp2 != Node->getOperand(1))
2264 Result = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2265 else
2266 Result = SDOperand(Node, 0);
2267 break;
2268 case TargetLowering::Expand: {
2269 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2270 Node->getOperand(2));
2271 // Increment the pointer, VAList, to the next vaarg
2272 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2273 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2274 TLI.getPointerTy()));
2275 // Store the incremented VAList to the legalized pointer
2276 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2277 Node->getOperand(2));
2278 // Load the actual argument out of the pointer VAList
2279 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
2280 Result = LegalizeOp(Result);
2281 break;
2282 }
2283 }
2284 // Since VAARG produces two values, make sure to remember that we
2285 // legalized both of them.
2286 AddLegalizedOperand(SDOperand(Node, 0), Result);
2287 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2288 return Result.getValue(Op.ResNo);
2289 }
2290
2291 case ISD::VACOPY:
2292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2294 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2295
2296 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2297 default: assert(0 && "This action is not supported yet!");
2298 case TargetLowering::Custom: {
2299 SDOperand Op = DAG.getNode(ISD::VACOPY, MVT::Other, Tmp1, Tmp2, Tmp3,
2300 Node->getOperand(3), Node->getOperand(4));
2301 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
2302 if (Tmp.Val) {
2303 Result = LegalizeOp(Tmp);
2304 break;
2305 }
2306 // FALLTHROUGH if the target thinks it is legal.
2307 }
2308 case TargetLowering::Legal:
2309 if (Tmp1 != Node->getOperand(0) ||
2310 Tmp2 != Node->getOperand(1) ||
2311 Tmp3 != Node->getOperand(2))
2312 Result = DAG.getNode(ISD::VACOPY, MVT::Other, Tmp1, Tmp2, Tmp3,
2313 Node->getOperand(3), Node->getOperand(4));
2314 break;
2315 case TargetLowering::Expand:
2316 // This defaults to loading a pointer from the input and storing it to the
2317 // output, returning the chain.
2318 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2319 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2320 Node->getOperand(4));
2321 Result = LegalizeOp(Result);
2322 break;
2323 }
2324 break;
2325
2326 case ISD::VAEND:
2327 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2328 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2329
2330 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2331 default: assert(0 && "This action is not supported yet!");
2332 case TargetLowering::Custom: {
2333 SDOperand Op = DAG.getNode(ISD::VAEND, MVT::Other, Tmp1, Tmp2,
2334 Node->getOperand(2));
2335 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
2336 if (Tmp.Val) {
2337 Result = LegalizeOp(Tmp);
2338 break;
2339 }
2340 // FALLTHROUGH if the target thinks it is legal.
2341 }
2342 case TargetLowering::Legal:
2343 if (Tmp1 != Node->getOperand(0) ||
2344 Tmp2 != Node->getOperand(1))
2345 Result = DAG.getNode(ISD::VAEND, MVT::Other, Tmp1, Tmp2,
2346 Node->getOperand(2));
2347 break;
2348 case TargetLowering::Expand:
2349 Result = Tmp1; // Default to a no-op, return the chain
2350 break;
2351 }
2352 break;
2353
2354 case ISD::VASTART:
2355 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2356 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2357
2358 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2359 default: assert(0 && "This action is not supported yet!");
2360 case TargetLowering::Custom: {
2361 SDOperand Op = DAG.getNode(ISD::VASTART, MVT::Other, Tmp1, Tmp2,
2362 Node->getOperand(2));
2363 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
2364 if (Tmp.Val) {
2365 Result = LegalizeOp(Tmp);
2366 break;
2367 }
2368 // FALLTHROUGH if the target thinks it is legal.
2369 }
2370 case TargetLowering::Legal:
2371 if (Tmp1 != Node->getOperand(0) ||
2372 Tmp2 != Node->getOperand(1))
2373 Result = DAG.getNode(ISD::VASTART, MVT::Other, Tmp1, Tmp2,
2374 Node->getOperand(2));
2375 break;
2376 }
2377 break;
2378
Nate Begeman35ef9132006-01-11 21:21:00 +00002379 case ISD::ROTL:
2380 case ISD::ROTR:
2381 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2382 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2383 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2384 case TargetLowering::Custom:
2385 case TargetLowering::Promote:
2386 case TargetLowering::Expand:
2387 assert(0 && "Cannot handle this yet!");
2388 case TargetLowering::Legal:
2389 if (Tmp1 != Node->getOperand(0) ||
2390 Tmp2 != Node->getOperand(1))
2391 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
2392 Tmp2);
2393 break;
2394 }
2395 break;
2396
Nate Begemand88fc032006-01-14 03:14:10 +00002397 case ISD::BSWAP:
2398 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2399 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2400 case TargetLowering::Legal:
2401 if (Tmp1 != Node->getOperand(0))
2402 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2403 break;
2404 case TargetLowering::Promote: {
2405 MVT::ValueType OVT = Tmp1.getValueType();
2406 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2407 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
2408
2409 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2410 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2411 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2412 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
Evan Cheng433f8ac2006-01-17 19:47:13 +00002413 Result = LegalizeOp(Result);
Nate Begemand88fc032006-01-14 03:14:10 +00002414 break;
2415 }
2416 case TargetLowering::Custom:
2417 assert(0 && "Cannot custom legalize this yet!");
2418 case TargetLowering::Expand: {
2419 MVT::ValueType VT = Tmp1.getValueType();
2420 switch (VT) {
2421 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2422 case MVT::i16:
2423 Tmp2 = DAG.getNode(ISD::SHL, VT, Tmp1,
2424 DAG.getConstant(8, TLI.getShiftAmountTy()));
2425 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2426 DAG.getConstant(8, TLI.getShiftAmountTy()));
2427 Result = DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
2428 break;
2429 case MVT::i32:
2430 Tmp4 = DAG.getNode(ISD::SHL, VT, Tmp1,
2431 DAG.getConstant(24, TLI.getShiftAmountTy()));
2432 Tmp3 = DAG.getNode(ISD::SHL, VT, Tmp1,
2433 DAG.getConstant(8, TLI.getShiftAmountTy()));
2434 Tmp2 = DAG.getNode(ISD::SRL, VT, Tmp1,
2435 DAG.getConstant(8, TLI.getShiftAmountTy()));
2436 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2437 DAG.getConstant(24, TLI.getShiftAmountTy()));
2438 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2439 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2440 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
2441 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
2442 Result = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
2443 break;
Nate Begemanc02d98e2006-01-16 07:59:13 +00002444 case MVT::i64: {
2445 SDOperand Tmp5, Tmp6, Tmp7, Tmp8;
2446 Tmp8 = DAG.getNode(ISD::SHL, VT, Tmp1,
2447 DAG.getConstant(56, TLI.getShiftAmountTy()));
2448 Tmp7 = DAG.getNode(ISD::SHL, VT, Tmp1,
2449 DAG.getConstant(40, TLI.getShiftAmountTy()));
2450 Tmp6 = DAG.getNode(ISD::SHL, VT, Tmp1,
2451 DAG.getConstant(24, TLI.getShiftAmountTy()));
2452 Tmp5 = DAG.getNode(ISD::SHL, VT, Tmp1,
2453 DAG.getConstant(8, TLI.getShiftAmountTy()));
2454 Tmp4 = DAG.getNode(ISD::SRL, VT, Tmp1,
2455 DAG.getConstant(8, TLI.getShiftAmountTy()));
2456 Tmp3 = DAG.getNode(ISD::SRL, VT, Tmp1,
2457 DAG.getConstant(24, TLI.getShiftAmountTy()));
2458 Tmp2 = DAG.getNode(ISD::SRL, VT, Tmp1,
2459 DAG.getConstant(40, TLI.getShiftAmountTy()));
2460 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2461 DAG.getConstant(56, TLI.getShiftAmountTy()));
2462 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7,
2463 DAG.getConstant(0x00FF000000000000ULL, VT));
2464 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp7,
2465 DAG.getConstant(0x0000FF0000000000ULL, VT));
2466 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp7,
2467 DAG.getConstant(0x000000FF00000000ULL, VT));
2468 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp7,
2469 DAG.getConstant(0x00000000FF000000ULL, VT));
2470 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp7,
2471 DAG.getConstant(0x0000000000FF0000ULL, VT));
2472 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp7,
2473 DAG.getConstant(0x000000000000FF00ULL, VT));
2474 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
2475 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
2476 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
2477 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
2478 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
2479 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
2480 Result = DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
2481 break;
2482 }
Nate Begemand88fc032006-01-14 03:14:10 +00002483 }
Evan Cheng433f8ac2006-01-17 19:47:13 +00002484 Result = LegalizeOp(Result);
Nate Begemand88fc032006-01-14 03:14:10 +00002485 break;
2486 }
2487 }
2488 break;
2489
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002490 case ISD::CTPOP:
2491 case ISD::CTTZ:
2492 case ISD::CTLZ:
2493 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2494 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2495 case TargetLowering::Legal:
2496 if (Tmp1 != Node->getOperand(0))
2497 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2498 break;
2499 case TargetLowering::Promote: {
2500 MVT::ValueType OVT = Tmp1.getValueType();
2501 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002502
2503 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002504 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2505 // Perform the larger operation, then subtract if needed.
2506 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2507 switch(Node->getOpcode())
2508 {
2509 case ISD::CTPOP:
2510 Result = Tmp1;
2511 break;
2512 case ISD::CTTZ:
2513 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002514 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2515 DAG.getConstant(getSizeInBits(NVT), NVT),
2516 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002517 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002518 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2519 break;
2520 case ISD::CTLZ:
2521 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002522 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2523 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002524 getSizeInBits(OVT), NVT));
2525 break;
2526 }
Evan Cheng433f8ac2006-01-17 19:47:13 +00002527 Result = LegalizeOp(Result);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002528 break;
2529 }
2530 case TargetLowering::Custom:
2531 assert(0 && "Cannot custom handle this yet!");
2532 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002533 switch(Node->getOpcode())
2534 {
2535 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002536 static const uint64_t mask[6] = {
2537 0x5555555555555555ULL, 0x3333333333333333ULL,
2538 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2539 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2540 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002541 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002542 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2543 unsigned len = getSizeInBits(VT);
2544 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002545 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002546 Tmp2 = DAG.getConstant(mask[i], VT);
2547 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002548 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002549 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2550 DAG.getNode(ISD::AND, VT,
2551 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2552 Tmp2));
2553 }
Evan Cheng433f8ac2006-01-17 19:47:13 +00002554 Result = LegalizeOp(Tmp1);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002555 break;
2556 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00002557 case ISD::CTLZ: {
2558 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002559 x = x | (x >> 1);
2560 x = x | (x >> 2);
2561 ...
2562 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00002563 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002564 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00002565
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002566 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2567 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00002568 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2569 unsigned len = getSizeInBits(VT);
2570 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2571 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002572 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00002573 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2574 }
2575 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002576 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00002577 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002578 }
2579 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00002580 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002581 // unless the target has ctlz but not ctpop, in which case we use:
2582 // { return 32 - nlz(~x & (x-1)); }
2583 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002584 MVT::ValueType VT = Tmp1.getValueType();
2585 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002586 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002587 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2588 DAG.getNode(ISD::SUB, VT, Tmp1,
2589 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002590 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002591 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2592 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00002593 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002594 DAG.getConstant(getSizeInBits(VT), VT),
2595 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2596 } else {
2597 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2598 }
Chris Lattner18aa6802005-05-11 05:27:09 +00002599 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002600 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002601 default:
2602 assert(0 && "Cannot expand this yet!");
2603 break;
2604 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002605 break;
2606 }
2607 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002608
Chris Lattner2c8086f2005-04-02 05:00:07 +00002609 // Unary operators
2610 case ISD::FABS:
2611 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002612 case ISD::FSQRT:
2613 case ISD::FSIN:
2614 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002615 Tmp1 = LegalizeOp(Node->getOperand(0));
2616 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2617 case TargetLowering::Legal:
2618 if (Tmp1 != Node->getOperand(0))
2619 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2620 break;
2621 case TargetLowering::Promote:
2622 case TargetLowering::Custom:
2623 assert(0 && "Cannot promote/custom handle this yet!");
2624 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002625 switch(Node->getOpcode()) {
2626 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00002627 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2628 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002629 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00002630 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002631 break;
2632 }
2633 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002634 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2635 MVT::ValueType VT = Node->getValueType(0);
2636 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002637 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002638 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2639 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2640 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002641 break;
2642 }
2643 case ISD::FSQRT:
2644 case ISD::FSIN:
2645 case ISD::FCOS: {
2646 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002647 const char *FnName = 0;
2648 switch(Node->getOpcode()) {
2649 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2650 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2651 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2652 default: assert(0 && "Unreachable!");
2653 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002654 SDOperand Dummy;
2655 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002656 break;
2657 }
2658 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002659 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002660 }
2661 break;
2662 }
2663 break;
Chris Lattner35481892005-12-23 00:16:34 +00002664
2665 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002666 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002667 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002668 Result = LegalizeOp(Result);
2669 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002670 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2671 Node->getOperand(0).getValueType())) {
2672 default: assert(0 && "Unknown operation action!");
2673 case TargetLowering::Expand:
2674 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002675 Result = LegalizeOp(Result);
Chris Lattner35481892005-12-23 00:16:34 +00002676 break;
2677 case TargetLowering::Legal:
2678 Tmp1 = LegalizeOp(Node->getOperand(0));
2679 if (Tmp1 != Node->getOperand(0))
2680 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2681 break;
2682 }
2683 }
2684 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002685 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002686 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002687 case ISD::UINT_TO_FP: {
2688 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002689 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2690 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002691 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002692 Node->getOperand(0).getValueType())) {
2693 default: assert(0 && "Unknown operation action!");
2694 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002695 Result = ExpandLegalINT_TO_FP(isSigned,
2696 LegalizeOp(Node->getOperand(0)),
2697 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002698 AddLegalizedOperand(Op, Result);
2699 return Result;
2700 case TargetLowering::Promote:
2701 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2702 Node->getValueType(0),
2703 isSigned);
2704 AddLegalizedOperand(Op, Result);
2705 return Result;
2706 case TargetLowering::Legal:
2707 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002708 case TargetLowering::Custom: {
2709 Tmp1 = LegalizeOp(Node->getOperand(0));
2710 SDOperand Tmp =
2711 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2712 Tmp = TLI.LowerOperation(Tmp, DAG);
2713 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002714 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002715 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002716 return Tmp;
2717 } else {
2718 assert(0 && "Target Must Lower this");
2719 }
2720 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00002721 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002722
Chris Lattner3e928bb2005-01-07 07:47:09 +00002723 Tmp1 = LegalizeOp(Node->getOperand(0));
2724 if (Tmp1 != Node->getOperand(0))
2725 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2726 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002727 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002728 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2729 Node->getValueType(0), Node->getOperand(0));
2730 break;
2731 case Promote:
2732 if (isSigned) {
2733 Result = PromoteOp(Node->getOperand(0));
2734 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2735 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2736 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2737 } else {
2738 Result = PromoteOp(Node->getOperand(0));
2739 Result = DAG.getZeroExtendInReg(Result,
2740 Node->getOperand(0).getValueType());
2741 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002742 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002743 break;
2744 }
2745 break;
2746 }
2747 case ISD::TRUNCATE:
2748 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2749 case Legal:
2750 Tmp1 = LegalizeOp(Node->getOperand(0));
2751 if (Tmp1 != Node->getOperand(0))
2752 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2753 break;
2754 case Expand:
2755 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2756
2757 // Since the result is legal, we should just be able to truncate the low
2758 // part of the source.
2759 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2760 break;
2761 case Promote:
2762 Result = PromoteOp(Node->getOperand(0));
2763 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2764 break;
2765 }
2766 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002767
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002768 case ISD::FP_TO_SINT:
2769 case ISD::FP_TO_UINT:
2770 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2771 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002772 Tmp1 = LegalizeOp(Node->getOperand(0));
2773
Chris Lattner1618beb2005-07-29 00:11:56 +00002774 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2775 default: assert(0 && "Unknown operation action!");
2776 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002777 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2778 SDOperand True, False;
2779 MVT::ValueType VT = Node->getOperand(0).getValueType();
2780 MVT::ValueType NVT = Node->getValueType(0);
2781 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2782 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2783 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2784 Node->getOperand(0), Tmp2, ISD::SETLT);
2785 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2786 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002787 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002788 Tmp2));
2789 False = DAG.getNode(ISD::XOR, NVT, False,
2790 DAG.getConstant(1ULL << ShiftAmt, NVT));
2791 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner69a889e2005-12-20 00:53:54 +00002792 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman2d56e722005-08-14 18:38:32 +00002793 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002794 } else {
2795 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2796 }
2797 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002798 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002799 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002800 Node->getOpcode() == ISD::FP_TO_SINT);
2801 AddLegalizedOperand(Op, Result);
2802 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002803 case TargetLowering::Custom: {
2804 SDOperand Tmp =
2805 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2806 Tmp = TLI.LowerOperation(Tmp, DAG);
2807 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002808 Tmp = LegalizeOp(Tmp);
Chris Lattner07dffd62005-08-26 00:14:16 +00002809 AddLegalizedOperand(Op, Tmp);
Chris Lattner507f7522005-08-29 17:30:00 +00002810 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002811 } else {
2812 // The target thinks this is legal afterall.
2813 break;
2814 }
2815 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002816 case TargetLowering::Legal:
2817 break;
2818 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002819
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002820 if (Tmp1 != Node->getOperand(0))
2821 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2822 break;
2823 case Expand:
2824 assert(0 && "Shouldn't need to expand other operators here!");
2825 case Promote:
2826 Result = PromoteOp(Node->getOperand(0));
2827 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2828 break;
2829 }
2830 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002831
Chris Lattner13c78e22005-09-02 00:18:10 +00002832 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002833 case ISD::ZERO_EXTEND:
2834 case ISD::SIGN_EXTEND:
2835 case ISD::FP_EXTEND:
2836 case ISD::FP_ROUND:
2837 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2838 case Legal:
2839 Tmp1 = LegalizeOp(Node->getOperand(0));
2840 if (Tmp1 != Node->getOperand(0))
2841 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2842 break;
2843 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002844 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002845
Chris Lattner03c85462005-01-15 05:21:40 +00002846 case Promote:
2847 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002848 case ISD::ANY_EXTEND:
2849 Result = PromoteOp(Node->getOperand(0));
2850 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2851 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002852 case ISD::ZERO_EXTEND:
2853 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002854 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002855 Result = DAG.getZeroExtendInReg(Result,
2856 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002857 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002858 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002859 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002860 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002861 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002862 Result,
2863 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002864 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002865 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002866 Result = PromoteOp(Node->getOperand(0));
2867 if (Result.getValueType() != Op.getValueType())
2868 // Dynamically dead while we have only 2 FP types.
2869 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2870 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002871 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002872 Result = PromoteOp(Node->getOperand(0));
2873 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2874 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002875 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002876 }
2877 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002878 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002879 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002880 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002881 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002882
2883 // If this operation is not supported, convert it to a shl/shr or load/store
2884 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002885 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2886 default: assert(0 && "This action not supported for this op yet!");
2887 case TargetLowering::Legal:
2888 if (Tmp1 != Node->getOperand(0))
2889 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002890 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002891 break;
2892 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002893 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002894 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002895 // NOTE: we could fall back on load/store here too for targets without
2896 // SAR. However, it is doubtful that any exist.
2897 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2898 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002899 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002900 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2901 Node->getOperand(0), ShiftCst);
2902 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2903 Result, ShiftCst);
2904 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2905 // The only way we can lower this is to turn it into a STORETRUNC,
2906 // EXTLOAD pair, targetting a temporary location (a stack slot).
2907
2908 // NOTE: there is a choice here between constantly creating new stack
2909 // slots and always reusing the same one. We currently always create
2910 // new ones, as reuse may inhibit scheduling.
2911 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2912 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2913 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2914 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002915 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002916 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2917 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2918 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002919 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002920 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002921 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2922 Result, StackSlot, DAG.getSrcValue(NULL),
2923 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002924 } else {
2925 assert(0 && "Unknown op");
2926 }
2927 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002928 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002929 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002930 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002931 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002932 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002933
Chris Lattner45982da2005-05-12 16:53:42 +00002934 // Note that LegalizeOp may be reentered even from single-use nodes, which
2935 // means that we always must cache transformed nodes.
2936 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002937 return Result;
2938}
2939
Chris Lattner8b6fa222005-01-15 22:16:26 +00002940/// PromoteOp - Given an operation that produces a value in an invalid type,
2941/// promote it to compute the value into a larger type. The produced value will
2942/// have the correct bits for the low portion of the register, but no guarantee
2943/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002944SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2945 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002946 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002947 assert(getTypeAction(VT) == Promote &&
2948 "Caller should expand or legalize operands that are not promotable!");
2949 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2950 "Cannot promote to smaller type!");
2951
Chris Lattner03c85462005-01-15 05:21:40 +00002952 SDOperand Tmp1, Tmp2, Tmp3;
2953
2954 SDOperand Result;
2955 SDNode *Node = Op.Val;
2956
Chris Lattner6fdcb252005-09-02 20:32:45 +00002957 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2958 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002959
Chris Lattner0f69b292005-01-15 06:18:18 +00002960 // Promotion needs an optimization step to clean up after it, and is not
2961 // careful to avoid operations the target does not support. Make sure that
2962 // all generated operations are legalized in the next iteration.
2963 NeedsAnotherIteration = true;
2964
Chris Lattner03c85462005-01-15 05:21:40 +00002965 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002966 case ISD::CopyFromReg:
2967 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002968 default:
2969 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2970 assert(0 && "Do not know how to promote this operator!");
2971 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002972 case ISD::UNDEF:
2973 Result = DAG.getNode(ISD::UNDEF, NVT);
2974 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002975 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002976 if (VT != MVT::i1)
2977 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2978 else
2979 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002980 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2981 break;
2982 case ISD::ConstantFP:
2983 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2984 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2985 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002986
Chris Lattner82fbfb62005-01-18 02:59:52 +00002987 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002988 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002989 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2990 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002991 Result = LegalizeOp(Result);
2992 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002993
2994 case ISD::TRUNCATE:
2995 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2996 case Legal:
2997 Result = LegalizeOp(Node->getOperand(0));
2998 assert(Result.getValueType() >= NVT &&
2999 "This truncation doesn't make sense!");
3000 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3001 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3002 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003003 case Promote:
3004 // The truncation is not required, because we don't guarantee anything
3005 // about high bits anyway.
3006 Result = PromoteOp(Node->getOperand(0));
3007 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003008 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003009 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3010 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003011 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003012 }
3013 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003014 case ISD::SIGN_EXTEND:
3015 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003016 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003017 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3018 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3019 case Legal:
3020 // Input is legal? Just do extend all the way to the larger type.
3021 Result = LegalizeOp(Node->getOperand(0));
3022 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
3023 break;
3024 case Promote:
3025 // Promote the reg if it's smaller.
3026 Result = PromoteOp(Node->getOperand(0));
3027 // The high bits are not guaranteed to be anything. Insert an extend.
3028 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003029 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003030 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003031 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003032 Result = DAG.getZeroExtendInReg(Result,
3033 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003034 break;
3035 }
3036 break;
Chris Lattner35481892005-12-23 00:16:34 +00003037 case ISD::BIT_CONVERT:
3038 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3039 Result = PromoteOp(Result);
3040 break;
3041
Chris Lattner8b6fa222005-01-15 22:16:26 +00003042 case ISD::FP_EXTEND:
3043 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3044 case ISD::FP_ROUND:
3045 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3046 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3047 case Promote: assert(0 && "Unreachable with 2 FP types!");
3048 case Legal:
3049 // Input is legal? Do an FP_ROUND_INREG.
3050 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003051 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3052 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003053 break;
3054 }
3055 break;
3056
3057 case ISD::SINT_TO_FP:
3058 case ISD::UINT_TO_FP:
3059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3060 case Legal:
3061 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003062 // No extra round required here.
3063 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003064 break;
3065
3066 case Promote:
3067 Result = PromoteOp(Node->getOperand(0));
3068 if (Node->getOpcode() == ISD::SINT_TO_FP)
3069 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003070 Result,
3071 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003072 else
Chris Lattner23993562005-04-13 02:38:47 +00003073 Result = DAG.getZeroExtendInReg(Result,
3074 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003075 // No extra round required here.
3076 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003077 break;
3078 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003079 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3080 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003081 // Round if we cannot tolerate excess precision.
3082 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003083 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3084 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003085 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003086 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003087 break;
3088
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003089 case ISD::SIGN_EXTEND_INREG:
3090 Result = PromoteOp(Node->getOperand(0));
3091 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3092 Node->getOperand(1));
3093 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003094 case ISD::FP_TO_SINT:
3095 case ISD::FP_TO_UINT:
3096 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3097 case Legal:
3098 Tmp1 = LegalizeOp(Node->getOperand(0));
3099 break;
3100 case Promote:
3101 // The input result is prerounded, so we don't have to do anything
3102 // special.
3103 Tmp1 = PromoteOp(Node->getOperand(0));
3104 break;
3105 case Expand:
3106 assert(0 && "not implemented");
3107 }
Nate Begemand2558e32005-08-14 01:20:53 +00003108 // If we're promoting a UINT to a larger size, check to see if the new node
3109 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3110 // we can use that instead. This allows us to generate better code for
3111 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3112 // legal, such as PowerPC.
3113 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003114 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003115 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3116 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003117 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3118 } else {
3119 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3120 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003121 break;
3122
Chris Lattner2c8086f2005-04-02 05:00:07 +00003123 case ISD::FABS:
3124 case ISD::FNEG:
3125 Tmp1 = PromoteOp(Node->getOperand(0));
3126 assert(Tmp1.getValueType() == NVT);
3127 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3128 // NOTE: we do not have to do any extra rounding here for
3129 // NoExcessFPPrecision, because we know the input will have the appropriate
3130 // precision, and these operations don't modify precision at all.
3131 break;
3132
Chris Lattnerda6ba872005-04-28 21:44:33 +00003133 case ISD::FSQRT:
3134 case ISD::FSIN:
3135 case ISD::FCOS:
3136 Tmp1 = PromoteOp(Node->getOperand(0));
3137 assert(Tmp1.getValueType() == NVT);
3138 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3139 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003140 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3141 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003142 break;
3143
Chris Lattner03c85462005-01-15 05:21:40 +00003144 case ISD::AND:
3145 case ISD::OR:
3146 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003147 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003148 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003149 case ISD::MUL:
3150 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003151 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003152 // that too is okay if they are integer operations.
3153 Tmp1 = PromoteOp(Node->getOperand(0));
3154 Tmp2 = PromoteOp(Node->getOperand(1));
3155 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3156 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003157 break;
3158 case ISD::FADD:
3159 case ISD::FSUB:
3160 case ISD::FMUL:
3161 // The input may have strange things in the top bits of the registers, but
3162 // these operations don't care.
3163 Tmp1 = PromoteOp(Node->getOperand(0));
3164 Tmp2 = PromoteOp(Node->getOperand(1));
3165 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3166 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3167
3168 // Floating point operations will give excess precision that we may not be
3169 // able to tolerate. If we DO allow excess precision, just leave it,
3170 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003171 // FIXME: Why would we need to round FP ops more than integer ones?
3172 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003173 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003174 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3175 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003176 break;
3177
Chris Lattner8b6fa222005-01-15 22:16:26 +00003178 case ISD::SDIV:
3179 case ISD::SREM:
3180 // These operators require that their input be sign extended.
3181 Tmp1 = PromoteOp(Node->getOperand(0));
3182 Tmp2 = PromoteOp(Node->getOperand(1));
3183 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003184 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3185 DAG.getValueType(VT));
3186 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3187 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003188 }
3189 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3190
3191 // Perform FP_ROUND: this is probably overly pessimistic.
3192 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003193 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3194 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003195 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003196 case ISD::FDIV:
3197 case ISD::FREM:
3198 // These operators require that their input be fp extended.
3199 Tmp1 = PromoteOp(Node->getOperand(0));
3200 Tmp2 = PromoteOp(Node->getOperand(1));
3201 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3202
3203 // Perform FP_ROUND: this is probably overly pessimistic.
3204 if (NoExcessFPPrecision)
3205 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3206 DAG.getValueType(VT));
3207 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003208
3209 case ISD::UDIV:
3210 case ISD::UREM:
3211 // These operators require that their input be zero extended.
3212 Tmp1 = PromoteOp(Node->getOperand(0));
3213 Tmp2 = PromoteOp(Node->getOperand(1));
3214 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003215 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3216 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003217 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3218 break;
3219
3220 case ISD::SHL:
3221 Tmp1 = PromoteOp(Node->getOperand(0));
3222 Tmp2 = LegalizeOp(Node->getOperand(1));
3223 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
3224 break;
3225 case ISD::SRA:
3226 // The input value must be properly sign extended.
3227 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003228 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3229 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003230 Tmp2 = LegalizeOp(Node->getOperand(1));
3231 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
3232 break;
3233 case ISD::SRL:
3234 // The input value must be properly zero extended.
3235 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003236 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003237 Tmp2 = LegalizeOp(Node->getOperand(1));
3238 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
3239 break;
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003240
Chris Lattner03c85462005-01-15 05:21:40 +00003241 case ISD::LOAD:
3242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3243 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00003244 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
3245 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003246 // Remember that we legalized the chain.
3247 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3248 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003249 case ISD::SEXTLOAD:
3250 case ISD::ZEXTLOAD:
3251 case ISD::EXTLOAD:
3252 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3253 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00003254 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
3255 Node->getOperand(2),
3256 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003257 // Remember that we legalized the chain.
3258 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3259 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003260 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00003261 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3262 case Expand: assert(0 && "It's impossible to expand bools");
3263 case Legal:
3264 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
3265 break;
3266 case Promote:
3267 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
3268 break;
3269 }
Chris Lattner03c85462005-01-15 05:21:40 +00003270 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3271 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
3272 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
3273 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003274 case ISD::SELECT_CC:
3275 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3276 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3277 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3278 Node->getOperand(1), Tmp2, Tmp3,
3279 Node->getOperand(4));
3280 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00003281 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00003282 case ISD::CALL: {
3283 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3284 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3285
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003286 std::vector<SDOperand> Ops;
3287 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
3288 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3289
Chris Lattner8ac532c2005-01-16 19:46:48 +00003290 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3291 "Can only promote single result calls");
3292 std::vector<MVT::ValueType> RetTyVTs;
3293 RetTyVTs.reserve(2);
3294 RetTyVTs.push_back(NVT);
3295 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003296 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
3297 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00003298 Result = SDOperand(NC, 0);
3299
3300 // Insert the new chain mapping.
3301 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3302 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003303 }
Nate Begemand88fc032006-01-14 03:14:10 +00003304 case ISD::BSWAP:
3305 Tmp1 = Node->getOperand(0);
3306 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3307 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3308 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3309 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3310 TLI.getShiftAmountTy()));
3311 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003312 case ISD::CTPOP:
3313 case ISD::CTTZ:
3314 case ISD::CTLZ:
3315 Tmp1 = Node->getOperand(0);
3316 //Zero extend the argument
3317 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3318 // Perform the larger operation, then subtract if needed.
3319 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3320 switch(Node->getOpcode())
3321 {
3322 case ISD::CTPOP:
3323 Result = Tmp1;
3324 break;
3325 case ISD::CTTZ:
3326 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003327 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003328 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003329 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003330 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
3331 break;
3332 case ISD::CTLZ:
3333 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003334 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3335 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003336 getSizeInBits(VT), NVT));
3337 break;
3338 }
3339 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003340 }
3341
3342 assert(Result.Val && "Didn't set a result!");
3343 AddPromotedOperand(Op, Result);
3344 return Result;
3345}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003346
Chris Lattner35481892005-12-23 00:16:34 +00003347/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003348/// The resultant code need not be legal. Note that SrcOp is the input operand
3349/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003350SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3351 SDOperand SrcOp) {
3352 // Create the stack frame object.
3353 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3354 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00003355 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00003356 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3357
3358 // Emit a store to the stack slot.
3359 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003360 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003361 // Result is a load from the stack slot.
3362 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3363}
3364
Chris Lattner84f67882005-01-20 18:52:28 +00003365/// ExpandAddSub - Find a clever way to expand this add operation into
3366/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00003367void SelectionDAGLegalize::
3368ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
3369 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00003370 // Expand the subcomponents.
3371 SDOperand LHSL, LHSH, RHSL, RHSH;
3372 ExpandOp(LHS, LHSL, LHSH);
3373 ExpandOp(RHS, RHSL, RHSH);
3374
Chris Lattner84f67882005-01-20 18:52:28 +00003375 std::vector<SDOperand> Ops;
3376 Ops.push_back(LHSL);
3377 Ops.push_back(LHSH);
3378 Ops.push_back(RHSL);
3379 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00003380 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3381 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00003382 Hi = Lo.getValue(1);
3383}
3384
Chris Lattner5b359c62005-04-02 04:00:59 +00003385void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3386 SDOperand Op, SDOperand Amt,
3387 SDOperand &Lo, SDOperand &Hi) {
3388 // Expand the subcomponents.
3389 SDOperand LHSL, LHSH;
3390 ExpandOp(Op, LHSL, LHSH);
3391
3392 std::vector<SDOperand> Ops;
3393 Ops.push_back(LHSL);
3394 Ops.push_back(LHSH);
3395 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003396 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003397 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003398 Hi = Lo.getValue(1);
3399}
3400
3401
Chris Lattnere34b3962005-01-19 04:19:40 +00003402/// ExpandShift - Try to find a clever way to expand this shift operation out to
3403/// smaller elements. If we can't find a way that is more efficient than a
3404/// libcall on this target, return false. Otherwise, return true with the
3405/// low-parts expanded into Lo and Hi.
3406bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3407 SDOperand &Lo, SDOperand &Hi) {
3408 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3409 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003410
Chris Lattnere34b3962005-01-19 04:19:40 +00003411 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003412 SDOperand ShAmt = LegalizeOp(Amt);
3413 MVT::ValueType ShTy = ShAmt.getValueType();
3414 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3415 unsigned NVTBits = MVT::getSizeInBits(NVT);
3416
3417 // Handle the case when Amt is an immediate. Other cases are currently broken
3418 // and are disabled.
3419 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3420 unsigned Cst = CN->getValue();
3421 // Expand the incoming operand to be shifted, so that we have its parts
3422 SDOperand InL, InH;
3423 ExpandOp(Op, InL, InH);
3424 switch(Opc) {
3425 case ISD::SHL:
3426 if (Cst > VTBits) {
3427 Lo = DAG.getConstant(0, NVT);
3428 Hi = DAG.getConstant(0, NVT);
3429 } else if (Cst > NVTBits) {
3430 Lo = DAG.getConstant(0, NVT);
3431 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003432 } else if (Cst == NVTBits) {
3433 Lo = DAG.getConstant(0, NVT);
3434 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003435 } else {
3436 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3437 Hi = DAG.getNode(ISD::OR, NVT,
3438 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3439 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3440 }
3441 return true;
3442 case ISD::SRL:
3443 if (Cst > VTBits) {
3444 Lo = DAG.getConstant(0, NVT);
3445 Hi = DAG.getConstant(0, NVT);
3446 } else if (Cst > NVTBits) {
3447 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3448 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003449 } else if (Cst == NVTBits) {
3450 Lo = InH;
3451 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003452 } else {
3453 Lo = DAG.getNode(ISD::OR, NVT,
3454 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3455 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3456 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3457 }
3458 return true;
3459 case ISD::SRA:
3460 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003461 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003462 DAG.getConstant(NVTBits-1, ShTy));
3463 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003464 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003465 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003466 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003467 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003468 } else if (Cst == NVTBits) {
3469 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003470 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003471 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003472 } else {
3473 Lo = DAG.getNode(ISD::OR, NVT,
3474 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3475 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3476 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3477 }
3478 return true;
3479 }
3480 }
3481 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
3482 // so disable it for now. Currently targets are handling this via SHL_PARTS
3483 // and friends.
3484 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003485
3486 // If we have an efficient select operation (or if the selects will all fold
3487 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003488 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00003489 return false;
3490
3491 SDOperand InL, InH;
3492 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00003493 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3494 DAG.getConstant(NVTBits, ShTy), ShAmt);
3495
Chris Lattnere5544f82005-01-20 20:29:23 +00003496 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003497 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3498 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00003499
Chris Lattnere34b3962005-01-19 04:19:40 +00003500 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3501 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3502 DAG.getConstant(NVTBits-1, ShTy));
3503 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3504 DAG.getConstant(NVTBits-1, ShTy));
3505 }
3506
3507 if (Opc == ISD::SHL) {
3508 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3509 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3510 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003511 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00003512
Chris Lattnere34b3962005-01-19 04:19:40 +00003513 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3514 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3515 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00003516 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003517 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3518 DAG.getConstant(32, ShTy),
3519 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00003520 DAG.getConstant(0, NVT),
3521 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00003522 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00003523 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00003524 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003525 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00003526
3527 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00003528 if (Opc == ISD::SRA)
3529 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3530 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00003531 else
3532 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00003533 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00003534 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00003535 }
3536 return true;
3537}
Chris Lattner77e77a62005-01-21 06:05:23 +00003538
Chris Lattner9530ddc2005-05-13 05:09:11 +00003539/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3540/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003541/// Found.
Chris Lattnerde387ce2006-01-09 23:21:49 +00003542static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
3543 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00003544 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattnerde387ce2006-01-09 23:21:49 +00003545 !Visited.insert(Node).second) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00003546
Chris Lattner16cd04d2005-05-12 23:24:06 +00003547 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003548 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003549 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003550 Found = Node;
3551 return;
3552 }
3553
3554 // Otherwise, scan the operands of Node to see if any of them is a call.
3555 assert(Node->getNumOperands() != 0 &&
3556 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00003557 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattnerde387ce2006-01-09 23:21:49 +00003558 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003559
3560 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003561 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattnerde387ce2006-01-09 23:21:49 +00003562 Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003563}
3564
3565
Chris Lattner9530ddc2005-05-13 05:09:11 +00003566/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3567/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003568/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00003569static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3570 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00003571 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner82299e72005-08-05 18:10:27 +00003572 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003573
Chris Lattner16cd04d2005-05-12 23:24:06 +00003574 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003575 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003576 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003577 Found = Node;
3578 return;
3579 }
3580
3581 // Otherwise, scan the operands of Node to see if any of them is a call.
3582 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3583 if (UI == E) return;
3584 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00003585 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003586
3587 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00003588 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003589}
3590
Chris Lattner9530ddc2005-05-13 05:09:11 +00003591/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003592/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003593static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00003594 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003595 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00003596 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00003597 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003598
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003599 // The chain is usually at the end.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003600 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003601 if (TheChain.getValueType() != MVT::Other) {
3602 // Sometimes it's at the beginning.
Chris Lattner2789bde2005-05-14 08:34:53 +00003603 TheChain = SDOperand(Node, 0);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003604 if (TheChain.getValueType() != MVT::Other) {
3605 // Otherwise, hunt for it.
3606 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
3607 if (Node->getValueType(i) == MVT::Other) {
3608 TheChain = SDOperand(Node, i);
3609 break;
3610 }
3611
3612 // Otherwise, we walked into a node without a chain.
3613 if (TheChain.getValueType() != MVT::Other)
3614 return 0;
3615 }
3616 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003617
3618 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003619 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003620
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003621 // Make sure to only follow users of our token chain.
3622 SDNode *User = *UI;
3623 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3624 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003625 if (SDNode *Result = FindCallSeqEnd(User))
3626 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003627 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003628 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003629}
3630
Chris Lattner9530ddc2005-05-13 05:09:11 +00003631/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003632/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003633static SDNode *FindCallSeqStart(SDNode *Node) {
3634 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003635 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003636
3637 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3638 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003639 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003640}
3641
3642
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003643/// FindInputOutputChains - If we are replacing an operation with a call we need
3644/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003645/// properly serialize the calls in the block. The returned operand is the
3646/// input chain value for the new call (e.g. the entry node or the previous
3647/// call), and OutChain is set to be the chain node to update to point to the
3648/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003649static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3650 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003651 SDNode *LatestCallSeqStart = Entry.Val;
3652 SDNode *LatestCallSeqEnd = 0;
Chris Lattnerde387ce2006-01-09 23:21:49 +00003653 std::set<SDNode*> Visited;
3654 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
3655 Visited.clear();
Chris Lattner9530ddc2005-05-13 05:09:11 +00003656 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003657
Chris Lattner16cd04d2005-05-12 23:24:06 +00003658 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003659 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003660 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3661 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003662 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003663 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003664 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3665 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003666 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003667 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003668 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003669
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003670 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003671 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003672 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003673 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattnerde387ce2006-01-09 23:21:49 +00003674 Visited.clear();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003675
Chris Lattner9530ddc2005-05-13 05:09:11 +00003676 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003677 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003678 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003679
Chris Lattner9530ddc2005-05-13 05:09:11 +00003680 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003681}
3682
Jeff Cohen00b168892005-07-27 06:12:32 +00003683/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003684void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3685 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003686 // Nothing to splice it into?
3687 if (OutChain == 0) return;
3688
3689 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3690 //OutChain->dump();
3691
3692 // Form a token factor node merging the old inval and the new inval.
3693 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3694 OutChain->getOperand(0));
3695 // Change the node to refer to the new token.
3696 OutChain->setAdjCallChain(InToken);
3697}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003698
3699
Chris Lattner77e77a62005-01-21 06:05:23 +00003700// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3701// does not fit into a register, return the lo part and set the hi part to the
3702// by-reg argument. If it does fit into a single register, return the result
3703// and leave the Hi part unset.
3704SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3705 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003706 SDNode *OutChain;
3707 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3708 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003709 if (InChain.Val == 0)
3710 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003711
Chris Lattner77e77a62005-01-21 06:05:23 +00003712 TargetLowering::ArgListTy Args;
3713 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3714 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3715 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3716 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3717 }
3718 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003719
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003720 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003721 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003722 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003723 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3724 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003725
Chris Lattner99c25b82005-09-02 20:26:58 +00003726 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003727 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003728 default: assert(0 && "Unknown thing");
3729 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00003730 Result = CallInfo.first;
3731 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003732 case Promote:
3733 assert(0 && "Cannot promote this yet!");
3734 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003735 ExpandOp(CallInfo.first, Result, Hi);
3736 CallInfo.second = LegalizeOp(CallInfo.second);
3737 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003738 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003739
3740 SpliceCallInto(CallInfo.second, OutChain);
3741 NeedsAnotherIteration = true;
3742 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003743}
3744
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003745
Chris Lattner77e77a62005-01-21 06:05:23 +00003746/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3747/// destination type is legal.
3748SDOperand SelectionDAGLegalize::
3749ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003750 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003751 assert(getTypeAction(Source.getValueType()) == Expand &&
3752 "This is not an expansion!");
3753 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3754
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003755 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003756 assert(Source.getValueType() == MVT::i64 &&
3757 "This only works for 64-bit -> FP");
3758 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3759 // incoming integer is set. To handle this, we dynamically test to see if
3760 // it is set, and, if so, add a fudge factor.
3761 SDOperand Lo, Hi;
3762 ExpandOp(Source, Lo, Hi);
3763
Chris Lattner66de05b2005-05-13 04:45:13 +00003764 // If this is unsigned, and not supported, first perform the conversion to
3765 // signed, then adjust the result if the sign bit is set.
3766 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3767 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3768
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003769 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3770 DAG.getConstant(0, Hi.getValueType()),
3771 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003772 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3773 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3774 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003775 uint64_t FF = 0x5f800000ULL;
3776 if (TLI.isLittleEndian()) FF <<= 32;
3777 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003778
Chris Lattner5839bf22005-08-26 17:15:30 +00003779 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003780 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3781 SDOperand FudgeInReg;
3782 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003783 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3784 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003785 else {
3786 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003787 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3788 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003789 }
Chris Lattner473a9902005-09-29 06:44:39 +00003790 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003791 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003792
Chris Lattnera88a2602005-05-14 05:33:54 +00003793 // Check to see if the target has a custom way to lower this. If so, use it.
3794 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3795 default: assert(0 && "This action not implemented for this operation!");
3796 case TargetLowering::Legal:
3797 case TargetLowering::Expand:
3798 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003799 case TargetLowering::Custom: {
3800 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3801 Source), DAG);
3802 if (NV.Val)
3803 return LegalizeOp(NV);
3804 break; // The target decided this was legal after all
3805 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003806 }
3807
Chris Lattner13689e22005-05-12 07:00:44 +00003808 // Expand the source, then glue it back together for the call. We must expand
3809 // the source in case it is shared (this pass of legalize must traverse it).
3810 SDOperand SrcLo, SrcHi;
3811 ExpandOp(Source, SrcLo, SrcHi);
3812 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3813
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003814 SDNode *OutChain = 0;
3815 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3816 DAG.getEntryNode());
3817 const char *FnName = 0;
3818 if (DestTy == MVT::f32)
3819 FnName = "__floatdisf";
3820 else {
3821 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3822 FnName = "__floatdidf";
3823 }
3824
Chris Lattner77e77a62005-01-21 06:05:23 +00003825 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3826
3827 TargetLowering::ArgListTy Args;
3828 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003829
Chris Lattner77e77a62005-01-21 06:05:23 +00003830 Args.push_back(std::make_pair(Source, ArgTy));
3831
3832 // We don't care about token chains for libcalls. We just use the entry
3833 // node as our input and ignore the output chain. This allows us to place
3834 // calls wherever we need them to satisfy data dependences.
3835 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003836
3837 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003838 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3839 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003840
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003841 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003842 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003843}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003844
Chris Lattnere34b3962005-01-19 04:19:40 +00003845
3846
Chris Lattner3e928bb2005-01-07 07:47:09 +00003847/// ExpandOp - Expand the specified SDOperand into its two component pieces
3848/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3849/// LegalizeNodes map is filled in for any results that are not expanded, the
3850/// ExpandedNodes map is filled in for any results that are expanded, and the
3851/// Lo/Hi values are returned.
3852void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3853 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003854 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003855 SDNode *Node = Op.Val;
3856 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003857 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3858 "Cannot expand FP values!");
3859 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003860 "Cannot expand to FP value or to larger int value!");
3861
Chris Lattner6fdcb252005-09-02 20:32:45 +00003862 // See if we already expanded it.
3863 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3864 = ExpandedNodes.find(Op);
3865 if (I != ExpandedNodes.end()) {
3866 Lo = I->second.first;
3867 Hi = I->second.second;
3868 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003869 }
3870
Chris Lattner4e6c7462005-01-08 19:27:05 +00003871 // Expanding to multiple registers needs to perform an optimization step, and
3872 // is not careful to avoid operations the target does not support. Make sure
3873 // that all generated operations are legalized in the next iteration.
3874 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003875
Chris Lattner3e928bb2005-01-07 07:47:09 +00003876 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003877 case ISD::CopyFromReg:
3878 assert(0 && "CopyFromReg must be legal!");
3879 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003880 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3881 assert(0 && "Do not know how to expand this operator!");
3882 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003883 case ISD::UNDEF:
3884 Lo = DAG.getNode(ISD::UNDEF, NVT);
3885 Hi = DAG.getNode(ISD::UNDEF, NVT);
3886 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003887 case ISD::Constant: {
3888 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3889 Lo = DAG.getConstant(Cst, NVT);
3890 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3891 break;
3892 }
Nate Begemancc827e62005-12-07 19:48:11 +00003893 case ISD::ConstantVec: {
3894 unsigned NumElements = Node->getNumOperands();
3895 // If we only have two elements left in the constant vector, just break it
3896 // apart into the two scalar constants it contains. Otherwise, bisect the
3897 // ConstantVec, and return each half as a new ConstantVec.
3898 // FIXME: this is hard coded as big endian, it may have to change to support
3899 // SSE and Alpha MVI
3900 if (NumElements == 2) {
3901 Hi = Node->getOperand(0);
3902 Lo = Node->getOperand(1);
3903 } else {
3904 NumElements /= 2;
3905 std::vector<SDOperand> LoOps, HiOps;
3906 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3907 HiOps.push_back(Node->getOperand(I));
3908 LoOps.push_back(Node->getOperand(I+NumElements));
3909 }
3910 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3911 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3912 }
3913 break;
3914 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003915
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003916 case ISD::BUILD_PAIR:
3917 // Legalize both operands. FIXME: in the future we should handle the case
3918 // where the two elements are not legal.
3919 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3920 Lo = LegalizeOp(Node->getOperand(0));
3921 Hi = LegalizeOp(Node->getOperand(1));
3922 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003923
3924 case ISD::SIGN_EXTEND_INREG:
3925 ExpandOp(Node->getOperand(0), Lo, Hi);
3926 // Sign extend the lo-part.
3927 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3928 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3929 TLI.getShiftAmountTy()));
3930 // sext_inreg the low part if needed.
3931 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3932 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003933
Nate Begemand88fc032006-01-14 03:14:10 +00003934 case ISD::BSWAP: {
3935 ExpandOp(Node->getOperand(0), Lo, Hi);
3936 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3937 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3938 Lo = TempLo;
3939 break;
3940 }
3941
Chris Lattneredb1add2005-05-11 04:51:16 +00003942 case ISD::CTPOP:
3943 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003944 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3945 DAG.getNode(ISD::CTPOP, NVT, Lo),
3946 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003947 Hi = DAG.getConstant(0, NVT);
3948 break;
3949
Chris Lattner39a8f332005-05-12 19:05:01 +00003950 case ISD::CTLZ: {
3951 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003952 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003953 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3954 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003955 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3956 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003957 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3958 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3959
3960 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3961 Hi = DAG.getConstant(0, NVT);
3962 break;
3963 }
3964
3965 case ISD::CTTZ: {
3966 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003967 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003968 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3969 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003970 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3971 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003972 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3973 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3974
3975 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3976 Hi = DAG.getConstant(0, NVT);
3977 break;
3978 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003979
Nate Begemanacc398c2006-01-25 18:21:52 +00003980 case ISD::VAARG: {
3981 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3982 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3983 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3984 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3985
3986 // Remember that we legalized the chain.
3987 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3988 if (!TLI.isLittleEndian())
3989 std::swap(Lo, Hi);
3990 break;
3991 }
3992
Chris Lattner3e928bb2005-01-07 07:47:09 +00003993 case ISD::LOAD: {
3994 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3995 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003996 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003997
3998 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003999 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004000 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4001 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00004002 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00004003 //are from the same instruction?
4004 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00004005
4006 // Build a factor node to remember that this load is independent of the
4007 // other one.
4008 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4009 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004010
Chris Lattner3e928bb2005-01-07 07:47:09 +00004011 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00004012 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004013 if (!TLI.isLittleEndian())
4014 std::swap(Lo, Hi);
4015 break;
4016 }
Nate Begemanab48be32005-11-22 18:16:00 +00004017 case ISD::VLOAD: {
4018 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4019 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
4020 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
4021 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4022
4023 // If we only have two elements, turn into a pair of scalar loads.
4024 // FIXME: handle case where a vector of two elements is fine, such as
4025 // 2 x double on SSE2.
4026 if (NumElements == 2) {
4027 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
4028 // Increment the pointer to the other half.
4029 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
4030 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4031 getIntPtrConstant(IncrementSize));
4032 //Is this safe? declaring that the two parts of the split load
4033 //are from the same instruction?
4034 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
4035 } else {
4036 NumElements /= 2; // Split the vector in half
4037 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
4038 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
4039 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4040 getIntPtrConstant(IncrementSize));
4041 //Is this safe? declaring that the two parts of the split load
4042 //are from the same instruction?
4043 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
4044 }
4045
4046 // Build a factor node to remember that this load is independent of the
4047 // other one.
4048 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4049 Hi.getValue(1));
4050
4051 // Remember that we legalized the chain.
4052 AddLegalizedOperand(Op.getValue(1), TF);
4053 if (!TLI.isLittleEndian())
4054 std::swap(Lo, Hi);
4055 break;
4056 }
4057 case ISD::VADD:
4058 case ISD::VSUB:
4059 case ISD::VMUL: {
4060 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
4061 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4062 SDOperand LL, LH, RL, RH;
4063
4064 ExpandOp(Node->getOperand(0), LL, LH);
4065 ExpandOp(Node->getOperand(1), RL, RH);
4066
4067 // If we only have two elements, turn into a pair of scalar loads.
4068 // FIXME: handle case where a vector of two elements is fine, such as
4069 // 2 x double on SSE2.
4070 if (NumElements == 2) {
4071 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
4072 Lo = DAG.getNode(Opc, EVT, LL, RL);
4073 Hi = DAG.getNode(Opc, EVT, LH, RH);
4074 } else {
4075 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
4076 LL.getOperand(3));
4077 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
4078 LH.getOperand(3));
4079 }
4080 break;
4081 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00004082 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004083 case ISD::CALL: {
4084 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4085 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
4086
Chris Lattner3d9dffc2005-01-19 20:24:35 +00004087 bool Changed = false;
4088 std::vector<SDOperand> Ops;
4089 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
4090 Ops.push_back(LegalizeOp(Node->getOperand(i)));
4091 Changed |= Ops.back() != Node->getOperand(i);
4092 }
4093
Chris Lattner3e928bb2005-01-07 07:47:09 +00004094 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
4095 "Can only expand a call once so far, not i64 -> i16!");
4096
4097 std::vector<MVT::ValueType> RetTyVTs;
4098 RetTyVTs.reserve(3);
4099 RetTyVTs.push_back(NVT);
4100 RetTyVTs.push_back(NVT);
4101 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00004102 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
4103 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004104 Lo = SDOperand(NC, 0);
4105 Hi = SDOperand(NC, 1);
4106
4107 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00004108 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004109 break;
4110 }
4111 case ISD::AND:
4112 case ISD::OR:
4113 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4114 SDOperand LL, LH, RL, RH;
4115 ExpandOp(Node->getOperand(0), LL, LH);
4116 ExpandOp(Node->getOperand(1), RL, RH);
4117 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4118 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4119 break;
4120 }
4121 case ISD::SELECT: {
4122 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00004123
4124 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4125 case Expand: assert(0 && "It's impossible to expand bools");
4126 case Legal:
4127 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
4128 break;
4129 case Promote:
4130 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
4131 break;
4132 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004133 ExpandOp(Node->getOperand(1), LL, LH);
4134 ExpandOp(Node->getOperand(2), RL, RH);
4135 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
4136 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
4137 break;
4138 }
Nate Begeman9373a812005-08-10 20:51:12 +00004139 case ISD::SELECT_CC: {
4140 SDOperand TL, TH, FL, FH;
4141 ExpandOp(Node->getOperand(2), TL, TH);
4142 ExpandOp(Node->getOperand(3), FL, FH);
4143 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4144 Node->getOperand(1), TL, FL, Node->getOperand(4));
4145 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4146 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00004147 Lo = LegalizeOp(Lo);
4148 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00004149 break;
4150 }
Nate Begeman144ff662005-10-13 17:15:37 +00004151 case ISD::SEXTLOAD: {
4152 SDOperand Chain = LegalizeOp(Node->getOperand(0));
4153 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
4154 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4155
4156 if (EVT == NVT)
4157 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4158 else
4159 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4160 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004161
4162 // Remember that we legalized the chain.
4163 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
4164
Nate Begeman144ff662005-10-13 17:15:37 +00004165 // The high part is obtained by SRA'ing all but one of the bits of the lo
4166 // part.
4167 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4168 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4169 TLI.getShiftAmountTy()));
4170 Lo = LegalizeOp(Lo);
4171 Hi = LegalizeOp(Hi);
4172 break;
4173 }
4174 case ISD::ZEXTLOAD: {
4175 SDOperand Chain = LegalizeOp(Node->getOperand(0));
4176 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
4177 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4178
4179 if (EVT == NVT)
4180 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4181 else
4182 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4183 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004184
4185 // Remember that we legalized the chain.
4186 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
4187
Nate Begeman144ff662005-10-13 17:15:37 +00004188 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00004189 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00004190 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00004191 break;
4192 }
4193 case ISD::EXTLOAD: {
4194 SDOperand Chain = LegalizeOp(Node->getOperand(0));
4195 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
4196 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4197
4198 if (EVT == NVT)
4199 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4200 else
4201 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4202 EVT);
4203
4204 // Remember that we legalized the chain.
4205 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
4206
4207 // The high part is undefined.
4208 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
4209 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00004210 break;
4211 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004212 case ISD::ANY_EXTEND: {
4213 SDOperand In;
4214 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4215 case Expand: assert(0 && "expand-expand not implemented yet!");
4216 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4217 case Promote:
4218 In = PromoteOp(Node->getOperand(0));
4219 break;
4220 }
4221
4222 // The low part is any extension of the input (which degenerates to a copy).
4223 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
4224 // The high part is undefined.
4225 Hi = DAG.getNode(ISD::UNDEF, NVT);
4226 break;
4227 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004228 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00004229 SDOperand In;
4230 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4231 case Expand: assert(0 && "expand-expand not implemented yet!");
4232 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4233 case Promote:
4234 In = PromoteOp(Node->getOperand(0));
4235 // Emit the appropriate sign_extend_inreg to get the value we want.
4236 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00004237 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00004238 break;
4239 }
4240
Chris Lattner3e928bb2005-01-07 07:47:09 +00004241 // The low part is just a sign extension of the input (which degenerates to
4242 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00004243 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004244
Chris Lattner3e928bb2005-01-07 07:47:09 +00004245 // The high part is obtained by SRA'ing all but one of the bits of the lo
4246 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004247 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00004248 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4249 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004250 break;
4251 }
Chris Lattner06098e02005-04-03 23:41:52 +00004252 case ISD::ZERO_EXTEND: {
4253 SDOperand In;
4254 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4255 case Expand: assert(0 && "expand-expand not implemented yet!");
4256 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4257 case Promote:
4258 In = PromoteOp(Node->getOperand(0));
4259 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00004260 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00004261 break;
4262 }
4263
Chris Lattner3e928bb2005-01-07 07:47:09 +00004264 // The low part is just a zero extension of the input (which degenerates to
4265 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00004266 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004267
Chris Lattner3e928bb2005-01-07 07:47:09 +00004268 // The high part is just a zero.
4269 Hi = DAG.getConstant(0, NVT);
4270 break;
Chris Lattner06098e02005-04-03 23:41:52 +00004271 }
Chris Lattner35481892005-12-23 00:16:34 +00004272
4273 case ISD::BIT_CONVERT: {
4274 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4275 Node->getOperand(0));
4276 ExpandOp(Tmp, Lo, Hi);
4277 break;
4278 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004279
Chris Lattner308575b2005-11-20 22:56:56 +00004280 case ISD::READCYCLECOUNTER: {
4281 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4282 TargetLowering::Custom &&
4283 "Must custom expand ReadCycleCounter");
4284 SDOperand T = TLI.LowerOperation(Op, DAG);
4285 assert(T.Val && "Node must be custom expanded!");
4286 Lo = LegalizeOp(T.getValue(0));
4287 Hi = LegalizeOp(T.getValue(1));
4288 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
4289 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004290 break;
Chris Lattner308575b2005-11-20 22:56:56 +00004291 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004292
Chris Lattner4e6c7462005-01-08 19:27:05 +00004293 // These operators cannot be expanded directly, emit them as calls to
4294 // library functions.
4295 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004296 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004297 SDOperand Op;
4298 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4299 case Expand: assert(0 && "cannot expand FP!");
4300 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4301 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
4302 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004303
Chris Lattnerf20d1832005-07-30 01:40:57 +00004304 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4305
Chris Lattner80a3e942005-07-29 00:33:32 +00004306 // Now that the custom expander is done, expand the result, which is still
4307 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004308 if (Op.Val) {
4309 ExpandOp(Op, Lo, Hi);
4310 break;
4311 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004312 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004313
Chris Lattner4e6c7462005-01-08 19:27:05 +00004314 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004315 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004316 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004317 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004318 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004319
Chris Lattner4e6c7462005-01-08 19:27:05 +00004320 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004321 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
4322 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
4323 LegalizeOp(Node->getOperand(0)));
4324 // Now that the custom expander is done, expand the result, which is still
4325 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004326 Op = TLI.LowerOperation(Op, DAG);
4327 if (Op.Val) {
4328 ExpandOp(Op, Lo, Hi);
4329 break;
4330 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004331 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004332
Chris Lattner4e6c7462005-01-08 19:27:05 +00004333 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004334 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004335 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004336 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004337 break;
4338
Evan Cheng05a2d562006-01-09 18:31:59 +00004339 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004340 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004341 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004342 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
4343 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
Chris Lattner348e93c2006-01-21 04:27:00 +00004344 ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004345 Op = TLI.LowerOperation(Op, DAG);
4346 if (Op.Val) {
4347 // Now that the custom expander is done, expand the result, which is
4348 // still VT.
4349 ExpandOp(Op, Lo, Hi);
4350 break;
4351 }
4352 }
4353
Chris Lattnere34b3962005-01-19 04:19:40 +00004354 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004355 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004356 break;
Chris Lattner47599822005-04-02 03:38:53 +00004357
4358 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004359 TargetLowering::LegalizeAction Action =
4360 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4361 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4362 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004363 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004364 break;
4365 }
4366
Chris Lattnere34b3962005-01-19 04:19:40 +00004367 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004368 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004369 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004370 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004371
Evan Cheng05a2d562006-01-09 18:31:59 +00004372 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004373 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004374 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004375 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
4376 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
Chris Lattner348e93c2006-01-21 04:27:00 +00004377 ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004378 Op = TLI.LowerOperation(Op, DAG);
4379 if (Op.Val) {
4380 // Now that the custom expander is done, expand the result, which is
4381 // still VT.
4382 ExpandOp(Op, Lo, Hi);
4383 break;
4384 }
4385 }
4386
Chris Lattnere34b3962005-01-19 04:19:40 +00004387 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004388 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004389 break;
Chris Lattner47599822005-04-02 03:38:53 +00004390
4391 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004392 TargetLowering::LegalizeAction Action =
4393 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4394 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4395 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004396 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004397 break;
4398 }
4399
Chris Lattnere34b3962005-01-19 04:19:40 +00004400 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004401 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004402 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004403 }
4404
4405 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004406 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004407 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004408 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
4409 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
Chris Lattner348e93c2006-01-21 04:27:00 +00004410 ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004411 Op = TLI.LowerOperation(Op, DAG);
4412 if (Op.Val) {
4413 // Now that the custom expander is done, expand the result, which is
4414 // still VT.
4415 ExpandOp(Op, Lo, Hi);
4416 break;
4417 }
4418 }
4419
Chris Lattnere34b3962005-01-19 04:19:40 +00004420 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004421 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004422 break;
Chris Lattner47599822005-04-02 03:38:53 +00004423
4424 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004425 TargetLowering::LegalizeAction Action =
4426 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4427 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4428 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004429 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004430 break;
4431 }
4432
Chris Lattnere34b3962005-01-19 04:19:40 +00004433 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004434 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004435 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004436 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004437
Misha Brukmanedf128a2005-04-21 22:36:52 +00004438 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00004439 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
4440 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00004441 break;
4442 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00004443 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
4444 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00004445 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00004446 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004447 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004448 SDOperand LL, LH, RL, RH;
4449 ExpandOp(Node->getOperand(0), LL, LH);
4450 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004451 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4452 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4453 // extended the sign bit of the low half through the upper half, and if so
4454 // emit a MULHS instead of the alternate sequence that is valid for any
4455 // i64 x i64 multiply.
4456 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4457 // is RH an extension of the sign bit of RL?
4458 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4459 RH.getOperand(1).getOpcode() == ISD::Constant &&
4460 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4461 // is LH an extension of the sign bit of LL?
4462 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4463 LH.getOperand(1).getOpcode() == ISD::Constant &&
4464 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4465 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4466 } else {
4467 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4468 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4469 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4470 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4471 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4472 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004473 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4474 } else {
4475 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
4476 }
4477 break;
4478 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004479 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4480 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4481 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4482 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004483 }
4484
Chris Lattner83397362005-12-21 18:02:52 +00004485 // Make sure the resultant values have been legalized themselves, unless this
4486 // is a type that requires multi-step expansion.
4487 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4488 Lo = LegalizeOp(Lo);
4489 Hi = LegalizeOp(Hi);
4490 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004491
4492 // Remember in a map if the values will be reused later.
4493 bool isNew =
4494 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4495 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004496}
4497
4498
4499// SelectionDAG::Legalize - This is the entry point for the file.
4500//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004501void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004502 /// run - This is the main entry point to this class.
4503 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004504 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004505}
4506