blob: dae6af4f563e9b9de33706d8358e097ef657191d [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));
570 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000571 Result = DAG.getCopyFromReg(Tmp1,
572 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
573 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000574 else
575 Result = Op.getValue(0);
576
577 // Since CopyFromReg produces two values, make sure to remember that we
578 // legalized both of them.
579 AddLegalizedOperand(Op.getValue(0), Result);
580 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
581 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000582 case ISD::ImplicitDef:
583 Tmp1 = LegalizeOp(Node->getOperand(0));
584 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000585 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
586 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000587 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000588 case ISD::UNDEF: {
589 MVT::ValueType VT = Op.getValueType();
590 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000591 default: assert(0 && "This action is not supported yet!");
592 case TargetLowering::Expand:
593 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000594 if (MVT::isInteger(VT))
595 Result = DAG.getConstant(0, VT);
596 else if (MVT::isFloatingPoint(VT))
597 Result = DAG.getConstantFP(0, VT);
598 else
599 assert(0 && "Unknown value type!");
600 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000601 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000602 break;
603 }
604 break;
605 }
Chris Lattner435b4022005-11-29 06:21:05 +0000606
607 case ISD::LOCATION:
608 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
610
611 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
612 case TargetLowering::Promote:
613 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000614 case TargetLowering::Expand: {
615 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
616 std::vector<SDOperand> Ops;
617 Ops.push_back(Tmp1); // chain
618 Ops.push_back(Node->getOperand(1)); // line #
619 Ops.push_back(Node->getOperand(2)); // col #
620 const std::string &fname = cast<StringSDNode>(Node->getOperand(3))->getValue();
621 const std::string &dirname=cast<StringSDNode>(Node->getOperand(4))->getValue();
622 unsigned id = DebugInfo.RecordSource(fname, dirname);
623 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
624 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
625 }
Chris Lattner435b4022005-11-29 06:21:05 +0000626 break;
627 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000628 if (Tmp1 != Node->getOperand(0) ||
629 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000630 std::vector<SDOperand> Ops;
631 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000632 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
633 Ops.push_back(Node->getOperand(1)); // line # must be legal.
634 Ops.push_back(Node->getOperand(2)); // col # must be legal.
635 } else {
636 // Otherwise promote them.
637 Ops.push_back(PromoteOp(Node->getOperand(1)));
638 Ops.push_back(PromoteOp(Node->getOperand(2)));
639 }
Chris Lattner435b4022005-11-29 06:21:05 +0000640 Ops.push_back(Node->getOperand(3)); // filename must be legal.
641 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
642 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
643 }
644 break;
645 }
646 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000647
648 case ISD::DEBUG_LOC:
649 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
650 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
651 case TargetLowering::Promote:
652 case TargetLowering::Expand:
653 default: assert(0 && "This action is not supported yet!");
654 case TargetLowering::Legal:
655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
656 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
657 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
658 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
659
660 if (Tmp1 != Node->getOperand(0) ||
661 Tmp2 != Node->getOperand(1) ||
662 Tmp3 != Node->getOperand(2) ||
663 Tmp4 != Node->getOperand(3)) {
664 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
665 }
666 break;
667 }
668 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000669
Chris Lattnerdc750592005-01-07 07:47:09 +0000670 case ISD::Constant:
671 // We know we don't need to expand constants here, constants only have one
672 // value and we check that it is fine above.
673
674 // FIXME: Maybe we should handle things like targets that don't support full
675 // 32-bit immediates?
676 break;
677 case ISD::ConstantFP: {
678 // Spill FP immediates to the constant pool if the target cannot directly
679 // codegen them. Targets often have some immediate values that can be
680 // efficiently generated into an FP register without a load. We explicitly
681 // leave these constants as ConstantFP nodes for the target to deal with.
682
683 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
684
685 // Check to see if this FP immediate is already legal.
686 bool isLegal = false;
687 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
688 E = TLI.legal_fpimm_end(); I != E; ++I)
689 if (CFP->isExactlyValue(*I)) {
690 isLegal = true;
691 break;
692 }
693
694 if (!isLegal) {
695 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000696 bool Extend = false;
697
698 // If a FP immediate is precise when represented as a float, we put it
699 // into the constant pool as a float, even if it's is statically typed
700 // as a double.
701 MVT::ValueType VT = CFP->getValueType(0);
702 bool isDouble = VT == MVT::f64;
703 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
704 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000705 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
706 // Only do this if the target has a native EXTLOAD instruction from
707 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000708 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000709 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
710 VT = MVT::f32;
711 Extend = true;
712 }
Misha Brukman835702a2005-04-21 22:36:52 +0000713
Nate Begeman4e56db62005-12-10 02:36:00 +0000714 SDOperand CPIdx =
715 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000716 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000717 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
718 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000719 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000720 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
721 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000722 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000723 }
724 break;
725 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000726 case ISD::ConstantVec: {
727 // We assume that vector constants are not legal, and will be immediately
728 // spilled to the constant pool.
729 //
730 // FIXME: revisit this when we have some kind of mechanism by which targets
731 // can decided legality of vector constants, of which there may be very
732 // many.
733 //
734 // Create a ConstantPacked, and put it in the constant pool.
735 std::vector<Constant*> CV;
736 MVT::ValueType VT = Node->getValueType(0);
737 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
738 SDOperand OpN = Node->getOperand(I);
739 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
740 if (MVT::isFloatingPoint(VT))
741 CV.push_back(ConstantFP::get(OpNTy,
742 cast<ConstantFPSDNode>(OpN)->getValue()));
743 else
744 CV.push_back(ConstantUInt::get(OpNTy,
745 cast<ConstantSDNode>(OpN)->getValue()));
746 }
747 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000748 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000749 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
750 break;
751 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000752 case ISD::TokenFactor:
753 if (Node->getNumOperands() == 2) {
754 bool Changed = false;
755 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
756 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
757 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
758 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
759 } else {
760 std::vector<SDOperand> Ops;
761 bool Changed = false;
762 // Legalize the operands.
763 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
764 SDOperand Op = Node->getOperand(i);
765 Ops.push_back(LegalizeOp(Op));
766 Changed |= Ops[i] != Op;
767 }
768 if (Changed)
769 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000770 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000771 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000772
Chris Lattner2dce7032005-05-12 23:24:06 +0000773 case ISD::CALLSEQ_START:
774 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000775 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000776 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000777 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000778 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000779 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000780
Chris Lattner2dce7032005-05-12 23:24:06 +0000781 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000782 // nodes are treated specially and are mutated in place. This makes the dag
783 // legalization process more efficient and also makes libcall insertion
784 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000785 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000786 case ISD::DYNAMIC_STACKALLOC:
787 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
788 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
789 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
790 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000791 Tmp3 != Node->getOperand(2)) {
792 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
793 std::vector<SDOperand> Ops;
794 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
795 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
796 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000797 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000798
799 // Since this op produces two values, make sure to remember that we
800 // legalized both of them.
801 AddLegalizedOperand(SDOperand(Node, 0), Result);
802 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
803 return Result.getValue(Op.ResNo);
804
Chris Lattnerd0feb642005-05-13 18:43:43 +0000805 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000806 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
808 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000809
810 bool Changed = false;
811 std::vector<SDOperand> Ops;
812 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
813 Ops.push_back(LegalizeOp(Node->getOperand(i)));
814 Changed |= Ops.back() != Node->getOperand(i);
815 }
816
817 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000818 std::vector<MVT::ValueType> RetTyVTs;
819 RetTyVTs.reserve(Node->getNumValues());
820 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000821 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000822 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
823 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000824 } else {
825 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000826 }
Chris Lattner9242c502005-01-09 19:43:23 +0000827 // Since calls produce multiple values, make sure to remember that we
828 // legalized all of them.
829 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
830 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
831 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000832 }
Chris Lattner68a12142005-01-07 22:12:08 +0000833 case ISD::BR:
834 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
835 if (Tmp1 != Node->getOperand(0))
836 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
837 break;
838
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000839 case ISD::BRCOND:
840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000841
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000842 switch (getTypeAction(Node->getOperand(1).getValueType())) {
843 case Expand: assert(0 && "It's impossible to expand bools");
844 case Legal:
845 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
846 break;
847 case Promote:
848 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
849 break;
850 }
Nate Begeman371e4952005-08-16 19:49:35 +0000851
852 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
853 default: assert(0 && "This action is not supported yet!");
854 case TargetLowering::Expand:
855 // Expand brcond's setcc into its constituent parts and create a BR_CC
856 // Node.
857 if (Tmp2.getOpcode() == ISD::SETCC) {
858 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
859 Tmp2.getOperand(0), Tmp2.getOperand(1),
860 Node->getOperand(2));
861 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000862 // Make sure the condition is either zero or one. It may have been
863 // promoted from something else.
864 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
865
Nate Begeman371e4952005-08-16 19:49:35 +0000866 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
867 DAG.getCondCode(ISD::SETNE), Tmp2,
868 DAG.getConstant(0, Tmp2.getValueType()),
869 Node->getOperand(2));
870 }
871 break;
872 case TargetLowering::Legal:
873 // Basic block destination (Op#2) is always legal.
874 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
875 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
876 Node->getOperand(2));
877 break;
878 }
879 break;
880 case ISD::BR_CC:
881 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
882
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000883 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000884 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
885 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
886 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
887 Tmp3 != Node->getOperand(3)) {
888 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
889 Tmp2, Tmp3, Node->getOperand(4));
890 }
891 break;
892 } else {
893 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 }
911 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000912 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000913 case ISD::BRCONDTWOWAY:
914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
915 switch (getTypeAction(Node->getOperand(1).getValueType())) {
916 case Expand: assert(0 && "It's impossible to expand bools");
917 case Legal:
918 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
919 break;
920 case Promote:
921 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
922 break;
923 }
924 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
925 // pair.
926 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
927 case TargetLowering::Promote:
928 default: assert(0 && "This action is not supported yet!");
929 case TargetLowering::Legal:
930 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
931 std::vector<SDOperand> Ops;
932 Ops.push_back(Tmp1);
933 Ops.push_back(Tmp2);
934 Ops.push_back(Node->getOperand(2));
935 Ops.push_back(Node->getOperand(3));
936 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
937 }
938 break;
939 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000940 // If BRTWOWAY_CC is legal for this target, then simply expand this node
941 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
942 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000943 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000944 if (Tmp2.getOpcode() == ISD::SETCC) {
945 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
946 Tmp2.getOperand(0), Tmp2.getOperand(1),
947 Node->getOperand(2), Node->getOperand(3));
948 } else {
949 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
950 DAG.getConstant(0, Tmp2.getValueType()),
951 Node->getOperand(2), Node->getOperand(3));
952 }
953 } else {
954 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000955 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000956 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
957 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000958 break;
959 }
960 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000961 case ISD::BRTWOWAY_CC:
962 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000963 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000964 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
965 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
966 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
967 Tmp3 != Node->getOperand(3)) {
968 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
969 Node->getOperand(4), Node->getOperand(5));
970 }
971 break;
972 } else {
973 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
974 Node->getOperand(2), // LHS
975 Node->getOperand(3), // RHS
976 Node->getOperand(1)));
977 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
978 // pair.
979 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
980 default: assert(0 && "This action is not supported yet!");
981 case TargetLowering::Legal:
982 // If we get a SETCC back from legalizing the SETCC node we just
983 // created, then use its LHS, RHS, and CC directly in creating a new
984 // node. Otherwise, select between the true and false value based on
985 // comparing the result of the legalized with zero.
986 if (Tmp2.getOpcode() == ISD::SETCC) {
987 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
988 Tmp2.getOperand(0), Tmp2.getOperand(1),
989 Node->getOperand(4), Node->getOperand(5));
990 } else {
991 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
992 DAG.getConstant(0, Tmp2.getValueType()),
993 Node->getOperand(4), Node->getOperand(5));
994 }
995 break;
996 case TargetLowering::Expand:
997 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
998 Node->getOperand(4));
999 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1000 break;
1001 }
1002 }
1003 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001004 case ISD::LOAD:
1005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1006 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001007
Chris Lattnerdc750592005-01-07 07:47:09 +00001008 if (Tmp1 != Node->getOperand(0) ||
1009 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +00001010 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1011 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001012 else
1013 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001014
Chris Lattnerea4ca942005-01-07 22:28:47 +00001015 // Since loads produce two values, make sure to remember that we legalized
1016 // both of them.
1017 AddLegalizedOperand(SDOperand(Node, 0), Result);
1018 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1019 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +00001020
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001021 case ISD::EXTLOAD:
1022 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001023 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001024 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1025 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001026
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001027 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001028 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001029 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001030 case TargetLowering::Promote:
1031 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001032 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1033 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001034 // Since loads produce two values, make sure to remember that we legalized
1035 // both of them.
1036 AddLegalizedOperand(SDOperand(Node, 0), Result);
1037 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1038 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001039
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001040 case TargetLowering::Legal:
1041 if (Tmp1 != Node->getOperand(0) ||
1042 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001043 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1044 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001045 else
1046 Result = SDOperand(Node, 0);
1047
1048 // Since loads produce two values, make sure to remember that we legalized
1049 // both of them.
1050 AddLegalizedOperand(SDOperand(Node, 0), Result);
1051 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1052 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001053 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001054 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1055 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1056 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001057 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001058 if (Op.ResNo)
1059 return Load.getValue(1);
1060 return Result;
1061 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001062 assert(Node->getOpcode() != ISD::EXTLOAD &&
1063 "EXTLOAD should always be supported!");
1064 // Turn the unsupported load into an EXTLOAD followed by an explicit
1065 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001066 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1067 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001068 SDOperand ValRes;
1069 if (Node->getOpcode() == ISD::SEXTLOAD)
1070 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001071 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001072 else
1073 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001074 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1075 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1076 if (Op.ResNo)
1077 return Result.getValue(1);
1078 return ValRes;
1079 }
1080 assert(0 && "Unreachable");
1081 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001082 case ISD::EXTRACT_ELEMENT: {
1083 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1084 switch (getTypeAction(OpTy)) {
1085 default:
1086 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1087 break;
1088 case Legal:
1089 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1090 // 1 -> Hi
1091 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1092 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1093 TLI.getShiftAmountTy()));
1094 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1095 } else {
1096 // 0 -> Lo
1097 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1098 Node->getOperand(0));
1099 }
1100 Result = LegalizeOp(Result);
1101 break;
1102 case Expand:
1103 // Get both the low and high parts.
1104 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1105 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1106 Result = Tmp2; // 1 -> Hi
1107 else
1108 Result = Tmp1; // 0 -> Lo
1109 break;
1110 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001111 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001112 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001113
1114 case ISD::CopyToReg:
1115 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001116
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001117 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001118 "Register type must be legal!");
1119 // Legalize the incoming value (must be legal).
1120 Tmp2 = LegalizeOp(Node->getOperand(2));
1121 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1122 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1123 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001124 break;
1125
1126 case ISD::RET:
1127 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1128 switch (Node->getNumOperands()) {
1129 case 2: // ret val
1130 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1131 case Legal:
1132 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001133 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001134 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1135 break;
1136 case Expand: {
1137 SDOperand Lo, Hi;
1138 ExpandOp(Node->getOperand(1), Lo, Hi);
1139 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001140 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001141 }
1142 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001143 Tmp2 = PromoteOp(Node->getOperand(1));
1144 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1145 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001146 }
1147 break;
1148 case 1: // ret void
1149 if (Tmp1 != Node->getOperand(0))
1150 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1151 break;
1152 default: { // ret <values>
1153 std::vector<SDOperand> NewValues;
1154 NewValues.push_back(Tmp1);
1155 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1156 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1157 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001158 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001159 break;
1160 case Expand: {
1161 SDOperand Lo, Hi;
1162 ExpandOp(Node->getOperand(i), Lo, Hi);
1163 NewValues.push_back(Lo);
1164 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001165 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001166 }
1167 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001168 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001169 }
1170 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1171 break;
1172 }
1173 }
1174 break;
1175 case ISD::STORE:
1176 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1177 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1178
Chris Lattnere69daaf2005-01-08 06:25:56 +00001179 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001180 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001181 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001182 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001183 DAG.getConstant(FloatToBits(CFP->getValue()),
1184 MVT::i32),
1185 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001186 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001187 } else {
1188 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001189 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001190 DAG.getConstant(DoubleToBits(CFP->getValue()),
1191 MVT::i64),
1192 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001193 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001194 }
Chris Lattnera4743132005-02-22 07:23:39 +00001195 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001196 }
1197
Chris Lattnerdc750592005-01-07 07:47:09 +00001198 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1199 case Legal: {
1200 SDOperand Val = LegalizeOp(Node->getOperand(1));
1201 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1202 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001203 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1204 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001205 break;
1206 }
1207 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001208 // Truncate the value and store the result.
1209 Tmp3 = PromoteOp(Node->getOperand(1));
1210 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001211 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001212 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001213 break;
1214
Chris Lattnerdc750592005-01-07 07:47:09 +00001215 case Expand:
1216 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001217 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001218 ExpandOp(Node->getOperand(1), Lo, Hi);
1219
1220 if (!TLI.isLittleEndian())
1221 std::swap(Lo, Hi);
1222
Chris Lattner55e9cde2005-05-11 04:51:16 +00001223 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1224 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001225 // If this is a vector type, then we have to calculate the increment as
1226 // the product of the element size in bytes, and the number of elements
1227 // in the high half of the vector.
1228 if (MVT::Vector == Hi.getValueType()) {
1229 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1230 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1231 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1232 } else {
1233 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1234 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001235 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1236 getIntPtrConstant(IncrementSize));
1237 assert(isTypeLegal(Tmp2.getValueType()) &&
1238 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001239 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001240 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1241 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001242 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1243 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001244 }
1245 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001246 case ISD::PCMARKER:
1247 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001248 if (Tmp1 != Node->getOperand(0))
1249 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001250 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001251 case ISD::READCYCLECOUNTER:
1252 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001253 if (Tmp1 != Node->getOperand(0)) {
1254 std::vector<MVT::ValueType> rtypes;
1255 std::vector<SDOperand> rvals;
1256 rtypes.push_back(MVT::i64);
1257 rtypes.push_back(MVT::Other);
1258 rvals.push_back(Tmp1);
1259 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1260 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001261
1262 // Since rdcc produce two values, make sure to remember that we legalized
1263 // both of them.
1264 AddLegalizedOperand(SDOperand(Node, 0), Result);
1265 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1266 return Result.getValue(Op.ResNo);
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001267 break;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001268
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001269 case ISD::TRUNCSTORE:
1270 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1271 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1272
1273 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1274 case Legal:
1275 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001276
1277 // The only promote case we handle is TRUNCSTORE:i1 X into
1278 // -> TRUNCSTORE:i8 (and X, 1)
1279 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1280 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1281 TargetLowering::Promote) {
1282 // Promote the bool to a mask then store.
1283 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1284 DAG.getConstant(1, Tmp2.getValueType()));
1285 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1286 Node->getOperand(3), DAG.getValueType(MVT::i8));
1287
1288 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1289 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001290 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001291 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001292 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001293 break;
1294 case Promote:
1295 case Expand:
1296 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1297 }
1298 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001299 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001300 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1301 case Expand: assert(0 && "It's impossible to expand bools");
1302 case Legal:
1303 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1304 break;
1305 case Promote:
1306 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1307 break;
1308 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001309 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001310 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001311
Nate Begeman987121a2005-08-23 04:29:48 +00001312 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001313 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001314 case TargetLowering::Expand:
1315 if (Tmp1.getOpcode() == ISD::SETCC) {
1316 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1317 Tmp2, Tmp3,
1318 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1319 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001320 // Make sure the condition is either zero or one. It may have been
1321 // promoted from something else.
1322 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001323 Result = DAG.getSelectCC(Tmp1,
1324 DAG.getConstant(0, Tmp1.getValueType()),
1325 Tmp2, Tmp3, ISD::SETNE);
1326 }
1327 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001328 case TargetLowering::Custom: {
1329 SDOperand Tmp =
1330 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1331 Tmp1, Tmp2, Tmp3), DAG);
1332 if (Tmp.Val) {
1333 Result = LegalizeOp(Tmp);
1334 break;
1335 }
1336 // FALLTHROUGH if the target thinks it is legal.
1337 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001338 case TargetLowering::Legal:
1339 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1340 Tmp3 != Node->getOperand(2))
1341 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1342 Tmp1, Tmp2, Tmp3);
1343 break;
1344 case TargetLowering::Promote: {
1345 MVT::ValueType NVT =
1346 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1347 unsigned ExtOp, TruncOp;
1348 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001349 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001350 TruncOp = ISD::TRUNCATE;
1351 } else {
1352 ExtOp = ISD::FP_EXTEND;
1353 TruncOp = ISD::FP_ROUND;
1354 }
1355 // Promote each of the values to the new type.
1356 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1357 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1358 // Perform the larger operation, then round down.
1359 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1360 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1361 break;
1362 }
1363 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001364 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001365 case ISD::SELECT_CC:
1366 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1367 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1368
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001369 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001370 // Everything is legal, see if we should expand this op or something.
1371 switch (TLI.getOperationAction(ISD::SELECT_CC,
1372 Node->getOperand(0).getValueType())) {
1373 default: assert(0 && "This action is not supported yet!");
1374 case TargetLowering::Custom: {
1375 SDOperand Tmp =
1376 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1377 Node->getOperand(0),
1378 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001379 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001380 if (Tmp.Val) {
1381 Result = LegalizeOp(Tmp);
1382 break;
1383 }
1384 } // FALLTHROUGH if the target can't lower this operation after all.
1385 case TargetLowering::Legal:
1386 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1387 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1388 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1389 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1390 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1391 Tmp3, Tmp4, Node->getOperand(4));
1392 }
1393 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001394 }
1395 break;
1396 } else {
1397 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1398 Node->getOperand(0), // LHS
1399 Node->getOperand(1), // RHS
1400 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001401 // If we get a SETCC back from legalizing the SETCC node we just
1402 // created, then use its LHS, RHS, and CC directly in creating a new
1403 // node. Otherwise, select between the true and false value based on
1404 // comparing the result of the legalized with zero.
1405 if (Tmp1.getOpcode() == ISD::SETCC) {
1406 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1407 Tmp1.getOperand(0), Tmp1.getOperand(1),
1408 Tmp3, Tmp4, Tmp1.getOperand(2));
1409 } else {
1410 Result = DAG.getSelectCC(Tmp1,
1411 DAG.getConstant(0, Tmp1.getValueType()),
1412 Tmp3, Tmp4, ISD::SETNE);
1413 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001414 }
1415 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001416 case ISD::SETCC:
1417 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1418 case Legal:
1419 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1420 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001421 break;
1422 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001423 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1424 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1425
1426 // If this is an FP compare, the operands have already been extended.
1427 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1428 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001429 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001430
1431 // Otherwise, we have to insert explicit sign or zero extends. Note
1432 // that we could insert sign extends for ALL conditions, but zero extend
1433 // is cheaper on many machines (an AND instead of two shifts), so prefer
1434 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001435 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001436 default: assert(0 && "Unknown integer comparison!");
1437 case ISD::SETEQ:
1438 case ISD::SETNE:
1439 case ISD::SETUGE:
1440 case ISD::SETUGT:
1441 case ISD::SETULE:
1442 case ISD::SETULT:
1443 // ALL of these operations will work if we either sign or zero extend
1444 // the operands (including the unsigned comparisons!). Zero extend is
1445 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001446 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1447 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001448 break;
1449 case ISD::SETGE:
1450 case ISD::SETGT:
1451 case ISD::SETLT:
1452 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001453 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1454 DAG.getValueType(VT));
1455 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1456 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001457 break;
1458 }
Chris Lattner4d978642005-01-15 22:16:26 +00001459 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001460 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001461 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001462 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1463 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1464 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001465 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001466 case ISD::SETEQ:
1467 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001468 if (RHSLo == RHSHi)
1469 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1470 if (RHSCST->isAllOnesValue()) {
1471 // Comparison to -1.
1472 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001473 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001474 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001475 }
1476
Chris Lattnerdc750592005-01-07 07:47:09 +00001477 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1478 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1479 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001480 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001481 break;
1482 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001483 // If this is a comparison of the sign bit, just look at the top part.
1484 // X > -1, x < 0
1485 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001486 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001487 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001488 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001489 (CST->isAllOnesValue()))) { // X > -1
1490 Tmp1 = LHSHi;
1491 Tmp2 = RHSHi;
1492 break;
1493 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001494
Chris Lattnerdc750592005-01-07 07:47:09 +00001495 // FIXME: This generated code sucks.
1496 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001497 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001498 default: assert(0 && "Unknown integer setcc!");
1499 case ISD::SETLT:
1500 case ISD::SETULT: LowCC = ISD::SETULT; break;
1501 case ISD::SETGT:
1502 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1503 case ISD::SETLE:
1504 case ISD::SETULE: LowCC = ISD::SETULE; break;
1505 case ISD::SETGE:
1506 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1507 }
Misha Brukman835702a2005-04-21 22:36:52 +00001508
Chris Lattnerdc750592005-01-07 07:47:09 +00001509 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1510 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1511 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1512
1513 // NOTE: on targets without efficient SELECT of bools, we can always use
1514 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001515 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1516 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1517 Node->getOperand(2));
1518 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001519 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1520 Result, Tmp1, Tmp2));
1521 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001522 }
1523 }
Nate Begeman987121a2005-08-23 04:29:48 +00001524
1525 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1526 default:
1527 assert(0 && "Cannot handle this action for SETCC yet!");
1528 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001529 case TargetLowering::Promote: {
1530 // First step, figure out the appropriate operation to use.
1531 // Allow SETCC to not be supported for all legal data types
1532 // Mostly this targets FP
1533 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1534 MVT::ValueType OldVT = NewInTy;
1535
1536 // Scan for the appropriate larger type to use.
1537 while (1) {
1538 NewInTy = (MVT::ValueType)(NewInTy+1);
1539
1540 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1541 "Fell off of the edge of the integer world");
1542 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1543 "Fell off of the edge of the floating point world");
1544
1545 // If the target supports SETCC of this type, use it.
1546 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1547 break;
1548 }
1549 if (MVT::isInteger(NewInTy))
1550 assert(0 && "Cannot promote Legal Integer SETCC yet");
1551 else {
1552 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1553 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1554 }
1555
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001556 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1557 Node->getOperand(2));
1558 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001559 }
Nate Begeman987121a2005-08-23 04:29:48 +00001560 case TargetLowering::Legal:
1561 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1562 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1563 Node->getOperand(2));
1564 break;
1565 case TargetLowering::Expand:
1566 // Expand a setcc node into a select_cc of the same condition, lhs, and
1567 // rhs that selects between const 1 (true) and const 0 (false).
1568 MVT::ValueType VT = Node->getValueType(0);
1569 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1570 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1571 Node->getOperand(2));
1572 Result = LegalizeOp(Result);
1573 break;
1574 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001575 break;
1576
Chris Lattner85d70c62005-01-11 05:57:22 +00001577 case ISD::MEMSET:
1578 case ISD::MEMCPY:
1579 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001580 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001581 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1582
1583 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1584 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1585 case Expand: assert(0 && "Cannot expand a byte!");
1586 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001587 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001588 break;
1589 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001590 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001591 break;
1592 }
1593 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001594 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001595 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001596
1597 SDOperand Tmp4;
1598 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001599 case Expand: {
1600 // Length is too big, just take the lo-part of the length.
1601 SDOperand HiPart;
1602 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1603 break;
1604 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001605 case Legal:
1606 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001607 break;
1608 case Promote:
1609 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001610 break;
1611 }
1612
1613 SDOperand Tmp5;
1614 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1615 case Expand: assert(0 && "Cannot expand this yet!");
1616 case Legal:
1617 Tmp5 = LegalizeOp(Node->getOperand(4));
1618 break;
1619 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001620 Tmp5 = PromoteOp(Node->getOperand(4));
1621 break;
1622 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001623
1624 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1625 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001626 case TargetLowering::Custom: {
1627 SDOperand Tmp =
1628 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1629 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1630 if (Tmp.Val) {
1631 Result = LegalizeOp(Tmp);
1632 break;
1633 }
1634 // FALLTHROUGH if the target thinks it is legal.
1635 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001636 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001637 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1638 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1639 Tmp5 != Node->getOperand(4)) {
1640 std::vector<SDOperand> Ops;
1641 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1642 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1643 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1644 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001645 break;
1646 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001647 // Otherwise, the target does not support this operation. Lower the
1648 // operation to an explicit libcall as appropriate.
1649 MVT::ValueType IntPtr = TLI.getPointerTy();
1650 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1651 std::vector<std::pair<SDOperand, const Type*> > Args;
1652
Reid Spencer6dced922005-01-12 14:53:45 +00001653 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001654 if (Node->getOpcode() == ISD::MEMSET) {
1655 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1656 // Extend the ubyte argument to be an int value for the call.
1657 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1658 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1659 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1660
1661 FnName = "memset";
1662 } else if (Node->getOpcode() == ISD::MEMCPY ||
1663 Node->getOpcode() == ISD::MEMMOVE) {
1664 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1665 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1666 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1667 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1668 } else {
1669 assert(0 && "Unknown op!");
1670 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001671
Chris Lattner85d70c62005-01-11 05:57:22 +00001672 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001673 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001674 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001675 Result = CallResult.second;
1676 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001677 break;
1678 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001679 }
1680 break;
1681 }
Chris Lattner5385db52005-05-09 20:23:03 +00001682
1683 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001684 Tmp1 = LegalizeOp(Node->getOperand(0));
1685 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001686
Chris Lattner86535992005-05-14 07:45:46 +00001687 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1688 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1689 std::vector<SDOperand> Ops;
1690 Ops.push_back(Tmp1);
1691 Ops.push_back(Tmp2);
1692 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1693 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001694 Result = SDOperand(Node, 0);
1695 // Since these produce two values, make sure to remember that we legalized
1696 // both of them.
1697 AddLegalizedOperand(SDOperand(Node, 0), Result);
1698 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1699 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001700 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001701 Tmp1 = LegalizeOp(Node->getOperand(0));
1702 Tmp2 = LegalizeOp(Node->getOperand(1));
1703 Tmp3 = LegalizeOp(Node->getOperand(2));
1704 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1705 Tmp3 != Node->getOperand(2))
1706 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1707 break;
1708
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001709 case ISD::READIO:
1710 Tmp1 = LegalizeOp(Node->getOperand(0));
1711 Tmp2 = LegalizeOp(Node->getOperand(1));
1712
1713 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1714 case TargetLowering::Custom:
1715 default: assert(0 && "This action not implemented for this operation!");
1716 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001717 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1718 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1719 std::vector<SDOperand> Ops;
1720 Ops.push_back(Tmp1);
1721 Ops.push_back(Tmp2);
1722 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1723 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001724 Result = SDOperand(Node, 0);
1725 break;
1726 case TargetLowering::Expand:
1727 // Replace this with a load from memory.
1728 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1729 Node->getOperand(1), DAG.getSrcValue(NULL));
1730 Result = LegalizeOp(Result);
1731 break;
1732 }
1733
1734 // Since these produce two values, make sure to remember that we legalized
1735 // both of them.
1736 AddLegalizedOperand(SDOperand(Node, 0), Result);
1737 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1738 return Result.getValue(Op.ResNo);
1739
1740 case ISD::WRITEIO:
1741 Tmp1 = LegalizeOp(Node->getOperand(0));
1742 Tmp2 = LegalizeOp(Node->getOperand(1));
1743 Tmp3 = LegalizeOp(Node->getOperand(2));
1744
1745 switch (TLI.getOperationAction(Node->getOpcode(),
1746 Node->getOperand(1).getValueType())) {
1747 case TargetLowering::Custom:
1748 default: assert(0 && "This action not implemented for this operation!");
1749 case TargetLowering::Legal:
1750 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1751 Tmp3 != Node->getOperand(2))
1752 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1753 break;
1754 case TargetLowering::Expand:
1755 // Replace this with a store to memory.
1756 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1757 Node->getOperand(1), Node->getOperand(2),
1758 DAG.getSrcValue(NULL));
1759 Result = LegalizeOp(Result);
1760 break;
1761 }
1762 break;
1763
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001764 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001765 case ISD::SUB_PARTS:
1766 case ISD::SHL_PARTS:
1767 case ISD::SRA_PARTS:
1768 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001769 std::vector<SDOperand> Ops;
1770 bool Changed = false;
1771 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1772 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1773 Changed |= Ops.back() != Node->getOperand(i);
1774 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001775 if (Changed) {
1776 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1777 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1778 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001779
1780 // Since these produce multiple values, make sure to remember that we
1781 // legalized all of them.
1782 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1783 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1784 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001785 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001786
1787 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001788 case ISD::ADD:
1789 case ISD::SUB:
1790 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001791 case ISD::MULHS:
1792 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001793 case ISD::UDIV:
1794 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001795 case ISD::AND:
1796 case ISD::OR:
1797 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001798 case ISD::SHL:
1799 case ISD::SRL:
1800 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001801 case ISD::FADD:
1802 case ISD::FSUB:
1803 case ISD::FMUL:
1804 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001805 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001806 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1807 case Expand: assert(0 && "Not possible");
1808 case Legal:
1809 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1810 break;
1811 case Promote:
1812 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1813 break;
1814 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001815 if (Tmp1 != Node->getOperand(0) ||
1816 Tmp2 != Node->getOperand(1))
1817 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1818 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001819
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001820 case ISD::BUILD_PAIR: {
1821 MVT::ValueType PairTy = Node->getValueType(0);
1822 // TODO: handle the case where the Lo and Hi operands are not of legal type
1823 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1824 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1825 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1826 case TargetLowering::Legal:
1827 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1828 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1829 break;
1830 case TargetLowering::Promote:
1831 case TargetLowering::Custom:
1832 assert(0 && "Cannot promote/custom this yet!");
1833 case TargetLowering::Expand:
1834 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1835 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1836 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1837 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1838 TLI.getShiftAmountTy()));
1839 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1840 break;
1841 }
1842 break;
1843 }
1844
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001845 case ISD::UREM:
1846 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001847 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001848 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1849 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1850 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1851 case TargetLowering::Legal:
1852 if (Tmp1 != Node->getOperand(0) ||
1853 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001854 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001855 Tmp2);
1856 break;
1857 case TargetLowering::Promote:
1858 case TargetLowering::Custom:
1859 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001860 case TargetLowering::Expand:
1861 if (MVT::isInteger(Node->getValueType(0))) {
1862 MVT::ValueType VT = Node->getValueType(0);
1863 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1864 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1865 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1866 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1867 } else {
1868 // Floating point mod -> fmod libcall.
1869 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1870 SDOperand Dummy;
1871 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001872 }
1873 break;
1874 }
1875 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001876
Andrew Lenharth5e177822005-05-03 17:19:30 +00001877 case ISD::CTPOP:
1878 case ISD::CTTZ:
1879 case ISD::CTLZ:
1880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1881 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1882 case TargetLowering::Legal:
1883 if (Tmp1 != Node->getOperand(0))
1884 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1885 break;
1886 case TargetLowering::Promote: {
1887 MVT::ValueType OVT = Tmp1.getValueType();
1888 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001889
1890 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001891 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1892 // Perform the larger operation, then subtract if needed.
1893 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1894 switch(Node->getOpcode())
1895 {
1896 case ISD::CTPOP:
1897 Result = Tmp1;
1898 break;
1899 case ISD::CTTZ:
1900 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001901 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1902 DAG.getConstant(getSizeInBits(NVT), NVT),
1903 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001904 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001905 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1906 break;
1907 case ISD::CTLZ:
1908 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001909 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1910 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001911 getSizeInBits(OVT), NVT));
1912 break;
1913 }
1914 break;
1915 }
1916 case TargetLowering::Custom:
1917 assert(0 && "Cannot custom handle this yet!");
1918 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001919 switch(Node->getOpcode())
1920 {
1921 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001922 static const uint64_t mask[6] = {
1923 0x5555555555555555ULL, 0x3333333333333333ULL,
1924 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1925 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1926 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001927 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001928 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1929 unsigned len = getSizeInBits(VT);
1930 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001931 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001932 Tmp2 = DAG.getConstant(mask[i], VT);
1933 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001934 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001935 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1936 DAG.getNode(ISD::AND, VT,
1937 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1938 Tmp2));
1939 }
1940 Result = Tmp1;
1941 break;
1942 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001943 case ISD::CTLZ: {
1944 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001945 x = x | (x >> 1);
1946 x = x | (x >> 2);
1947 ...
1948 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001949 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001950 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001951
Chris Lattner56add052005-05-11 18:35:21 +00001952 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1953 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001954 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1955 unsigned len = getSizeInBits(VT);
1956 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1957 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001958 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001959 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1960 }
1961 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001962 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001963 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001964 }
1965 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001966 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001967 // unless the target has ctlz but not ctpop, in which case we use:
1968 // { return 32 - nlz(~x & (x-1)); }
1969 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001970 MVT::ValueType VT = Tmp1.getValueType();
1971 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001972 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001973 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1974 DAG.getNode(ISD::SUB, VT, Tmp1,
1975 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001976 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001977 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1978 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001979 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001980 DAG.getConstant(getSizeInBits(VT), VT),
1981 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1982 } else {
1983 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1984 }
Chris Lattner72473242005-05-11 05:27:09 +00001985 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001986 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001987 default:
1988 assert(0 && "Cannot expand this yet!");
1989 break;
1990 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001991 break;
1992 }
1993 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001994
Chris Lattner13fe99c2005-04-02 05:00:07 +00001995 // Unary operators
1996 case ISD::FABS:
1997 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001998 case ISD::FSQRT:
1999 case ISD::FSIN:
2000 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002001 Tmp1 = LegalizeOp(Node->getOperand(0));
2002 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2003 case TargetLowering::Legal:
2004 if (Tmp1 != Node->getOperand(0))
2005 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2006 break;
2007 case TargetLowering::Promote:
2008 case TargetLowering::Custom:
2009 assert(0 && "Cannot promote/custom handle this yet!");
2010 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002011 switch(Node->getOpcode()) {
2012 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002013 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2014 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002015 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002016 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002017 break;
2018 }
2019 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002020 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2021 MVT::ValueType VT = Node->getValueType(0);
2022 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002023 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002024 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2025 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2026 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002027 break;
2028 }
2029 case ISD::FSQRT:
2030 case ISD::FSIN:
2031 case ISD::FCOS: {
2032 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002033 const char *FnName = 0;
2034 switch(Node->getOpcode()) {
2035 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2036 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2037 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2038 default: assert(0 && "Unreachable!");
2039 }
Nate Begeman77558da2005-08-04 21:43:28 +00002040 SDOperand Dummy;
2041 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002042 break;
2043 }
2044 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002045 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002046 }
2047 break;
2048 }
2049 break;
2050
2051 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002052 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002053 case ISD::UINT_TO_FP: {
2054 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2056 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002057 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002058 Node->getOperand(0).getValueType())) {
2059 default: assert(0 && "Unknown operation action!");
2060 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002061 Result = ExpandLegalINT_TO_FP(isSigned,
2062 LegalizeOp(Node->getOperand(0)),
2063 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002064 AddLegalizedOperand(Op, Result);
2065 return Result;
2066 case TargetLowering::Promote:
2067 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2068 Node->getValueType(0),
2069 isSigned);
2070 AddLegalizedOperand(Op, Result);
2071 return Result;
2072 case TargetLowering::Legal:
2073 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002074 case TargetLowering::Custom: {
2075 Tmp1 = LegalizeOp(Node->getOperand(0));
2076 SDOperand Tmp =
2077 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2078 Tmp = TLI.LowerOperation(Tmp, DAG);
2079 if (Tmp.Val) {
2080 AddLegalizedOperand(Op, Tmp);
2081 NeedsAnotherIteration = true;
2082 return Tmp;
2083 } else {
2084 assert(0 && "Target Must Lower this");
2085 }
2086 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002087 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002088
Chris Lattnerdc750592005-01-07 07:47:09 +00002089 Tmp1 = LegalizeOp(Node->getOperand(0));
2090 if (Tmp1 != Node->getOperand(0))
2091 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2092 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002093 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002094 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2095 Node->getValueType(0), Node->getOperand(0));
2096 break;
2097 case Promote:
2098 if (isSigned) {
2099 Result = PromoteOp(Node->getOperand(0));
2100 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2101 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2102 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2103 } else {
2104 Result = PromoteOp(Node->getOperand(0));
2105 Result = DAG.getZeroExtendInReg(Result,
2106 Node->getOperand(0).getValueType());
2107 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002108 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002109 break;
2110 }
2111 break;
2112 }
2113 case ISD::TRUNCATE:
2114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2115 case Legal:
2116 Tmp1 = LegalizeOp(Node->getOperand(0));
2117 if (Tmp1 != Node->getOperand(0))
2118 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2119 break;
2120 case Expand:
2121 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2122
2123 // Since the result is legal, we should just be able to truncate the low
2124 // part of the source.
2125 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2126 break;
2127 case Promote:
2128 Result = PromoteOp(Node->getOperand(0));
2129 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2130 break;
2131 }
2132 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002133
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002134 case ISD::FP_TO_SINT:
2135 case ISD::FP_TO_UINT:
2136 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2137 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002138 Tmp1 = LegalizeOp(Node->getOperand(0));
2139
Chris Lattner44fe26f2005-07-29 00:11:56 +00002140 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2141 default: assert(0 && "Unknown operation action!");
2142 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002143 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2144 SDOperand True, False;
2145 MVT::ValueType VT = Node->getOperand(0).getValueType();
2146 MVT::ValueType NVT = Node->getValueType(0);
2147 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2148 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2149 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2150 Node->getOperand(0), Tmp2, ISD::SETLT);
2151 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2152 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002153 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002154 Tmp2));
2155 False = DAG.getNode(ISD::XOR, NVT, False,
2156 DAG.getConstant(1ULL << ShiftAmt, NVT));
2157 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002158 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002159 } else {
2160 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2161 }
2162 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002163 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002164 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002165 Node->getOpcode() == ISD::FP_TO_SINT);
2166 AddLegalizedOperand(Op, Result);
2167 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002168 case TargetLowering::Custom: {
2169 SDOperand Tmp =
2170 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2171 Tmp = TLI.LowerOperation(Tmp, DAG);
2172 if (Tmp.Val) {
2173 AddLegalizedOperand(Op, Tmp);
2174 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002175 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002176 } else {
2177 // The target thinks this is legal afterall.
2178 break;
2179 }
2180 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002181 case TargetLowering::Legal:
2182 break;
2183 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002184
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002185 if (Tmp1 != Node->getOperand(0))
2186 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2187 break;
2188 case Expand:
2189 assert(0 && "Shouldn't need to expand other operators here!");
2190 case Promote:
2191 Result = PromoteOp(Node->getOperand(0));
2192 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2193 break;
2194 }
2195 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002196
Chris Lattner7753f172005-09-02 00:18:10 +00002197 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002198 case ISD::ZERO_EXTEND:
2199 case ISD::SIGN_EXTEND:
2200 case ISD::FP_EXTEND:
2201 case ISD::FP_ROUND:
2202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2203 case Legal:
2204 Tmp1 = LegalizeOp(Node->getOperand(0));
2205 if (Tmp1 != Node->getOperand(0))
2206 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2207 break;
2208 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002209 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002210
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002211 case Promote:
2212 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002213 case ISD::ANY_EXTEND:
2214 Result = PromoteOp(Node->getOperand(0));
2215 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2216 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002217 case ISD::ZERO_EXTEND:
2218 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002219 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002220 Result = DAG.getZeroExtendInReg(Result,
2221 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002222 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002223 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002224 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002225 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002226 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002227 Result,
2228 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002229 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002230 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002231 Result = PromoteOp(Node->getOperand(0));
2232 if (Result.getValueType() != Op.getValueType())
2233 // Dynamically dead while we have only 2 FP types.
2234 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2235 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002236 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002237 Result = PromoteOp(Node->getOperand(0));
2238 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2239 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002240 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002241 }
2242 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002243 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002244 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002245 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002246 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002247
2248 // If this operation is not supported, convert it to a shl/shr or load/store
2249 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002250 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2251 default: assert(0 && "This action not supported for this op yet!");
2252 case TargetLowering::Legal:
2253 if (Tmp1 != Node->getOperand(0))
2254 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002255 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002256 break;
2257 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002258 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002259 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002260 // NOTE: we could fall back on load/store here too for targets without
2261 // SAR. However, it is doubtful that any exist.
2262 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2263 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002264 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002265 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2266 Node->getOperand(0), ShiftCst);
2267 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2268 Result, ShiftCst);
2269 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2270 // The only way we can lower this is to turn it into a STORETRUNC,
2271 // EXTLOAD pair, targetting a temporary location (a stack slot).
2272
2273 // NOTE: there is a choice here between constantly creating new stack
2274 // slots and always reusing the same one. We currently always create
2275 // new ones, as reuse may inhibit scheduling.
2276 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2277 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2278 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2279 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002280 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002281 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2282 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2283 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002284 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002285 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002286 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2287 Result, StackSlot, DAG.getSrcValue(NULL),
2288 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002289 } else {
2290 assert(0 && "Unknown op");
2291 }
2292 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002293 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002294 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002295 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002296 }
Chris Lattner99222f72005-01-15 07:15:18 +00002297 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002298
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002299 // Note that LegalizeOp may be reentered even from single-use nodes, which
2300 // means that we always must cache transformed nodes.
2301 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002302 return Result;
2303}
2304
Chris Lattner4d978642005-01-15 22:16:26 +00002305/// PromoteOp - Given an operation that produces a value in an invalid type,
2306/// promote it to compute the value into a larger type. The produced value will
2307/// have the correct bits for the low portion of the register, but no guarantee
2308/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002309SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2310 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002311 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002312 assert(getTypeAction(VT) == Promote &&
2313 "Caller should expand or legalize operands that are not promotable!");
2314 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2315 "Cannot promote to smaller type!");
2316
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002317 SDOperand Tmp1, Tmp2, Tmp3;
2318
2319 SDOperand Result;
2320 SDNode *Node = Op.Val;
2321
Chris Lattner1a570f12005-09-02 20:32:45 +00002322 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2323 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002324
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002325 // Promotion needs an optimization step to clean up after it, and is not
2326 // careful to avoid operations the target does not support. Make sure that
2327 // all generated operations are legalized in the next iteration.
2328 NeedsAnotherIteration = true;
2329
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002330 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002331 case ISD::CopyFromReg:
2332 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002333 default:
2334 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2335 assert(0 && "Do not know how to promote this operator!");
2336 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002337 case ISD::UNDEF:
2338 Result = DAG.getNode(ISD::UNDEF, NVT);
2339 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002340 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002341 if (VT != MVT::i1)
2342 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2343 else
2344 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002345 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2346 break;
2347 case ISD::ConstantFP:
2348 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2349 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2350 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002351
Chris Lattner2cb338d2005-01-18 02:59:52 +00002352 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002353 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002354 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2355 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002356 Result = LegalizeOp(Result);
2357 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002358
2359 case ISD::TRUNCATE:
2360 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2361 case Legal:
2362 Result = LegalizeOp(Node->getOperand(0));
2363 assert(Result.getValueType() >= NVT &&
2364 "This truncation doesn't make sense!");
2365 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2366 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2367 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002368 case Promote:
2369 // The truncation is not required, because we don't guarantee anything
2370 // about high bits anyway.
2371 Result = PromoteOp(Node->getOperand(0));
2372 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002373 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002374 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2375 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002376 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002377 }
2378 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002379 case ISD::SIGN_EXTEND:
2380 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002381 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002382 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2383 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2384 case Legal:
2385 // Input is legal? Just do extend all the way to the larger type.
2386 Result = LegalizeOp(Node->getOperand(0));
2387 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2388 break;
2389 case Promote:
2390 // Promote the reg if it's smaller.
2391 Result = PromoteOp(Node->getOperand(0));
2392 // The high bits are not guaranteed to be anything. Insert an extend.
2393 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002394 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002395 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002396 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002397 Result = DAG.getZeroExtendInReg(Result,
2398 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002399 break;
2400 }
2401 break;
2402
2403 case ISD::FP_EXTEND:
2404 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2405 case ISD::FP_ROUND:
2406 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2407 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2408 case Promote: assert(0 && "Unreachable with 2 FP types!");
2409 case Legal:
2410 // Input is legal? Do an FP_ROUND_INREG.
2411 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002412 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2413 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002414 break;
2415 }
2416 break;
2417
2418 case ISD::SINT_TO_FP:
2419 case ISD::UINT_TO_FP:
2420 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2421 case Legal:
2422 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002423 // No extra round required here.
2424 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002425 break;
2426
2427 case Promote:
2428 Result = PromoteOp(Node->getOperand(0));
2429 if (Node->getOpcode() == ISD::SINT_TO_FP)
2430 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002431 Result,
2432 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002433 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002434 Result = DAG.getZeroExtendInReg(Result,
2435 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002436 // No extra round required here.
2437 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002438 break;
2439 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002440 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2441 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002442 // Round if we cannot tolerate excess precision.
2443 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002444 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2445 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002446 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002447 }
Chris Lattner4d978642005-01-15 22:16:26 +00002448 break;
2449
Chris Lattner268d4572005-12-09 17:32:47 +00002450 case ISD::SIGN_EXTEND_INREG:
2451 Result = PromoteOp(Node->getOperand(0));
2452 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2453 Node->getOperand(1));
2454 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002455 case ISD::FP_TO_SINT:
2456 case ISD::FP_TO_UINT:
2457 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2458 case Legal:
2459 Tmp1 = LegalizeOp(Node->getOperand(0));
2460 break;
2461 case Promote:
2462 // The input result is prerounded, so we don't have to do anything
2463 // special.
2464 Tmp1 = PromoteOp(Node->getOperand(0));
2465 break;
2466 case Expand:
2467 assert(0 && "not implemented");
2468 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002469 // If we're promoting a UINT to a larger size, check to see if the new node
2470 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2471 // we can use that instead. This allows us to generate better code for
2472 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2473 // legal, such as PowerPC.
2474 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002475 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002476 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2477 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002478 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2479 } else {
2480 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2481 }
Chris Lattner4d978642005-01-15 22:16:26 +00002482 break;
2483
Chris Lattner13fe99c2005-04-02 05:00:07 +00002484 case ISD::FABS:
2485 case ISD::FNEG:
2486 Tmp1 = PromoteOp(Node->getOperand(0));
2487 assert(Tmp1.getValueType() == NVT);
2488 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2489 // NOTE: we do not have to do any extra rounding here for
2490 // NoExcessFPPrecision, because we know the input will have the appropriate
2491 // precision, and these operations don't modify precision at all.
2492 break;
2493
Chris Lattner9d6fa982005-04-28 21:44:33 +00002494 case ISD::FSQRT:
2495 case ISD::FSIN:
2496 case ISD::FCOS:
2497 Tmp1 = PromoteOp(Node->getOperand(0));
2498 assert(Tmp1.getValueType() == NVT);
2499 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2500 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002501 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2502 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002503 break;
2504
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002505 case ISD::AND:
2506 case ISD::OR:
2507 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002508 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002509 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002510 case ISD::MUL:
2511 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002512 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002513 // that too is okay if they are integer operations.
2514 Tmp1 = PromoteOp(Node->getOperand(0));
2515 Tmp2 = PromoteOp(Node->getOperand(1));
2516 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2517 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002518 break;
2519 case ISD::FADD:
2520 case ISD::FSUB:
2521 case ISD::FMUL:
2522 // The input may have strange things in the top bits of the registers, but
2523 // these operations don't care.
2524 Tmp1 = PromoteOp(Node->getOperand(0));
2525 Tmp2 = PromoteOp(Node->getOperand(1));
2526 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2527 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2528
2529 // Floating point operations will give excess precision that we may not be
2530 // able to tolerate. If we DO allow excess precision, just leave it,
2531 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002532 // FIXME: Why would we need to round FP ops more than integer ones?
2533 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002534 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002535 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2536 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002537 break;
2538
Chris Lattner4d978642005-01-15 22:16:26 +00002539 case ISD::SDIV:
2540 case ISD::SREM:
2541 // These operators require that their input be sign extended.
2542 Tmp1 = PromoteOp(Node->getOperand(0));
2543 Tmp2 = PromoteOp(Node->getOperand(1));
2544 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002545 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2546 DAG.getValueType(VT));
2547 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2548 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002549 }
2550 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2551
2552 // Perform FP_ROUND: this is probably overly pessimistic.
2553 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002554 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2555 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002556 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002557 case ISD::FDIV:
2558 case ISD::FREM:
2559 // These operators require that their input be fp extended.
2560 Tmp1 = PromoteOp(Node->getOperand(0));
2561 Tmp2 = PromoteOp(Node->getOperand(1));
2562 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2563
2564 // Perform FP_ROUND: this is probably overly pessimistic.
2565 if (NoExcessFPPrecision)
2566 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2567 DAG.getValueType(VT));
2568 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002569
2570 case ISD::UDIV:
2571 case ISD::UREM:
2572 // These operators require that their input be zero extended.
2573 Tmp1 = PromoteOp(Node->getOperand(0));
2574 Tmp2 = PromoteOp(Node->getOperand(1));
2575 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002576 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2577 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002578 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2579 break;
2580
2581 case ISD::SHL:
2582 Tmp1 = PromoteOp(Node->getOperand(0));
2583 Tmp2 = LegalizeOp(Node->getOperand(1));
2584 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2585 break;
2586 case ISD::SRA:
2587 // The input value must be properly sign extended.
2588 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002589 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2590 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002591 Tmp2 = LegalizeOp(Node->getOperand(1));
2592 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2593 break;
2594 case ISD::SRL:
2595 // The input value must be properly zero extended.
2596 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002597 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002598 Tmp2 = LegalizeOp(Node->getOperand(1));
2599 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2600 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002601 case ISD::LOAD:
2602 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2603 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002604 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2605 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002606 // Remember that we legalized the chain.
2607 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2608 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002609 case ISD::SEXTLOAD:
2610 case ISD::ZEXTLOAD:
2611 case ISD::EXTLOAD:
2612 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2613 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002614 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2615 Node->getOperand(2),
2616 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002617 // Remember that we legalized the chain.
2618 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2619 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002620 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002621 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2622 case Expand: assert(0 && "It's impossible to expand bools");
2623 case Legal:
2624 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2625 break;
2626 case Promote:
2627 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2628 break;
2629 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002630 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2631 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2632 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2633 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002634 case ISD::SELECT_CC:
2635 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2636 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2637 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2638 Node->getOperand(1), Tmp2, Tmp3,
2639 Node->getOperand(4));
2640 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002641 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002642 case ISD::CALL: {
2643 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2644 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2645
Chris Lattner3d95c142005-01-19 20:24:35 +00002646 std::vector<SDOperand> Ops;
2647 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2648 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2649
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002650 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2651 "Can only promote single result calls");
2652 std::vector<MVT::ValueType> RetTyVTs;
2653 RetTyVTs.reserve(2);
2654 RetTyVTs.push_back(NVT);
2655 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002656 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2657 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002658 Result = SDOperand(NC, 0);
2659
2660 // Insert the new chain mapping.
2661 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2662 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002663 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002664 case ISD::CTPOP:
2665 case ISD::CTTZ:
2666 case ISD::CTLZ:
2667 Tmp1 = Node->getOperand(0);
2668 //Zero extend the argument
2669 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2670 // Perform the larger operation, then subtract if needed.
2671 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2672 switch(Node->getOpcode())
2673 {
2674 case ISD::CTPOP:
2675 Result = Tmp1;
2676 break;
2677 case ISD::CTTZ:
2678 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002679 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002680 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002681 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002682 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2683 break;
2684 case ISD::CTLZ:
2685 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002686 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2687 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002688 getSizeInBits(VT), NVT));
2689 break;
2690 }
2691 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002692 }
2693
2694 assert(Result.Val && "Didn't set a result!");
2695 AddPromotedOperand(Op, Result);
2696 return Result;
2697}
Chris Lattnerdc750592005-01-07 07:47:09 +00002698
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002699/// ExpandAddSub - Find a clever way to expand this add operation into
2700/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002701void SelectionDAGLegalize::
2702ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2703 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002704 // Expand the subcomponents.
2705 SDOperand LHSL, LHSH, RHSL, RHSH;
2706 ExpandOp(LHS, LHSL, LHSH);
2707 ExpandOp(RHS, RHSL, RHSH);
2708
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002709 std::vector<SDOperand> Ops;
2710 Ops.push_back(LHSL);
2711 Ops.push_back(LHSH);
2712 Ops.push_back(RHSL);
2713 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002714 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2715 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002716 Hi = Lo.getValue(1);
2717}
2718
Chris Lattner4157c412005-04-02 04:00:59 +00002719void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2720 SDOperand Op, SDOperand Amt,
2721 SDOperand &Lo, SDOperand &Hi) {
2722 // Expand the subcomponents.
2723 SDOperand LHSL, LHSH;
2724 ExpandOp(Op, LHSL, LHSH);
2725
2726 std::vector<SDOperand> Ops;
2727 Ops.push_back(LHSL);
2728 Ops.push_back(LHSH);
2729 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002730 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002731 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002732 Hi = Lo.getValue(1);
2733}
2734
2735
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002736/// ExpandShift - Try to find a clever way to expand this shift operation out to
2737/// smaller elements. If we can't find a way that is more efficient than a
2738/// libcall on this target, return false. Otherwise, return true with the
2739/// low-parts expanded into Lo and Hi.
2740bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2741 SDOperand &Lo, SDOperand &Hi) {
2742 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2743 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002744
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002745 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002746 SDOperand ShAmt = LegalizeOp(Amt);
2747 MVT::ValueType ShTy = ShAmt.getValueType();
2748 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2749 unsigned NVTBits = MVT::getSizeInBits(NVT);
2750
2751 // Handle the case when Amt is an immediate. Other cases are currently broken
2752 // and are disabled.
2753 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2754 unsigned Cst = CN->getValue();
2755 // Expand the incoming operand to be shifted, so that we have its parts
2756 SDOperand InL, InH;
2757 ExpandOp(Op, InL, InH);
2758 switch(Opc) {
2759 case ISD::SHL:
2760 if (Cst > VTBits) {
2761 Lo = DAG.getConstant(0, NVT);
2762 Hi = DAG.getConstant(0, NVT);
2763 } else if (Cst > NVTBits) {
2764 Lo = DAG.getConstant(0, NVT);
2765 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002766 } else if (Cst == NVTBits) {
2767 Lo = DAG.getConstant(0, NVT);
2768 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002769 } else {
2770 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2771 Hi = DAG.getNode(ISD::OR, NVT,
2772 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2773 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2774 }
2775 return true;
2776 case ISD::SRL:
2777 if (Cst > VTBits) {
2778 Lo = DAG.getConstant(0, NVT);
2779 Hi = DAG.getConstant(0, NVT);
2780 } else if (Cst > NVTBits) {
2781 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2782 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002783 } else if (Cst == NVTBits) {
2784 Lo = InH;
2785 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002786 } else {
2787 Lo = DAG.getNode(ISD::OR, NVT,
2788 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2789 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2790 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2791 }
2792 return true;
2793 case ISD::SRA:
2794 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002795 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002796 DAG.getConstant(NVTBits-1, ShTy));
2797 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002798 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002799 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002800 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002801 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002802 } else if (Cst == NVTBits) {
2803 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002804 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002805 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002806 } else {
2807 Lo = DAG.getNode(ISD::OR, NVT,
2808 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2809 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2810 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2811 }
2812 return true;
2813 }
2814 }
2815 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2816 // so disable it for now. Currently targets are handling this via SHL_PARTS
2817 // and friends.
2818 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002819
2820 // If we have an efficient select operation (or if the selects will all fold
2821 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002822 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002823 return false;
2824
2825 SDOperand InL, InH;
2826 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002827 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2828 DAG.getConstant(NVTBits, ShTy), ShAmt);
2829
Chris Lattner4d25c042005-01-20 20:29:23 +00002830 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002831 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2832 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002833
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002834 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2835 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2836 DAG.getConstant(NVTBits-1, ShTy));
2837 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2838 DAG.getConstant(NVTBits-1, ShTy));
2839 }
2840
2841 if (Opc == ISD::SHL) {
2842 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2843 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2844 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002845 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002846
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002847 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2848 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2849 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002850 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002851 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2852 DAG.getConstant(32, ShTy),
2853 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002854 DAG.getConstant(0, NVT),
2855 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002856 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002857 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002858 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002859 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002860
2861 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002862 if (Opc == ISD::SRA)
2863 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2864 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002865 else
2866 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002867 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002868 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002869 }
2870 return true;
2871}
Chris Lattneraac464e2005-01-21 06:05:23 +00002872
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002873/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2874/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002875/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002876static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002877 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002878
Chris Lattner2dce7032005-05-12 23:24:06 +00002879 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002880 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002881 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002882 Found = Node;
2883 return;
2884 }
2885
2886 // Otherwise, scan the operands of Node to see if any of them is a call.
2887 assert(Node->getNumOperands() != 0 &&
2888 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002889 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002890 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002891
2892 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002893 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002894 Found);
2895}
2896
2897
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002898/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2899/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002900/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002901static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2902 std::set<SDNode*> &Visited) {
2903 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2904 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002905
Chris Lattner2dce7032005-05-12 23:24:06 +00002906 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002907 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002908 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002909 Found = Node;
2910 return;
2911 }
2912
2913 // Otherwise, scan the operands of Node to see if any of them is a call.
2914 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2915 if (UI == E) return;
2916 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002917 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002918
2919 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002920 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002921}
2922
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002923/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002924/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002925static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002926 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002927 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002928 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002929 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002930
Chris Lattner4add7e32005-01-23 04:42:50 +00002931 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002932 if (TheChain.getValueType() != MVT::Other)
2933 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002934 if (TheChain.getValueType() != MVT::Other)
2935 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002936
2937 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002938 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002939
Chris Lattner4add7e32005-01-23 04:42:50 +00002940 // Make sure to only follow users of our token chain.
2941 SDNode *User = *UI;
2942 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2943 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002944 if (SDNode *Result = FindCallSeqEnd(User))
2945 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002946 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002947 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002948}
2949
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002950/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002951/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002952static SDNode *FindCallSeqStart(SDNode *Node) {
2953 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002954 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002955
2956 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2957 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002958 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002959}
2960
2961
Chris Lattner4add7e32005-01-23 04:42:50 +00002962/// FindInputOutputChains - If we are replacing an operation with a call we need
2963/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002964/// properly serialize the calls in the block. The returned operand is the
2965/// input chain value for the new call (e.g. the entry node or the previous
2966/// call), and OutChain is set to be the chain node to update to point to the
2967/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002968static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2969 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002970 SDNode *LatestCallSeqStart = Entry.Val;
2971 SDNode *LatestCallSeqEnd = 0;
2972 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2973 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002974
Chris Lattner2dce7032005-05-12 23:24:06 +00002975 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002976 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002977 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2978 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002979 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002980 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002981 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2982 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002983 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002984 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002985 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002986
Chris Lattner06bbeb62005-05-11 19:02:11 +00002987 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002988 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002989 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002990 std::set<SDNode*> Visited;
2991 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002992
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002993 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002994 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002995 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002996
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002997 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002998}
2999
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003000/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003001void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3002 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003003 // Nothing to splice it into?
3004 if (OutChain == 0) return;
3005
3006 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3007 //OutChain->dump();
3008
3009 // Form a token factor node merging the old inval and the new inval.
3010 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3011 OutChain->getOperand(0));
3012 // Change the node to refer to the new token.
3013 OutChain->setAdjCallChain(InToken);
3014}
Chris Lattner4add7e32005-01-23 04:42:50 +00003015
3016
Chris Lattneraac464e2005-01-21 06:05:23 +00003017// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3018// does not fit into a register, return the lo part and set the hi part to the
3019// by-reg argument. If it does fit into a single register, return the result
3020// and leave the Hi part unset.
3021SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3022 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003023 SDNode *OutChain;
3024 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3025 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003026 if (InChain.Val == 0)
3027 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003028
Chris Lattneraac464e2005-01-21 06:05:23 +00003029 TargetLowering::ArgListTy Args;
3030 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3031 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3032 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3033 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3034 }
3035 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003036
Chris Lattner06bbeb62005-05-11 19:02:11 +00003037 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003038 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003039 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003040 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3041 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003042
Chris Lattner63022662005-09-02 20:26:58 +00003043 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003044 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003045 default: assert(0 && "Unknown thing");
3046 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003047 Result = CallInfo.first;
3048 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003049 case Promote:
3050 assert(0 && "Cannot promote this yet!");
3051 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003052 ExpandOp(CallInfo.first, Result, Hi);
3053 CallInfo.second = LegalizeOp(CallInfo.second);
3054 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003055 }
Chris Lattner63022662005-09-02 20:26:58 +00003056
3057 SpliceCallInto(CallInfo.second, OutChain);
3058 NeedsAnotherIteration = true;
3059 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003060}
3061
Chris Lattner4add7e32005-01-23 04:42:50 +00003062
Chris Lattneraac464e2005-01-21 06:05:23 +00003063/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3064/// destination type is legal.
3065SDOperand SelectionDAGLegalize::
3066ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003067 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003068 assert(getTypeAction(Source.getValueType()) == Expand &&
3069 "This is not an expansion!");
3070 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3071
Chris Lattner06bbeb62005-05-11 19:02:11 +00003072 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003073 assert(Source.getValueType() == MVT::i64 &&
3074 "This only works for 64-bit -> FP");
3075 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3076 // incoming integer is set. To handle this, we dynamically test to see if
3077 // it is set, and, if so, add a fudge factor.
3078 SDOperand Lo, Hi;
3079 ExpandOp(Source, Lo, Hi);
3080
Chris Lattner2a4f7312005-05-13 04:45:13 +00003081 // If this is unsigned, and not supported, first perform the conversion to
3082 // signed, then adjust the result if the sign bit is set.
3083 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3084 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3085
Chris Lattnerd47675e2005-08-09 20:20:18 +00003086 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3087 DAG.getConstant(0, Hi.getValueType()),
3088 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003089 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3090 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3091 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003092 uint64_t FF = 0x5f800000ULL;
3093 if (TLI.isLittleEndian()) FF <<= 32;
3094 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003095
Chris Lattnerc30405e2005-08-26 17:15:30 +00003096 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003097 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3098 SDOperand FudgeInReg;
3099 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003100 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3101 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003102 else {
3103 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003104 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3105 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003106 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003107 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003108 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003109
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003110 // Check to see if the target has a custom way to lower this. If so, use it.
3111 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3112 default: assert(0 && "This action not implemented for this operation!");
3113 case TargetLowering::Legal:
3114 case TargetLowering::Expand:
3115 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003116 case TargetLowering::Custom: {
3117 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3118 Source), DAG);
3119 if (NV.Val)
3120 return LegalizeOp(NV);
3121 break; // The target decided this was legal after all
3122 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003123 }
3124
Chris Lattner153587e2005-05-12 07:00:44 +00003125 // Expand the source, then glue it back together for the call. We must expand
3126 // the source in case it is shared (this pass of legalize must traverse it).
3127 SDOperand SrcLo, SrcHi;
3128 ExpandOp(Source, SrcLo, SrcHi);
3129 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3130
Chris Lattner06bbeb62005-05-11 19:02:11 +00003131 SDNode *OutChain = 0;
3132 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3133 DAG.getEntryNode());
3134 const char *FnName = 0;
3135 if (DestTy == MVT::f32)
3136 FnName = "__floatdisf";
3137 else {
3138 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3139 FnName = "__floatdidf";
3140 }
3141
Chris Lattneraac464e2005-01-21 06:05:23 +00003142 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3143
3144 TargetLowering::ArgListTy Args;
3145 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003146
Chris Lattneraac464e2005-01-21 06:05:23 +00003147 Args.push_back(std::make_pair(Source, ArgTy));
3148
3149 // We don't care about token chains for libcalls. We just use the entry
3150 // node as our input and ignore the output chain. This allows us to place
3151 // calls wherever we need them to satisfy data dependences.
3152 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003153
3154 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003155 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3156 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003157
Chris Lattnera5bf1032005-05-12 04:49:08 +00003158 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003159 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003160}
Misha Brukman835702a2005-04-21 22:36:52 +00003161
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003162
3163
Chris Lattnerdc750592005-01-07 07:47:09 +00003164/// ExpandOp - Expand the specified SDOperand into its two component pieces
3165/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3166/// LegalizeNodes map is filled in for any results that are not expanded, the
3167/// ExpandedNodes map is filled in for any results that are expanded, and the
3168/// Lo/Hi values are returned.
3169void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3170 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003171 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003172 SDNode *Node = Op.Val;
3173 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003174 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3175 "Cannot expand FP values!");
3176 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003177 "Cannot expand to FP value or to larger int value!");
3178
Chris Lattner1a570f12005-09-02 20:32:45 +00003179 // See if we already expanded it.
3180 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3181 = ExpandedNodes.find(Op);
3182 if (I != ExpandedNodes.end()) {
3183 Lo = I->second.first;
3184 Hi = I->second.second;
3185 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003186 }
3187
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003188 // Expanding to multiple registers needs to perform an optimization step, and
3189 // is not careful to avoid operations the target does not support. Make sure
3190 // that all generated operations are legalized in the next iteration.
3191 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003192
Chris Lattnerdc750592005-01-07 07:47:09 +00003193 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003194 case ISD::CopyFromReg:
3195 assert(0 && "CopyFromReg must be legal!");
3196 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003197 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3198 assert(0 && "Do not know how to expand this operator!");
3199 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003200 case ISD::UNDEF:
3201 Lo = DAG.getNode(ISD::UNDEF, NVT);
3202 Hi = DAG.getNode(ISD::UNDEF, NVT);
3203 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003204 case ISD::Constant: {
3205 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3206 Lo = DAG.getConstant(Cst, NVT);
3207 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3208 break;
3209 }
Nate Begemanae89d862005-12-07 19:48:11 +00003210 case ISD::ConstantVec: {
3211 unsigned NumElements = Node->getNumOperands();
3212 // If we only have two elements left in the constant vector, just break it
3213 // apart into the two scalar constants it contains. Otherwise, bisect the
3214 // ConstantVec, and return each half as a new ConstantVec.
3215 // FIXME: this is hard coded as big endian, it may have to change to support
3216 // SSE and Alpha MVI
3217 if (NumElements == 2) {
3218 Hi = Node->getOperand(0);
3219 Lo = Node->getOperand(1);
3220 } else {
3221 NumElements /= 2;
3222 std::vector<SDOperand> LoOps, HiOps;
3223 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3224 HiOps.push_back(Node->getOperand(I));
3225 LoOps.push_back(Node->getOperand(I+NumElements));
3226 }
3227 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3228 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3229 }
3230 break;
3231 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003232
Chris Lattner32e08b72005-03-28 22:03:13 +00003233 case ISD::BUILD_PAIR:
3234 // Legalize both operands. FIXME: in the future we should handle the case
3235 // where the two elements are not legal.
3236 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3237 Lo = LegalizeOp(Node->getOperand(0));
3238 Hi = LegalizeOp(Node->getOperand(1));
3239 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003240
3241 case ISD::SIGN_EXTEND_INREG:
3242 ExpandOp(Node->getOperand(0), Lo, Hi);
3243 // Sign extend the lo-part.
3244 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3245 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3246 TLI.getShiftAmountTy()));
3247 // sext_inreg the low part if needed.
3248 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3249 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003250
Chris Lattner55e9cde2005-05-11 04:51:16 +00003251 case ISD::CTPOP:
3252 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003253 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3254 DAG.getNode(ISD::CTPOP, NVT, Lo),
3255 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003256 Hi = DAG.getConstant(0, NVT);
3257 break;
3258
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003259 case ISD::CTLZ: {
3260 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003261 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003262 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3263 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003264 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3265 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003266 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3267 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3268
3269 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3270 Hi = DAG.getConstant(0, NVT);
3271 break;
3272 }
3273
3274 case ISD::CTTZ: {
3275 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003276 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003277 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3278 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003279 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3280 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003281 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3282 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3283
3284 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3285 Hi = DAG.getConstant(0, NVT);
3286 break;
3287 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003288
Chris Lattnerdc750592005-01-07 07:47:09 +00003289 case ISD::LOAD: {
3290 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3291 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003292 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003293
3294 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003295 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003296 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3297 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003298 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003299 //are from the same instruction?
3300 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003301
3302 // Build a factor node to remember that this load is independent of the
3303 // other one.
3304 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3305 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003306
Chris Lattnerdc750592005-01-07 07:47:09 +00003307 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003308 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003309 if (!TLI.isLittleEndian())
3310 std::swap(Lo, Hi);
3311 break;
3312 }
Nate Begemand37c1312005-11-22 18:16:00 +00003313 case ISD::VLOAD: {
3314 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3315 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3316 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3317 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3318
3319 // If we only have two elements, turn into a pair of scalar loads.
3320 // FIXME: handle case where a vector of two elements is fine, such as
3321 // 2 x double on SSE2.
3322 if (NumElements == 2) {
3323 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3324 // Increment the pointer to the other half.
3325 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3326 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3327 getIntPtrConstant(IncrementSize));
3328 //Is this safe? declaring that the two parts of the split load
3329 //are from the same instruction?
3330 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3331 } else {
3332 NumElements /= 2; // Split the vector in half
3333 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3334 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3335 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3336 getIntPtrConstant(IncrementSize));
3337 //Is this safe? declaring that the two parts of the split load
3338 //are from the same instruction?
3339 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3340 }
3341
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));
3346
3347 // Remember that we legalized the chain.
3348 AddLegalizedOperand(Op.getValue(1), TF);
3349 if (!TLI.isLittleEndian())
3350 std::swap(Lo, Hi);
3351 break;
3352 }
3353 case ISD::VADD:
3354 case ISD::VSUB:
3355 case ISD::VMUL: {
3356 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3357 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3358 SDOperand LL, LH, RL, RH;
3359
3360 ExpandOp(Node->getOperand(0), LL, LH);
3361 ExpandOp(Node->getOperand(1), RL, RH);
3362
3363 // If we only have two elements, turn into a pair of scalar loads.
3364 // FIXME: handle case where a vector of two elements is fine, such as
3365 // 2 x double on SSE2.
3366 if (NumElements == 2) {
3367 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3368 Lo = DAG.getNode(Opc, EVT, LL, RL);
3369 Hi = DAG.getNode(Opc, EVT, LH, RH);
3370 } else {
3371 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3372 LL.getOperand(3));
3373 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3374 LH.getOperand(3));
3375 }
3376 break;
3377 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003378 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003379 case ISD::CALL: {
3380 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3381 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3382
Chris Lattner3d95c142005-01-19 20:24:35 +00003383 bool Changed = false;
3384 std::vector<SDOperand> Ops;
3385 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3386 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3387 Changed |= Ops.back() != Node->getOperand(i);
3388 }
3389
Chris Lattnerdc750592005-01-07 07:47:09 +00003390 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3391 "Can only expand a call once so far, not i64 -> i16!");
3392
3393 std::vector<MVT::ValueType> RetTyVTs;
3394 RetTyVTs.reserve(3);
3395 RetTyVTs.push_back(NVT);
3396 RetTyVTs.push_back(NVT);
3397 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003398 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3399 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003400 Lo = SDOperand(NC, 0);
3401 Hi = SDOperand(NC, 1);
3402
3403 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003404 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003405 break;
3406 }
3407 case ISD::AND:
3408 case ISD::OR:
3409 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3410 SDOperand LL, LH, RL, RH;
3411 ExpandOp(Node->getOperand(0), LL, LH);
3412 ExpandOp(Node->getOperand(1), RL, RH);
3413 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3414 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3415 break;
3416 }
3417 case ISD::SELECT: {
3418 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003419
3420 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3421 case Expand: assert(0 && "It's impossible to expand bools");
3422 case Legal:
3423 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3424 break;
3425 case Promote:
3426 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3427 break;
3428 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003429 ExpandOp(Node->getOperand(1), LL, LH);
3430 ExpandOp(Node->getOperand(2), RL, RH);
3431 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3432 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3433 break;
3434 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003435 case ISD::SELECT_CC: {
3436 SDOperand TL, TH, FL, FH;
3437 ExpandOp(Node->getOperand(2), TL, TH);
3438 ExpandOp(Node->getOperand(3), FL, FH);
3439 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3440 Node->getOperand(1), TL, FL, Node->getOperand(4));
3441 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3442 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003443 Lo = LegalizeOp(Lo);
3444 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003445 break;
3446 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003447 case ISD::SEXTLOAD: {
3448 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3449 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3450 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3451
3452 if (EVT == NVT)
3453 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3454 else
3455 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3456 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003457
3458 // Remember that we legalized the chain.
3459 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3460
Nate Begemanc3a89c52005-10-13 17:15:37 +00003461 // The high part is obtained by SRA'ing all but one of the bits of the lo
3462 // part.
3463 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3464 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3465 TLI.getShiftAmountTy()));
3466 Lo = LegalizeOp(Lo);
3467 Hi = LegalizeOp(Hi);
3468 break;
3469 }
3470 case ISD::ZEXTLOAD: {
3471 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3472 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3473 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3474
3475 if (EVT == NVT)
3476 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3477 else
3478 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3479 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003480
3481 // Remember that we legalized the chain.
3482 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3483
Nate Begemanc3a89c52005-10-13 17:15:37 +00003484 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003485 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003486 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003487 break;
3488 }
3489 case ISD::EXTLOAD: {
3490 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3491 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3492 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3493
3494 if (EVT == NVT)
3495 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3496 else
3497 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3498 EVT);
3499
3500 // Remember that we legalized the chain.
3501 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3502
3503 // The high part is undefined.
3504 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3505 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003506 break;
3507 }
Chris Lattner7753f172005-09-02 00:18:10 +00003508 case ISD::ANY_EXTEND: {
3509 SDOperand In;
3510 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3511 case Expand: assert(0 && "expand-expand not implemented yet!");
3512 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3513 case Promote:
3514 In = PromoteOp(Node->getOperand(0));
3515 break;
3516 }
3517
3518 // The low part is any extension of the input (which degenerates to a copy).
3519 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3520 // The high part is undefined.
3521 Hi = DAG.getNode(ISD::UNDEF, NVT);
3522 break;
3523 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003524 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003525 SDOperand In;
3526 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3527 case Expand: assert(0 && "expand-expand not implemented yet!");
3528 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3529 case Promote:
3530 In = PromoteOp(Node->getOperand(0));
3531 // Emit the appropriate sign_extend_inreg to get the value we want.
3532 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003533 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003534 break;
3535 }
3536
Chris Lattnerdc750592005-01-07 07:47:09 +00003537 // The low part is just a sign extension of the input (which degenerates to
3538 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003539 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003540
Chris Lattnerdc750592005-01-07 07:47:09 +00003541 // The high part is obtained by SRA'ing all but one of the bits of the lo
3542 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003543 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003544 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3545 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003546 break;
3547 }
Chris Lattner47844892005-04-03 23:41:52 +00003548 case ISD::ZERO_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 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003556 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003557 break;
3558 }
3559
Chris Lattnerdc750592005-01-07 07:47:09 +00003560 // The low part is just a zero extension of the input (which degenerates to
3561 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003562 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003563
Chris Lattnerdc750592005-01-07 07:47:09 +00003564 // The high part is just a zero.
3565 Hi = DAG.getConstant(0, NVT);
3566 break;
Chris Lattner47844892005-04-03 23:41:52 +00003567 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003568
Chris Lattner44c28c22005-11-20 22:56:56 +00003569 case ISD::READCYCLECOUNTER: {
3570 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3571 TargetLowering::Custom &&
3572 "Must custom expand ReadCycleCounter");
3573 SDOperand T = TLI.LowerOperation(Op, DAG);
3574 assert(T.Val && "Node must be custom expanded!");
3575 Lo = LegalizeOp(T.getValue(0));
3576 Hi = LegalizeOp(T.getValue(1));
3577 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3578 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003579 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003580 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003581
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003582 // These operators cannot be expanded directly, emit them as calls to
3583 // library functions.
3584 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003585 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003586 SDOperand Op;
3587 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3588 case Expand: assert(0 && "cannot expand FP!");
3589 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3590 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3591 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003592
Chris Lattner941d84a2005-07-30 01:40:57 +00003593 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3594
Chris Lattnerfe68d752005-07-29 00:33:32 +00003595 // Now that the custom expander is done, expand the result, which is still
3596 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003597 if (Op.Val) {
3598 ExpandOp(Op, Lo, Hi);
3599 break;
3600 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003601 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003602
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003603 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003604 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003605 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003606 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003607 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003608
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003609 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003610 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3611 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3612 LegalizeOp(Node->getOperand(0)));
3613 // Now that the custom expander is done, expand the result, which is still
3614 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003615 Op = TLI.LowerOperation(Op, DAG);
3616 if (Op.Val) {
3617 ExpandOp(Op, Lo, Hi);
3618 break;
3619 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003620 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003621
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003622 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003623 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003624 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003625 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003626 break;
3627
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003628 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003629 // If the target wants custom lowering, do so.
3630 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3631 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3632 LegalizeOp(Node->getOperand(1)));
3633 Op = TLI.LowerOperation(Op, DAG);
3634 if (Op.Val) {
3635 // Now that the custom expander is done, expand the result, which is
3636 // still VT.
3637 ExpandOp(Op, Lo, Hi);
3638 break;
3639 }
3640 }
3641
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003642 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003643 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003644 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003645
3646 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003647 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003648 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3649 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003650 break;
3651 }
3652
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003653 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003654 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003655 break;
3656
3657 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003658 // If the target wants custom lowering, do so.
3659 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3660 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3661 LegalizeOp(Node->getOperand(1)));
3662 Op = TLI.LowerOperation(Op, DAG);
3663 if (Op.Val) {
3664 // Now that the custom expander is done, expand the result, which is
3665 // still VT.
3666 ExpandOp(Op, Lo, Hi);
3667 break;
3668 }
3669 }
3670
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003671 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003672 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003673 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003674
3675 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003676 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003677 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3678 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003679 break;
3680 }
3681
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003682 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003683 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003684 break;
3685 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003686 // If the target wants custom lowering, do so.
3687 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3688 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3689 LegalizeOp(Node->getOperand(1)));
3690 Op = TLI.LowerOperation(Op, DAG);
3691 if (Op.Val) {
3692 // Now that the custom expander is done, expand the result, which is
3693 // still VT.
3694 ExpandOp(Op, Lo, Hi);
3695 break;
3696 }
3697 }
3698
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003699 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003700 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003701 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003702
3703 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003704 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003705 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3706 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003707 break;
3708 }
3709
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003710 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003711 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003712 break;
3713
Misha Brukman835702a2005-04-21 22:36:52 +00003714 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003715 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3716 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003717 break;
3718 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003719 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3720 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003721 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003722 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003723 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003724 SDOperand LL, LH, RL, RH;
3725 ExpandOp(Node->getOperand(0), LL, LH);
3726 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003727 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3728 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3729 // extended the sign bit of the low half through the upper half, and if so
3730 // emit a MULHS instead of the alternate sequence that is valid for any
3731 // i64 x i64 multiply.
3732 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3733 // is RH an extension of the sign bit of RL?
3734 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3735 RH.getOperand(1).getOpcode() == ISD::Constant &&
3736 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3737 // is LH an extension of the sign bit of LL?
3738 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3739 LH.getOperand(1).getOpcode() == ISD::Constant &&
3740 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3741 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3742 } else {
3743 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3744 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3745 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3746 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3747 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3748 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003749 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3750 } else {
3751 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3752 }
3753 break;
3754 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003755 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3756 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3757 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3758 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003759 }
3760
3761 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003762 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3763 std::make_pair(Lo, Hi))).second;
3764 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003765}
3766
3767
3768// SelectionDAG::Legalize - This is the entry point for the file.
3769//
Chris Lattner4add7e32005-01-23 04:42:50 +00003770void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003771 /// run - This is the main entry point to this class.
3772 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003773 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003774}
3775