blob: 0521270b071bb1ac091dba46c4889f6f7b899224 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-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 Lattnerdc750592005-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 Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Jim Laskeyf2516a92005-08-17 00:39:29 +0000168/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
169/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000170/// we expand it. At this point, we know that the result and operand types are
171/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand Op0,
174 MVT::ValueType DestVT) {
175 if (Op0.getValueType() == MVT::i32) {
176 // simple 32-bit [signed|unsigned] integer to float/double expansion
177
178 // get the stack frame index of a 8 byte buffer
179 MachineFunction &MF = DAG.getMachineFunction();
180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
181 // get address of 8 byte buffer
182 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
183 // word offset constant for Hi/Lo address computation
184 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
185 // set up Hi and Lo (into buffer) address based on endian
186 SDOperand Hi, Lo;
187 if (TLI.isLittleEndian()) {
188 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
189 Lo = StackSlot;
190 } else {
191 Hi = StackSlot;
192 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 }
194 // if signed map to unsigned space
195 SDOperand Op0Mapped;
196 if (isSigned) {
197 // constant used to invert sign bit (signed to unsigned mapping)
198 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
199 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
200 } else {
201 Op0Mapped = Op0;
202 }
203 // store the lo of the constructed double - based on integer input
204 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
205 Op0Mapped, Lo, DAG.getSrcValue(NULL));
206 // initial hi portion of constructed double
207 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
208 // store the hi of the constructed double - biased exponent
209 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
210 InitialHi, Hi, DAG.getSrcValue(NULL));
211 // load the constructed double
212 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
213 DAG.getSrcValue(NULL));
214 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000221 // final result
222 SDOperand Result;
223 // handle final rounding
224 if (DestVT == MVT::f64) {
225 // do nothing
226 Result = Sub;
227 } else {
228 // if f32 then cast to f32
229 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
230 }
231 NeedsAnotherIteration = true;
232 return Result;
233 }
234 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattnerd47675e2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000240 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
241 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
242 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Jim Laskeyf2516a92005-08-17 00:39:29 +0000244 // If the sign bit of the integer is set, the large number will be treated
245 // as a negative number. To counteract this, the dynamic code adds an
246 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000247 uint64_t FF;
248 switch (Op0.getValueType()) {
249 default: assert(0 && "Unsupported integer type!");
250 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
251 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
252 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
253 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
254 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnerc30405e2005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
260 SDOperand FudgeInReg;
261 if (DestVT == MVT::f32)
262 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL));
264 else {
265 assert(DestVT == MVT::f64 && "Unexpected conversion");
266 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
267 DAG.getEntryNode(), CPIdx,
268 DAG.getSrcValue(NULL), MVT::f32));
269 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000273}
274
Chris Lattner19732782005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000277/// we promote it. At this point, we know that the result and operand types are
278/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
279/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // First step, figure out the appropriate *INT_TO_FP operation to use.
284 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000285
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // Scan for the appropriate larger type to use.
289 while (1) {
290 NewInTy = (MVT::ValueType)(NewInTy+1);
291 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Chris Lattnere3e847b2005-07-16 00:19:57 +0000293 // If the target supports SINT_TO_FP of this type, use it.
294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
295 default: break;
296 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000298 break; // Can't use this datatype.
299 // FALL THROUGH.
300 case TargetLowering::Custom:
301 OpToUse = ISD::SINT_TO_FP;
302 break;
303 }
304 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Chris Lattnere3e847b2005-07-16 00:19:57 +0000307 // If the target supports UINT_TO_FP of this type, use it.
308 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
309 default: break;
310 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000312 break; // Can't use this datatype.
313 // FALL THROUGH.
314 case TargetLowering::Custom:
315 OpToUse = ISD::UINT_TO_FP;
316 break;
317 }
318 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000319
Chris Lattnere3e847b2005-07-16 00:19:57 +0000320 // Otherwise, try a larger type.
321 }
322
323 // Make sure to legalize any nodes we create here in the next pass.
324 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000325
Chris Lattnere3e847b2005-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.
328 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000331}
332
Chris Lattner44fe26f2005-07-29 00:11:56 +0000333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
334/// FP_TO_*INT operation of the specified operand when the target requests that
335/// we promote it. At this point, we know that the result and operand types are
336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
337/// operation that returns a larger result.
338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
339 MVT::ValueType DestVT,
340 bool isSigned) {
341 // First step, figure out the appropriate FP_TO*INT operation to use.
342 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000343
Chris Lattner44fe26f2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // Scan for the appropriate larger type to use.
347 while (1) {
348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000350
Chris Lattner44fe26f2005-07-29 00:11:56 +0000351 // If the target supports FP_TO_SINT returning this type, use it.
352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
353 default: break;
354 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000356 break; // Can't use this datatype.
357 // FALL THROUGH.
358 case TargetLowering::Custom:
359 OpToUse = ISD::FP_TO_SINT;
360 break;
361 }
362 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-07-29 00:11:56 +0000364 // If the target supports FP_TO_UINT of this type, use it.
365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
366 default: break;
367 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 break; // Can't use this datatype.
370 // FALL THROUGH.
371 case TargetLowering::Custom:
372 OpToUse = ISD::FP_TO_UINT;
373 break;
374 }
375 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000376
Chris Lattner44fe26f2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000379
Chris Lattner44fe26f2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000382
Chris Lattner44fe26f2005-07-29 00:11:56 +0000383 // Okay, we found the operation and type to use. Truncate the result of the
384 // extended FP_TO_*INT operation to the desired size.
385 return DAG.getNode(ISD::TRUNCATE, DestVT,
386 DAG.getNode(OpToUse, NewOutTy, LegalOp));
387}
388
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000535 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000536 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000537 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000538 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000539 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000540 case ISD::BasicBlock:
541 case ISD::CONDCODE:
542 case ISD::VALUETYPE:
543 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000544 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000545 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
546 default: assert(0 && "This action is not supported yet!");
547 case TargetLowering::Custom: {
548 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
549 if (Tmp.Val) {
550 Result = LegalizeOp(Tmp);
551 break;
552 }
553 } // FALLTHROUGH if the target doesn't want to lower this op after all.
554 case TargetLowering::Legal:
555 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
556 break;
557 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000558 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000559 case ISD::AssertSext:
560 case ISD::AssertZext:
561 Tmp1 = LegalizeOp(Node->getOperand(0));
562 if (Tmp1 != Node->getOperand(0))
563 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
564 Node->getOperand(1));
565 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000566 case ISD::MERGE_VALUES:
567 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000570 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000571 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000572 if (Tmp1 != Node->getOperand(0))
573 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000574 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000575 Node->getValueType(0));
576 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000577 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
578 if (Node->getNumOperands() == 3)
579 Tmp2 = LegalizeOp(Node->getOperand(2));
580 if (Tmp1 != Node->getOperand(0) ||
581 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattnere3c67e92005-12-18 15:27:43 +0000582 Result = DAG.getCopyFromReg(Tmp1,
583 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
584 Node->getValueType(0), Tmp2);
585 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
586 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000587 // Since CopyFromReg produces two values, make sure to remember that we
588 // legalized both of them.
589 AddLegalizedOperand(Op.getValue(0), Result);
590 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
591 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000592 case ISD::ImplicitDef:
593 Tmp1 = LegalizeOp(Node->getOperand(0));
594 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000595 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
596 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000597 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000598 case ISD::UNDEF: {
599 MVT::ValueType VT = Op.getValueType();
600 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000601 default: assert(0 && "This action is not supported yet!");
602 case TargetLowering::Expand:
603 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000604 if (MVT::isInteger(VT))
605 Result = DAG.getConstant(0, VT);
606 else if (MVT::isFloatingPoint(VT))
607 Result = DAG.getConstantFP(0, VT);
608 else
609 assert(0 && "Unknown value type!");
610 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000611 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000612 break;
613 }
614 break;
615 }
Chris Lattner435b4022005-11-29 06:21:05 +0000616
617 case ISD::LOCATION:
618 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
620
621 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
622 case TargetLowering::Promote:
623 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000624 case TargetLowering::Expand: {
625 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
626 std::vector<SDOperand> Ops;
627 Ops.push_back(Tmp1); // chain
628 Ops.push_back(Node->getOperand(1)); // line #
629 Ops.push_back(Node->getOperand(2)); // col #
630 const std::string &fname = cast<StringSDNode>(Node->getOperand(3))->getValue();
631 const std::string &dirname=cast<StringSDNode>(Node->getOperand(4))->getValue();
632 unsigned id = DebugInfo.RecordSource(fname, dirname);
633 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
634 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
635 }
Chris Lattner435b4022005-11-29 06:21:05 +0000636 break;
637 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000638 if (Tmp1 != Node->getOperand(0) ||
639 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000640 std::vector<SDOperand> Ops;
641 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000642 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
643 Ops.push_back(Node->getOperand(1)); // line # must be legal.
644 Ops.push_back(Node->getOperand(2)); // col # must be legal.
645 } else {
646 // Otherwise promote them.
647 Ops.push_back(PromoteOp(Node->getOperand(1)));
648 Ops.push_back(PromoteOp(Node->getOperand(2)));
649 }
Chris Lattner435b4022005-11-29 06:21:05 +0000650 Ops.push_back(Node->getOperand(3)); // filename must be legal.
651 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
652 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
653 }
654 break;
655 }
656 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000657
658 case ISD::DEBUG_LOC:
659 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
660 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
661 case TargetLowering::Promote:
662 case TargetLowering::Expand:
663 default: assert(0 && "This action is not supported yet!");
664 case TargetLowering::Legal:
665 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
666 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
667 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
668 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
669
670 if (Tmp1 != Node->getOperand(0) ||
671 Tmp2 != Node->getOperand(1) ||
672 Tmp3 != Node->getOperand(2) ||
673 Tmp4 != Node->getOperand(3)) {
674 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
675 }
676 break;
677 }
678 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000679
Chris Lattnerdc750592005-01-07 07:47:09 +0000680 case ISD::Constant:
681 // We know we don't need to expand constants here, constants only have one
682 // value and we check that it is fine above.
683
684 // FIXME: Maybe we should handle things like targets that don't support full
685 // 32-bit immediates?
686 break;
687 case ISD::ConstantFP: {
688 // Spill FP immediates to the constant pool if the target cannot directly
689 // codegen them. Targets often have some immediate values that can be
690 // efficiently generated into an FP register without a load. We explicitly
691 // leave these constants as ConstantFP nodes for the target to deal with.
692
693 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
694
695 // Check to see if this FP immediate is already legal.
696 bool isLegal = false;
697 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
698 E = TLI.legal_fpimm_end(); I != E; ++I)
699 if (CFP->isExactlyValue(*I)) {
700 isLegal = true;
701 break;
702 }
703
704 if (!isLegal) {
705 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000706 bool Extend = false;
707
708 // If a FP immediate is precise when represented as a float, we put it
709 // into the constant pool as a float, even if it's is statically typed
710 // as a double.
711 MVT::ValueType VT = CFP->getValueType(0);
712 bool isDouble = VT == MVT::f64;
713 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
714 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000715 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
716 // Only do this if the target has a native EXTLOAD instruction from
717 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000718 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000719 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
720 VT = MVT::f32;
721 Extend = true;
722 }
Misha Brukman835702a2005-04-21 22:36:52 +0000723
Nate Begeman4e56db62005-12-10 02:36:00 +0000724 SDOperand CPIdx =
725 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000726 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000727 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
728 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000729 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000730 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
731 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000732 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000733 }
734 break;
735 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000736 case ISD::ConstantVec: {
737 // We assume that vector constants are not legal, and will be immediately
738 // spilled to the constant pool.
739 //
740 // FIXME: revisit this when we have some kind of mechanism by which targets
741 // can decided legality of vector constants, of which there may be very
742 // many.
743 //
744 // Create a ConstantPacked, and put it in the constant pool.
745 std::vector<Constant*> CV;
746 MVT::ValueType VT = Node->getValueType(0);
747 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
748 SDOperand OpN = Node->getOperand(I);
749 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
750 if (MVT::isFloatingPoint(VT))
751 CV.push_back(ConstantFP::get(OpNTy,
752 cast<ConstantFPSDNode>(OpN)->getValue()));
753 else
754 CV.push_back(ConstantUInt::get(OpNTy,
755 cast<ConstantSDNode>(OpN)->getValue()));
756 }
757 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000758 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000759 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
760 break;
761 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000762 case ISD::TokenFactor:
763 if (Node->getNumOperands() == 2) {
764 bool Changed = false;
765 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
766 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
767 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
768 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
769 } else {
770 std::vector<SDOperand> Ops;
771 bool Changed = false;
772 // Legalize the operands.
773 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
774 SDOperand Op = Node->getOperand(i);
775 Ops.push_back(LegalizeOp(Op));
776 Changed |= Ops[i] != Op;
777 }
778 if (Changed)
779 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000780 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000781 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000782
Chris Lattner2dce7032005-05-12 23:24:06 +0000783 case ISD::CALLSEQ_START:
784 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000785 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000786 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000787 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000788 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000789 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000790
Chris Lattner2dce7032005-05-12 23:24:06 +0000791 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000792 // nodes are treated specially and are mutated in place. This makes the dag
793 // legalization process more efficient and also makes libcall insertion
794 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000795 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000796 case ISD::DYNAMIC_STACKALLOC:
797 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
798 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
799 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
800 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000801 Tmp3 != Node->getOperand(2)) {
802 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
803 std::vector<SDOperand> Ops;
804 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
805 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
806 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000807 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000808
809 // Since this op produces two values, make sure to remember that we
810 // legalized both of them.
811 AddLegalizedOperand(SDOperand(Node, 0), Result);
812 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
813 return Result.getValue(Op.ResNo);
814
Chris Lattnerd0feb642005-05-13 18:43:43 +0000815 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000816 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000817 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
818 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000819
820 bool Changed = false;
821 std::vector<SDOperand> Ops;
822 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
823 Ops.push_back(LegalizeOp(Node->getOperand(i)));
824 Changed |= Ops.back() != Node->getOperand(i);
825 }
826
827 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000828 std::vector<MVT::ValueType> RetTyVTs;
829 RetTyVTs.reserve(Node->getNumValues());
830 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000831 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000832 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
833 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000834 } else {
835 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000836 }
Chris Lattner9242c502005-01-09 19:43:23 +0000837 // Since calls produce multiple values, make sure to remember that we
838 // legalized all of them.
839 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
840 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
841 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000842 }
Chris Lattner68a12142005-01-07 22:12:08 +0000843 case ISD::BR:
844 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
845 if (Tmp1 != Node->getOperand(0))
846 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
847 break;
848
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000849 case ISD::BRCOND:
850 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000851
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000852 switch (getTypeAction(Node->getOperand(1).getValueType())) {
853 case Expand: assert(0 && "It's impossible to expand bools");
854 case Legal:
855 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
856 break;
857 case Promote:
858 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
859 break;
860 }
Nate Begeman371e4952005-08-16 19:49:35 +0000861
862 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
863 default: assert(0 && "This action is not supported yet!");
864 case TargetLowering::Expand:
865 // Expand brcond's setcc into its constituent parts and create a BR_CC
866 // Node.
867 if (Tmp2.getOpcode() == ISD::SETCC) {
868 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
869 Tmp2.getOperand(0), Tmp2.getOperand(1),
870 Node->getOperand(2));
871 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000872 // Make sure the condition is either zero or one. It may have been
873 // promoted from something else.
874 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
875
Nate Begeman371e4952005-08-16 19:49:35 +0000876 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
877 DAG.getCondCode(ISD::SETNE), Tmp2,
878 DAG.getConstant(0, Tmp2.getValueType()),
879 Node->getOperand(2));
880 }
881 break;
882 case TargetLowering::Legal:
883 // Basic block destination (Op#2) is always legal.
884 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
885 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
886 Node->getOperand(2));
887 break;
888 }
889 break;
890 case ISD::BR_CC:
891 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000892 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000893 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
894 Node->getOperand(2), // LHS
895 Node->getOperand(3), // RHS
896 Node->getOperand(1)));
897 // If we get a SETCC back from legalizing the SETCC node we just
898 // created, then use its LHS, RHS, and CC directly in creating a new
899 // node. Otherwise, select between the true and false value based on
900 // comparing the result of the legalized with zero.
901 if (Tmp2.getOpcode() == ISD::SETCC) {
902 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
903 Tmp2.getOperand(0), Tmp2.getOperand(1),
904 Node->getOperand(4));
905 } else {
906 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
907 DAG.getCondCode(ISD::SETNE),
908 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
909 Node->getOperand(4));
910 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000911 break;
912 }
913
914 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
915 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
916
917 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
918 default: assert(0 && "Unexpected action for BR_CC!");
919 case TargetLowering::Custom: {
920 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
921 Tmp2, Tmp3, Node->getOperand(4));
922 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
923 if (Tmp4.Val) {
924 Result = LegalizeOp(Tmp4);
925 break;
926 }
927 } // FALLTHROUGH if the target doesn't want to lower this op after all.
928 case TargetLowering::Legal:
929 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
930 Tmp3 != Node->getOperand(3)) {
931 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
932 Tmp2, Tmp3, Node->getOperand(4));
933 }
934 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000935 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000936 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000937 case ISD::BRCONDTWOWAY:
938 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
939 switch (getTypeAction(Node->getOperand(1).getValueType())) {
940 case Expand: assert(0 && "It's impossible to expand bools");
941 case Legal:
942 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
943 break;
944 case Promote:
945 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
946 break;
947 }
948 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
949 // pair.
950 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
951 case TargetLowering::Promote:
952 default: assert(0 && "This action is not supported yet!");
953 case TargetLowering::Legal:
954 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
955 std::vector<SDOperand> Ops;
956 Ops.push_back(Tmp1);
957 Ops.push_back(Tmp2);
958 Ops.push_back(Node->getOperand(2));
959 Ops.push_back(Node->getOperand(3));
960 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
961 }
962 break;
963 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000964 // If BRTWOWAY_CC is legal for this target, then simply expand this node
965 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
966 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000967 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000968 if (Tmp2.getOpcode() == ISD::SETCC) {
969 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
970 Tmp2.getOperand(0), Tmp2.getOperand(1),
971 Node->getOperand(2), Node->getOperand(3));
972 } else {
973 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
974 DAG.getConstant(0, Tmp2.getValueType()),
975 Node->getOperand(2), Node->getOperand(3));
976 }
977 } else {
978 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000979 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000980 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
981 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000982 break;
983 }
984 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000985 case ISD::BRTWOWAY_CC:
986 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000987 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000988 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
989 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
990 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
991 Tmp3 != Node->getOperand(3)) {
992 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
993 Node->getOperand(4), Node->getOperand(5));
994 }
995 break;
996 } else {
997 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
998 Node->getOperand(2), // LHS
999 Node->getOperand(3), // RHS
1000 Node->getOperand(1)));
1001 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1002 // pair.
1003 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1004 default: assert(0 && "This action is not supported yet!");
1005 case TargetLowering::Legal:
1006 // If we get a SETCC back from legalizing the SETCC node we just
1007 // created, then use its LHS, RHS, and CC directly in creating a new
1008 // node. Otherwise, select between the true and false value based on
1009 // comparing the result of the legalized with zero.
1010 if (Tmp2.getOpcode() == ISD::SETCC) {
1011 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1012 Tmp2.getOperand(0), Tmp2.getOperand(1),
1013 Node->getOperand(4), Node->getOperand(5));
1014 } else {
1015 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1016 DAG.getConstant(0, Tmp2.getValueType()),
1017 Node->getOperand(4), Node->getOperand(5));
1018 }
1019 break;
1020 case TargetLowering::Expand:
1021 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1022 Node->getOperand(4));
1023 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1024 break;
1025 }
1026 }
1027 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001028 case ISD::LOAD:
1029 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1030 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001031
Chris Lattnerdc750592005-01-07 07:47:09 +00001032 if (Tmp1 != Node->getOperand(0) ||
1033 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +00001034 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1035 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001036 else
1037 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001038
Chris Lattnerea4ca942005-01-07 22:28:47 +00001039 // Since loads produce two values, make sure to remember that we legalized
1040 // both of them.
1041 AddLegalizedOperand(SDOperand(Node, 0), Result);
1042 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1043 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +00001044
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001045 case ISD::EXTLOAD:
1046 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001047 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001048 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1049 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001050
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001051 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001052 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001053 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001054 case TargetLowering::Promote:
1055 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001056 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1057 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001058 // Since loads produce two values, make sure to remember that we legalized
1059 // both of them.
1060 AddLegalizedOperand(SDOperand(Node, 0), Result);
1061 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1062 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001063
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001064 case TargetLowering::Legal:
1065 if (Tmp1 != Node->getOperand(0) ||
1066 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001067 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1068 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001069 else
1070 Result = SDOperand(Node, 0);
1071
1072 // Since loads produce two values, make sure to remember that we legalized
1073 // both of them.
1074 AddLegalizedOperand(SDOperand(Node, 0), Result);
1075 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1076 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001077 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001078 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1079 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1080 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001081 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001082 if (Op.ResNo)
1083 return Load.getValue(1);
1084 return Result;
1085 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001086 assert(Node->getOpcode() != ISD::EXTLOAD &&
1087 "EXTLOAD should always be supported!");
1088 // Turn the unsupported load into an EXTLOAD followed by an explicit
1089 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001090 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1091 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001092 SDOperand ValRes;
1093 if (Node->getOpcode() == ISD::SEXTLOAD)
1094 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001095 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001096 else
1097 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001098 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1099 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1100 if (Op.ResNo)
1101 return Result.getValue(1);
1102 return ValRes;
1103 }
1104 assert(0 && "Unreachable");
1105 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001106 case ISD::EXTRACT_ELEMENT: {
1107 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1108 switch (getTypeAction(OpTy)) {
1109 default:
1110 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1111 break;
1112 case Legal:
1113 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1114 // 1 -> Hi
1115 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1116 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1117 TLI.getShiftAmountTy()));
1118 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1119 } else {
1120 // 0 -> Lo
1121 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1122 Node->getOperand(0));
1123 }
1124 Result = LegalizeOp(Result);
1125 break;
1126 case Expand:
1127 // Get both the low and high parts.
1128 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1129 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1130 Result = Tmp2; // 1 -> Hi
1131 else
1132 Result = Tmp1; // 0 -> Lo
1133 break;
1134 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001135 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001136 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001137
1138 case ISD::CopyToReg:
1139 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001140
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001141 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001142 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001143 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001144 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001145 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001146 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1147 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1148 Node->getOperand(1), Tmp2);
1149 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001150 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1151 if (Node->getNumOperands() == 4)
1152 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001153 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001154 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001155 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1156 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1157 }
1158
1159 // Since this produces two values, make sure to remember that we legalized
1160 // both of them.
1161 AddLegalizedOperand(SDOperand(Node, 0), Result);
1162 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1163 return Result.getValue(Op.ResNo);
1164 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001165 break;
1166
1167 case ISD::RET:
1168 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1169 switch (Node->getNumOperands()) {
1170 case 2: // ret val
1171 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1172 case Legal:
1173 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001174 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001175 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1176 break;
1177 case Expand: {
1178 SDOperand Lo, Hi;
1179 ExpandOp(Node->getOperand(1), Lo, Hi);
1180 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001181 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001182 }
1183 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001184 Tmp2 = PromoteOp(Node->getOperand(1));
1185 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1186 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001187 }
1188 break;
1189 case 1: // ret void
1190 if (Tmp1 != Node->getOperand(0))
1191 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1192 break;
1193 default: { // ret <values>
1194 std::vector<SDOperand> NewValues;
1195 NewValues.push_back(Tmp1);
1196 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1197 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1198 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001199 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001200 break;
1201 case Expand: {
1202 SDOperand Lo, Hi;
1203 ExpandOp(Node->getOperand(i), Lo, Hi);
1204 NewValues.push_back(Lo);
1205 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001206 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001207 }
1208 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001209 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001210 }
1211 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1212 break;
1213 }
1214 }
1215 break;
1216 case ISD::STORE:
1217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1218 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1219
Chris Lattnere69daaf2005-01-08 06:25:56 +00001220 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001221 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001222 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001223 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001224 DAG.getConstant(FloatToBits(CFP->getValue()),
1225 MVT::i32),
1226 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001227 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001228 } else {
1229 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001230 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001231 DAG.getConstant(DoubleToBits(CFP->getValue()),
1232 MVT::i64),
1233 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001234 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001235 }
Chris Lattnera4743132005-02-22 07:23:39 +00001236 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001237 }
1238
Chris Lattnerdc750592005-01-07 07:47:09 +00001239 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1240 case Legal: {
1241 SDOperand Val = LegalizeOp(Node->getOperand(1));
1242 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1243 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001244 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1245 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001246 break;
1247 }
1248 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001249 // Truncate the value and store the result.
1250 Tmp3 = PromoteOp(Node->getOperand(1));
1251 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001252 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001253 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001254 break;
1255
Chris Lattnerdc750592005-01-07 07:47:09 +00001256 case Expand:
1257 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001258 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001259 ExpandOp(Node->getOperand(1), Lo, Hi);
1260
1261 if (!TLI.isLittleEndian())
1262 std::swap(Lo, Hi);
1263
Chris Lattner55e9cde2005-05-11 04:51:16 +00001264 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1265 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001266 // If this is a vector type, then we have to calculate the increment as
1267 // the product of the element size in bytes, and the number of elements
1268 // in the high half of the vector.
1269 if (MVT::Vector == Hi.getValueType()) {
1270 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1271 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1272 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1273 } else {
1274 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1275 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001276 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1277 getIntPtrConstant(IncrementSize));
1278 assert(isTypeLegal(Tmp2.getValueType()) &&
1279 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001280 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001281 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1282 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001283 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1284 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001285 }
1286 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001287 case ISD::PCMARKER:
1288 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001289 if (Tmp1 != Node->getOperand(0))
1290 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001291 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001292 case ISD::READCYCLECOUNTER:
1293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001294 if (Tmp1 != Node->getOperand(0)) {
1295 std::vector<MVT::ValueType> rtypes;
1296 std::vector<SDOperand> rvals;
1297 rtypes.push_back(MVT::i64);
1298 rtypes.push_back(MVT::Other);
1299 rvals.push_back(Tmp1);
1300 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1301 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001302
1303 // Since rdcc produce two values, make sure to remember that we legalized
1304 // both of them.
1305 AddLegalizedOperand(SDOperand(Node, 0), Result);
1306 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1307 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001308
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001309 case ISD::TRUNCSTORE:
1310 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1311 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1312
1313 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1314 case Legal:
1315 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001316
1317 // The only promote case we handle is TRUNCSTORE:i1 X into
1318 // -> TRUNCSTORE:i8 (and X, 1)
1319 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1320 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1321 TargetLowering::Promote) {
1322 // Promote the bool to a mask then store.
1323 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1324 DAG.getConstant(1, Tmp2.getValueType()));
1325 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1326 Node->getOperand(3), DAG.getValueType(MVT::i8));
1327
1328 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1329 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001330 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001331 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001332 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001333 break;
1334 case Promote:
1335 case Expand:
1336 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1337 }
1338 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001339 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001340 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1341 case Expand: assert(0 && "It's impossible to expand bools");
1342 case Legal:
1343 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1344 break;
1345 case Promote:
1346 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1347 break;
1348 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001349 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001350 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001351
Nate Begeman987121a2005-08-23 04:29:48 +00001352 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001353 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001354 case TargetLowering::Expand:
1355 if (Tmp1.getOpcode() == ISD::SETCC) {
1356 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1357 Tmp2, Tmp3,
1358 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1359 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001360 // Make sure the condition is either zero or one. It may have been
1361 // promoted from something else.
1362 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001363 Result = DAG.getSelectCC(Tmp1,
1364 DAG.getConstant(0, Tmp1.getValueType()),
1365 Tmp2, Tmp3, ISD::SETNE);
1366 }
1367 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001368 case TargetLowering::Custom: {
1369 SDOperand Tmp =
1370 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1371 Tmp1, Tmp2, Tmp3), DAG);
1372 if (Tmp.Val) {
1373 Result = LegalizeOp(Tmp);
1374 break;
1375 }
1376 // FALLTHROUGH if the target thinks it is legal.
1377 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001378 case TargetLowering::Legal:
1379 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1380 Tmp3 != Node->getOperand(2))
1381 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1382 Tmp1, Tmp2, Tmp3);
1383 break;
1384 case TargetLowering::Promote: {
1385 MVT::ValueType NVT =
1386 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1387 unsigned ExtOp, TruncOp;
1388 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001389 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001390 TruncOp = ISD::TRUNCATE;
1391 } else {
1392 ExtOp = ISD::FP_EXTEND;
1393 TruncOp = ISD::FP_ROUND;
1394 }
1395 // Promote each of the values to the new type.
1396 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1397 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1398 // Perform the larger operation, then round down.
1399 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1400 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1401 break;
1402 }
1403 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001404 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001405 case ISD::SELECT_CC:
1406 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1407 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1408
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001409 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001410 // Everything is legal, see if we should expand this op or something.
1411 switch (TLI.getOperationAction(ISD::SELECT_CC,
1412 Node->getOperand(0).getValueType())) {
1413 default: assert(0 && "This action is not supported yet!");
1414 case TargetLowering::Custom: {
1415 SDOperand Tmp =
1416 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1417 Node->getOperand(0),
1418 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001419 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001420 if (Tmp.Val) {
1421 Result = LegalizeOp(Tmp);
1422 break;
1423 }
1424 } // FALLTHROUGH if the target can't lower this operation after all.
1425 case TargetLowering::Legal:
1426 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1427 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1428 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1429 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1430 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1431 Tmp3, Tmp4, Node->getOperand(4));
1432 }
1433 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001434 }
1435 break;
1436 } else {
1437 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1438 Node->getOperand(0), // LHS
1439 Node->getOperand(1), // RHS
1440 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001441 // If we get a SETCC back from legalizing the SETCC node we just
1442 // created, then use its LHS, RHS, and CC directly in creating a new
1443 // node. Otherwise, select between the true and false value based on
1444 // comparing the result of the legalized with zero.
1445 if (Tmp1.getOpcode() == ISD::SETCC) {
1446 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1447 Tmp1.getOperand(0), Tmp1.getOperand(1),
1448 Tmp3, Tmp4, Tmp1.getOperand(2));
1449 } else {
1450 Result = DAG.getSelectCC(Tmp1,
1451 DAG.getConstant(0, Tmp1.getValueType()),
1452 Tmp3, Tmp4, ISD::SETNE);
1453 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001454 }
1455 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001456 case ISD::SETCC:
1457 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1458 case Legal:
1459 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1460 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001461 break;
1462 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001463 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1464 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1465
1466 // If this is an FP compare, the operands have already been extended.
1467 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1468 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001469 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001470
1471 // Otherwise, we have to insert explicit sign or zero extends. Note
1472 // that we could insert sign extends for ALL conditions, but zero extend
1473 // is cheaper on many machines (an AND instead of two shifts), so prefer
1474 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001475 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001476 default: assert(0 && "Unknown integer comparison!");
1477 case ISD::SETEQ:
1478 case ISD::SETNE:
1479 case ISD::SETUGE:
1480 case ISD::SETUGT:
1481 case ISD::SETULE:
1482 case ISD::SETULT:
1483 // ALL of these operations will work if we either sign or zero extend
1484 // the operands (including the unsigned comparisons!). Zero extend is
1485 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001486 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1487 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001488 break;
1489 case ISD::SETGE:
1490 case ISD::SETGT:
1491 case ISD::SETLT:
1492 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001493 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1494 DAG.getValueType(VT));
1495 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1496 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001497 break;
1498 }
Chris Lattner4d978642005-01-15 22:16:26 +00001499 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001500 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001501 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001502 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1503 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1504 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001505 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001506 case ISD::SETEQ:
1507 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001508 if (RHSLo == RHSHi)
1509 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1510 if (RHSCST->isAllOnesValue()) {
1511 // Comparison to -1.
1512 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001513 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001514 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001515 }
1516
Chris Lattnerdc750592005-01-07 07:47:09 +00001517 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1518 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1519 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001520 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001521 break;
1522 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001523 // If this is a comparison of the sign bit, just look at the top part.
1524 // X > -1, x < 0
1525 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001526 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001527 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001528 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001529 (CST->isAllOnesValue()))) { // X > -1
1530 Tmp1 = LHSHi;
1531 Tmp2 = RHSHi;
1532 break;
1533 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001534
Chris Lattnerdc750592005-01-07 07:47:09 +00001535 // FIXME: This generated code sucks.
1536 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001537 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001538 default: assert(0 && "Unknown integer setcc!");
1539 case ISD::SETLT:
1540 case ISD::SETULT: LowCC = ISD::SETULT; break;
1541 case ISD::SETGT:
1542 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1543 case ISD::SETLE:
1544 case ISD::SETULE: LowCC = ISD::SETULE; break;
1545 case ISD::SETGE:
1546 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1547 }
Misha Brukman835702a2005-04-21 22:36:52 +00001548
Chris Lattnerdc750592005-01-07 07:47:09 +00001549 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1550 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1551 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1552
1553 // NOTE: on targets without efficient SELECT of bools, we can always use
1554 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001555 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1556 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1557 Node->getOperand(2));
1558 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001559 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1560 Result, Tmp1, Tmp2));
1561 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001562 }
1563 }
Nate Begeman987121a2005-08-23 04:29:48 +00001564
1565 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1566 default:
1567 assert(0 && "Cannot handle this action for SETCC yet!");
1568 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001569 case TargetLowering::Promote: {
1570 // First step, figure out the appropriate operation to use.
1571 // Allow SETCC to not be supported for all legal data types
1572 // Mostly this targets FP
1573 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1574 MVT::ValueType OldVT = NewInTy;
1575
1576 // Scan for the appropriate larger type to use.
1577 while (1) {
1578 NewInTy = (MVT::ValueType)(NewInTy+1);
1579
1580 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1581 "Fell off of the edge of the integer world");
1582 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1583 "Fell off of the edge of the floating point world");
1584
1585 // If the target supports SETCC of this type, use it.
1586 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1587 break;
1588 }
1589 if (MVT::isInteger(NewInTy))
1590 assert(0 && "Cannot promote Legal Integer SETCC yet");
1591 else {
1592 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1593 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1594 }
1595
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001596 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1597 Node->getOperand(2));
1598 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001599 }
Nate Begeman987121a2005-08-23 04:29:48 +00001600 case TargetLowering::Legal:
1601 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1602 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1603 Node->getOperand(2));
1604 break;
1605 case TargetLowering::Expand:
1606 // Expand a setcc node into a select_cc of the same condition, lhs, and
1607 // rhs that selects between const 1 (true) and const 0 (false).
1608 MVT::ValueType VT = Node->getValueType(0);
1609 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1610 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1611 Node->getOperand(2));
1612 Result = LegalizeOp(Result);
1613 break;
1614 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001615 break;
1616
Chris Lattner85d70c62005-01-11 05:57:22 +00001617 case ISD::MEMSET:
1618 case ISD::MEMCPY:
1619 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001620 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001621 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1622
1623 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1624 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1625 case Expand: assert(0 && "Cannot expand a byte!");
1626 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001627 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001628 break;
1629 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001630 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001631 break;
1632 }
1633 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001634 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001635 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001636
1637 SDOperand Tmp4;
1638 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001639 case Expand: {
1640 // Length is too big, just take the lo-part of the length.
1641 SDOperand HiPart;
1642 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1643 break;
1644 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001645 case Legal:
1646 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001647 break;
1648 case Promote:
1649 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001650 break;
1651 }
1652
1653 SDOperand Tmp5;
1654 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1655 case Expand: assert(0 && "Cannot expand this yet!");
1656 case Legal:
1657 Tmp5 = LegalizeOp(Node->getOperand(4));
1658 break;
1659 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001660 Tmp5 = PromoteOp(Node->getOperand(4));
1661 break;
1662 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001663
1664 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1665 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001666 case TargetLowering::Custom: {
1667 SDOperand Tmp =
1668 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1669 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1670 if (Tmp.Val) {
1671 Result = LegalizeOp(Tmp);
1672 break;
1673 }
1674 // FALLTHROUGH if the target thinks it is legal.
1675 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001676 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001677 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1678 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1679 Tmp5 != Node->getOperand(4)) {
1680 std::vector<SDOperand> Ops;
1681 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1682 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1683 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1684 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001685 break;
1686 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001687 // Otherwise, the target does not support this operation. Lower the
1688 // operation to an explicit libcall as appropriate.
1689 MVT::ValueType IntPtr = TLI.getPointerTy();
1690 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1691 std::vector<std::pair<SDOperand, const Type*> > Args;
1692
Reid Spencer6dced922005-01-12 14:53:45 +00001693 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001694 if (Node->getOpcode() == ISD::MEMSET) {
1695 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1696 // Extend the ubyte argument to be an int value for the call.
1697 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1698 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1699 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1700
1701 FnName = "memset";
1702 } else if (Node->getOpcode() == ISD::MEMCPY ||
1703 Node->getOpcode() == ISD::MEMMOVE) {
1704 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1705 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1706 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1707 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1708 } else {
1709 assert(0 && "Unknown op!");
1710 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001711
Chris Lattner85d70c62005-01-11 05:57:22 +00001712 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001713 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001714 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001715 Result = CallResult.second;
1716 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001717 break;
1718 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001719 }
1720 break;
1721 }
Chris Lattner5385db52005-05-09 20:23:03 +00001722
1723 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001724 Tmp1 = LegalizeOp(Node->getOperand(0));
1725 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001726
Chris Lattner86535992005-05-14 07:45:46 +00001727 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1728 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1729 std::vector<SDOperand> Ops;
1730 Ops.push_back(Tmp1);
1731 Ops.push_back(Tmp2);
1732 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1733 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001734 Result = SDOperand(Node, 0);
1735 // Since these produce two values, make sure to remember that we legalized
1736 // both of them.
1737 AddLegalizedOperand(SDOperand(Node, 0), Result);
1738 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1739 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001740 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001741 Tmp1 = LegalizeOp(Node->getOperand(0));
1742 Tmp2 = LegalizeOp(Node->getOperand(1));
1743 Tmp3 = LegalizeOp(Node->getOperand(2));
1744 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1745 Tmp3 != Node->getOperand(2))
1746 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1747 break;
1748
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001749 case ISD::READIO:
1750 Tmp1 = LegalizeOp(Node->getOperand(0));
1751 Tmp2 = LegalizeOp(Node->getOperand(1));
1752
1753 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1754 case TargetLowering::Custom:
1755 default: assert(0 && "This action not implemented for this operation!");
1756 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001757 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1758 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1759 std::vector<SDOperand> Ops;
1760 Ops.push_back(Tmp1);
1761 Ops.push_back(Tmp2);
1762 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1763 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001764 Result = SDOperand(Node, 0);
1765 break;
1766 case TargetLowering::Expand:
1767 // Replace this with a load from memory.
1768 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1769 Node->getOperand(1), DAG.getSrcValue(NULL));
1770 Result = LegalizeOp(Result);
1771 break;
1772 }
1773
1774 // Since these produce two values, make sure to remember that we legalized
1775 // both of them.
1776 AddLegalizedOperand(SDOperand(Node, 0), Result);
1777 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1778 return Result.getValue(Op.ResNo);
1779
1780 case ISD::WRITEIO:
1781 Tmp1 = LegalizeOp(Node->getOperand(0));
1782 Tmp2 = LegalizeOp(Node->getOperand(1));
1783 Tmp3 = LegalizeOp(Node->getOperand(2));
1784
1785 switch (TLI.getOperationAction(Node->getOpcode(),
1786 Node->getOperand(1).getValueType())) {
1787 case TargetLowering::Custom:
1788 default: assert(0 && "This action not implemented for this operation!");
1789 case TargetLowering::Legal:
1790 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1791 Tmp3 != Node->getOperand(2))
1792 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1793 break;
1794 case TargetLowering::Expand:
1795 // Replace this with a store to memory.
1796 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1797 Node->getOperand(1), Node->getOperand(2),
1798 DAG.getSrcValue(NULL));
1799 Result = LegalizeOp(Result);
1800 break;
1801 }
1802 break;
1803
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001804 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001805 case ISD::SUB_PARTS:
1806 case ISD::SHL_PARTS:
1807 case ISD::SRA_PARTS:
1808 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001809 std::vector<SDOperand> Ops;
1810 bool Changed = false;
1811 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1812 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1813 Changed |= Ops.back() != Node->getOperand(i);
1814 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001815 if (Changed) {
1816 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1817 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1818 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001819
1820 // Since these produce multiple values, make sure to remember that we
1821 // legalized all of them.
1822 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1823 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1824 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001825 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001826
1827 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001828 case ISD::ADD:
1829 case ISD::SUB:
1830 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001831 case ISD::MULHS:
1832 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001833 case ISD::UDIV:
1834 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001835 case ISD::AND:
1836 case ISD::OR:
1837 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001838 case ISD::SHL:
1839 case ISD::SRL:
1840 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001841 case ISD::FADD:
1842 case ISD::FSUB:
1843 case ISD::FMUL:
1844 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001845 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001846 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1847 case Expand: assert(0 && "Not possible");
1848 case Legal:
1849 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1850 break;
1851 case Promote:
1852 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1853 break;
1854 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001855 if (Tmp1 != Node->getOperand(0) ||
1856 Tmp2 != Node->getOperand(1))
1857 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1858 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001859
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001860 case ISD::BUILD_PAIR: {
1861 MVT::ValueType PairTy = Node->getValueType(0);
1862 // TODO: handle the case where the Lo and Hi operands are not of legal type
1863 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1864 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1865 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1866 case TargetLowering::Legal:
1867 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1868 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1869 break;
1870 case TargetLowering::Promote:
1871 case TargetLowering::Custom:
1872 assert(0 && "Cannot promote/custom this yet!");
1873 case TargetLowering::Expand:
1874 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1875 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1876 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1877 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1878 TLI.getShiftAmountTy()));
1879 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1880 break;
1881 }
1882 break;
1883 }
1884
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001885 case ISD::UREM:
1886 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001887 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1889 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1890 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1891 case TargetLowering::Legal:
1892 if (Tmp1 != Node->getOperand(0) ||
1893 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001894 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001895 Tmp2);
1896 break;
1897 case TargetLowering::Promote:
1898 case TargetLowering::Custom:
1899 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001900 case TargetLowering::Expand:
1901 if (MVT::isInteger(Node->getValueType(0))) {
1902 MVT::ValueType VT = Node->getValueType(0);
1903 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1904 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1905 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1906 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1907 } else {
1908 // Floating point mod -> fmod libcall.
1909 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1910 SDOperand Dummy;
1911 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001912 }
1913 break;
1914 }
1915 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001916
Andrew Lenharth5e177822005-05-03 17:19:30 +00001917 case ISD::CTPOP:
1918 case ISD::CTTZ:
1919 case ISD::CTLZ:
1920 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1921 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1922 case TargetLowering::Legal:
1923 if (Tmp1 != Node->getOperand(0))
1924 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1925 break;
1926 case TargetLowering::Promote: {
1927 MVT::ValueType OVT = Tmp1.getValueType();
1928 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001929
1930 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001931 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1932 // Perform the larger operation, then subtract if needed.
1933 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1934 switch(Node->getOpcode())
1935 {
1936 case ISD::CTPOP:
1937 Result = Tmp1;
1938 break;
1939 case ISD::CTTZ:
1940 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001941 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1942 DAG.getConstant(getSizeInBits(NVT), NVT),
1943 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001944 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001945 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1946 break;
1947 case ISD::CTLZ:
1948 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001949 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1950 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001951 getSizeInBits(OVT), NVT));
1952 break;
1953 }
1954 break;
1955 }
1956 case TargetLowering::Custom:
1957 assert(0 && "Cannot custom handle this yet!");
1958 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001959 switch(Node->getOpcode())
1960 {
1961 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001962 static const uint64_t mask[6] = {
1963 0x5555555555555555ULL, 0x3333333333333333ULL,
1964 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1965 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1966 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001967 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001968 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1969 unsigned len = getSizeInBits(VT);
1970 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001971 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001972 Tmp2 = DAG.getConstant(mask[i], VT);
1973 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001974 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001975 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1976 DAG.getNode(ISD::AND, VT,
1977 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1978 Tmp2));
1979 }
1980 Result = Tmp1;
1981 break;
1982 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001983 case ISD::CTLZ: {
1984 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001985 x = x | (x >> 1);
1986 x = x | (x >> 2);
1987 ...
1988 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001989 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001990 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001991
Chris Lattner56add052005-05-11 18:35:21 +00001992 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1993 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001994 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1995 unsigned len = getSizeInBits(VT);
1996 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1997 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001998 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001999 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2000 }
2001 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002002 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002003 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002004 }
2005 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002006 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002007 // unless the target has ctlz but not ctpop, in which case we use:
2008 // { return 32 - nlz(~x & (x-1)); }
2009 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002010 MVT::ValueType VT = Tmp1.getValueType();
2011 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002012 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002013 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2014 DAG.getNode(ISD::SUB, VT, Tmp1,
2015 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002016 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002017 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2018 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002019 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002020 DAG.getConstant(getSizeInBits(VT), VT),
2021 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2022 } else {
2023 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2024 }
Chris Lattner72473242005-05-11 05:27:09 +00002025 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002026 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002027 default:
2028 assert(0 && "Cannot expand this yet!");
2029 break;
2030 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002031 break;
2032 }
2033 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002034
Chris Lattner13fe99c2005-04-02 05:00:07 +00002035 // Unary operators
2036 case ISD::FABS:
2037 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002038 case ISD::FSQRT:
2039 case ISD::FSIN:
2040 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002041 Tmp1 = LegalizeOp(Node->getOperand(0));
2042 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2043 case TargetLowering::Legal:
2044 if (Tmp1 != Node->getOperand(0))
2045 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2046 break;
2047 case TargetLowering::Promote:
2048 case TargetLowering::Custom:
2049 assert(0 && "Cannot promote/custom handle this yet!");
2050 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002051 switch(Node->getOpcode()) {
2052 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002053 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2054 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002055 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002056 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002057 break;
2058 }
2059 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002060 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2061 MVT::ValueType VT = Node->getValueType(0);
2062 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002063 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002064 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2065 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2066 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002067 break;
2068 }
2069 case ISD::FSQRT:
2070 case ISD::FSIN:
2071 case ISD::FCOS: {
2072 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002073 const char *FnName = 0;
2074 switch(Node->getOpcode()) {
2075 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2076 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2077 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2078 default: assert(0 && "Unreachable!");
2079 }
Nate Begeman77558da2005-08-04 21:43:28 +00002080 SDOperand Dummy;
2081 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002082 break;
2083 }
2084 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002085 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002086 }
2087 break;
2088 }
2089 break;
2090
2091 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002092 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002093 case ISD::UINT_TO_FP: {
2094 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002095 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2096 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002097 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002098 Node->getOperand(0).getValueType())) {
2099 default: assert(0 && "Unknown operation action!");
2100 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002101 Result = ExpandLegalINT_TO_FP(isSigned,
2102 LegalizeOp(Node->getOperand(0)),
2103 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002104 AddLegalizedOperand(Op, Result);
2105 return Result;
2106 case TargetLowering::Promote:
2107 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2108 Node->getValueType(0),
2109 isSigned);
2110 AddLegalizedOperand(Op, Result);
2111 return Result;
2112 case TargetLowering::Legal:
2113 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002114 case TargetLowering::Custom: {
2115 Tmp1 = LegalizeOp(Node->getOperand(0));
2116 SDOperand Tmp =
2117 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2118 Tmp = TLI.LowerOperation(Tmp, DAG);
2119 if (Tmp.Val) {
2120 AddLegalizedOperand(Op, Tmp);
2121 NeedsAnotherIteration = true;
2122 return Tmp;
2123 } else {
2124 assert(0 && "Target Must Lower this");
2125 }
2126 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002127 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002128
Chris Lattnerdc750592005-01-07 07:47:09 +00002129 Tmp1 = LegalizeOp(Node->getOperand(0));
2130 if (Tmp1 != Node->getOperand(0))
2131 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2132 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002133 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002134 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2135 Node->getValueType(0), Node->getOperand(0));
2136 break;
2137 case Promote:
2138 if (isSigned) {
2139 Result = PromoteOp(Node->getOperand(0));
2140 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2141 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2142 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2143 } else {
2144 Result = PromoteOp(Node->getOperand(0));
2145 Result = DAG.getZeroExtendInReg(Result,
2146 Node->getOperand(0).getValueType());
2147 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002148 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002149 break;
2150 }
2151 break;
2152 }
2153 case ISD::TRUNCATE:
2154 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2155 case Legal:
2156 Tmp1 = LegalizeOp(Node->getOperand(0));
2157 if (Tmp1 != Node->getOperand(0))
2158 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2159 break;
2160 case Expand:
2161 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2162
2163 // Since the result is legal, we should just be able to truncate the low
2164 // part of the source.
2165 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2166 break;
2167 case Promote:
2168 Result = PromoteOp(Node->getOperand(0));
2169 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2170 break;
2171 }
2172 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002173
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002174 case ISD::FP_TO_SINT:
2175 case ISD::FP_TO_UINT:
2176 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2177 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002178 Tmp1 = LegalizeOp(Node->getOperand(0));
2179
Chris Lattner44fe26f2005-07-29 00:11:56 +00002180 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2181 default: assert(0 && "Unknown operation action!");
2182 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002183 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2184 SDOperand True, False;
2185 MVT::ValueType VT = Node->getOperand(0).getValueType();
2186 MVT::ValueType NVT = Node->getValueType(0);
2187 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2188 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2189 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2190 Node->getOperand(0), Tmp2, ISD::SETLT);
2191 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2192 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002193 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002194 Tmp2));
2195 False = DAG.getNode(ISD::XOR, NVT, False,
2196 DAG.getConstant(1ULL << ShiftAmt, NVT));
2197 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002198 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002199 } else {
2200 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2201 }
2202 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002203 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002204 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002205 Node->getOpcode() == ISD::FP_TO_SINT);
2206 AddLegalizedOperand(Op, Result);
2207 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002208 case TargetLowering::Custom: {
2209 SDOperand Tmp =
2210 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2211 Tmp = TLI.LowerOperation(Tmp, DAG);
2212 if (Tmp.Val) {
2213 AddLegalizedOperand(Op, Tmp);
2214 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002215 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002216 } else {
2217 // The target thinks this is legal afterall.
2218 break;
2219 }
2220 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002221 case TargetLowering::Legal:
2222 break;
2223 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002224
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002225 if (Tmp1 != Node->getOperand(0))
2226 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2227 break;
2228 case Expand:
2229 assert(0 && "Shouldn't need to expand other operators here!");
2230 case Promote:
2231 Result = PromoteOp(Node->getOperand(0));
2232 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2233 break;
2234 }
2235 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002236
Chris Lattner7753f172005-09-02 00:18:10 +00002237 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002238 case ISD::ZERO_EXTEND:
2239 case ISD::SIGN_EXTEND:
2240 case ISD::FP_EXTEND:
2241 case ISD::FP_ROUND:
2242 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2243 case Legal:
2244 Tmp1 = LegalizeOp(Node->getOperand(0));
2245 if (Tmp1 != Node->getOperand(0))
2246 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2247 break;
2248 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002249 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002250
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002251 case Promote:
2252 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002253 case ISD::ANY_EXTEND:
2254 Result = PromoteOp(Node->getOperand(0));
2255 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2256 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002257 case ISD::ZERO_EXTEND:
2258 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002259 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002260 Result = DAG.getZeroExtendInReg(Result,
2261 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002262 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002263 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002264 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002265 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002266 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002267 Result,
2268 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002269 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002270 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002271 Result = PromoteOp(Node->getOperand(0));
2272 if (Result.getValueType() != Op.getValueType())
2273 // Dynamically dead while we have only 2 FP types.
2274 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2275 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002276 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002277 Result = PromoteOp(Node->getOperand(0));
2278 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2279 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002280 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002281 }
2282 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002283 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002284 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002285 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002286 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002287
2288 // If this operation is not supported, convert it to a shl/shr or load/store
2289 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002290 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2291 default: assert(0 && "This action not supported for this op yet!");
2292 case TargetLowering::Legal:
2293 if (Tmp1 != Node->getOperand(0))
2294 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002295 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002296 break;
2297 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002298 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002299 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002300 // NOTE: we could fall back on load/store here too for targets without
2301 // SAR. However, it is doubtful that any exist.
2302 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2303 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002304 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002305 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2306 Node->getOperand(0), ShiftCst);
2307 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2308 Result, ShiftCst);
2309 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2310 // The only way we can lower this is to turn it into a STORETRUNC,
2311 // EXTLOAD pair, targetting a temporary location (a stack slot).
2312
2313 // NOTE: there is a choice here between constantly creating new stack
2314 // slots and always reusing the same one. We currently always create
2315 // new ones, as reuse may inhibit scheduling.
2316 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2317 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2318 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2319 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002320 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002321 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2322 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2323 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002324 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002325 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002326 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2327 Result, StackSlot, DAG.getSrcValue(NULL),
2328 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002329 } else {
2330 assert(0 && "Unknown op");
2331 }
2332 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002333 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002334 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002335 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002336 }
Chris Lattner99222f72005-01-15 07:15:18 +00002337 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002338
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002339 // Note that LegalizeOp may be reentered even from single-use nodes, which
2340 // means that we always must cache transformed nodes.
2341 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002342 return Result;
2343}
2344
Chris Lattner4d978642005-01-15 22:16:26 +00002345/// PromoteOp - Given an operation that produces a value in an invalid type,
2346/// promote it to compute the value into a larger type. The produced value will
2347/// have the correct bits for the low portion of the register, but no guarantee
2348/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002349SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2350 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002351 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002352 assert(getTypeAction(VT) == Promote &&
2353 "Caller should expand or legalize operands that are not promotable!");
2354 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2355 "Cannot promote to smaller type!");
2356
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002357 SDOperand Tmp1, Tmp2, Tmp3;
2358
2359 SDOperand Result;
2360 SDNode *Node = Op.Val;
2361
Chris Lattner1a570f12005-09-02 20:32:45 +00002362 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2363 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002364
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002365 // Promotion needs an optimization step to clean up after it, and is not
2366 // careful to avoid operations the target does not support. Make sure that
2367 // all generated operations are legalized in the next iteration.
2368 NeedsAnotherIteration = true;
2369
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002370 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002371 case ISD::CopyFromReg:
2372 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002373 default:
2374 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2375 assert(0 && "Do not know how to promote this operator!");
2376 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002377 case ISD::UNDEF:
2378 Result = DAG.getNode(ISD::UNDEF, NVT);
2379 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002380 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002381 if (VT != MVT::i1)
2382 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2383 else
2384 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002385 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2386 break;
2387 case ISD::ConstantFP:
2388 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2389 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2390 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002391
Chris Lattner2cb338d2005-01-18 02:59:52 +00002392 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002393 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002394 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2395 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002396 Result = LegalizeOp(Result);
2397 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002398
2399 case ISD::TRUNCATE:
2400 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2401 case Legal:
2402 Result = LegalizeOp(Node->getOperand(0));
2403 assert(Result.getValueType() >= NVT &&
2404 "This truncation doesn't make sense!");
2405 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2406 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2407 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002408 case Promote:
2409 // The truncation is not required, because we don't guarantee anything
2410 // about high bits anyway.
2411 Result = PromoteOp(Node->getOperand(0));
2412 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002413 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002414 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2415 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002416 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002417 }
2418 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002419 case ISD::SIGN_EXTEND:
2420 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002421 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002422 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2423 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2424 case Legal:
2425 // Input is legal? Just do extend all the way to the larger type.
2426 Result = LegalizeOp(Node->getOperand(0));
2427 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2428 break;
2429 case Promote:
2430 // Promote the reg if it's smaller.
2431 Result = PromoteOp(Node->getOperand(0));
2432 // The high bits are not guaranteed to be anything. Insert an extend.
2433 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002434 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002435 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002436 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002437 Result = DAG.getZeroExtendInReg(Result,
2438 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002439 break;
2440 }
2441 break;
2442
2443 case ISD::FP_EXTEND:
2444 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2445 case ISD::FP_ROUND:
2446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2447 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2448 case Promote: assert(0 && "Unreachable with 2 FP types!");
2449 case Legal:
2450 // Input is legal? Do an FP_ROUND_INREG.
2451 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002452 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2453 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002454 break;
2455 }
2456 break;
2457
2458 case ISD::SINT_TO_FP:
2459 case ISD::UINT_TO_FP:
2460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2461 case Legal:
2462 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002463 // No extra round required here.
2464 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002465 break;
2466
2467 case Promote:
2468 Result = PromoteOp(Node->getOperand(0));
2469 if (Node->getOpcode() == ISD::SINT_TO_FP)
2470 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002471 Result,
2472 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002473 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002474 Result = DAG.getZeroExtendInReg(Result,
2475 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002476 // No extra round required here.
2477 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002478 break;
2479 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002480 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2481 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002482 // Round if we cannot tolerate excess precision.
2483 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002484 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2485 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002486 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002487 }
Chris Lattner4d978642005-01-15 22:16:26 +00002488 break;
2489
Chris Lattner268d4572005-12-09 17:32:47 +00002490 case ISD::SIGN_EXTEND_INREG:
2491 Result = PromoteOp(Node->getOperand(0));
2492 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2493 Node->getOperand(1));
2494 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002495 case ISD::FP_TO_SINT:
2496 case ISD::FP_TO_UINT:
2497 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2498 case Legal:
2499 Tmp1 = LegalizeOp(Node->getOperand(0));
2500 break;
2501 case Promote:
2502 // The input result is prerounded, so we don't have to do anything
2503 // special.
2504 Tmp1 = PromoteOp(Node->getOperand(0));
2505 break;
2506 case Expand:
2507 assert(0 && "not implemented");
2508 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002509 // If we're promoting a UINT to a larger size, check to see if the new node
2510 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2511 // we can use that instead. This allows us to generate better code for
2512 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2513 // legal, such as PowerPC.
2514 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002515 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002516 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2517 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002518 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2519 } else {
2520 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2521 }
Chris Lattner4d978642005-01-15 22:16:26 +00002522 break;
2523
Chris Lattner13fe99c2005-04-02 05:00:07 +00002524 case ISD::FABS:
2525 case ISD::FNEG:
2526 Tmp1 = PromoteOp(Node->getOperand(0));
2527 assert(Tmp1.getValueType() == NVT);
2528 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2529 // NOTE: we do not have to do any extra rounding here for
2530 // NoExcessFPPrecision, because we know the input will have the appropriate
2531 // precision, and these operations don't modify precision at all.
2532 break;
2533
Chris Lattner9d6fa982005-04-28 21:44:33 +00002534 case ISD::FSQRT:
2535 case ISD::FSIN:
2536 case ISD::FCOS:
2537 Tmp1 = PromoteOp(Node->getOperand(0));
2538 assert(Tmp1.getValueType() == NVT);
2539 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2540 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002541 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2542 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002543 break;
2544
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002545 case ISD::AND:
2546 case ISD::OR:
2547 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002548 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002549 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002550 case ISD::MUL:
2551 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002552 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002553 // that too is okay if they are integer operations.
2554 Tmp1 = PromoteOp(Node->getOperand(0));
2555 Tmp2 = PromoteOp(Node->getOperand(1));
2556 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2557 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002558 break;
2559 case ISD::FADD:
2560 case ISD::FSUB:
2561 case ISD::FMUL:
2562 // The input may have strange things in the top bits of the registers, but
2563 // these operations don't care.
2564 Tmp1 = PromoteOp(Node->getOperand(0));
2565 Tmp2 = PromoteOp(Node->getOperand(1));
2566 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2567 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2568
2569 // Floating point operations will give excess precision that we may not be
2570 // able to tolerate. If we DO allow excess precision, just leave it,
2571 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002572 // FIXME: Why would we need to round FP ops more than integer ones?
2573 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002574 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002575 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2576 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002577 break;
2578
Chris Lattner4d978642005-01-15 22:16:26 +00002579 case ISD::SDIV:
2580 case ISD::SREM:
2581 // These operators require that their input be sign extended.
2582 Tmp1 = PromoteOp(Node->getOperand(0));
2583 Tmp2 = PromoteOp(Node->getOperand(1));
2584 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002585 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2586 DAG.getValueType(VT));
2587 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2588 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002589 }
2590 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2591
2592 // Perform FP_ROUND: this is probably overly pessimistic.
2593 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002594 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2595 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002596 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002597 case ISD::FDIV:
2598 case ISD::FREM:
2599 // These operators require that their input be fp extended.
2600 Tmp1 = PromoteOp(Node->getOperand(0));
2601 Tmp2 = PromoteOp(Node->getOperand(1));
2602 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2603
2604 // Perform FP_ROUND: this is probably overly pessimistic.
2605 if (NoExcessFPPrecision)
2606 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2607 DAG.getValueType(VT));
2608 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002609
2610 case ISD::UDIV:
2611 case ISD::UREM:
2612 // These operators require that their input be zero extended.
2613 Tmp1 = PromoteOp(Node->getOperand(0));
2614 Tmp2 = PromoteOp(Node->getOperand(1));
2615 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002616 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2617 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002618 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2619 break;
2620
2621 case ISD::SHL:
2622 Tmp1 = PromoteOp(Node->getOperand(0));
2623 Tmp2 = LegalizeOp(Node->getOperand(1));
2624 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2625 break;
2626 case ISD::SRA:
2627 // The input value must be properly sign extended.
2628 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002629 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2630 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002631 Tmp2 = LegalizeOp(Node->getOperand(1));
2632 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2633 break;
2634 case ISD::SRL:
2635 // The input value must be properly zero extended.
2636 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002637 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002638 Tmp2 = LegalizeOp(Node->getOperand(1));
2639 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2640 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002641 case ISD::LOAD:
2642 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2643 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002644 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2645 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002646 // Remember that we legalized the chain.
2647 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2648 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002649 case ISD::SEXTLOAD:
2650 case ISD::ZEXTLOAD:
2651 case ISD::EXTLOAD:
2652 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2653 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002654 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2655 Node->getOperand(2),
2656 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002657 // Remember that we legalized the chain.
2658 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2659 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002660 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002661 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2662 case Expand: assert(0 && "It's impossible to expand bools");
2663 case Legal:
2664 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2665 break;
2666 case Promote:
2667 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2668 break;
2669 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002670 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2671 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2672 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2673 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002674 case ISD::SELECT_CC:
2675 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2676 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2677 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2678 Node->getOperand(1), Tmp2, Tmp3,
2679 Node->getOperand(4));
2680 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002681 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002682 case ISD::CALL: {
2683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2684 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2685
Chris Lattner3d95c142005-01-19 20:24:35 +00002686 std::vector<SDOperand> Ops;
2687 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2688 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2689
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002690 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2691 "Can only promote single result calls");
2692 std::vector<MVT::ValueType> RetTyVTs;
2693 RetTyVTs.reserve(2);
2694 RetTyVTs.push_back(NVT);
2695 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002696 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2697 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002698 Result = SDOperand(NC, 0);
2699
2700 // Insert the new chain mapping.
2701 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2702 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002703 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002704 case ISD::CTPOP:
2705 case ISD::CTTZ:
2706 case ISD::CTLZ:
2707 Tmp1 = Node->getOperand(0);
2708 //Zero extend the argument
2709 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2710 // Perform the larger operation, then subtract if needed.
2711 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2712 switch(Node->getOpcode())
2713 {
2714 case ISD::CTPOP:
2715 Result = Tmp1;
2716 break;
2717 case ISD::CTTZ:
2718 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002719 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002720 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002721 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002722 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2723 break;
2724 case ISD::CTLZ:
2725 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002726 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2727 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002728 getSizeInBits(VT), NVT));
2729 break;
2730 }
2731 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002732 }
2733
2734 assert(Result.Val && "Didn't set a result!");
2735 AddPromotedOperand(Op, Result);
2736 return Result;
2737}
Chris Lattnerdc750592005-01-07 07:47:09 +00002738
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002739/// ExpandAddSub - Find a clever way to expand this add operation into
2740/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002741void SelectionDAGLegalize::
2742ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2743 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002744 // Expand the subcomponents.
2745 SDOperand LHSL, LHSH, RHSL, RHSH;
2746 ExpandOp(LHS, LHSL, LHSH);
2747 ExpandOp(RHS, RHSL, RHSH);
2748
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002749 std::vector<SDOperand> Ops;
2750 Ops.push_back(LHSL);
2751 Ops.push_back(LHSH);
2752 Ops.push_back(RHSL);
2753 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002754 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2755 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002756 Hi = Lo.getValue(1);
2757}
2758
Chris Lattner4157c412005-04-02 04:00:59 +00002759void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2760 SDOperand Op, SDOperand Amt,
2761 SDOperand &Lo, SDOperand &Hi) {
2762 // Expand the subcomponents.
2763 SDOperand LHSL, LHSH;
2764 ExpandOp(Op, LHSL, LHSH);
2765
2766 std::vector<SDOperand> Ops;
2767 Ops.push_back(LHSL);
2768 Ops.push_back(LHSH);
2769 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002770 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002771 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002772 Hi = Lo.getValue(1);
2773}
2774
2775
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002776/// ExpandShift - Try to find a clever way to expand this shift operation out to
2777/// smaller elements. If we can't find a way that is more efficient than a
2778/// libcall on this target, return false. Otherwise, return true with the
2779/// low-parts expanded into Lo and Hi.
2780bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2781 SDOperand &Lo, SDOperand &Hi) {
2782 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2783 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002784
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002785 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002786 SDOperand ShAmt = LegalizeOp(Amt);
2787 MVT::ValueType ShTy = ShAmt.getValueType();
2788 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2789 unsigned NVTBits = MVT::getSizeInBits(NVT);
2790
2791 // Handle the case when Amt is an immediate. Other cases are currently broken
2792 // and are disabled.
2793 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2794 unsigned Cst = CN->getValue();
2795 // Expand the incoming operand to be shifted, so that we have its parts
2796 SDOperand InL, InH;
2797 ExpandOp(Op, InL, InH);
2798 switch(Opc) {
2799 case ISD::SHL:
2800 if (Cst > VTBits) {
2801 Lo = DAG.getConstant(0, NVT);
2802 Hi = DAG.getConstant(0, NVT);
2803 } else if (Cst > NVTBits) {
2804 Lo = DAG.getConstant(0, NVT);
2805 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002806 } else if (Cst == NVTBits) {
2807 Lo = DAG.getConstant(0, NVT);
2808 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002809 } else {
2810 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2811 Hi = DAG.getNode(ISD::OR, NVT,
2812 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2813 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2814 }
2815 return true;
2816 case ISD::SRL:
2817 if (Cst > VTBits) {
2818 Lo = DAG.getConstant(0, NVT);
2819 Hi = DAG.getConstant(0, NVT);
2820 } else if (Cst > NVTBits) {
2821 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2822 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002823 } else if (Cst == NVTBits) {
2824 Lo = InH;
2825 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002826 } else {
2827 Lo = DAG.getNode(ISD::OR, NVT,
2828 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2829 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2830 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2831 }
2832 return true;
2833 case ISD::SRA:
2834 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002835 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002836 DAG.getConstant(NVTBits-1, ShTy));
2837 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002838 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002839 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002840 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002841 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002842 } else if (Cst == NVTBits) {
2843 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002844 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002845 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002846 } else {
2847 Lo = DAG.getNode(ISD::OR, NVT,
2848 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2849 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2850 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2851 }
2852 return true;
2853 }
2854 }
2855 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2856 // so disable it for now. Currently targets are handling this via SHL_PARTS
2857 // and friends.
2858 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002859
2860 // If we have an efficient select operation (or if the selects will all fold
2861 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002862 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002863 return false;
2864
2865 SDOperand InL, InH;
2866 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002867 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2868 DAG.getConstant(NVTBits, ShTy), ShAmt);
2869
Chris Lattner4d25c042005-01-20 20:29:23 +00002870 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002871 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2872 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002873
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002874 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2875 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2876 DAG.getConstant(NVTBits-1, ShTy));
2877 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2878 DAG.getConstant(NVTBits-1, ShTy));
2879 }
2880
2881 if (Opc == ISD::SHL) {
2882 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2883 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2884 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002885 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002886
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002887 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2888 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2889 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002890 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002891 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2892 DAG.getConstant(32, ShTy),
2893 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002894 DAG.getConstant(0, NVT),
2895 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002896 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002897 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002898 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002899 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002900
2901 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002902 if (Opc == ISD::SRA)
2903 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2904 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002905 else
2906 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002907 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002908 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002909 }
2910 return true;
2911}
Chris Lattneraac464e2005-01-21 06:05:23 +00002912
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002913/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2914/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002915/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002916static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002917 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002918
Chris Lattner2dce7032005-05-12 23:24:06 +00002919 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002920 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002921 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002922 Found = Node;
2923 return;
2924 }
2925
2926 // Otherwise, scan the operands of Node to see if any of them is a call.
2927 assert(Node->getNumOperands() != 0 &&
2928 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002929 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002930 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002931
2932 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002933 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002934 Found);
2935}
2936
2937
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002938/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2939/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002940/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002941static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2942 std::set<SDNode*> &Visited) {
2943 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2944 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002945
Chris Lattner2dce7032005-05-12 23:24:06 +00002946 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002947 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002948 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002949 Found = Node;
2950 return;
2951 }
2952
2953 // Otherwise, scan the operands of Node to see if any of them is a call.
2954 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2955 if (UI == E) return;
2956 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002957 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002958
2959 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002960 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002961}
2962
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002963/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002964/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002965static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002966 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002967 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002968 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002969 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002970
Chris Lattner4add7e32005-01-23 04:42:50 +00002971 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002972 if (TheChain.getValueType() != MVT::Other)
2973 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002974 if (TheChain.getValueType() != MVT::Other)
2975 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002976
2977 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002978 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002979
Chris Lattner4add7e32005-01-23 04:42:50 +00002980 // Make sure to only follow users of our token chain.
2981 SDNode *User = *UI;
2982 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2983 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002984 if (SDNode *Result = FindCallSeqEnd(User))
2985 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002986 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002987 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002988}
2989
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002990/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002991/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002992static SDNode *FindCallSeqStart(SDNode *Node) {
2993 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002994 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002995
2996 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2997 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002998 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002999}
3000
3001
Chris Lattner4add7e32005-01-23 04:42:50 +00003002/// FindInputOutputChains - If we are replacing an operation with a call we need
3003/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003004/// properly serialize the calls in the block. The returned operand is the
3005/// input chain value for the new call (e.g. the entry node or the previous
3006/// call), and OutChain is set to be the chain node to update to point to the
3007/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003008static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3009 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003010 SDNode *LatestCallSeqStart = Entry.Val;
3011 SDNode *LatestCallSeqEnd = 0;
3012 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3013 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003014
Chris Lattner2dce7032005-05-12 23:24:06 +00003015 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003016 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003017 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3018 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003019 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003020 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003021 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3022 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003023 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003024 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003025 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003026
Chris Lattner06bbeb62005-05-11 19:02:11 +00003027 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003028 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003029 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003030 std::set<SDNode*> Visited;
3031 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003032
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003033 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003034 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003035 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003036
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003037 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003038}
3039
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003040/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003041void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3042 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003043 // Nothing to splice it into?
3044 if (OutChain == 0) return;
3045
3046 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3047 //OutChain->dump();
3048
3049 // Form a token factor node merging the old inval and the new inval.
3050 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3051 OutChain->getOperand(0));
3052 // Change the node to refer to the new token.
3053 OutChain->setAdjCallChain(InToken);
3054}
Chris Lattner4add7e32005-01-23 04:42:50 +00003055
3056
Chris Lattneraac464e2005-01-21 06:05:23 +00003057// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3058// does not fit into a register, return the lo part and set the hi part to the
3059// by-reg argument. If it does fit into a single register, return the result
3060// and leave the Hi part unset.
3061SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3062 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003063 SDNode *OutChain;
3064 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3065 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003066 if (InChain.Val == 0)
3067 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003068
Chris Lattneraac464e2005-01-21 06:05:23 +00003069 TargetLowering::ArgListTy Args;
3070 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3071 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3072 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3073 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3074 }
3075 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003076
Chris Lattner06bbeb62005-05-11 19:02:11 +00003077 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003078 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003079 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003080 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3081 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003082
Chris Lattner63022662005-09-02 20:26:58 +00003083 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003084 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003085 default: assert(0 && "Unknown thing");
3086 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003087 Result = CallInfo.first;
3088 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003089 case Promote:
3090 assert(0 && "Cannot promote this yet!");
3091 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003092 ExpandOp(CallInfo.first, Result, Hi);
3093 CallInfo.second = LegalizeOp(CallInfo.second);
3094 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003095 }
Chris Lattner63022662005-09-02 20:26:58 +00003096
3097 SpliceCallInto(CallInfo.second, OutChain);
3098 NeedsAnotherIteration = true;
3099 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003100}
3101
Chris Lattner4add7e32005-01-23 04:42:50 +00003102
Chris Lattneraac464e2005-01-21 06:05:23 +00003103/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3104/// destination type is legal.
3105SDOperand SelectionDAGLegalize::
3106ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003107 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003108 assert(getTypeAction(Source.getValueType()) == Expand &&
3109 "This is not an expansion!");
3110 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3111
Chris Lattner06bbeb62005-05-11 19:02:11 +00003112 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003113 assert(Source.getValueType() == MVT::i64 &&
3114 "This only works for 64-bit -> FP");
3115 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3116 // incoming integer is set. To handle this, we dynamically test to see if
3117 // it is set, and, if so, add a fudge factor.
3118 SDOperand Lo, Hi;
3119 ExpandOp(Source, Lo, Hi);
3120
Chris Lattner2a4f7312005-05-13 04:45:13 +00003121 // If this is unsigned, and not supported, first perform the conversion to
3122 // signed, then adjust the result if the sign bit is set.
3123 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3124 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3125
Chris Lattnerd47675e2005-08-09 20:20:18 +00003126 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3127 DAG.getConstant(0, Hi.getValueType()),
3128 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003129 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3130 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3131 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003132 uint64_t FF = 0x5f800000ULL;
3133 if (TLI.isLittleEndian()) FF <<= 32;
3134 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003135
Chris Lattnerc30405e2005-08-26 17:15:30 +00003136 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003137 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3138 SDOperand FudgeInReg;
3139 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003140 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3141 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003142 else {
3143 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003144 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3145 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003146 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003147 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003148 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003149
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003150 // Check to see if the target has a custom way to lower this. If so, use it.
3151 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3152 default: assert(0 && "This action not implemented for this operation!");
3153 case TargetLowering::Legal:
3154 case TargetLowering::Expand:
3155 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003156 case TargetLowering::Custom: {
3157 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3158 Source), DAG);
3159 if (NV.Val)
3160 return LegalizeOp(NV);
3161 break; // The target decided this was legal after all
3162 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003163 }
3164
Chris Lattner153587e2005-05-12 07:00:44 +00003165 // Expand the source, then glue it back together for the call. We must expand
3166 // the source in case it is shared (this pass of legalize must traverse it).
3167 SDOperand SrcLo, SrcHi;
3168 ExpandOp(Source, SrcLo, SrcHi);
3169 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3170
Chris Lattner06bbeb62005-05-11 19:02:11 +00003171 SDNode *OutChain = 0;
3172 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3173 DAG.getEntryNode());
3174 const char *FnName = 0;
3175 if (DestTy == MVT::f32)
3176 FnName = "__floatdisf";
3177 else {
3178 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3179 FnName = "__floatdidf";
3180 }
3181
Chris Lattneraac464e2005-01-21 06:05:23 +00003182 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3183
3184 TargetLowering::ArgListTy Args;
3185 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003186
Chris Lattneraac464e2005-01-21 06:05:23 +00003187 Args.push_back(std::make_pair(Source, ArgTy));
3188
3189 // We don't care about token chains for libcalls. We just use the entry
3190 // node as our input and ignore the output chain. This allows us to place
3191 // calls wherever we need them to satisfy data dependences.
3192 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003193
3194 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003195 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3196 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003197
Chris Lattnera5bf1032005-05-12 04:49:08 +00003198 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003199 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003200}
Misha Brukman835702a2005-04-21 22:36:52 +00003201
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003202
3203
Chris Lattnerdc750592005-01-07 07:47:09 +00003204/// ExpandOp - Expand the specified SDOperand into its two component pieces
3205/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3206/// LegalizeNodes map is filled in for any results that are not expanded, the
3207/// ExpandedNodes map is filled in for any results that are expanded, and the
3208/// Lo/Hi values are returned.
3209void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3210 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003211 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003212 SDNode *Node = Op.Val;
3213 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003214 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3215 "Cannot expand FP values!");
3216 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003217 "Cannot expand to FP value or to larger int value!");
3218
Chris Lattner1a570f12005-09-02 20:32:45 +00003219 // See if we already expanded it.
3220 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3221 = ExpandedNodes.find(Op);
3222 if (I != ExpandedNodes.end()) {
3223 Lo = I->second.first;
3224 Hi = I->second.second;
3225 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003226 }
3227
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003228 // Expanding to multiple registers needs to perform an optimization step, and
3229 // is not careful to avoid operations the target does not support. Make sure
3230 // that all generated operations are legalized in the next iteration.
3231 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003232
Chris Lattnerdc750592005-01-07 07:47:09 +00003233 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003234 case ISD::CopyFromReg:
3235 assert(0 && "CopyFromReg must be legal!");
3236 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003237 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3238 assert(0 && "Do not know how to expand this operator!");
3239 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003240 case ISD::UNDEF:
3241 Lo = DAG.getNode(ISD::UNDEF, NVT);
3242 Hi = DAG.getNode(ISD::UNDEF, NVT);
3243 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003244 case ISD::Constant: {
3245 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3246 Lo = DAG.getConstant(Cst, NVT);
3247 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3248 break;
3249 }
Nate Begemanae89d862005-12-07 19:48:11 +00003250 case ISD::ConstantVec: {
3251 unsigned NumElements = Node->getNumOperands();
3252 // If we only have two elements left in the constant vector, just break it
3253 // apart into the two scalar constants it contains. Otherwise, bisect the
3254 // ConstantVec, and return each half as a new ConstantVec.
3255 // FIXME: this is hard coded as big endian, it may have to change to support
3256 // SSE and Alpha MVI
3257 if (NumElements == 2) {
3258 Hi = Node->getOperand(0);
3259 Lo = Node->getOperand(1);
3260 } else {
3261 NumElements /= 2;
3262 std::vector<SDOperand> LoOps, HiOps;
3263 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3264 HiOps.push_back(Node->getOperand(I));
3265 LoOps.push_back(Node->getOperand(I+NumElements));
3266 }
3267 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3268 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3269 }
3270 break;
3271 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003272
Chris Lattner32e08b72005-03-28 22:03:13 +00003273 case ISD::BUILD_PAIR:
3274 // Legalize both operands. FIXME: in the future we should handle the case
3275 // where the two elements are not legal.
3276 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3277 Lo = LegalizeOp(Node->getOperand(0));
3278 Hi = LegalizeOp(Node->getOperand(1));
3279 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003280
3281 case ISD::SIGN_EXTEND_INREG:
3282 ExpandOp(Node->getOperand(0), Lo, Hi);
3283 // Sign extend the lo-part.
3284 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3285 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3286 TLI.getShiftAmountTy()));
3287 // sext_inreg the low part if needed.
3288 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3289 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003290
Chris Lattner55e9cde2005-05-11 04:51:16 +00003291 case ISD::CTPOP:
3292 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003293 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3294 DAG.getNode(ISD::CTPOP, NVT, Lo),
3295 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003296 Hi = DAG.getConstant(0, NVT);
3297 break;
3298
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003299 case ISD::CTLZ: {
3300 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003301 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003302 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3303 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003304 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3305 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003306 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3307 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3308
3309 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3310 Hi = DAG.getConstant(0, NVT);
3311 break;
3312 }
3313
3314 case ISD::CTTZ: {
3315 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003316 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003317 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3318 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003319 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3320 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003321 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3322 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3323
3324 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3325 Hi = DAG.getConstant(0, NVT);
3326 break;
3327 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003328
Chris Lattnerdc750592005-01-07 07:47:09 +00003329 case ISD::LOAD: {
3330 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3331 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003332 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003333
3334 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003335 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003336 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3337 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003338 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003339 //are from the same instruction?
3340 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003341
3342 // Build a factor node to remember that this load is independent of the
3343 // other one.
3344 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3345 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003346
Chris Lattnerdc750592005-01-07 07:47:09 +00003347 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003348 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003349 if (!TLI.isLittleEndian())
3350 std::swap(Lo, Hi);
3351 break;
3352 }
Nate Begemand37c1312005-11-22 18:16:00 +00003353 case ISD::VLOAD: {
3354 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3355 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3356 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3357 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3358
3359 // If we only have two elements, turn into a pair of scalar loads.
3360 // FIXME: handle case where a vector of two elements is fine, such as
3361 // 2 x double on SSE2.
3362 if (NumElements == 2) {
3363 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3364 // Increment the pointer to the other half.
3365 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3366 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3367 getIntPtrConstant(IncrementSize));
3368 //Is this safe? declaring that the two parts of the split load
3369 //are from the same instruction?
3370 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3371 } else {
3372 NumElements /= 2; // Split the vector in half
3373 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3374 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3375 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3376 getIntPtrConstant(IncrementSize));
3377 //Is this safe? declaring that the two parts of the split load
3378 //are from the same instruction?
3379 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3380 }
3381
3382 // Build a factor node to remember that this load is independent of the
3383 // other one.
3384 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3385 Hi.getValue(1));
3386
3387 // Remember that we legalized the chain.
3388 AddLegalizedOperand(Op.getValue(1), TF);
3389 if (!TLI.isLittleEndian())
3390 std::swap(Lo, Hi);
3391 break;
3392 }
3393 case ISD::VADD:
3394 case ISD::VSUB:
3395 case ISD::VMUL: {
3396 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3397 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3398 SDOperand LL, LH, RL, RH;
3399
3400 ExpandOp(Node->getOperand(0), LL, LH);
3401 ExpandOp(Node->getOperand(1), RL, RH);
3402
3403 // If we only have two elements, turn into a pair of scalar loads.
3404 // FIXME: handle case where a vector of two elements is fine, such as
3405 // 2 x double on SSE2.
3406 if (NumElements == 2) {
3407 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3408 Lo = DAG.getNode(Opc, EVT, LL, RL);
3409 Hi = DAG.getNode(Opc, EVT, LH, RH);
3410 } else {
3411 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3412 LL.getOperand(3));
3413 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3414 LH.getOperand(3));
3415 }
3416 break;
3417 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003418 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003419 case ISD::CALL: {
3420 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3421 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3422
Chris Lattner3d95c142005-01-19 20:24:35 +00003423 bool Changed = false;
3424 std::vector<SDOperand> Ops;
3425 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3426 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3427 Changed |= Ops.back() != Node->getOperand(i);
3428 }
3429
Chris Lattnerdc750592005-01-07 07:47:09 +00003430 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3431 "Can only expand a call once so far, not i64 -> i16!");
3432
3433 std::vector<MVT::ValueType> RetTyVTs;
3434 RetTyVTs.reserve(3);
3435 RetTyVTs.push_back(NVT);
3436 RetTyVTs.push_back(NVT);
3437 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003438 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3439 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003440 Lo = SDOperand(NC, 0);
3441 Hi = SDOperand(NC, 1);
3442
3443 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003444 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003445 break;
3446 }
3447 case ISD::AND:
3448 case ISD::OR:
3449 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3450 SDOperand LL, LH, RL, RH;
3451 ExpandOp(Node->getOperand(0), LL, LH);
3452 ExpandOp(Node->getOperand(1), RL, RH);
3453 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3454 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3455 break;
3456 }
3457 case ISD::SELECT: {
3458 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003459
3460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3461 case Expand: assert(0 && "It's impossible to expand bools");
3462 case Legal:
3463 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3464 break;
3465 case Promote:
3466 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3467 break;
3468 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003469 ExpandOp(Node->getOperand(1), LL, LH);
3470 ExpandOp(Node->getOperand(2), RL, RH);
3471 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3472 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3473 break;
3474 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003475 case ISD::SELECT_CC: {
3476 SDOperand TL, TH, FL, FH;
3477 ExpandOp(Node->getOperand(2), TL, TH);
3478 ExpandOp(Node->getOperand(3), FL, FH);
3479 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3480 Node->getOperand(1), TL, FL, Node->getOperand(4));
3481 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3482 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003483 Lo = LegalizeOp(Lo);
3484 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003485 break;
3486 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003487 case ISD::SEXTLOAD: {
3488 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3489 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3490 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3491
3492 if (EVT == NVT)
3493 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3494 else
3495 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3496 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003497
3498 // Remember that we legalized the chain.
3499 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3500
Nate Begemanc3a89c52005-10-13 17:15:37 +00003501 // The high part is obtained by SRA'ing all but one of the bits of the lo
3502 // part.
3503 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3504 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3505 TLI.getShiftAmountTy()));
3506 Lo = LegalizeOp(Lo);
3507 Hi = LegalizeOp(Hi);
3508 break;
3509 }
3510 case ISD::ZEXTLOAD: {
3511 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3512 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3513 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3514
3515 if (EVT == NVT)
3516 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3517 else
3518 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3519 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003520
3521 // Remember that we legalized the chain.
3522 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3523
Nate Begemanc3a89c52005-10-13 17:15:37 +00003524 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003525 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003526 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003527 break;
3528 }
3529 case ISD::EXTLOAD: {
3530 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3531 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3532 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3533
3534 if (EVT == NVT)
3535 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3536 else
3537 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3538 EVT);
3539
3540 // Remember that we legalized the chain.
3541 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3542
3543 // The high part is undefined.
3544 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3545 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003546 break;
3547 }
Chris Lattner7753f172005-09-02 00:18:10 +00003548 case ISD::ANY_EXTEND: {
3549 SDOperand In;
3550 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3551 case Expand: assert(0 && "expand-expand not implemented yet!");
3552 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3553 case Promote:
3554 In = PromoteOp(Node->getOperand(0));
3555 break;
3556 }
3557
3558 // The low part is any extension of the input (which degenerates to a copy).
3559 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3560 // The high part is undefined.
3561 Hi = DAG.getNode(ISD::UNDEF, NVT);
3562 break;
3563 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003564 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003565 SDOperand In;
3566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3567 case Expand: assert(0 && "expand-expand not implemented yet!");
3568 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3569 case Promote:
3570 In = PromoteOp(Node->getOperand(0));
3571 // Emit the appropriate sign_extend_inreg to get the value we want.
3572 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003573 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003574 break;
3575 }
3576
Chris Lattnerdc750592005-01-07 07:47:09 +00003577 // The low part is just a sign extension of the input (which degenerates to
3578 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003579 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003580
Chris Lattnerdc750592005-01-07 07:47:09 +00003581 // The high part is obtained by SRA'ing all but one of the bits of the lo
3582 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003583 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003584 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3585 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003586 break;
3587 }
Chris Lattner47844892005-04-03 23:41:52 +00003588 case ISD::ZERO_EXTEND: {
3589 SDOperand In;
3590 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3591 case Expand: assert(0 && "expand-expand not implemented yet!");
3592 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3593 case Promote:
3594 In = PromoteOp(Node->getOperand(0));
3595 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003596 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003597 break;
3598 }
3599
Chris Lattnerdc750592005-01-07 07:47:09 +00003600 // The low part is just a zero extension of the input (which degenerates to
3601 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003602 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003603
Chris Lattnerdc750592005-01-07 07:47:09 +00003604 // The high part is just a zero.
3605 Hi = DAG.getConstant(0, NVT);
3606 break;
Chris Lattner47844892005-04-03 23:41:52 +00003607 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003608
Chris Lattner44c28c22005-11-20 22:56:56 +00003609 case ISD::READCYCLECOUNTER: {
3610 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3611 TargetLowering::Custom &&
3612 "Must custom expand ReadCycleCounter");
3613 SDOperand T = TLI.LowerOperation(Op, DAG);
3614 assert(T.Val && "Node must be custom expanded!");
3615 Lo = LegalizeOp(T.getValue(0));
3616 Hi = LegalizeOp(T.getValue(1));
3617 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3618 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003619 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003620 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003621
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003622 // These operators cannot be expanded directly, emit them as calls to
3623 // library functions.
3624 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003625 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003626 SDOperand Op;
3627 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3628 case Expand: assert(0 && "cannot expand FP!");
3629 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3630 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3631 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003632
Chris Lattner941d84a2005-07-30 01:40:57 +00003633 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3634
Chris Lattnerfe68d752005-07-29 00:33:32 +00003635 // Now that the custom expander is done, expand the result, which is still
3636 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003637 if (Op.Val) {
3638 ExpandOp(Op, Lo, Hi);
3639 break;
3640 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003641 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003642
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003643 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003644 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003645 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003646 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003647 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003648
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003649 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003650 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3651 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3652 LegalizeOp(Node->getOperand(0)));
3653 // Now that the custom expander is done, expand the result, which is still
3654 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003655 Op = TLI.LowerOperation(Op, DAG);
3656 if (Op.Val) {
3657 ExpandOp(Op, Lo, Hi);
3658 break;
3659 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003660 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003661
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003662 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003663 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003664 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003665 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003666 break;
3667
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003668 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003669 // If the target wants custom lowering, do so.
3670 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3671 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3672 LegalizeOp(Node->getOperand(1)));
3673 Op = TLI.LowerOperation(Op, DAG);
3674 if (Op.Val) {
3675 // Now that the custom expander is done, expand the result, which is
3676 // still VT.
3677 ExpandOp(Op, Lo, Hi);
3678 break;
3679 }
3680 }
3681
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003682 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003683 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003684 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003685
3686 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003687 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003688 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3689 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003690 break;
3691 }
3692
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003693 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003694 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003695 break;
3696
3697 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003698 // If the target wants custom lowering, do so.
3699 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3700 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3701 LegalizeOp(Node->getOperand(1)));
3702 Op = TLI.LowerOperation(Op, DAG);
3703 if (Op.Val) {
3704 // Now that the custom expander is done, expand the result, which is
3705 // still VT.
3706 ExpandOp(Op, Lo, Hi);
3707 break;
3708 }
3709 }
3710
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003711 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003712 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003713 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003714
3715 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003716 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003717 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3718 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003719 break;
3720 }
3721
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003722 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003723 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003724 break;
3725 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003726 // If the target wants custom lowering, do so.
3727 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3728 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3729 LegalizeOp(Node->getOperand(1)));
3730 Op = TLI.LowerOperation(Op, DAG);
3731 if (Op.Val) {
3732 // Now that the custom expander is done, expand the result, which is
3733 // still VT.
3734 ExpandOp(Op, Lo, Hi);
3735 break;
3736 }
3737 }
3738
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003739 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003740 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003741 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003742
3743 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003744 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003745 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3746 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003747 break;
3748 }
3749
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003750 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003751 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003752 break;
3753
Misha Brukman835702a2005-04-21 22:36:52 +00003754 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003755 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3756 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003757 break;
3758 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003759 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3760 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003761 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003762 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003763 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003764 SDOperand LL, LH, RL, RH;
3765 ExpandOp(Node->getOperand(0), LL, LH);
3766 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003767 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3768 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3769 // extended the sign bit of the low half through the upper half, and if so
3770 // emit a MULHS instead of the alternate sequence that is valid for any
3771 // i64 x i64 multiply.
3772 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3773 // is RH an extension of the sign bit of RL?
3774 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3775 RH.getOperand(1).getOpcode() == ISD::Constant &&
3776 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3777 // is LH an extension of the sign bit of LL?
3778 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3779 LH.getOperand(1).getOpcode() == ISD::Constant &&
3780 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3781 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3782 } else {
3783 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3784 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3785 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3786 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3787 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3788 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003789 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3790 } else {
3791 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3792 }
3793 break;
3794 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003795 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3796 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3797 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3798 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003799 }
3800
3801 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003802 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3803 std::make_pair(Lo, Hi))).second;
3804 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003805}
3806
3807
3808// SelectionDAG::Legalize - This is the entry point for the file.
3809//
Chris Lattner4add7e32005-01-23 04:42:50 +00003810void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003811 /// run - This is the main entry point to this class.
3812 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003813 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003814}
3815