blob: e5dd538eb45986e312cba671df14d40ba2ce6b3f [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 Lattnerd71c0412005-05-13 18:43:43 +0000884 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000885 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000886 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
887 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000888
889 bool Changed = false;
890 std::vector<SDOperand> Ops;
891 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
892 Ops.push_back(LegalizeOp(Node->getOperand(i)));
893 Changed |= Ops.back() != Node->getOperand(i);
894 }
895
896 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000897 std::vector<MVT::ValueType> RetTyVTs;
898 RetTyVTs.reserve(Node->getNumValues());
899 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000900 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000901 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
902 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000903 } else {
904 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000905 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000906 // Since calls produce multiple values, make sure to remember that we
907 // legalized all of them.
908 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
909 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
910 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000911 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000912 case ISD::BR:
913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
914 if (Tmp1 != Node->getOperand(0))
915 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
916 break;
917
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000918 case ISD::BRCOND:
919 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000920
Chris Lattner47e92232005-01-18 19:27:06 +0000921 switch (getTypeAction(Node->getOperand(1).getValueType())) {
922 case Expand: assert(0 && "It's impossible to expand bools");
923 case Legal:
924 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
925 break;
926 case Promote:
927 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
928 break;
929 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000930
931 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
932 default: assert(0 && "This action is not supported yet!");
933 case TargetLowering::Expand:
934 // Expand brcond's setcc into its constituent parts and create a BR_CC
935 // Node.
936 if (Tmp2.getOpcode() == ISD::SETCC) {
937 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
938 Tmp2.getOperand(0), Tmp2.getOperand(1),
939 Node->getOperand(2));
940 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000941 // Make sure the condition is either zero or one. It may have been
942 // promoted from something else.
943 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
944
Nate Begeman7cbd5252005-08-16 19:49:35 +0000945 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
946 DAG.getCondCode(ISD::SETNE), Tmp2,
947 DAG.getConstant(0, Tmp2.getValueType()),
948 Node->getOperand(2));
949 }
Chris Lattnere7736732005-12-18 23:54:29 +0000950 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000951 break;
Evan Cheng898101c2005-12-19 23:12:38 +0000952 case TargetLowering::Custom: {
953 SDOperand Tmp =
954 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
955 Tmp1, Tmp2, Node->getOperand(2)), DAG);
956 if (Tmp.Val) {
957 Result = LegalizeOp(Tmp);
958 break;
959 }
960 // FALLTHROUGH if the target thinks it is legal.
961 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000962 case TargetLowering::Legal:
963 // Basic block destination (Op#2) is always legal.
964 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
965 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
966 Node->getOperand(2));
967 break;
968 }
969 break;
970 case ISD::BR_CC:
971 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000972 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000973 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
974 Node->getOperand(2), // LHS
975 Node->getOperand(3), // RHS
976 Node->getOperand(1)));
977 // If we get a SETCC back from legalizing the SETCC node we just
978 // created, then use its LHS, RHS, and CC directly in creating a new
979 // node. Otherwise, select between the true and false value based on
980 // comparing the result of the legalized with zero.
981 if (Tmp2.getOpcode() == ISD::SETCC) {
982 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
983 Tmp2.getOperand(0), Tmp2.getOperand(1),
984 Node->getOperand(4));
985 } else {
986 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
987 DAG.getCondCode(ISD::SETNE),
988 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
989 Node->getOperand(4));
990 }
Chris Lattner181b7a32005-12-17 23:46:46 +0000991 break;
992 }
993
994 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
995 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
996
997 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
998 default: assert(0 && "Unexpected action for BR_CC!");
999 case TargetLowering::Custom: {
1000 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
1001 Tmp2, Tmp3, Node->getOperand(4));
1002 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
1003 if (Tmp4.Val) {
1004 Result = LegalizeOp(Tmp4);
1005 break;
1006 }
1007 } // FALLTHROUGH if the target doesn't want to lower this op after all.
1008 case TargetLowering::Legal:
1009 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1010 Tmp3 != Node->getOperand(3)) {
1011 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
1012 Tmp2, Tmp3, Node->getOperand(4));
1013 }
1014 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001015 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001016 break;
Chris Lattner411e8882005-04-09 03:30:19 +00001017 case ISD::BRCONDTWOWAY:
1018 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1019 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1020 case Expand: assert(0 && "It's impossible to expand bools");
1021 case Legal:
1022 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1023 break;
1024 case Promote:
1025 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1026 break;
1027 }
1028 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
1029 // pair.
1030 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
1031 case TargetLowering::Promote:
1032 default: assert(0 && "This action is not supported yet!");
1033 case TargetLowering::Legal:
1034 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1035 std::vector<SDOperand> Ops;
1036 Ops.push_back(Tmp1);
1037 Ops.push_back(Tmp2);
1038 Ops.push_back(Node->getOperand(2));
1039 Ops.push_back(Node->getOperand(3));
1040 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
1041 }
1042 break;
1043 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +00001044 // If BRTWOWAY_CC is legal for this target, then simply expand this node
1045 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
1046 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001047 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001048 if (Tmp2.getOpcode() == ISD::SETCC) {
1049 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1050 Tmp2.getOperand(0), Tmp2.getOperand(1),
1051 Node->getOperand(2), Node->getOperand(3));
1052 } else {
1053 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1054 DAG.getConstant(0, Tmp2.getValueType()),
1055 Node->getOperand(2), Node->getOperand(3));
1056 }
1057 } else {
1058 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +00001059 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001060 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1061 }
Chris Lattnere7736732005-12-18 23:54:29 +00001062 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner411e8882005-04-09 03:30:19 +00001063 break;
1064 }
1065 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001066 case ISD::BRTWOWAY_CC:
1067 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001068 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001069 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1070 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1071 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1072 Tmp3 != Node->getOperand(3)) {
1073 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1074 Node->getOperand(4), Node->getOperand(5));
1075 }
1076 break;
1077 } else {
1078 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1079 Node->getOperand(2), // LHS
1080 Node->getOperand(3), // RHS
1081 Node->getOperand(1)));
1082 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1083 // pair.
1084 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1085 default: assert(0 && "This action is not supported yet!");
1086 case TargetLowering::Legal:
1087 // If we get a SETCC back from legalizing the SETCC node we just
1088 // created, then use its LHS, RHS, and CC directly in creating a new
1089 // node. Otherwise, select between the true and false value based on
1090 // comparing the result of the legalized with zero.
1091 if (Tmp2.getOpcode() == ISD::SETCC) {
1092 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1093 Tmp2.getOperand(0), Tmp2.getOperand(1),
1094 Node->getOperand(4), Node->getOperand(5));
1095 } else {
1096 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1097 DAG.getConstant(0, Tmp2.getValueType()),
1098 Node->getOperand(4), Node->getOperand(5));
1099 }
1100 break;
1101 case TargetLowering::Expand:
1102 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1103 Node->getOperand(4));
1104 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1105 break;
1106 }
Chris Lattnerf9dee6a2005-12-21 19:40:42 +00001107 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001108 }
1109 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001110 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001113
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001114 MVT::ValueType VT = Node->getValueType(0);
1115 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1116 default: assert(0 && "This action is not supported yet!");
1117 case TargetLowering::Custom: {
1118 SDOperand Op = DAG.getLoad(Node->getValueType(0),
1119 Tmp1, Tmp2, Node->getOperand(2));
1120 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1121 if (Tmp.Val) {
1122 Result = LegalizeOp(Tmp);
1123 // Since loads produce two values, make sure to remember that we legalized
1124 // both of them.
1125 AddLegalizedOperand(SDOperand(Node, 0), Result);
1126 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1127 return Result.getValue(Op.ResNo);
1128 }
1129 // FALLTHROUGH if the target thinks it is legal.
1130 }
1131 case TargetLowering::Legal:
1132 if (Tmp1 != Node->getOperand(0) ||
1133 Tmp2 != Node->getOperand(1))
1134 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1135 Node->getOperand(2));
1136 else
1137 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001138
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001139 // Since loads produce two values, make sure to remember that we legalized
1140 // both of them.
1141 AddLegalizedOperand(SDOperand(Node, 0), Result);
1142 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1143 return Result.getValue(Op.ResNo);
1144 }
1145 assert(0 && "Unreachable");
1146 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001147 case ISD::EXTLOAD:
1148 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001149 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1151 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001152
Chris Lattner5f056bf2005-07-10 01:55:33 +00001153 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001154 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001155 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001156 case TargetLowering::Promote:
1157 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001158 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1159 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001160 // Since loads produce two values, make sure to remember that we legalized
1161 // both of them.
1162 AddLegalizedOperand(SDOperand(Node, 0), Result);
1163 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1164 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001165
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001166 case TargetLowering::Custom: {
1167 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1168 Tmp1, Tmp2, Node->getOperand(2),
1169 SrcVT);
1170 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1171 if (Tmp.Val) {
1172 Result = LegalizeOp(Tmp);
1173 // Since loads produce two values, make sure to remember that we legalized
1174 // both of them.
1175 AddLegalizedOperand(SDOperand(Node, 0), Result);
1176 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1177 return Result.getValue(Op.ResNo);
1178 }
1179 // FALLTHROUGH if the target thinks it is legal.
1180 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001181 case TargetLowering::Legal:
1182 if (Tmp1 != Node->getOperand(0) ||
1183 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001184 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1185 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001186 else
1187 Result = SDOperand(Node, 0);
1188
1189 // Since loads produce two values, make sure to remember that we legalized
1190 // both of them.
1191 AddLegalizedOperand(SDOperand(Node, 0), Result);
1192 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1193 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +00001194 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001195 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001196 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1197 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001198 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001199 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001200 Load = LegalizeOp(Load);
1201 AddLegalizedOperand(SDOperand(Node, 0), Result);
1202 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001203 if (Op.ResNo)
1204 return Load.getValue(1);
1205 return Result;
1206 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001207 assert(Node->getOpcode() != ISD::EXTLOAD &&
1208 "EXTLOAD should always be supported!");
1209 // Turn the unsupported load into an EXTLOAD followed by an explicit
1210 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001211 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1212 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001213 SDOperand ValRes;
1214 if (Node->getOpcode() == ISD::SEXTLOAD)
1215 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001216 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001217 else
1218 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001219 Result = LegalizeOp(Result); // Relegalize new nodes.
1220 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001221 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1222 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner01ff7212005-04-10 22:54:25 +00001223 if (Op.ResNo)
1224 return Result.getValue(1);
1225 return ValRes;
1226 }
1227 assert(0 && "Unreachable");
1228 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001229 case ISD::EXTRACT_ELEMENT: {
1230 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1231 switch (getTypeAction(OpTy)) {
1232 default:
1233 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1234 break;
1235 case Legal:
1236 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1237 // 1 -> Hi
1238 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1239 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1240 TLI.getShiftAmountTy()));
1241 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1242 } else {
1243 // 0 -> Lo
1244 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1245 Node->getOperand(0));
1246 }
1247 Result = LegalizeOp(Result);
1248 break;
1249 case Expand:
1250 // Get both the low and high parts.
1251 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1252 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1253 Result = Tmp2; // 1 -> Hi
1254 else
1255 Result = Tmp1; // 0 -> Lo
1256 break;
1257 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001258 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001259 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001260
1261 case ISD::CopyToReg:
1262 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001263
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001264 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001265 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001266 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001267 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001268 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001269 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1270 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1271 Node->getOperand(1), Tmp2);
1272 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001273 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1274 if (Node->getNumOperands() == 4)
1275 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001276 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001277 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001278 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1279 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1280 }
1281
1282 // Since this produces two values, make sure to remember that we legalized
1283 // both of them.
1284 AddLegalizedOperand(SDOperand(Node, 0), Result);
1285 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1286 return Result.getValue(Op.ResNo);
1287 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001288 break;
1289
1290 case ISD::RET:
1291 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1292 switch (Node->getNumOperands()) {
1293 case 2: // ret val
1294 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1295 case Legal:
1296 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001297 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001298 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1299 break;
1300 case Expand: {
1301 SDOperand Lo, Hi;
1302 ExpandOp(Node->getOperand(1), Lo, Hi);
1303 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001304 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001305 }
1306 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001307 Tmp2 = PromoteOp(Node->getOperand(1));
1308 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1309 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001310 }
1311 break;
1312 case 1: // ret void
1313 if (Tmp1 != Node->getOperand(0))
1314 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1315 break;
1316 default: { // ret <values>
1317 std::vector<SDOperand> NewValues;
1318 NewValues.push_back(Tmp1);
1319 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1320 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1321 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001322 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001323 break;
1324 case Expand: {
1325 SDOperand Lo, Hi;
1326 ExpandOp(Node->getOperand(i), Lo, Hi);
1327 NewValues.push_back(Lo);
1328 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001329 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001330 }
1331 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001332 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001333 }
1334 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1335 break;
1336 }
1337 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001338
Chris Lattner47f5bea2006-01-06 05:47:48 +00001339 switch (TLI.getOperationAction(Node->getOpcode(),
1340 Node->getValueType(0))) {
Evan Cheng17c428e2006-01-06 00:41:43 +00001341 default: assert(0 && "This action is not supported yet!");
1342 case TargetLowering::Custom: {
1343 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1344 if (Tmp.Val) {
1345 Result = LegalizeOp(Tmp);
1346 break;
1347 }
1348 // FALLTHROUGH if the target thinks it is legal.
1349 }
1350 case TargetLowering::Legal:
1351 // Nothing to do.
1352 break;
1353 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001354 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001355 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1357 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1358
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001359 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001360 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001361 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001362 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001363 DAG.getConstant(FloatToBits(CFP->getValue()),
1364 MVT::i32),
1365 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001366 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001367 } else {
1368 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001369 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001370 DAG.getConstant(DoubleToBits(CFP->getValue()),
1371 MVT::i64),
1372 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001373 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001374 }
Chris Lattner6a542892006-01-24 05:48:21 +00001375 Result = LegalizeOp(Result);
1376 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001377 }
1378
Chris Lattner3e928bb2005-01-07 07:47:09 +00001379 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1380 case Legal: {
1381 SDOperand Val = LegalizeOp(Node->getOperand(1));
1382 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1383 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001384 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1385 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001386
1387 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1388 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1389 default: assert(0 && "This action is not supported yet!");
1390 case TargetLowering::Custom: {
1391 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1392 if (Tmp.Val) {
1393 Result = LegalizeOp(Tmp);
1394 break;
1395 }
1396 // FALLTHROUGH if the target thinks it is legal.
1397 }
1398 case TargetLowering::Legal:
1399 // Nothing to do.
1400 break;
1401 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001402 break;
1403 }
1404 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001405 // Truncate the value and store the result.
1406 Tmp3 = PromoteOp(Node->getOperand(1));
1407 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001408 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001409 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001410 break;
1411
Chris Lattner3e928bb2005-01-07 07:47:09 +00001412 case Expand:
1413 SDOperand Lo, Hi;
Nate Begemanab48be32005-11-22 18:16:00 +00001414 unsigned IncrementSize;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001415 ExpandOp(Node->getOperand(1), Lo, Hi);
1416
1417 if (!TLI.isLittleEndian())
1418 std::swap(Lo, Hi);
1419
Chris Lattneredb1add2005-05-11 04:51:16 +00001420 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1421 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001422 // If this is a vector type, then we have to calculate the increment as
1423 // the product of the element size in bytes, and the number of elements
1424 // in the high half of the vector.
1425 if (MVT::Vector == Hi.getValueType()) {
1426 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1427 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1428 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1429 } else {
1430 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1431 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001432 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1433 getIntPtrConstant(IncrementSize));
1434 assert(isTypeLegal(Tmp2.getValueType()) &&
1435 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001436 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001437 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1438 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001439 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1440 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001441 }
1442 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001443 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001444 case ISD::PCMARKER:
1445 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001446 if (Tmp1 != Node->getOperand(0))
1447 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001448 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001449 case ISD::STACKSAVE:
1450 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1451 if (Tmp1 != Node->getOperand(0)) {
1452 std::vector<MVT::ValueType> VTs;
1453 VTs.push_back(Node->getValueType(0));
1454 VTs.push_back(MVT::Other);
1455 std::vector<SDOperand> Ops;
1456 Ops.push_back(Tmp1);
1457 Result = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1458 }
1459
1460 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1461 default: assert(0 && "This action is not supported yet!");
1462 case TargetLowering::Custom: {
1463 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1464 if (Tmp.Val) {
1465 Result = LegalizeOp(Tmp);
1466 break;
1467 }
1468 // FALLTHROUGH if the target thinks it is legal.
1469 }
1470 case TargetLowering::Legal:
1471 // Since stacksave produce two values, make sure to remember that we
1472 // legalized both of them.
1473 AddLegalizedOperand(SDOperand(Node, 0), Result);
1474 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1475 return Result.getValue(Op.ResNo);
1476 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001477 // Expand to CopyFromReg if the target set
1478 // StackPointerRegisterToSaveRestore.
1479 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Andrew Lenharth8ff318b2006-01-18 23:19:08 +00001480 Tmp1 = DAG.getCopyFromReg(Tmp1, SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001481 Node->getValueType(0));
1482 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1483 AddLegalizedOperand(SDOperand(Node, 1), Tmp1.getValue(1));
1484 return Tmp1.getValue(Op.ResNo);
1485 } else {
1486 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1487 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1488 AddLegalizedOperand(SDOperand(Node, 1), Node->getOperand(0));
1489 return Op.ResNo ? Node->getOperand(0) : Tmp1;
1490 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001491 }
1492
1493 case ISD::STACKRESTORE:
1494 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1495 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1496 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1497 Result = DAG.getNode(ISD::STACKRESTORE, MVT::Other, Tmp1, Tmp2);
1498
1499 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1500 default: assert(0 && "This action is not supported yet!");
1501 case TargetLowering::Custom: {
1502 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1503 if (Tmp.Val) {
1504 Result = LegalizeOp(Tmp);
1505 break;
1506 }
1507 // FALLTHROUGH if the target thinks it is legal.
1508 }
1509 case TargetLowering::Legal:
1510 break;
1511 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001512 // Expand to CopyToReg if the target set
1513 // StackPointerRegisterToSaveRestore.
1514 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1515 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1516 } else {
1517 Result = Tmp1;
1518 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001519 break;
1520 }
1521 break;
1522
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001523 case ISD::READCYCLECOUNTER:
1524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001525 if (Tmp1 != Node->getOperand(0)) {
1526 std::vector<MVT::ValueType> rtypes;
1527 std::vector<SDOperand> rvals;
1528 rtypes.push_back(MVT::i64);
1529 rtypes.push_back(MVT::Other);
1530 rvals.push_back(Tmp1);
1531 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1532 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001533
1534 // Since rdcc produce two values, make sure to remember that we legalized
1535 // both of them.
1536 AddLegalizedOperand(SDOperand(Node, 0), Result);
1537 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1538 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001539
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001540 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001541 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1542 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1543
1544 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattnerc26f7a02005-12-23 16:12:20 +00001545 case Promote:
1546 case Expand:
1547 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
Chris Lattner0f69b292005-01-15 06:18:18 +00001548 case Legal:
1549 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001550
1551 // The only promote case we handle is TRUNCSTORE:i1 X into
1552 // -> TRUNCSTORE:i8 (and X, 1)
1553 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1554 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1555 TargetLowering::Promote) {
1556 // Promote the bool to a mask then store.
1557 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1558 DAG.getConstant(1, Tmp2.getValueType()));
1559 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1560 Node->getOperand(3), DAG.getValueType(MVT::i8));
1561
1562 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1563 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001564 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001565 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001566 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001567
1568 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1569 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1570 default: assert(0 && "This action is not supported yet!");
1571 case TargetLowering::Custom: {
1572 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1573 if (Tmp.Val) {
1574 Result = LegalizeOp(Tmp);
1575 break;
1576 }
1577 // FALLTHROUGH if the target thinks it is legal.
1578 }
1579 case TargetLowering::Legal:
1580 // Nothing to do.
1581 break;
1582 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001583 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001584 }
1585 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001586 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001587 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001588 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1589 case Expand: assert(0 && "It's impossible to expand bools");
1590 case Legal:
1591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1592 break;
1593 case Promote:
1594 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1595 break;
1596 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001597 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001598 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001599
Nate Begemanb942a3d2005-08-23 04:29:48 +00001600 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001601 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001602 case TargetLowering::Expand:
1603 if (Tmp1.getOpcode() == ISD::SETCC) {
1604 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1605 Tmp2, Tmp3,
1606 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1607 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001608 // Make sure the condition is either zero or one. It may have been
1609 // promoted from something else.
1610 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001611 Result = DAG.getSelectCC(Tmp1,
1612 DAG.getConstant(0, Tmp1.getValueType()),
1613 Tmp2, Tmp3, ISD::SETNE);
1614 }
Chris Lattnere7736732005-12-18 23:54:29 +00001615 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman9373a812005-08-10 20:51:12 +00001616 break;
Evan Cheng7df96d62005-12-17 01:21:05 +00001617 case TargetLowering::Custom: {
1618 SDOperand Tmp =
1619 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1620 Tmp1, Tmp2, Tmp3), DAG);
1621 if (Tmp.Val) {
1622 Result = LegalizeOp(Tmp);
1623 break;
1624 }
1625 // FALLTHROUGH if the target thinks it is legal.
1626 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001627 case TargetLowering::Legal:
1628 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1629 Tmp3 != Node->getOperand(2))
1630 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1631 Tmp1, Tmp2, Tmp3);
1632 break;
1633 case TargetLowering::Promote: {
1634 MVT::ValueType NVT =
1635 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1636 unsigned ExtOp, TruncOp;
1637 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001638 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001639 TruncOp = ISD::TRUNCATE;
1640 } else {
1641 ExtOp = ISD::FP_EXTEND;
1642 TruncOp = ISD::FP_ROUND;
1643 }
1644 // Promote each of the values to the new type.
1645 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1646 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1647 // Perform the larger operation, then round down.
1648 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1649 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
Evan Cheng433f8ac2006-01-17 19:47:13 +00001650 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001651 break;
1652 }
1653 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001654 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001655 case ISD::SELECT_CC:
1656 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1657 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1658
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001659 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001660 // Everything is legal, see if we should expand this op or something.
1661 switch (TLI.getOperationAction(ISD::SELECT_CC,
1662 Node->getOperand(0).getValueType())) {
1663 default: assert(0 && "This action is not supported yet!");
1664 case TargetLowering::Custom: {
1665 SDOperand Tmp =
1666 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1667 Node->getOperand(0),
1668 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001669 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001670 if (Tmp.Val) {
1671 Result = LegalizeOp(Tmp);
1672 break;
1673 }
1674 } // FALLTHROUGH if the target can't lower this operation after all.
1675 case TargetLowering::Legal:
1676 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1677 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1678 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1679 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001680 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner23004e52005-08-26 00:23:59 +00001681 Tmp3, Tmp4, Node->getOperand(4));
1682 }
1683 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001684 }
1685 break;
1686 } else {
1687 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1688 Node->getOperand(0), // LHS
1689 Node->getOperand(1), // RHS
1690 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001691 // If we get a SETCC back from legalizing the SETCC node we just
1692 // created, then use its LHS, RHS, and CC directly in creating a new
1693 // node. Otherwise, select between the true and false value based on
1694 // comparing the result of the legalized with zero.
1695 if (Tmp1.getOpcode() == ISD::SETCC) {
1696 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1697 Tmp1.getOperand(0), Tmp1.getOperand(1),
1698 Tmp3, Tmp4, Tmp1.getOperand(2));
1699 } else {
1700 Result = DAG.getSelectCC(Tmp1,
1701 DAG.getConstant(0, Tmp1.getValueType()),
1702 Tmp3, Tmp4, ISD::SETNE);
1703 }
Nate Begeman9373a812005-08-10 20:51:12 +00001704 }
1705 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001706 case ISD::SETCC:
1707 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1708 case Legal:
1709 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1710 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001711 break;
1712 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001713 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1714 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1715
1716 // If this is an FP compare, the operands have already been extended.
1717 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1718 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001719 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001720
1721 // Otherwise, we have to insert explicit sign or zero extends. Note
1722 // that we could insert sign extends for ALL conditions, but zero extend
1723 // is cheaper on many machines (an AND instead of two shifts), so prefer
1724 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001725 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001726 default: assert(0 && "Unknown integer comparison!");
1727 case ISD::SETEQ:
1728 case ISD::SETNE:
1729 case ISD::SETUGE:
1730 case ISD::SETUGT:
1731 case ISD::SETULE:
1732 case ISD::SETULT:
1733 // ALL of these operations will work if we either sign or zero extend
1734 // the operands (including the unsigned comparisons!). Zero extend is
1735 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001736 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1737 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001738 break;
1739 case ISD::SETGE:
1740 case ISD::SETGT:
1741 case ISD::SETLT:
1742 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001743 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1744 DAG.getValueType(VT));
1745 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1746 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001747 break;
1748 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001749 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001750 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001751 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001752 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1753 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1754 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001755 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001756 case ISD::SETEQ:
1757 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001758 if (RHSLo == RHSHi)
1759 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1760 if (RHSCST->isAllOnesValue()) {
1761 // Comparison to -1.
1762 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001763 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001764 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001765 }
1766
Chris Lattner3e928bb2005-01-07 07:47:09 +00001767 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1768 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1769 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001770 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001771 break;
1772 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001773 // If this is a comparison of the sign bit, just look at the top part.
1774 // X > -1, x < 0
1775 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001776 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001777 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001778 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001779 (CST->isAllOnesValue()))) { // X > -1
1780 Tmp1 = LHSHi;
1781 Tmp2 = RHSHi;
1782 break;
1783 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001784
Chris Lattner3e928bb2005-01-07 07:47:09 +00001785 // FIXME: This generated code sucks.
1786 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001787 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001788 default: assert(0 && "Unknown integer setcc!");
1789 case ISD::SETLT:
1790 case ISD::SETULT: LowCC = ISD::SETULT; break;
1791 case ISD::SETGT:
1792 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1793 case ISD::SETLE:
1794 case ISD::SETULE: LowCC = ISD::SETULE; break;
1795 case ISD::SETGE:
1796 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1797 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001798
Chris Lattner3e928bb2005-01-07 07:47:09 +00001799 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1800 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1801 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1802
1803 // NOTE: on targets without efficient SELECT of bools, we can always use
1804 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001805 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1806 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1807 Node->getOperand(2));
1808 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001809 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1810 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001811 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001812 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001813 }
1814 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001815
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001816 switch(TLI.getOperationAction(ISD::SETCC,
1817 Node->getOperand(0).getValueType())) {
Nate Begemanb942a3d2005-08-23 04:29:48 +00001818 default:
1819 assert(0 && "Cannot handle this action for SETCC yet!");
1820 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001821 case TargetLowering::Promote: {
1822 // First step, figure out the appropriate operation to use.
1823 // Allow SETCC to not be supported for all legal data types
1824 // Mostly this targets FP
1825 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1826 MVT::ValueType OldVT = NewInTy;
1827
1828 // Scan for the appropriate larger type to use.
1829 while (1) {
1830 NewInTy = (MVT::ValueType)(NewInTy+1);
1831
1832 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1833 "Fell off of the edge of the integer world");
1834 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1835 "Fell off of the edge of the floating point world");
1836
1837 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001838 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001839 break;
1840 }
1841 if (MVT::isInteger(NewInTy))
1842 assert(0 && "Cannot promote Legal Integer SETCC yet");
1843 else {
1844 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1845 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1846 }
1847
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001848 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1849 Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001850 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001851 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001852 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001853 case TargetLowering::Custom: {
1854 SDOperand Tmp =
1855 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1856 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1857 if (Tmp.Val) {
1858 Result = LegalizeOp(Tmp);
1859 break;
1860 }
1861 // FALLTHROUGH if the target thinks it is legal.
1862 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001863 case TargetLowering::Legal:
1864 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1865 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1866 Node->getOperand(2));
1867 break;
1868 case TargetLowering::Expand:
1869 // Expand a setcc node into a select_cc of the same condition, lhs, and
1870 // rhs that selects between const 1 (true) and const 0 (false).
1871 MVT::ValueType VT = Node->getValueType(0);
1872 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1873 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1874 Node->getOperand(2));
1875 Result = LegalizeOp(Result);
1876 break;
1877 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001878 break;
1879
Chris Lattnere1bd8222005-01-11 05:57:22 +00001880 case ISD::MEMSET:
1881 case ISD::MEMCPY:
1882 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1885
1886 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1887 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1888 case Expand: assert(0 && "Cannot expand a byte!");
1889 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001890 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001891 break;
1892 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001893 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001894 break;
1895 }
1896 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001897 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001898 }
Chris Lattner272455b2005-02-02 03:44:41 +00001899
1900 SDOperand Tmp4;
1901 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001902 case Expand: {
1903 // Length is too big, just take the lo-part of the length.
1904 SDOperand HiPart;
1905 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1906 break;
1907 }
Chris Lattnere5605212005-01-28 22:29:18 +00001908 case Legal:
1909 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001910 break;
1911 case Promote:
1912 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001913 break;
1914 }
1915
1916 SDOperand Tmp5;
1917 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1918 case Expand: assert(0 && "Cannot expand this yet!");
1919 case Legal:
1920 Tmp5 = LegalizeOp(Node->getOperand(4));
1921 break;
1922 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001923 Tmp5 = PromoteOp(Node->getOperand(4));
1924 break;
1925 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001926
1927 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1928 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001929 case TargetLowering::Custom: {
1930 SDOperand Tmp =
1931 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1932 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1933 if (Tmp.Val) {
1934 Result = LegalizeOp(Tmp);
1935 break;
1936 }
1937 // FALLTHROUGH if the target thinks it is legal.
1938 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001939 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001940 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1941 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1942 Tmp5 != Node->getOperand(4)) {
1943 std::vector<SDOperand> Ops;
1944 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1945 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1946 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1947 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001948 break;
1949 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001950 // Otherwise, the target does not support this operation. Lower the
1951 // operation to an explicit libcall as appropriate.
1952 MVT::ValueType IntPtr = TLI.getPointerTy();
1953 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1954 std::vector<std::pair<SDOperand, const Type*> > Args;
1955
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001956 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001957 if (Node->getOpcode() == ISD::MEMSET) {
1958 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1959 // Extend the ubyte argument to be an int value for the call.
1960 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1961 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1962 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1963
1964 FnName = "memset";
1965 } else if (Node->getOpcode() == ISD::MEMCPY ||
1966 Node->getOpcode() == ISD::MEMMOVE) {
1967 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1968 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1969 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1970 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1971 } else {
1972 assert(0 && "Unknown op!");
1973 }
Chris Lattner45982da2005-05-12 16:53:42 +00001974
Chris Lattnere1bd8222005-01-11 05:57:22 +00001975 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001976 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001977 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner69a889e2005-12-20 00:53:54 +00001978 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001979 break;
1980 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001981 }
1982 break;
1983 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001984
1985 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001986 Tmp1 = LegalizeOp(Node->getOperand(0));
1987 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001988
Chris Lattner3e011362005-05-14 07:45:46 +00001989 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1990 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1991 std::vector<SDOperand> Ops;
1992 Ops.push_back(Tmp1);
1993 Ops.push_back(Tmp2);
1994 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1995 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001996 Result = SDOperand(Node, 0);
1997 // Since these produce two values, make sure to remember that we legalized
1998 // both of them.
1999 AddLegalizedOperand(SDOperand(Node, 0), Result);
2000 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2001 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00002002 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00002003 Tmp1 = LegalizeOp(Node->getOperand(0));
2004 Tmp2 = LegalizeOp(Node->getOperand(1));
2005 Tmp3 = LegalizeOp(Node->getOperand(2));
2006 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2007 Tmp3 != Node->getOperand(2))
2008 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2009 break;
2010
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002011 case ISD::READIO:
2012 Tmp1 = LegalizeOp(Node->getOperand(0));
2013 Tmp2 = LegalizeOp(Node->getOperand(1));
2014
2015 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2016 case TargetLowering::Custom:
2017 default: assert(0 && "This action not implemented for this operation!");
2018 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00002019 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
2020 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2021 std::vector<SDOperand> Ops;
2022 Ops.push_back(Tmp1);
2023 Ops.push_back(Tmp2);
2024 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
2025 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002026 Result = SDOperand(Node, 0);
2027 break;
2028 case TargetLowering::Expand:
2029 // Replace this with a load from memory.
2030 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
2031 Node->getOperand(1), DAG.getSrcValue(NULL));
2032 Result = LegalizeOp(Result);
2033 break;
2034 }
2035
2036 // Since these produce two values, make sure to remember that we legalized
2037 // both of them.
2038 AddLegalizedOperand(SDOperand(Node, 0), Result);
2039 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2040 return Result.getValue(Op.ResNo);
2041
2042 case ISD::WRITEIO:
2043 Tmp1 = LegalizeOp(Node->getOperand(0));
2044 Tmp2 = LegalizeOp(Node->getOperand(1));
2045 Tmp3 = LegalizeOp(Node->getOperand(2));
2046
2047 switch (TLI.getOperationAction(Node->getOpcode(),
2048 Node->getOperand(1).getValueType())) {
2049 case TargetLowering::Custom:
2050 default: assert(0 && "This action not implemented for this operation!");
2051 case TargetLowering::Legal:
2052 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2053 Tmp3 != Node->getOperand(2))
2054 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2055 break;
2056 case TargetLowering::Expand:
2057 // Replace this with a store to memory.
2058 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
2059 Node->getOperand(1), Node->getOperand(2),
2060 DAG.getSrcValue(NULL));
2061 Result = LegalizeOp(Result);
2062 break;
2063 }
2064 break;
2065
Chris Lattner84f67882005-01-20 18:52:28 +00002066 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00002067 case ISD::SUB_PARTS:
2068 case ISD::SHL_PARTS:
2069 case ISD::SRA_PARTS:
2070 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002071 std::vector<SDOperand> Ops;
2072 bool Changed = false;
2073 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2074 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2075 Changed |= Ops.back() != Node->getOperand(i);
2076 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002077 if (Changed) {
2078 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2079 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
2080 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002081
Evan Cheng05a2d562006-01-09 18:31:59 +00002082 switch (TLI.getOperationAction(Node->getOpcode(),
2083 Node->getValueType(0))) {
2084 default: assert(0 && "This action is not supported yet!");
2085 case TargetLowering::Custom: {
2086 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2087 if (Tmp.Val) {
Chris Lattner269f8c02006-01-10 19:43:26 +00002088 SDOperand Tmp2, RetVal(0,0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002089 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2090 Tmp2 = LegalizeOp(Tmp.getValue(i));
2091 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2092 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002093 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002094 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002095 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002096 return RetVal;
2097 }
2098 // FALLTHROUGH if the target thinks it is legal.
2099 }
2100 case TargetLowering::Legal:
2101 // Nothing to do.
2102 break;
2103 }
2104
Chris Lattner2c8086f2005-04-02 05:00:07 +00002105 // Since these produce multiple values, make sure to remember that we
2106 // legalized all of them.
2107 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2108 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2109 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002110 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002111
2112 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002113 case ISD::ADD:
2114 case ISD::SUB:
2115 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002116 case ISD::MULHS:
2117 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002118 case ISD::UDIV:
2119 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002120 case ISD::AND:
2121 case ISD::OR:
2122 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002123 case ISD::SHL:
2124 case ISD::SRL:
2125 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002126 case ISD::FADD:
2127 case ISD::FSUB:
2128 case ISD::FMUL:
2129 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002130 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002131 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2132 case Expand: assert(0 && "Not possible");
2133 case Legal:
2134 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2135 break;
2136 case Promote:
2137 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2138 break;
2139 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002140 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002141 case TargetLowering::Custom: {
2142 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2143 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2144 if (Tmp.Val) {
2145 Tmp = LegalizeOp(Tmp); // Relegalize input.
2146 AddLegalizedOperand(Op, Tmp);
2147 return Tmp;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002148 } //else it was considered legal and we fall through
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002149 }
Andrew Lenharth57030e32005-12-25 01:07:37 +00002150 case TargetLowering::Legal:
2151 if (Tmp1 != Node->getOperand(0) ||
2152 Tmp2 != Node->getOperand(1))
2153 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
2154 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002155 default:
2156 assert(0 && "Operation not supported");
2157 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002158 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002159
Nate Begeman419f8b62005-10-18 00:27:41 +00002160 case ISD::BUILD_PAIR: {
2161 MVT::ValueType PairTy = Node->getValueType(0);
2162 // TODO: handle the case where the Lo and Hi operands are not of legal type
2163 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2164 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2165 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2166 case TargetLowering::Legal:
2167 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2168 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2169 break;
2170 case TargetLowering::Promote:
2171 case TargetLowering::Custom:
2172 assert(0 && "Cannot promote/custom this yet!");
2173 case TargetLowering::Expand:
2174 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2175 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2176 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2177 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2178 TLI.getShiftAmountTy()));
2179 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
2180 break;
2181 }
2182 break;
2183 }
2184
Nate Begemanc105e192005-04-06 00:23:54 +00002185 case ISD::UREM:
2186 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002187 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002188 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2189 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2190 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth57030e32005-12-25 01:07:37 +00002191 case TargetLowering::Custom: {
Nate Begemanc02d98e2006-01-16 07:59:13 +00002192 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
Andrew Lenharth57030e32005-12-25 01:07:37 +00002193 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2194 if (Tmp.Val) {
Nate Begemanc02d98e2006-01-16 07:59:13 +00002195 Tmp = LegalizeOp(Tmp); // Relegalize input.
2196 AddLegalizedOperand(Op, Tmp);
2197 return Tmp;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002198 } //else it was considered legal and we fall through
2199 }
Nate Begemanc105e192005-04-06 00:23:54 +00002200 case TargetLowering::Legal:
2201 if (Tmp1 != Node->getOperand(0) ||
2202 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00002203 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00002204 Tmp2);
2205 break;
2206 case TargetLowering::Promote:
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002207 assert(0 && "Cannot promote handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00002208 case TargetLowering::Expand:
2209 if (MVT::isInteger(Node->getValueType(0))) {
2210 MVT::ValueType VT = Node->getValueType(0);
2211 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2212 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2213 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2214 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2215 } else {
2216 // Floating point mod -> fmod libcall.
2217 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2218 SDOperand Dummy;
2219 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002220 }
2221 break;
2222 }
2223 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002224
Nate Begeman35ef9132006-01-11 21:21:00 +00002225 case ISD::ROTL:
2226 case ISD::ROTR:
2227 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2228 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2229 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2230 case TargetLowering::Custom:
2231 case TargetLowering::Promote:
2232 case TargetLowering::Expand:
2233 assert(0 && "Cannot handle this yet!");
2234 case TargetLowering::Legal:
2235 if (Tmp1 != Node->getOperand(0) ||
2236 Tmp2 != Node->getOperand(1))
2237 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
2238 Tmp2);
2239 break;
2240 }
2241 break;
2242
Nate Begemand88fc032006-01-14 03:14:10 +00002243 case ISD::BSWAP:
2244 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2245 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2246 case TargetLowering::Legal:
2247 if (Tmp1 != Node->getOperand(0))
2248 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2249 break;
2250 case TargetLowering::Promote: {
2251 MVT::ValueType OVT = Tmp1.getValueType();
2252 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2253 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
2254
2255 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2256 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2257 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2258 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
Evan Cheng433f8ac2006-01-17 19:47:13 +00002259 Result = LegalizeOp(Result);
Nate Begemand88fc032006-01-14 03:14:10 +00002260 break;
2261 }
2262 case TargetLowering::Custom:
2263 assert(0 && "Cannot custom legalize this yet!");
2264 case TargetLowering::Expand: {
2265 MVT::ValueType VT = Tmp1.getValueType();
2266 switch (VT) {
2267 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2268 case MVT::i16:
2269 Tmp2 = DAG.getNode(ISD::SHL, VT, Tmp1,
2270 DAG.getConstant(8, TLI.getShiftAmountTy()));
2271 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2272 DAG.getConstant(8, TLI.getShiftAmountTy()));
2273 Result = DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
2274 break;
2275 case MVT::i32:
2276 Tmp4 = DAG.getNode(ISD::SHL, VT, Tmp1,
2277 DAG.getConstant(24, TLI.getShiftAmountTy()));
2278 Tmp3 = DAG.getNode(ISD::SHL, VT, Tmp1,
2279 DAG.getConstant(8, TLI.getShiftAmountTy()));
2280 Tmp2 = DAG.getNode(ISD::SRL, VT, Tmp1,
2281 DAG.getConstant(8, TLI.getShiftAmountTy()));
2282 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2283 DAG.getConstant(24, TLI.getShiftAmountTy()));
2284 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2285 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2286 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
2287 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
2288 Result = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
2289 break;
Nate Begemanc02d98e2006-01-16 07:59:13 +00002290 case MVT::i64: {
2291 SDOperand Tmp5, Tmp6, Tmp7, Tmp8;
2292 Tmp8 = DAG.getNode(ISD::SHL, VT, Tmp1,
2293 DAG.getConstant(56, TLI.getShiftAmountTy()));
2294 Tmp7 = DAG.getNode(ISD::SHL, VT, Tmp1,
2295 DAG.getConstant(40, TLI.getShiftAmountTy()));
2296 Tmp6 = DAG.getNode(ISD::SHL, VT, Tmp1,
2297 DAG.getConstant(24, TLI.getShiftAmountTy()));
2298 Tmp5 = DAG.getNode(ISD::SHL, VT, Tmp1,
2299 DAG.getConstant(8, TLI.getShiftAmountTy()));
2300 Tmp4 = DAG.getNode(ISD::SRL, VT, Tmp1,
2301 DAG.getConstant(8, TLI.getShiftAmountTy()));
2302 Tmp3 = DAG.getNode(ISD::SRL, VT, Tmp1,
2303 DAG.getConstant(24, TLI.getShiftAmountTy()));
2304 Tmp2 = DAG.getNode(ISD::SRL, VT, Tmp1,
2305 DAG.getConstant(40, TLI.getShiftAmountTy()));
2306 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2307 DAG.getConstant(56, TLI.getShiftAmountTy()));
2308 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7,
2309 DAG.getConstant(0x00FF000000000000ULL, VT));
2310 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp7,
2311 DAG.getConstant(0x0000FF0000000000ULL, VT));
2312 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp7,
2313 DAG.getConstant(0x000000FF00000000ULL, VT));
2314 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp7,
2315 DAG.getConstant(0x00000000FF000000ULL, VT));
2316 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp7,
2317 DAG.getConstant(0x0000000000FF0000ULL, VT));
2318 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp7,
2319 DAG.getConstant(0x000000000000FF00ULL, VT));
2320 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
2321 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
2322 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
2323 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
2324 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
2325 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
2326 Result = DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
2327 break;
2328 }
Nate Begemand88fc032006-01-14 03:14:10 +00002329 }
Evan Cheng433f8ac2006-01-17 19:47:13 +00002330 Result = LegalizeOp(Result);
Nate Begemand88fc032006-01-14 03:14:10 +00002331 break;
2332 }
2333 }
2334 break;
2335
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002336 case ISD::CTPOP:
2337 case ISD::CTTZ:
2338 case ISD::CTLZ:
2339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2340 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2341 case TargetLowering::Legal:
2342 if (Tmp1 != Node->getOperand(0))
2343 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2344 break;
2345 case TargetLowering::Promote: {
2346 MVT::ValueType OVT = Tmp1.getValueType();
2347 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002348
2349 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002350 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2351 // Perform the larger operation, then subtract if needed.
2352 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2353 switch(Node->getOpcode())
2354 {
2355 case ISD::CTPOP:
2356 Result = Tmp1;
2357 break;
2358 case ISD::CTTZ:
2359 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002360 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2361 DAG.getConstant(getSizeInBits(NVT), NVT),
2362 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002363 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002364 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2365 break;
2366 case ISD::CTLZ:
2367 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002368 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2369 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002370 getSizeInBits(OVT), NVT));
2371 break;
2372 }
Evan Cheng433f8ac2006-01-17 19:47:13 +00002373 Result = LegalizeOp(Result);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002374 break;
2375 }
2376 case TargetLowering::Custom:
2377 assert(0 && "Cannot custom handle this yet!");
2378 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002379 switch(Node->getOpcode())
2380 {
2381 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002382 static const uint64_t mask[6] = {
2383 0x5555555555555555ULL, 0x3333333333333333ULL,
2384 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2385 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2386 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002387 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002388 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2389 unsigned len = getSizeInBits(VT);
2390 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002391 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002392 Tmp2 = DAG.getConstant(mask[i], VT);
2393 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002394 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002395 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2396 DAG.getNode(ISD::AND, VT,
2397 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2398 Tmp2));
2399 }
Evan Cheng433f8ac2006-01-17 19:47:13 +00002400 Result = LegalizeOp(Tmp1);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002401 break;
2402 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00002403 case ISD::CTLZ: {
2404 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002405 x = x | (x >> 1);
2406 x = x | (x >> 2);
2407 ...
2408 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00002409 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002410 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00002411
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002412 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2413 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00002414 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2415 unsigned len = getSizeInBits(VT);
2416 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2417 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002418 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00002419 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2420 }
2421 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002422 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00002423 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002424 }
2425 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00002426 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002427 // unless the target has ctlz but not ctpop, in which case we use:
2428 // { return 32 - nlz(~x & (x-1)); }
2429 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002430 MVT::ValueType VT = Tmp1.getValueType();
2431 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002432 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002433 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2434 DAG.getNode(ISD::SUB, VT, Tmp1,
2435 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002436 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002437 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2438 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00002439 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002440 DAG.getConstant(getSizeInBits(VT), VT),
2441 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2442 } else {
2443 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2444 }
Chris Lattner18aa6802005-05-11 05:27:09 +00002445 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002446 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002447 default:
2448 assert(0 && "Cannot expand this yet!");
2449 break;
2450 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002451 break;
2452 }
2453 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002454
Chris Lattner2c8086f2005-04-02 05:00:07 +00002455 // Unary operators
2456 case ISD::FABS:
2457 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002458 case ISD::FSQRT:
2459 case ISD::FSIN:
2460 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002461 Tmp1 = LegalizeOp(Node->getOperand(0));
2462 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2463 case TargetLowering::Legal:
2464 if (Tmp1 != Node->getOperand(0))
2465 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2466 break;
2467 case TargetLowering::Promote:
2468 case TargetLowering::Custom:
2469 assert(0 && "Cannot promote/custom handle this yet!");
2470 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002471 switch(Node->getOpcode()) {
2472 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00002473 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2474 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002475 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00002476 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002477 break;
2478 }
2479 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002480 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2481 MVT::ValueType VT = Node->getValueType(0);
2482 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002483 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002484 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2485 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2486 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002487 break;
2488 }
2489 case ISD::FSQRT:
2490 case ISD::FSIN:
2491 case ISD::FCOS: {
2492 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002493 const char *FnName = 0;
2494 switch(Node->getOpcode()) {
2495 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2496 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2497 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2498 default: assert(0 && "Unreachable!");
2499 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002500 SDOperand Dummy;
2501 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002502 break;
2503 }
2504 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002505 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002506 }
2507 break;
2508 }
2509 break;
Chris Lattner35481892005-12-23 00:16:34 +00002510
2511 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002512 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002513 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002514 Result = LegalizeOp(Result);
2515 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002516 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2517 Node->getOperand(0).getValueType())) {
2518 default: assert(0 && "Unknown operation action!");
2519 case TargetLowering::Expand:
2520 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002521 Result = LegalizeOp(Result);
Chris Lattner35481892005-12-23 00:16:34 +00002522 break;
2523 case TargetLowering::Legal:
2524 Tmp1 = LegalizeOp(Node->getOperand(0));
2525 if (Tmp1 != Node->getOperand(0))
2526 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2527 break;
2528 }
2529 }
2530 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002531 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002532 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002533 case ISD::UINT_TO_FP: {
2534 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002535 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2536 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002537 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002538 Node->getOperand(0).getValueType())) {
2539 default: assert(0 && "Unknown operation action!");
2540 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002541 Result = ExpandLegalINT_TO_FP(isSigned,
2542 LegalizeOp(Node->getOperand(0)),
2543 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002544 AddLegalizedOperand(Op, Result);
2545 return Result;
2546 case TargetLowering::Promote:
2547 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2548 Node->getValueType(0),
2549 isSigned);
2550 AddLegalizedOperand(Op, Result);
2551 return Result;
2552 case TargetLowering::Legal:
2553 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002554 case TargetLowering::Custom: {
2555 Tmp1 = LegalizeOp(Node->getOperand(0));
2556 SDOperand Tmp =
2557 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2558 Tmp = TLI.LowerOperation(Tmp, DAG);
2559 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002560 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002561 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002562 return Tmp;
2563 } else {
2564 assert(0 && "Target Must Lower this");
2565 }
2566 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00002567 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002568
Chris Lattner3e928bb2005-01-07 07:47:09 +00002569 Tmp1 = LegalizeOp(Node->getOperand(0));
2570 if (Tmp1 != Node->getOperand(0))
2571 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2572 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002573 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002574 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2575 Node->getValueType(0), Node->getOperand(0));
2576 break;
2577 case Promote:
2578 if (isSigned) {
2579 Result = PromoteOp(Node->getOperand(0));
2580 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2581 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2582 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2583 } else {
2584 Result = PromoteOp(Node->getOperand(0));
2585 Result = DAG.getZeroExtendInReg(Result,
2586 Node->getOperand(0).getValueType());
2587 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002588 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002589 break;
2590 }
2591 break;
2592 }
2593 case ISD::TRUNCATE:
2594 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2595 case Legal:
2596 Tmp1 = LegalizeOp(Node->getOperand(0));
2597 if (Tmp1 != Node->getOperand(0))
2598 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2599 break;
2600 case Expand:
2601 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2602
2603 // Since the result is legal, we should just be able to truncate the low
2604 // part of the source.
2605 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2606 break;
2607 case Promote:
2608 Result = PromoteOp(Node->getOperand(0));
2609 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2610 break;
2611 }
2612 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002613
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002614 case ISD::FP_TO_SINT:
2615 case ISD::FP_TO_UINT:
2616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2617 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002618 Tmp1 = LegalizeOp(Node->getOperand(0));
2619
Chris Lattner1618beb2005-07-29 00:11:56 +00002620 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2621 default: assert(0 && "Unknown operation action!");
2622 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002623 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2624 SDOperand True, False;
2625 MVT::ValueType VT = Node->getOperand(0).getValueType();
2626 MVT::ValueType NVT = Node->getValueType(0);
2627 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2628 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2629 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2630 Node->getOperand(0), Tmp2, ISD::SETLT);
2631 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2632 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002633 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002634 Tmp2));
2635 False = DAG.getNode(ISD::XOR, NVT, False,
2636 DAG.getConstant(1ULL << ShiftAmt, NVT));
2637 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner69a889e2005-12-20 00:53:54 +00002638 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman2d56e722005-08-14 18:38:32 +00002639 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002640 } else {
2641 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2642 }
2643 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002644 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002645 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002646 Node->getOpcode() == ISD::FP_TO_SINT);
2647 AddLegalizedOperand(Op, Result);
2648 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002649 case TargetLowering::Custom: {
2650 SDOperand Tmp =
2651 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2652 Tmp = TLI.LowerOperation(Tmp, DAG);
2653 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002654 Tmp = LegalizeOp(Tmp);
Chris Lattner07dffd62005-08-26 00:14:16 +00002655 AddLegalizedOperand(Op, Tmp);
Chris Lattner507f7522005-08-29 17:30:00 +00002656 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002657 } else {
2658 // The target thinks this is legal afterall.
2659 break;
2660 }
2661 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002662 case TargetLowering::Legal:
2663 break;
2664 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002665
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002666 if (Tmp1 != Node->getOperand(0))
2667 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2668 break;
2669 case Expand:
2670 assert(0 && "Shouldn't need to expand other operators here!");
2671 case Promote:
2672 Result = PromoteOp(Node->getOperand(0));
2673 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2674 break;
2675 }
2676 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002677
Chris Lattner13c78e22005-09-02 00:18:10 +00002678 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002679 case ISD::ZERO_EXTEND:
2680 case ISD::SIGN_EXTEND:
2681 case ISD::FP_EXTEND:
2682 case ISD::FP_ROUND:
2683 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2684 case Legal:
2685 Tmp1 = LegalizeOp(Node->getOperand(0));
2686 if (Tmp1 != Node->getOperand(0))
2687 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2688 break;
2689 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002690 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002691
Chris Lattner03c85462005-01-15 05:21:40 +00002692 case Promote:
2693 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002694 case ISD::ANY_EXTEND:
2695 Result = PromoteOp(Node->getOperand(0));
2696 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2697 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002698 case ISD::ZERO_EXTEND:
2699 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002700 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002701 Result = DAG.getZeroExtendInReg(Result,
2702 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002703 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002704 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002705 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002706 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002707 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002708 Result,
2709 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002710 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002711 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002712 Result = PromoteOp(Node->getOperand(0));
2713 if (Result.getValueType() != Op.getValueType())
2714 // Dynamically dead while we have only 2 FP types.
2715 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2716 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002717 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002718 Result = PromoteOp(Node->getOperand(0));
2719 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2720 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002721 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002722 }
2723 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002724 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002725 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002726 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002727 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002728
2729 // If this operation is not supported, convert it to a shl/shr or load/store
2730 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002731 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2732 default: assert(0 && "This action not supported for this op yet!");
2733 case TargetLowering::Legal:
2734 if (Tmp1 != Node->getOperand(0))
2735 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002736 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002737 break;
2738 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002739 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002740 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002741 // NOTE: we could fall back on load/store here too for targets without
2742 // SAR. However, it is doubtful that any exist.
2743 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2744 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002745 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002746 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2747 Node->getOperand(0), ShiftCst);
2748 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2749 Result, ShiftCst);
2750 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2751 // The only way we can lower this is to turn it into a STORETRUNC,
2752 // EXTLOAD pair, targetting a temporary location (a stack slot).
2753
2754 // NOTE: there is a choice here between constantly creating new stack
2755 // slots and always reusing the same one. We currently always create
2756 // new ones, as reuse may inhibit scheduling.
2757 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2758 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2759 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2760 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002761 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002762 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2763 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2764 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002765 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002766 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002767 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2768 Result, StackSlot, DAG.getSrcValue(NULL),
2769 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002770 } else {
2771 assert(0 && "Unknown op");
2772 }
2773 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002774 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002775 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002776 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002777 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002778 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002779
Chris Lattner45982da2005-05-12 16:53:42 +00002780 // Note that LegalizeOp may be reentered even from single-use nodes, which
2781 // means that we always must cache transformed nodes.
2782 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002783 return Result;
2784}
2785
Chris Lattner8b6fa222005-01-15 22:16:26 +00002786/// PromoteOp - Given an operation that produces a value in an invalid type,
2787/// promote it to compute the value into a larger type. The produced value will
2788/// have the correct bits for the low portion of the register, but no guarantee
2789/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002790SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2791 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002792 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002793 assert(getTypeAction(VT) == Promote &&
2794 "Caller should expand or legalize operands that are not promotable!");
2795 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2796 "Cannot promote to smaller type!");
2797
Chris Lattner03c85462005-01-15 05:21:40 +00002798 SDOperand Tmp1, Tmp2, Tmp3;
2799
2800 SDOperand Result;
2801 SDNode *Node = Op.Val;
2802
Chris Lattner6fdcb252005-09-02 20:32:45 +00002803 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2804 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002805
Chris Lattner0f69b292005-01-15 06:18:18 +00002806 // Promotion needs an optimization step to clean up after it, and is not
2807 // careful to avoid operations the target does not support. Make sure that
2808 // all generated operations are legalized in the next iteration.
2809 NeedsAnotherIteration = true;
2810
Chris Lattner03c85462005-01-15 05:21:40 +00002811 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002812 case ISD::CopyFromReg:
2813 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002814 default:
2815 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2816 assert(0 && "Do not know how to promote this operator!");
2817 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002818 case ISD::UNDEF:
2819 Result = DAG.getNode(ISD::UNDEF, NVT);
2820 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002821 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002822 if (VT != MVT::i1)
2823 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2824 else
2825 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002826 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2827 break;
2828 case ISD::ConstantFP:
2829 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2830 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2831 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002832
Chris Lattner82fbfb62005-01-18 02:59:52 +00002833 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002834 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002835 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2836 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002837 Result = LegalizeOp(Result);
2838 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002839
2840 case ISD::TRUNCATE:
2841 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2842 case Legal:
2843 Result = LegalizeOp(Node->getOperand(0));
2844 assert(Result.getValueType() >= NVT &&
2845 "This truncation doesn't make sense!");
2846 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2847 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2848 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002849 case Promote:
2850 // The truncation is not required, because we don't guarantee anything
2851 // about high bits anyway.
2852 Result = PromoteOp(Node->getOperand(0));
2853 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002854 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002855 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2856 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002857 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002858 }
2859 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002860 case ISD::SIGN_EXTEND:
2861 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002862 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002863 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2864 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2865 case Legal:
2866 // Input is legal? Just do extend all the way to the larger type.
2867 Result = LegalizeOp(Node->getOperand(0));
2868 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2869 break;
2870 case Promote:
2871 // Promote the reg if it's smaller.
2872 Result = PromoteOp(Node->getOperand(0));
2873 // The high bits are not guaranteed to be anything. Insert an extend.
2874 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002875 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002876 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002877 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002878 Result = DAG.getZeroExtendInReg(Result,
2879 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002880 break;
2881 }
2882 break;
Chris Lattner35481892005-12-23 00:16:34 +00002883 case ISD::BIT_CONVERT:
2884 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2885 Result = PromoteOp(Result);
2886 break;
2887
Chris Lattner8b6fa222005-01-15 22:16:26 +00002888 case ISD::FP_EXTEND:
2889 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2890 case ISD::FP_ROUND:
2891 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2892 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2893 case Promote: assert(0 && "Unreachable with 2 FP types!");
2894 case Legal:
2895 // Input is legal? Do an FP_ROUND_INREG.
2896 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002897 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2898 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002899 break;
2900 }
2901 break;
2902
2903 case ISD::SINT_TO_FP:
2904 case ISD::UINT_TO_FP:
2905 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2906 case Legal:
2907 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002908 // No extra round required here.
2909 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002910 break;
2911
2912 case Promote:
2913 Result = PromoteOp(Node->getOperand(0));
2914 if (Node->getOpcode() == ISD::SINT_TO_FP)
2915 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002916 Result,
2917 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002918 else
Chris Lattner23993562005-04-13 02:38:47 +00002919 Result = DAG.getZeroExtendInReg(Result,
2920 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002921 // No extra round required here.
2922 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002923 break;
2924 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002925 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2926 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002927 // Round if we cannot tolerate excess precision.
2928 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002929 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2930 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002931 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002932 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002933 break;
2934
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002935 case ISD::SIGN_EXTEND_INREG:
2936 Result = PromoteOp(Node->getOperand(0));
2937 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2938 Node->getOperand(1));
2939 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002940 case ISD::FP_TO_SINT:
2941 case ISD::FP_TO_UINT:
2942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2943 case Legal:
2944 Tmp1 = LegalizeOp(Node->getOperand(0));
2945 break;
2946 case Promote:
2947 // The input result is prerounded, so we don't have to do anything
2948 // special.
2949 Tmp1 = PromoteOp(Node->getOperand(0));
2950 break;
2951 case Expand:
2952 assert(0 && "not implemented");
2953 }
Nate Begemand2558e32005-08-14 01:20:53 +00002954 // If we're promoting a UINT to a larger size, check to see if the new node
2955 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2956 // we can use that instead. This allows us to generate better code for
2957 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2958 // legal, such as PowerPC.
2959 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002960 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002961 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2962 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002963 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2964 } else {
2965 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2966 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002967 break;
2968
Chris Lattner2c8086f2005-04-02 05:00:07 +00002969 case ISD::FABS:
2970 case ISD::FNEG:
2971 Tmp1 = PromoteOp(Node->getOperand(0));
2972 assert(Tmp1.getValueType() == NVT);
2973 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2974 // NOTE: we do not have to do any extra rounding here for
2975 // NoExcessFPPrecision, because we know the input will have the appropriate
2976 // precision, and these operations don't modify precision at all.
2977 break;
2978
Chris Lattnerda6ba872005-04-28 21:44:33 +00002979 case ISD::FSQRT:
2980 case ISD::FSIN:
2981 case ISD::FCOS:
2982 Tmp1 = PromoteOp(Node->getOperand(0));
2983 assert(Tmp1.getValueType() == NVT);
2984 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2985 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002986 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2987 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002988 break;
2989
Chris Lattner03c85462005-01-15 05:21:40 +00002990 case ISD::AND:
2991 case ISD::OR:
2992 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002993 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002994 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002995 case ISD::MUL:
2996 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002997 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002998 // that too is okay if they are integer operations.
2999 Tmp1 = PromoteOp(Node->getOperand(0));
3000 Tmp2 = PromoteOp(Node->getOperand(1));
3001 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3002 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003003 break;
3004 case ISD::FADD:
3005 case ISD::FSUB:
3006 case ISD::FMUL:
3007 // The input may have strange things in the top bits of the registers, but
3008 // these operations don't care.
3009 Tmp1 = PromoteOp(Node->getOperand(0));
3010 Tmp2 = PromoteOp(Node->getOperand(1));
3011 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3012 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3013
3014 // Floating point operations will give excess precision that we may not be
3015 // able to tolerate. If we DO allow excess precision, just leave it,
3016 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003017 // FIXME: Why would we need to round FP ops more than integer ones?
3018 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003019 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003020 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3021 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003022 break;
3023
Chris Lattner8b6fa222005-01-15 22:16:26 +00003024 case ISD::SDIV:
3025 case ISD::SREM:
3026 // These operators require that their input be sign extended.
3027 Tmp1 = PromoteOp(Node->getOperand(0));
3028 Tmp2 = PromoteOp(Node->getOperand(1));
3029 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003030 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3031 DAG.getValueType(VT));
3032 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3033 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003034 }
3035 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3036
3037 // Perform FP_ROUND: this is probably overly pessimistic.
3038 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003039 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3040 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003041 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003042 case ISD::FDIV:
3043 case ISD::FREM:
3044 // These operators require that their input be fp extended.
3045 Tmp1 = PromoteOp(Node->getOperand(0));
3046 Tmp2 = PromoteOp(Node->getOperand(1));
3047 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3048
3049 // Perform FP_ROUND: this is probably overly pessimistic.
3050 if (NoExcessFPPrecision)
3051 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3052 DAG.getValueType(VT));
3053 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003054
3055 case ISD::UDIV:
3056 case ISD::UREM:
3057 // These operators require that their input be zero extended.
3058 Tmp1 = PromoteOp(Node->getOperand(0));
3059 Tmp2 = PromoteOp(Node->getOperand(1));
3060 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003061 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3062 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003063 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3064 break;
3065
3066 case ISD::SHL:
3067 Tmp1 = PromoteOp(Node->getOperand(0));
3068 Tmp2 = LegalizeOp(Node->getOperand(1));
3069 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
3070 break;
3071 case ISD::SRA:
3072 // The input value must be properly sign extended.
3073 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003074 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3075 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003076 Tmp2 = LegalizeOp(Node->getOperand(1));
3077 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
3078 break;
3079 case ISD::SRL:
3080 // The input value must be properly zero extended.
3081 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003082 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003083 Tmp2 = LegalizeOp(Node->getOperand(1));
3084 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
3085 break;
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003086
Chris Lattner03c85462005-01-15 05:21:40 +00003087 case ISD::LOAD:
3088 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3089 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00003090 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
3091 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003092 // Remember that we legalized the chain.
3093 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3094 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003095 case ISD::SEXTLOAD:
3096 case ISD::ZEXTLOAD:
3097 case ISD::EXTLOAD:
3098 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3099 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00003100 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
3101 Node->getOperand(2),
3102 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003103 // Remember that we legalized the chain.
3104 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3105 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003106 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00003107 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3108 case Expand: assert(0 && "It's impossible to expand bools");
3109 case Legal:
3110 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
3111 break;
3112 case Promote:
3113 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
3114 break;
3115 }
Chris Lattner03c85462005-01-15 05:21:40 +00003116 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3117 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
3118 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
3119 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003120 case ISD::SELECT_CC:
3121 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3122 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3123 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3124 Node->getOperand(1), Tmp2, Tmp3,
3125 Node->getOperand(4));
3126 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00003127 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00003128 case ISD::CALL: {
3129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3130 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3131
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003132 std::vector<SDOperand> Ops;
3133 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
3134 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3135
Chris Lattner8ac532c2005-01-16 19:46:48 +00003136 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3137 "Can only promote single result calls");
3138 std::vector<MVT::ValueType> RetTyVTs;
3139 RetTyVTs.reserve(2);
3140 RetTyVTs.push_back(NVT);
3141 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003142 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
3143 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00003144 Result = SDOperand(NC, 0);
3145
3146 // Insert the new chain mapping.
3147 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3148 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003149 }
Nate Begemand88fc032006-01-14 03:14:10 +00003150 case ISD::BSWAP:
3151 Tmp1 = Node->getOperand(0);
3152 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3153 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3154 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3155 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3156 TLI.getShiftAmountTy()));
3157 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003158 case ISD::CTPOP:
3159 case ISD::CTTZ:
3160 case ISD::CTLZ:
3161 Tmp1 = Node->getOperand(0);
3162 //Zero extend the argument
3163 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3164 // Perform the larger operation, then subtract if needed.
3165 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3166 switch(Node->getOpcode())
3167 {
3168 case ISD::CTPOP:
3169 Result = Tmp1;
3170 break;
3171 case ISD::CTTZ:
3172 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003173 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003174 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003175 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003176 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
3177 break;
3178 case ISD::CTLZ:
3179 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003180 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3181 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003182 getSizeInBits(VT), NVT));
3183 break;
3184 }
3185 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003186 }
3187
3188 assert(Result.Val && "Didn't set a result!");
3189 AddPromotedOperand(Op, Result);
3190 return Result;
3191}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003192
Chris Lattner35481892005-12-23 00:16:34 +00003193/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003194/// The resultant code need not be legal. Note that SrcOp is the input operand
3195/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003196SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3197 SDOperand SrcOp) {
3198 // Create the stack frame object.
3199 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3200 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00003201 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00003202 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3203
3204 // Emit a store to the stack slot.
3205 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003206 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003207 // Result is a load from the stack slot.
3208 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3209}
3210
Chris Lattner84f67882005-01-20 18:52:28 +00003211/// ExpandAddSub - Find a clever way to expand this add operation into
3212/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00003213void SelectionDAGLegalize::
3214ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
3215 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00003216 // Expand the subcomponents.
3217 SDOperand LHSL, LHSH, RHSL, RHSH;
3218 ExpandOp(LHS, LHSL, LHSH);
3219 ExpandOp(RHS, RHSL, RHSH);
3220
Chris Lattner84f67882005-01-20 18:52:28 +00003221 std::vector<SDOperand> Ops;
3222 Ops.push_back(LHSL);
3223 Ops.push_back(LHSH);
3224 Ops.push_back(RHSL);
3225 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00003226 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3227 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00003228 Hi = Lo.getValue(1);
3229}
3230
Chris Lattner5b359c62005-04-02 04:00:59 +00003231void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3232 SDOperand Op, SDOperand Amt,
3233 SDOperand &Lo, SDOperand &Hi) {
3234 // Expand the subcomponents.
3235 SDOperand LHSL, LHSH;
3236 ExpandOp(Op, LHSL, LHSH);
3237
3238 std::vector<SDOperand> Ops;
3239 Ops.push_back(LHSL);
3240 Ops.push_back(LHSH);
3241 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003242 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003243 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003244 Hi = Lo.getValue(1);
3245}
3246
3247
Chris Lattnere34b3962005-01-19 04:19:40 +00003248/// ExpandShift - Try to find a clever way to expand this shift operation out to
3249/// smaller elements. If we can't find a way that is more efficient than a
3250/// libcall on this target, return false. Otherwise, return true with the
3251/// low-parts expanded into Lo and Hi.
3252bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3253 SDOperand &Lo, SDOperand &Hi) {
3254 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3255 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003256
Chris Lattnere34b3962005-01-19 04:19:40 +00003257 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003258 SDOperand ShAmt = LegalizeOp(Amt);
3259 MVT::ValueType ShTy = ShAmt.getValueType();
3260 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3261 unsigned NVTBits = MVT::getSizeInBits(NVT);
3262
3263 // Handle the case when Amt is an immediate. Other cases are currently broken
3264 // and are disabled.
3265 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3266 unsigned Cst = CN->getValue();
3267 // Expand the incoming operand to be shifted, so that we have its parts
3268 SDOperand InL, InH;
3269 ExpandOp(Op, InL, InH);
3270 switch(Opc) {
3271 case ISD::SHL:
3272 if (Cst > VTBits) {
3273 Lo = DAG.getConstant(0, NVT);
3274 Hi = DAG.getConstant(0, NVT);
3275 } else if (Cst > NVTBits) {
3276 Lo = DAG.getConstant(0, NVT);
3277 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003278 } else if (Cst == NVTBits) {
3279 Lo = DAG.getConstant(0, NVT);
3280 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003281 } else {
3282 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3283 Hi = DAG.getNode(ISD::OR, NVT,
3284 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3285 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3286 }
3287 return true;
3288 case ISD::SRL:
3289 if (Cst > VTBits) {
3290 Lo = DAG.getConstant(0, NVT);
3291 Hi = DAG.getConstant(0, NVT);
3292 } else if (Cst > NVTBits) {
3293 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3294 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003295 } else if (Cst == NVTBits) {
3296 Lo = InH;
3297 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003298 } else {
3299 Lo = DAG.getNode(ISD::OR, NVT,
3300 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3301 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3302 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3303 }
3304 return true;
3305 case ISD::SRA:
3306 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003307 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003308 DAG.getConstant(NVTBits-1, ShTy));
3309 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003310 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003311 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003312 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003313 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003314 } else if (Cst == NVTBits) {
3315 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003316 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003317 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003318 } else {
3319 Lo = DAG.getNode(ISD::OR, NVT,
3320 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3321 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3322 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3323 }
3324 return true;
3325 }
3326 }
3327 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
3328 // so disable it for now. Currently targets are handling this via SHL_PARTS
3329 // and friends.
3330 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003331
3332 // If we have an efficient select operation (or if the selects will all fold
3333 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003334 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00003335 return false;
3336
3337 SDOperand InL, InH;
3338 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00003339 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3340 DAG.getConstant(NVTBits, ShTy), ShAmt);
3341
Chris Lattnere5544f82005-01-20 20:29:23 +00003342 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003343 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3344 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00003345
Chris Lattnere34b3962005-01-19 04:19:40 +00003346 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3347 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3348 DAG.getConstant(NVTBits-1, ShTy));
3349 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3350 DAG.getConstant(NVTBits-1, ShTy));
3351 }
3352
3353 if (Opc == ISD::SHL) {
3354 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3355 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3356 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003357 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00003358
Chris Lattnere34b3962005-01-19 04:19:40 +00003359 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3360 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3361 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00003362 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003363 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3364 DAG.getConstant(32, ShTy),
3365 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00003366 DAG.getConstant(0, NVT),
3367 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00003368 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00003369 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00003370 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003371 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00003372
3373 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00003374 if (Opc == ISD::SRA)
3375 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3376 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00003377 else
3378 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00003379 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00003380 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00003381 }
3382 return true;
3383}
Chris Lattner77e77a62005-01-21 06:05:23 +00003384
Chris Lattner9530ddc2005-05-13 05:09:11 +00003385/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3386/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003387/// Found.
Chris Lattnerde387ce2006-01-09 23:21:49 +00003388static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
3389 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00003390 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattnerde387ce2006-01-09 23:21:49 +00003391 !Visited.insert(Node).second) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00003392
Chris Lattner16cd04d2005-05-12 23:24:06 +00003393 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003394 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003395 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003396 Found = Node;
3397 return;
3398 }
3399
3400 // Otherwise, scan the operands of Node to see if any of them is a call.
3401 assert(Node->getNumOperands() != 0 &&
3402 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00003403 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattnerde387ce2006-01-09 23:21:49 +00003404 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003405
3406 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003407 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattnerde387ce2006-01-09 23:21:49 +00003408 Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003409}
3410
3411
Chris Lattner9530ddc2005-05-13 05:09:11 +00003412/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3413/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003414/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00003415static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3416 std::set<SDNode*> &Visited) {
Chris Lattnera68d2042006-01-20 18:40:10 +00003417 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner82299e72005-08-05 18:10:27 +00003418 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003419
Chris Lattner16cd04d2005-05-12 23:24:06 +00003420 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003421 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003422 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003423 Found = Node;
3424 return;
3425 }
3426
3427 // Otherwise, scan the operands of Node to see if any of them is a call.
3428 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3429 if (UI == E) return;
3430 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00003431 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003432
3433 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00003434 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003435}
3436
Chris Lattner9530ddc2005-05-13 05:09:11 +00003437/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003438/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003439static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00003440 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003441 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00003442 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00003443 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003444
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003445 // The chain is usually at the end.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003446 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003447 if (TheChain.getValueType() != MVT::Other) {
3448 // Sometimes it's at the beginning.
Chris Lattner2789bde2005-05-14 08:34:53 +00003449 TheChain = SDOperand(Node, 0);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003450 if (TheChain.getValueType() != MVT::Other) {
3451 // Otherwise, hunt for it.
3452 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
3453 if (Node->getValueType(i) == MVT::Other) {
3454 TheChain = SDOperand(Node, i);
3455 break;
3456 }
3457
3458 // Otherwise, we walked into a node without a chain.
3459 if (TheChain.getValueType() != MVT::Other)
3460 return 0;
3461 }
3462 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003463
3464 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003465 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003466
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003467 // Make sure to only follow users of our token chain.
3468 SDNode *User = *UI;
3469 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3470 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003471 if (SDNode *Result = FindCallSeqEnd(User))
3472 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003473 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003474 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003475}
3476
Chris Lattner9530ddc2005-05-13 05:09:11 +00003477/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003478/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003479static SDNode *FindCallSeqStart(SDNode *Node) {
3480 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003481 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003482
3483 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3484 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003485 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003486}
3487
3488
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003489/// FindInputOutputChains - If we are replacing an operation with a call we need
3490/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003491/// properly serialize the calls in the block. The returned operand is the
3492/// input chain value for the new call (e.g. the entry node or the previous
3493/// call), and OutChain is set to be the chain node to update to point to the
3494/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003495static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3496 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003497 SDNode *LatestCallSeqStart = Entry.Val;
3498 SDNode *LatestCallSeqEnd = 0;
Chris Lattnerde387ce2006-01-09 23:21:49 +00003499 std::set<SDNode*> Visited;
3500 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
3501 Visited.clear();
Chris Lattner9530ddc2005-05-13 05:09:11 +00003502 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003503
Chris Lattner16cd04d2005-05-12 23:24:06 +00003504 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003505 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003506 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3507 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003508 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003509 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003510 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3511 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003512 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003513 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003514 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003515
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003516 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003517 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003518 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003519 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattnerde387ce2006-01-09 23:21:49 +00003520 Visited.clear();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003521
Chris Lattner9530ddc2005-05-13 05:09:11 +00003522 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003523 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003524 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003525
Chris Lattner9530ddc2005-05-13 05:09:11 +00003526 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003527}
3528
Jeff Cohen00b168892005-07-27 06:12:32 +00003529/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003530void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3531 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003532 // Nothing to splice it into?
3533 if (OutChain == 0) return;
3534
3535 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3536 //OutChain->dump();
3537
3538 // Form a token factor node merging the old inval and the new inval.
3539 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3540 OutChain->getOperand(0));
3541 // Change the node to refer to the new token.
3542 OutChain->setAdjCallChain(InToken);
3543}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003544
3545
Chris Lattner77e77a62005-01-21 06:05:23 +00003546// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3547// does not fit into a register, return the lo part and set the hi part to the
3548// by-reg argument. If it does fit into a single register, return the result
3549// and leave the Hi part unset.
3550SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3551 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003552 SDNode *OutChain;
3553 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3554 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003555 if (InChain.Val == 0)
3556 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003557
Chris Lattner77e77a62005-01-21 06:05:23 +00003558 TargetLowering::ArgListTy Args;
3559 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3560 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3561 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3562 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3563 }
3564 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003565
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003566 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003567 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003568 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003569 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3570 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003571
Chris Lattner99c25b82005-09-02 20:26:58 +00003572 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003573 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003574 default: assert(0 && "Unknown thing");
3575 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00003576 Result = CallInfo.first;
3577 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003578 case Promote:
3579 assert(0 && "Cannot promote this yet!");
3580 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003581 ExpandOp(CallInfo.first, Result, Hi);
3582 CallInfo.second = LegalizeOp(CallInfo.second);
3583 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003584 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003585
3586 SpliceCallInto(CallInfo.second, OutChain);
3587 NeedsAnotherIteration = true;
3588 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003589}
3590
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003591
Chris Lattner77e77a62005-01-21 06:05:23 +00003592/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3593/// destination type is legal.
3594SDOperand SelectionDAGLegalize::
3595ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003596 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003597 assert(getTypeAction(Source.getValueType()) == Expand &&
3598 "This is not an expansion!");
3599 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3600
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003601 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003602 assert(Source.getValueType() == MVT::i64 &&
3603 "This only works for 64-bit -> FP");
3604 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3605 // incoming integer is set. To handle this, we dynamically test to see if
3606 // it is set, and, if so, add a fudge factor.
3607 SDOperand Lo, Hi;
3608 ExpandOp(Source, Lo, Hi);
3609
Chris Lattner66de05b2005-05-13 04:45:13 +00003610 // If this is unsigned, and not supported, first perform the conversion to
3611 // signed, then adjust the result if the sign bit is set.
3612 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3613 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3614
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003615 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3616 DAG.getConstant(0, Hi.getValueType()),
3617 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003618 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3619 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3620 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003621 uint64_t FF = 0x5f800000ULL;
3622 if (TLI.isLittleEndian()) FF <<= 32;
3623 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003624
Chris Lattner5839bf22005-08-26 17:15:30 +00003625 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003626 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3627 SDOperand FudgeInReg;
3628 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003629 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3630 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003631 else {
3632 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003633 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3634 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003635 }
Chris Lattner473a9902005-09-29 06:44:39 +00003636 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003637 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003638
Chris Lattnera88a2602005-05-14 05:33:54 +00003639 // Check to see if the target has a custom way to lower this. If so, use it.
3640 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3641 default: assert(0 && "This action not implemented for this operation!");
3642 case TargetLowering::Legal:
3643 case TargetLowering::Expand:
3644 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003645 case TargetLowering::Custom: {
3646 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3647 Source), DAG);
3648 if (NV.Val)
3649 return LegalizeOp(NV);
3650 break; // The target decided this was legal after all
3651 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003652 }
3653
Chris Lattner13689e22005-05-12 07:00:44 +00003654 // Expand the source, then glue it back together for the call. We must expand
3655 // the source in case it is shared (this pass of legalize must traverse it).
3656 SDOperand SrcLo, SrcHi;
3657 ExpandOp(Source, SrcLo, SrcHi);
3658 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3659
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003660 SDNode *OutChain = 0;
3661 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3662 DAG.getEntryNode());
3663 const char *FnName = 0;
3664 if (DestTy == MVT::f32)
3665 FnName = "__floatdisf";
3666 else {
3667 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3668 FnName = "__floatdidf";
3669 }
3670
Chris Lattner77e77a62005-01-21 06:05:23 +00003671 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3672
3673 TargetLowering::ArgListTy Args;
3674 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003675
Chris Lattner77e77a62005-01-21 06:05:23 +00003676 Args.push_back(std::make_pair(Source, ArgTy));
3677
3678 // We don't care about token chains for libcalls. We just use the entry
3679 // node as our input and ignore the output chain. This allows us to place
3680 // calls wherever we need them to satisfy data dependences.
3681 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003682
3683 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003684 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3685 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003686
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003687 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003688 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003689}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003690
Chris Lattnere34b3962005-01-19 04:19:40 +00003691
3692
Chris Lattner3e928bb2005-01-07 07:47:09 +00003693/// ExpandOp - Expand the specified SDOperand into its two component pieces
3694/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3695/// LegalizeNodes map is filled in for any results that are not expanded, the
3696/// ExpandedNodes map is filled in for any results that are expanded, and the
3697/// Lo/Hi values are returned.
3698void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3699 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003700 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003701 SDNode *Node = Op.Val;
3702 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003703 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3704 "Cannot expand FP values!");
3705 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003706 "Cannot expand to FP value or to larger int value!");
3707
Chris Lattner6fdcb252005-09-02 20:32:45 +00003708 // See if we already expanded it.
3709 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3710 = ExpandedNodes.find(Op);
3711 if (I != ExpandedNodes.end()) {
3712 Lo = I->second.first;
3713 Hi = I->second.second;
3714 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003715 }
3716
Chris Lattner4e6c7462005-01-08 19:27:05 +00003717 // Expanding to multiple registers needs to perform an optimization step, and
3718 // is not careful to avoid operations the target does not support. Make sure
3719 // that all generated operations are legalized in the next iteration.
3720 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003721
Chris Lattner3e928bb2005-01-07 07:47:09 +00003722 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003723 case ISD::CopyFromReg:
3724 assert(0 && "CopyFromReg must be legal!");
3725 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003726 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3727 assert(0 && "Do not know how to expand this operator!");
3728 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003729 case ISD::UNDEF:
3730 Lo = DAG.getNode(ISD::UNDEF, NVT);
3731 Hi = DAG.getNode(ISD::UNDEF, NVT);
3732 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003733 case ISD::Constant: {
3734 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3735 Lo = DAG.getConstant(Cst, NVT);
3736 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3737 break;
3738 }
Nate Begemancc827e62005-12-07 19:48:11 +00003739 case ISD::ConstantVec: {
3740 unsigned NumElements = Node->getNumOperands();
3741 // If we only have two elements left in the constant vector, just break it
3742 // apart into the two scalar constants it contains. Otherwise, bisect the
3743 // ConstantVec, and return each half as a new ConstantVec.
3744 // FIXME: this is hard coded as big endian, it may have to change to support
3745 // SSE and Alpha MVI
3746 if (NumElements == 2) {
3747 Hi = Node->getOperand(0);
3748 Lo = Node->getOperand(1);
3749 } else {
3750 NumElements /= 2;
3751 std::vector<SDOperand> LoOps, HiOps;
3752 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3753 HiOps.push_back(Node->getOperand(I));
3754 LoOps.push_back(Node->getOperand(I+NumElements));
3755 }
3756 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3757 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3758 }
3759 break;
3760 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003761
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003762 case ISD::BUILD_PAIR:
3763 // Legalize both operands. FIXME: in the future we should handle the case
3764 // where the two elements are not legal.
3765 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3766 Lo = LegalizeOp(Node->getOperand(0));
3767 Hi = LegalizeOp(Node->getOperand(1));
3768 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003769
3770 case ISD::SIGN_EXTEND_INREG:
3771 ExpandOp(Node->getOperand(0), Lo, Hi);
3772 // Sign extend the lo-part.
3773 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3774 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3775 TLI.getShiftAmountTy()));
3776 // sext_inreg the low part if needed.
3777 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3778 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003779
Nate Begemand88fc032006-01-14 03:14:10 +00003780 case ISD::BSWAP: {
3781 ExpandOp(Node->getOperand(0), Lo, Hi);
3782 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3783 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3784 Lo = TempLo;
3785 break;
3786 }
3787
Chris Lattneredb1add2005-05-11 04:51:16 +00003788 case ISD::CTPOP:
3789 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003790 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3791 DAG.getNode(ISD::CTPOP, NVT, Lo),
3792 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003793 Hi = DAG.getConstant(0, NVT);
3794 break;
3795
Chris Lattner39a8f332005-05-12 19:05:01 +00003796 case ISD::CTLZ: {
3797 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003798 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003799 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3800 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003801 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3802 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003803 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3804 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3805
3806 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3807 Hi = DAG.getConstant(0, NVT);
3808 break;
3809 }
3810
3811 case ISD::CTTZ: {
3812 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003813 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003814 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3815 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003816 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3817 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003818 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3819 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3820
3821 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3822 Hi = DAG.getConstant(0, NVT);
3823 break;
3824 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003825
Chris Lattner3e928bb2005-01-07 07:47:09 +00003826 case ISD::LOAD: {
3827 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3828 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003829 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003830
3831 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003832 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003833 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3834 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003835 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003836 //are from the same instruction?
3837 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003838
3839 // Build a factor node to remember that this load is independent of the
3840 // other one.
3841 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3842 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003843
Chris Lattner3e928bb2005-01-07 07:47:09 +00003844 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003845 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003846 if (!TLI.isLittleEndian())
3847 std::swap(Lo, Hi);
3848 break;
3849 }
Nate Begemanab48be32005-11-22 18:16:00 +00003850 case ISD::VLOAD: {
3851 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3852 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3853 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3854 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3855
3856 // If we only have two elements, turn into a pair of scalar loads.
3857 // FIXME: handle case where a vector of two elements is fine, such as
3858 // 2 x double on SSE2.
3859 if (NumElements == 2) {
3860 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3861 // Increment the pointer to the other half.
3862 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3863 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3864 getIntPtrConstant(IncrementSize));
3865 //Is this safe? declaring that the two parts of the split load
3866 //are from the same instruction?
3867 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3868 } else {
3869 NumElements /= 2; // Split the vector in half
3870 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3871 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3872 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3873 getIntPtrConstant(IncrementSize));
3874 //Is this safe? declaring that the two parts of the split load
3875 //are from the same instruction?
3876 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3877 }
3878
3879 // Build a factor node to remember that this load is independent of the
3880 // other one.
3881 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3882 Hi.getValue(1));
3883
3884 // Remember that we legalized the chain.
3885 AddLegalizedOperand(Op.getValue(1), TF);
3886 if (!TLI.isLittleEndian())
3887 std::swap(Lo, Hi);
3888 break;
3889 }
3890 case ISD::VADD:
3891 case ISD::VSUB:
3892 case ISD::VMUL: {
3893 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3894 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3895 SDOperand LL, LH, RL, RH;
3896
3897 ExpandOp(Node->getOperand(0), LL, LH);
3898 ExpandOp(Node->getOperand(1), RL, RH);
3899
3900 // If we only have two elements, turn into a pair of scalar loads.
3901 // FIXME: handle case where a vector of two elements is fine, such as
3902 // 2 x double on SSE2.
3903 if (NumElements == 2) {
3904 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3905 Lo = DAG.getNode(Opc, EVT, LL, RL);
3906 Hi = DAG.getNode(Opc, EVT, LH, RH);
3907 } else {
3908 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3909 LL.getOperand(3));
3910 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3911 LH.getOperand(3));
3912 }
3913 break;
3914 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003915 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003916 case ISD::CALL: {
3917 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3918 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3919
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003920 bool Changed = false;
3921 std::vector<SDOperand> Ops;
3922 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3923 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3924 Changed |= Ops.back() != Node->getOperand(i);
3925 }
3926
Chris Lattner3e928bb2005-01-07 07:47:09 +00003927 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3928 "Can only expand a call once so far, not i64 -> i16!");
3929
3930 std::vector<MVT::ValueType> RetTyVTs;
3931 RetTyVTs.reserve(3);
3932 RetTyVTs.push_back(NVT);
3933 RetTyVTs.push_back(NVT);
3934 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003935 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3936 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003937 Lo = SDOperand(NC, 0);
3938 Hi = SDOperand(NC, 1);
3939
3940 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003941 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003942 break;
3943 }
3944 case ISD::AND:
3945 case ISD::OR:
3946 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3947 SDOperand LL, LH, RL, RH;
3948 ExpandOp(Node->getOperand(0), LL, LH);
3949 ExpandOp(Node->getOperand(1), RL, RH);
3950 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3951 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3952 break;
3953 }
3954 case ISD::SELECT: {
3955 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003956
3957 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3958 case Expand: assert(0 && "It's impossible to expand bools");
3959 case Legal:
3960 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3961 break;
3962 case Promote:
3963 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3964 break;
3965 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003966 ExpandOp(Node->getOperand(1), LL, LH);
3967 ExpandOp(Node->getOperand(2), RL, RH);
3968 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3969 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3970 break;
3971 }
Nate Begeman9373a812005-08-10 20:51:12 +00003972 case ISD::SELECT_CC: {
3973 SDOperand TL, TH, FL, FH;
3974 ExpandOp(Node->getOperand(2), TL, TH);
3975 ExpandOp(Node->getOperand(3), FL, FH);
3976 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3977 Node->getOperand(1), TL, FL, Node->getOperand(4));
3978 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3979 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003980 Lo = LegalizeOp(Lo);
3981 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003982 break;
3983 }
Nate Begeman144ff662005-10-13 17:15:37 +00003984 case ISD::SEXTLOAD: {
3985 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3986 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3987 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3988
3989 if (EVT == NVT)
3990 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3991 else
3992 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3993 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003994
3995 // Remember that we legalized the chain.
3996 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3997
Nate Begeman144ff662005-10-13 17:15:37 +00003998 // The high part is obtained by SRA'ing all but one of the bits of the lo
3999 // part.
4000 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4001 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4002 TLI.getShiftAmountTy()));
4003 Lo = LegalizeOp(Lo);
4004 Hi = LegalizeOp(Hi);
4005 break;
4006 }
4007 case ISD::ZEXTLOAD: {
4008 SDOperand Chain = LegalizeOp(Node->getOperand(0));
4009 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
4010 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4011
4012 if (EVT == NVT)
4013 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4014 else
4015 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4016 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004017
4018 // Remember that we legalized the chain.
4019 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
4020
Nate Begeman144ff662005-10-13 17:15:37 +00004021 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00004022 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00004023 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00004024 break;
4025 }
4026 case ISD::EXTLOAD: {
4027 SDOperand Chain = LegalizeOp(Node->getOperand(0));
4028 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
4029 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4030
4031 if (EVT == NVT)
4032 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4033 else
4034 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4035 EVT);
4036
4037 // Remember that we legalized the chain.
4038 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
4039
4040 // The high part is undefined.
4041 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
4042 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00004043 break;
4044 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004045 case ISD::ANY_EXTEND: {
4046 SDOperand In;
4047 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4048 case Expand: assert(0 && "expand-expand not implemented yet!");
4049 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4050 case Promote:
4051 In = PromoteOp(Node->getOperand(0));
4052 break;
4053 }
4054
4055 // The low part is any extension of the input (which degenerates to a copy).
4056 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
4057 // The high part is undefined.
4058 Hi = DAG.getNode(ISD::UNDEF, NVT);
4059 break;
4060 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004061 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00004062 SDOperand In;
4063 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4064 case Expand: assert(0 && "expand-expand not implemented yet!");
4065 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4066 case Promote:
4067 In = PromoteOp(Node->getOperand(0));
4068 // Emit the appropriate sign_extend_inreg to get the value we want.
4069 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00004070 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00004071 break;
4072 }
4073
Chris Lattner3e928bb2005-01-07 07:47:09 +00004074 // The low part is just a sign extension of the input (which degenerates to
4075 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00004076 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004077
Chris Lattner3e928bb2005-01-07 07:47:09 +00004078 // The high part is obtained by SRA'ing all but one of the bits of the lo
4079 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004080 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00004081 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4082 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004083 break;
4084 }
Chris Lattner06098e02005-04-03 23:41:52 +00004085 case ISD::ZERO_EXTEND: {
4086 SDOperand In;
4087 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4088 case Expand: assert(0 && "expand-expand not implemented yet!");
4089 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4090 case Promote:
4091 In = PromoteOp(Node->getOperand(0));
4092 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00004093 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00004094 break;
4095 }
4096
Chris Lattner3e928bb2005-01-07 07:47:09 +00004097 // The low part is just a zero extension of the input (which degenerates to
4098 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00004099 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004100
Chris Lattner3e928bb2005-01-07 07:47:09 +00004101 // The high part is just a zero.
4102 Hi = DAG.getConstant(0, NVT);
4103 break;
Chris Lattner06098e02005-04-03 23:41:52 +00004104 }
Chris Lattner35481892005-12-23 00:16:34 +00004105
4106 case ISD::BIT_CONVERT: {
4107 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4108 Node->getOperand(0));
4109 ExpandOp(Tmp, Lo, Hi);
4110 break;
4111 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004112
Chris Lattner308575b2005-11-20 22:56:56 +00004113 case ISD::READCYCLECOUNTER: {
4114 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4115 TargetLowering::Custom &&
4116 "Must custom expand ReadCycleCounter");
4117 SDOperand T = TLI.LowerOperation(Op, DAG);
4118 assert(T.Val && "Node must be custom expanded!");
4119 Lo = LegalizeOp(T.getValue(0));
4120 Hi = LegalizeOp(T.getValue(1));
4121 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
4122 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004123 break;
Chris Lattner308575b2005-11-20 22:56:56 +00004124 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004125
Chris Lattner4e6c7462005-01-08 19:27:05 +00004126 // These operators cannot be expanded directly, emit them as calls to
4127 // library functions.
4128 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004129 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004130 SDOperand Op;
4131 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4132 case Expand: assert(0 && "cannot expand FP!");
4133 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4134 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
4135 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004136
Chris Lattnerf20d1832005-07-30 01:40:57 +00004137 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4138
Chris Lattner80a3e942005-07-29 00:33:32 +00004139 // Now that the custom expander is done, expand the result, which is still
4140 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004141 if (Op.Val) {
4142 ExpandOp(Op, Lo, Hi);
4143 break;
4144 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004145 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004146
Chris Lattner4e6c7462005-01-08 19:27:05 +00004147 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004148 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004149 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004150 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004151 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004152
Chris Lattner4e6c7462005-01-08 19:27:05 +00004153 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004154 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
4155 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
4156 LegalizeOp(Node->getOperand(0)));
4157 // Now that the custom expander is done, expand the result, which is still
4158 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004159 Op = TLI.LowerOperation(Op, DAG);
4160 if (Op.Val) {
4161 ExpandOp(Op, Lo, Hi);
4162 break;
4163 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004164 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004165
Chris Lattner4e6c7462005-01-08 19:27:05 +00004166 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004167 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004168 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004169 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004170 break;
4171
Evan Cheng05a2d562006-01-09 18:31:59 +00004172 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004173 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004174 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004175 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
4176 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
Chris Lattner348e93c2006-01-21 04:27:00 +00004177 ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004178 Op = TLI.LowerOperation(Op, DAG);
4179 if (Op.Val) {
4180 // Now that the custom expander is done, expand the result, which is
4181 // still VT.
4182 ExpandOp(Op, Lo, Hi);
4183 break;
4184 }
4185 }
4186
Chris Lattnere34b3962005-01-19 04:19:40 +00004187 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004188 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004189 break;
Chris Lattner47599822005-04-02 03:38:53 +00004190
4191 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004192 TargetLowering::LegalizeAction Action =
4193 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4194 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4195 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004196 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004197 break;
4198 }
4199
Chris Lattnere34b3962005-01-19 04:19:40 +00004200 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004201 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004202 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004203 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004204
Evan Cheng05a2d562006-01-09 18:31:59 +00004205 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004206 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004207 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004208 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
4209 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
Chris Lattner348e93c2006-01-21 04:27:00 +00004210 ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004211 Op = TLI.LowerOperation(Op, DAG);
4212 if (Op.Val) {
4213 // Now that the custom expander is done, expand the result, which is
4214 // still VT.
4215 ExpandOp(Op, Lo, Hi);
4216 break;
4217 }
4218 }
4219
Chris Lattnere34b3962005-01-19 04:19:40 +00004220 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004221 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004222 break;
Chris Lattner47599822005-04-02 03:38:53 +00004223
4224 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004225 TargetLowering::LegalizeAction Action =
4226 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4227 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4228 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004229 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004230 break;
4231 }
4232
Chris Lattnere34b3962005-01-19 04:19:40 +00004233 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004234 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004235 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004236 }
4237
4238 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004239 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004240 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004241 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
4242 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
Chris Lattner348e93c2006-01-21 04:27:00 +00004243 ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004244 Op = TLI.LowerOperation(Op, DAG);
4245 if (Op.Val) {
4246 // Now that the custom expander is done, expand the result, which is
4247 // still VT.
4248 ExpandOp(Op, Lo, Hi);
4249 break;
4250 }
4251 }
4252
Chris Lattnere34b3962005-01-19 04:19:40 +00004253 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004254 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004255 break;
Chris Lattner47599822005-04-02 03:38:53 +00004256
4257 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004258 TargetLowering::LegalizeAction Action =
4259 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4260 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4261 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004262 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004263 break;
4264 }
4265
Chris Lattnere34b3962005-01-19 04:19:40 +00004266 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004267 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004268 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004269 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004270
Misha Brukmanedf128a2005-04-21 22:36:52 +00004271 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00004272 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
4273 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00004274 break;
4275 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00004276 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
4277 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00004278 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00004279 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004280 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004281 SDOperand LL, LH, RL, RH;
4282 ExpandOp(Node->getOperand(0), LL, LH);
4283 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004284 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4285 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4286 // extended the sign bit of the low half through the upper half, and if so
4287 // emit a MULHS instead of the alternate sequence that is valid for any
4288 // i64 x i64 multiply.
4289 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4290 // is RH an extension of the sign bit of RL?
4291 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4292 RH.getOperand(1).getOpcode() == ISD::Constant &&
4293 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4294 // is LH an extension of the sign bit of LL?
4295 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4296 LH.getOperand(1).getOpcode() == ISD::Constant &&
4297 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4298 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4299 } else {
4300 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4301 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4302 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4303 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4304 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4305 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004306 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4307 } else {
4308 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
4309 }
4310 break;
4311 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004312 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4313 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4314 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4315 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004316 }
4317
Chris Lattner83397362005-12-21 18:02:52 +00004318 // Make sure the resultant values have been legalized themselves, unless this
4319 // is a type that requires multi-step expansion.
4320 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4321 Lo = LegalizeOp(Lo);
4322 Hi = LegalizeOp(Hi);
4323 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004324
4325 // Remember in a map if the values will be reused later.
4326 bool isNew =
4327 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4328 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004329}
4330
4331
4332// SelectionDAG::Legalize - This is the entry point for the file.
4333//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004334void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004335 /// run - This is the main entry point to this class.
4336 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004337 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004338}
4339