blob: c5b7bf9f5413ad30f3dc0c7adbae634b493de1af [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;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001328 case TargetLowering::Legal:
1329 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1330 Tmp3 != Node->getOperand(2))
1331 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1332 Tmp1, Tmp2, Tmp3);
1333 break;
1334 case TargetLowering::Promote: {
1335 MVT::ValueType NVT =
1336 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1337 unsigned ExtOp, TruncOp;
1338 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001339 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001340 TruncOp = ISD::TRUNCATE;
1341 } else {
1342 ExtOp = ISD::FP_EXTEND;
1343 TruncOp = ISD::FP_ROUND;
1344 }
1345 // Promote each of the values to the new type.
1346 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1347 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1348 // Perform the larger operation, then round down.
1349 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1350 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1351 break;
1352 }
1353 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001354 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001355 case ISD::SELECT_CC:
1356 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1357 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1358
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001359 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001360 // Everything is legal, see if we should expand this op or something.
1361 switch (TLI.getOperationAction(ISD::SELECT_CC,
1362 Node->getOperand(0).getValueType())) {
1363 default: assert(0 && "This action is not supported yet!");
1364 case TargetLowering::Custom: {
1365 SDOperand Tmp =
1366 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1367 Node->getOperand(0),
1368 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001369 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001370 if (Tmp.Val) {
1371 Result = LegalizeOp(Tmp);
1372 break;
1373 }
1374 } // FALLTHROUGH if the target can't lower this operation after all.
1375 case TargetLowering::Legal:
1376 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1377 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1378 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1379 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1380 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1381 Tmp3, Tmp4, Node->getOperand(4));
1382 }
1383 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001384 }
1385 break;
1386 } else {
1387 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1388 Node->getOperand(0), // LHS
1389 Node->getOperand(1), // RHS
1390 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001391 // If we get a SETCC back from legalizing the SETCC node we just
1392 // created, then use its LHS, RHS, and CC directly in creating a new
1393 // node. Otherwise, select between the true and false value based on
1394 // comparing the result of the legalized with zero.
1395 if (Tmp1.getOpcode() == ISD::SETCC) {
1396 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1397 Tmp1.getOperand(0), Tmp1.getOperand(1),
1398 Tmp3, Tmp4, Tmp1.getOperand(2));
1399 } else {
1400 Result = DAG.getSelectCC(Tmp1,
1401 DAG.getConstant(0, Tmp1.getValueType()),
1402 Tmp3, Tmp4, ISD::SETNE);
1403 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001404 }
1405 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001406 case ISD::SETCC:
1407 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1408 case Legal:
1409 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1410 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001411 break;
1412 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001413 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1414 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1415
1416 // If this is an FP compare, the operands have already been extended.
1417 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1418 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001419 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001420
1421 // Otherwise, we have to insert explicit sign or zero extends. Note
1422 // that we could insert sign extends for ALL conditions, but zero extend
1423 // is cheaper on many machines (an AND instead of two shifts), so prefer
1424 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001425 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001426 default: assert(0 && "Unknown integer comparison!");
1427 case ISD::SETEQ:
1428 case ISD::SETNE:
1429 case ISD::SETUGE:
1430 case ISD::SETUGT:
1431 case ISD::SETULE:
1432 case ISD::SETULT:
1433 // ALL of these operations will work if we either sign or zero extend
1434 // the operands (including the unsigned comparisons!). Zero extend is
1435 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001436 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1437 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001438 break;
1439 case ISD::SETGE:
1440 case ISD::SETGT:
1441 case ISD::SETLT:
1442 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001443 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1444 DAG.getValueType(VT));
1445 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1446 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001447 break;
1448 }
Chris Lattner4d978642005-01-15 22:16:26 +00001449 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001450 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001451 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001452 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1453 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1454 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001455 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001456 case ISD::SETEQ:
1457 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001458 if (RHSLo == RHSHi)
1459 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1460 if (RHSCST->isAllOnesValue()) {
1461 // Comparison to -1.
1462 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001463 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001464 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001465 }
1466
Chris Lattnerdc750592005-01-07 07:47:09 +00001467 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1468 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1469 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001470 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001471 break;
1472 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001473 // If this is a comparison of the sign bit, just look at the top part.
1474 // X > -1, x < 0
1475 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001476 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001477 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001478 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001479 (CST->isAllOnesValue()))) { // X > -1
1480 Tmp1 = LHSHi;
1481 Tmp2 = RHSHi;
1482 break;
1483 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001484
Chris Lattnerdc750592005-01-07 07:47:09 +00001485 // FIXME: This generated code sucks.
1486 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001487 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001488 default: assert(0 && "Unknown integer setcc!");
1489 case ISD::SETLT:
1490 case ISD::SETULT: LowCC = ISD::SETULT; break;
1491 case ISD::SETGT:
1492 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1493 case ISD::SETLE:
1494 case ISD::SETULE: LowCC = ISD::SETULE; break;
1495 case ISD::SETGE:
1496 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1497 }
Misha Brukman835702a2005-04-21 22:36:52 +00001498
Chris Lattnerdc750592005-01-07 07:47:09 +00001499 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1500 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1501 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1502
1503 // NOTE: on targets without efficient SELECT of bools, we can always use
1504 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001505 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1506 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1507 Node->getOperand(2));
1508 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001509 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1510 Result, Tmp1, Tmp2));
1511 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001512 }
1513 }
Nate Begeman987121a2005-08-23 04:29:48 +00001514
1515 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1516 default:
1517 assert(0 && "Cannot handle this action for SETCC yet!");
1518 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001519 case TargetLowering::Promote: {
1520 // First step, figure out the appropriate operation to use.
1521 // Allow SETCC to not be supported for all legal data types
1522 // Mostly this targets FP
1523 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1524 MVT::ValueType OldVT = NewInTy;
1525
1526 // Scan for the appropriate larger type to use.
1527 while (1) {
1528 NewInTy = (MVT::ValueType)(NewInTy+1);
1529
1530 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1531 "Fell off of the edge of the integer world");
1532 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1533 "Fell off of the edge of the floating point world");
1534
1535 // If the target supports SETCC of this type, use it.
1536 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1537 break;
1538 }
1539 if (MVT::isInteger(NewInTy))
1540 assert(0 && "Cannot promote Legal Integer SETCC yet");
1541 else {
1542 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1543 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1544 }
1545
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001546 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1547 Node->getOperand(2));
1548 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001549 }
Nate Begeman987121a2005-08-23 04:29:48 +00001550 case TargetLowering::Legal:
1551 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1552 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1553 Node->getOperand(2));
1554 break;
1555 case TargetLowering::Expand:
1556 // Expand a setcc node into a select_cc of the same condition, lhs, and
1557 // rhs that selects between const 1 (true) and const 0 (false).
1558 MVT::ValueType VT = Node->getValueType(0);
1559 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1560 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1561 Node->getOperand(2));
1562 Result = LegalizeOp(Result);
1563 break;
1564 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001565 break;
1566
Chris Lattner85d70c62005-01-11 05:57:22 +00001567 case ISD::MEMSET:
1568 case ISD::MEMCPY:
1569 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001570 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001571 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1572
1573 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1574 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1575 case Expand: assert(0 && "Cannot expand a byte!");
1576 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001577 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001578 break;
1579 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001580 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001581 break;
1582 }
1583 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001584 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001585 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001586
1587 SDOperand Tmp4;
1588 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001589 case Expand: {
1590 // Length is too big, just take the lo-part of the length.
1591 SDOperand HiPart;
1592 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1593 break;
1594 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001595 case Legal:
1596 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001597 break;
1598 case Promote:
1599 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001600 break;
1601 }
1602
1603 SDOperand Tmp5;
1604 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1605 case Expand: assert(0 && "Cannot expand this yet!");
1606 case Legal:
1607 Tmp5 = LegalizeOp(Node->getOperand(4));
1608 break;
1609 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001610 Tmp5 = PromoteOp(Node->getOperand(4));
1611 break;
1612 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001613
1614 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1615 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001616 case TargetLowering::Custom: {
1617 SDOperand Tmp =
1618 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1619 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1620 if (Tmp.Val) {
1621 Result = LegalizeOp(Tmp);
1622 break;
1623 }
1624 // FALLTHROUGH if the target thinks it is legal.
1625 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001626 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001627 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1628 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1629 Tmp5 != Node->getOperand(4)) {
1630 std::vector<SDOperand> Ops;
1631 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1632 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1633 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1634 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001635 break;
1636 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001637 // Otherwise, the target does not support this operation. Lower the
1638 // operation to an explicit libcall as appropriate.
1639 MVT::ValueType IntPtr = TLI.getPointerTy();
1640 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1641 std::vector<std::pair<SDOperand, const Type*> > Args;
1642
Reid Spencer6dced922005-01-12 14:53:45 +00001643 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001644 if (Node->getOpcode() == ISD::MEMSET) {
1645 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1646 // Extend the ubyte argument to be an int value for the call.
1647 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1648 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1649 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1650
1651 FnName = "memset";
1652 } else if (Node->getOpcode() == ISD::MEMCPY ||
1653 Node->getOpcode() == ISD::MEMMOVE) {
1654 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1655 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1656 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1657 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1658 } else {
1659 assert(0 && "Unknown op!");
1660 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001661
Chris Lattner85d70c62005-01-11 05:57:22 +00001662 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001663 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001664 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001665 Result = CallResult.second;
1666 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001667 break;
1668 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001669 }
1670 break;
1671 }
Chris Lattner5385db52005-05-09 20:23:03 +00001672
1673 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001674 Tmp1 = LegalizeOp(Node->getOperand(0));
1675 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001676
Chris Lattner86535992005-05-14 07:45:46 +00001677 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1678 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1679 std::vector<SDOperand> Ops;
1680 Ops.push_back(Tmp1);
1681 Ops.push_back(Tmp2);
1682 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1683 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001684 Result = SDOperand(Node, 0);
1685 // Since these produce two values, make sure to remember that we legalized
1686 // both of them.
1687 AddLegalizedOperand(SDOperand(Node, 0), Result);
1688 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1689 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001690 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001691 Tmp1 = LegalizeOp(Node->getOperand(0));
1692 Tmp2 = LegalizeOp(Node->getOperand(1));
1693 Tmp3 = LegalizeOp(Node->getOperand(2));
1694 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1695 Tmp3 != Node->getOperand(2))
1696 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1697 break;
1698
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001699 case ISD::READIO:
1700 Tmp1 = LegalizeOp(Node->getOperand(0));
1701 Tmp2 = LegalizeOp(Node->getOperand(1));
1702
1703 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1704 case TargetLowering::Custom:
1705 default: assert(0 && "This action not implemented for this operation!");
1706 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001707 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1708 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1709 std::vector<SDOperand> Ops;
1710 Ops.push_back(Tmp1);
1711 Ops.push_back(Tmp2);
1712 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1713 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001714 Result = SDOperand(Node, 0);
1715 break;
1716 case TargetLowering::Expand:
1717 // Replace this with a load from memory.
1718 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1719 Node->getOperand(1), DAG.getSrcValue(NULL));
1720 Result = LegalizeOp(Result);
1721 break;
1722 }
1723
1724 // Since these produce two values, make sure to remember that we legalized
1725 // both of them.
1726 AddLegalizedOperand(SDOperand(Node, 0), Result);
1727 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1728 return Result.getValue(Op.ResNo);
1729
1730 case ISD::WRITEIO:
1731 Tmp1 = LegalizeOp(Node->getOperand(0));
1732 Tmp2 = LegalizeOp(Node->getOperand(1));
1733 Tmp3 = LegalizeOp(Node->getOperand(2));
1734
1735 switch (TLI.getOperationAction(Node->getOpcode(),
1736 Node->getOperand(1).getValueType())) {
1737 case TargetLowering::Custom:
1738 default: assert(0 && "This action not implemented for this operation!");
1739 case TargetLowering::Legal:
1740 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1741 Tmp3 != Node->getOperand(2))
1742 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1743 break;
1744 case TargetLowering::Expand:
1745 // Replace this with a store to memory.
1746 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1747 Node->getOperand(1), Node->getOperand(2),
1748 DAG.getSrcValue(NULL));
1749 Result = LegalizeOp(Result);
1750 break;
1751 }
1752 break;
1753
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001754 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001755 case ISD::SUB_PARTS:
1756 case ISD::SHL_PARTS:
1757 case ISD::SRA_PARTS:
1758 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001759 std::vector<SDOperand> Ops;
1760 bool Changed = false;
1761 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1762 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1763 Changed |= Ops.back() != Node->getOperand(i);
1764 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001765 if (Changed) {
1766 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1767 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1768 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001769
1770 // Since these produce multiple values, make sure to remember that we
1771 // legalized all of them.
1772 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1773 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1774 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001775 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001776
1777 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001778 case ISD::ADD:
1779 case ISD::SUB:
1780 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001781 case ISD::MULHS:
1782 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001783 case ISD::UDIV:
1784 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001785 case ISD::AND:
1786 case ISD::OR:
1787 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001788 case ISD::SHL:
1789 case ISD::SRL:
1790 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001791 case ISD::FADD:
1792 case ISD::FSUB:
1793 case ISD::FMUL:
1794 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001795 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001796 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1797 case Expand: assert(0 && "Not possible");
1798 case Legal:
1799 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1800 break;
1801 case Promote:
1802 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1803 break;
1804 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001805 if (Tmp1 != Node->getOperand(0) ||
1806 Tmp2 != Node->getOperand(1))
1807 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1808 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001809
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001810 case ISD::BUILD_PAIR: {
1811 MVT::ValueType PairTy = Node->getValueType(0);
1812 // TODO: handle the case where the Lo and Hi operands are not of legal type
1813 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1814 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1815 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1816 case TargetLowering::Legal:
1817 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1818 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1819 break;
1820 case TargetLowering::Promote:
1821 case TargetLowering::Custom:
1822 assert(0 && "Cannot promote/custom this yet!");
1823 case TargetLowering::Expand:
1824 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1825 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1826 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1827 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1828 TLI.getShiftAmountTy()));
1829 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1830 break;
1831 }
1832 break;
1833 }
1834
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001835 case ISD::UREM:
1836 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001837 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001838 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1839 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1840 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1841 case TargetLowering::Legal:
1842 if (Tmp1 != Node->getOperand(0) ||
1843 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001844 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001845 Tmp2);
1846 break;
1847 case TargetLowering::Promote:
1848 case TargetLowering::Custom:
1849 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001850 case TargetLowering::Expand:
1851 if (MVT::isInteger(Node->getValueType(0))) {
1852 MVT::ValueType VT = Node->getValueType(0);
1853 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1854 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1855 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1856 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1857 } else {
1858 // Floating point mod -> fmod libcall.
1859 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1860 SDOperand Dummy;
1861 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001862 }
1863 break;
1864 }
1865 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001866
Andrew Lenharth5e177822005-05-03 17:19:30 +00001867 case ISD::CTPOP:
1868 case ISD::CTTZ:
1869 case ISD::CTLZ:
1870 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1871 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1872 case TargetLowering::Legal:
1873 if (Tmp1 != Node->getOperand(0))
1874 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1875 break;
1876 case TargetLowering::Promote: {
1877 MVT::ValueType OVT = Tmp1.getValueType();
1878 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001879
1880 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001881 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1882 // Perform the larger operation, then subtract if needed.
1883 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1884 switch(Node->getOpcode())
1885 {
1886 case ISD::CTPOP:
1887 Result = Tmp1;
1888 break;
1889 case ISD::CTTZ:
1890 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001891 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1892 DAG.getConstant(getSizeInBits(NVT), NVT),
1893 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001894 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001895 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1896 break;
1897 case ISD::CTLZ:
1898 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001899 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1900 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001901 getSizeInBits(OVT), NVT));
1902 break;
1903 }
1904 break;
1905 }
1906 case TargetLowering::Custom:
1907 assert(0 && "Cannot custom handle this yet!");
1908 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001909 switch(Node->getOpcode())
1910 {
1911 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001912 static const uint64_t mask[6] = {
1913 0x5555555555555555ULL, 0x3333333333333333ULL,
1914 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1915 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1916 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001917 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001918 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1919 unsigned len = getSizeInBits(VT);
1920 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001921 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001922 Tmp2 = DAG.getConstant(mask[i], VT);
1923 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001924 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001925 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1926 DAG.getNode(ISD::AND, VT,
1927 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1928 Tmp2));
1929 }
1930 Result = Tmp1;
1931 break;
1932 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001933 case ISD::CTLZ: {
1934 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001935 x = x | (x >> 1);
1936 x = x | (x >> 2);
1937 ...
1938 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001939 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001940 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001941
Chris Lattner56add052005-05-11 18:35:21 +00001942 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1943 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001944 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1945 unsigned len = getSizeInBits(VT);
1946 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1947 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001948 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001949 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1950 }
1951 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001952 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001953 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001954 }
1955 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001956 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001957 // unless the target has ctlz but not ctpop, in which case we use:
1958 // { return 32 - nlz(~x & (x-1)); }
1959 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001960 MVT::ValueType VT = Tmp1.getValueType();
1961 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001962 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001963 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1964 DAG.getNode(ISD::SUB, VT, Tmp1,
1965 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001966 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001967 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1968 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001969 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001970 DAG.getConstant(getSizeInBits(VT), VT),
1971 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1972 } else {
1973 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1974 }
Chris Lattner72473242005-05-11 05:27:09 +00001975 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001976 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001977 default:
1978 assert(0 && "Cannot expand this yet!");
1979 break;
1980 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001981 break;
1982 }
1983 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001984
Chris Lattner13fe99c2005-04-02 05:00:07 +00001985 // Unary operators
1986 case ISD::FABS:
1987 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001988 case ISD::FSQRT:
1989 case ISD::FSIN:
1990 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001991 Tmp1 = LegalizeOp(Node->getOperand(0));
1992 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1993 case TargetLowering::Legal:
1994 if (Tmp1 != Node->getOperand(0))
1995 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1996 break;
1997 case TargetLowering::Promote:
1998 case TargetLowering::Custom:
1999 assert(0 && "Cannot promote/custom handle this yet!");
2000 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002001 switch(Node->getOpcode()) {
2002 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002003 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2004 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002005 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002006 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002007 break;
2008 }
2009 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002010 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2011 MVT::ValueType VT = Node->getValueType(0);
2012 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002013 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002014 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2015 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2016 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002017 break;
2018 }
2019 case ISD::FSQRT:
2020 case ISD::FSIN:
2021 case ISD::FCOS: {
2022 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002023 const char *FnName = 0;
2024 switch(Node->getOpcode()) {
2025 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2026 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2027 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2028 default: assert(0 && "Unreachable!");
2029 }
Nate Begeman77558da2005-08-04 21:43:28 +00002030 SDOperand Dummy;
2031 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002032 break;
2033 }
2034 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002035 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002036 }
2037 break;
2038 }
2039 break;
2040
2041 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002042 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002043 case ISD::UINT_TO_FP: {
2044 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002045 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2046 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002047 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002048 Node->getOperand(0).getValueType())) {
2049 default: assert(0 && "Unknown operation action!");
2050 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002051 Result = ExpandLegalINT_TO_FP(isSigned,
2052 LegalizeOp(Node->getOperand(0)),
2053 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002054 AddLegalizedOperand(Op, Result);
2055 return Result;
2056 case TargetLowering::Promote:
2057 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2058 Node->getValueType(0),
2059 isSigned);
2060 AddLegalizedOperand(Op, Result);
2061 return Result;
2062 case TargetLowering::Legal:
2063 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002064 case TargetLowering::Custom: {
2065 Tmp1 = LegalizeOp(Node->getOperand(0));
2066 SDOperand Tmp =
2067 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2068 Tmp = TLI.LowerOperation(Tmp, DAG);
2069 if (Tmp.Val) {
2070 AddLegalizedOperand(Op, Tmp);
2071 NeedsAnotherIteration = true;
2072 return Tmp;
2073 } else {
2074 assert(0 && "Target Must Lower this");
2075 }
2076 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002077 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002078
Chris Lattnerdc750592005-01-07 07:47:09 +00002079 Tmp1 = LegalizeOp(Node->getOperand(0));
2080 if (Tmp1 != Node->getOperand(0))
2081 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2082 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002083 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002084 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2085 Node->getValueType(0), Node->getOperand(0));
2086 break;
2087 case Promote:
2088 if (isSigned) {
2089 Result = PromoteOp(Node->getOperand(0));
2090 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2091 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2092 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2093 } else {
2094 Result = PromoteOp(Node->getOperand(0));
2095 Result = DAG.getZeroExtendInReg(Result,
2096 Node->getOperand(0).getValueType());
2097 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002098 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002099 break;
2100 }
2101 break;
2102 }
2103 case ISD::TRUNCATE:
2104 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2105 case Legal:
2106 Tmp1 = LegalizeOp(Node->getOperand(0));
2107 if (Tmp1 != Node->getOperand(0))
2108 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2109 break;
2110 case Expand:
2111 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2112
2113 // Since the result is legal, we should just be able to truncate the low
2114 // part of the source.
2115 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2116 break;
2117 case Promote:
2118 Result = PromoteOp(Node->getOperand(0));
2119 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2120 break;
2121 }
2122 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002123
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002124 case ISD::FP_TO_SINT:
2125 case ISD::FP_TO_UINT:
2126 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2127 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002128 Tmp1 = LegalizeOp(Node->getOperand(0));
2129
Chris Lattner44fe26f2005-07-29 00:11:56 +00002130 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2131 default: assert(0 && "Unknown operation action!");
2132 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002133 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2134 SDOperand True, False;
2135 MVT::ValueType VT = Node->getOperand(0).getValueType();
2136 MVT::ValueType NVT = Node->getValueType(0);
2137 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2138 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2139 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2140 Node->getOperand(0), Tmp2, ISD::SETLT);
2141 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2142 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002143 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002144 Tmp2));
2145 False = DAG.getNode(ISD::XOR, NVT, False,
2146 DAG.getConstant(1ULL << ShiftAmt, NVT));
2147 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002148 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002149 } else {
2150 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2151 }
2152 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002153 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002154 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002155 Node->getOpcode() == ISD::FP_TO_SINT);
2156 AddLegalizedOperand(Op, Result);
2157 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002158 case TargetLowering::Custom: {
2159 SDOperand Tmp =
2160 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2161 Tmp = TLI.LowerOperation(Tmp, DAG);
2162 if (Tmp.Val) {
2163 AddLegalizedOperand(Op, Tmp);
2164 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002165 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002166 } else {
2167 // The target thinks this is legal afterall.
2168 break;
2169 }
2170 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002171 case TargetLowering::Legal:
2172 break;
2173 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002174
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002175 if (Tmp1 != Node->getOperand(0))
2176 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2177 break;
2178 case Expand:
2179 assert(0 && "Shouldn't need to expand other operators here!");
2180 case Promote:
2181 Result = PromoteOp(Node->getOperand(0));
2182 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2183 break;
2184 }
2185 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002186
Chris Lattner7753f172005-09-02 00:18:10 +00002187 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002188 case ISD::ZERO_EXTEND:
2189 case ISD::SIGN_EXTEND:
2190 case ISD::FP_EXTEND:
2191 case ISD::FP_ROUND:
2192 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2193 case Legal:
2194 Tmp1 = LegalizeOp(Node->getOperand(0));
2195 if (Tmp1 != Node->getOperand(0))
2196 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2197 break;
2198 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002199 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002200
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002201 case Promote:
2202 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002203 case ISD::ANY_EXTEND:
2204 Result = PromoteOp(Node->getOperand(0));
2205 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2206 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002207 case ISD::ZERO_EXTEND:
2208 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002209 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002210 Result = DAG.getZeroExtendInReg(Result,
2211 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002212 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002213 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002214 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002215 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002216 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002217 Result,
2218 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002219 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002220 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002221 Result = PromoteOp(Node->getOperand(0));
2222 if (Result.getValueType() != Op.getValueType())
2223 // Dynamically dead while we have only 2 FP types.
2224 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2225 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002226 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002227 Result = PromoteOp(Node->getOperand(0));
2228 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2229 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002230 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002231 }
2232 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002233 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002234 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002235 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002236 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002237
2238 // If this operation is not supported, convert it to a shl/shr or load/store
2239 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002240 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2241 default: assert(0 && "This action not supported for this op yet!");
2242 case TargetLowering::Legal:
2243 if (Tmp1 != Node->getOperand(0))
2244 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002245 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002246 break;
2247 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002248 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002249 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002250 // NOTE: we could fall back on load/store here too for targets without
2251 // SAR. However, it is doubtful that any exist.
2252 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2253 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002254 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002255 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2256 Node->getOperand(0), ShiftCst);
2257 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2258 Result, ShiftCst);
2259 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2260 // The only way we can lower this is to turn it into a STORETRUNC,
2261 // EXTLOAD pair, targetting a temporary location (a stack slot).
2262
2263 // NOTE: there is a choice here between constantly creating new stack
2264 // slots and always reusing the same one. We currently always create
2265 // new ones, as reuse may inhibit scheduling.
2266 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2267 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2268 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2269 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002270 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002271 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2272 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2273 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002274 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002275 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002276 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2277 Result, StackSlot, DAG.getSrcValue(NULL),
2278 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002279 } else {
2280 assert(0 && "Unknown op");
2281 }
2282 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002283 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002284 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002285 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002286 }
Chris Lattner99222f72005-01-15 07:15:18 +00002287 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002288
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002289 // Note that LegalizeOp may be reentered even from single-use nodes, which
2290 // means that we always must cache transformed nodes.
2291 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002292 return Result;
2293}
2294
Chris Lattner4d978642005-01-15 22:16:26 +00002295/// PromoteOp - Given an operation that produces a value in an invalid type,
2296/// promote it to compute the value into a larger type. The produced value will
2297/// have the correct bits for the low portion of the register, but no guarantee
2298/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002299SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2300 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002301 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002302 assert(getTypeAction(VT) == Promote &&
2303 "Caller should expand or legalize operands that are not promotable!");
2304 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2305 "Cannot promote to smaller type!");
2306
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002307 SDOperand Tmp1, Tmp2, Tmp3;
2308
2309 SDOperand Result;
2310 SDNode *Node = Op.Val;
2311
Chris Lattner1a570f12005-09-02 20:32:45 +00002312 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2313 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002314
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002315 // Promotion needs an optimization step to clean up after it, and is not
2316 // careful to avoid operations the target does not support. Make sure that
2317 // all generated operations are legalized in the next iteration.
2318 NeedsAnotherIteration = true;
2319
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002320 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002321 case ISD::CopyFromReg:
2322 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002323 default:
2324 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2325 assert(0 && "Do not know how to promote this operator!");
2326 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002327 case ISD::UNDEF:
2328 Result = DAG.getNode(ISD::UNDEF, NVT);
2329 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002330 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002331 if (VT != MVT::i1)
2332 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2333 else
2334 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002335 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2336 break;
2337 case ISD::ConstantFP:
2338 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2339 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2340 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002341
Chris Lattner2cb338d2005-01-18 02:59:52 +00002342 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002343 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002344 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2345 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002346 Result = LegalizeOp(Result);
2347 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002348
2349 case ISD::TRUNCATE:
2350 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2351 case Legal:
2352 Result = LegalizeOp(Node->getOperand(0));
2353 assert(Result.getValueType() >= NVT &&
2354 "This truncation doesn't make sense!");
2355 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2356 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2357 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002358 case Promote:
2359 // The truncation is not required, because we don't guarantee anything
2360 // about high bits anyway.
2361 Result = PromoteOp(Node->getOperand(0));
2362 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002363 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002364 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2365 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002366 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002367 }
2368 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002369 case ISD::SIGN_EXTEND:
2370 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002371 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002372 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2373 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2374 case Legal:
2375 // Input is legal? Just do extend all the way to the larger type.
2376 Result = LegalizeOp(Node->getOperand(0));
2377 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2378 break;
2379 case Promote:
2380 // Promote the reg if it's smaller.
2381 Result = PromoteOp(Node->getOperand(0));
2382 // The high bits are not guaranteed to be anything. Insert an extend.
2383 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002384 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002385 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002386 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002387 Result = DAG.getZeroExtendInReg(Result,
2388 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002389 break;
2390 }
2391 break;
2392
2393 case ISD::FP_EXTEND:
2394 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2395 case ISD::FP_ROUND:
2396 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2397 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2398 case Promote: assert(0 && "Unreachable with 2 FP types!");
2399 case Legal:
2400 // Input is legal? Do an FP_ROUND_INREG.
2401 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002402 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2403 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002404 break;
2405 }
2406 break;
2407
2408 case ISD::SINT_TO_FP:
2409 case ISD::UINT_TO_FP:
2410 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2411 case Legal:
2412 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002413 // No extra round required here.
2414 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002415 break;
2416
2417 case Promote:
2418 Result = PromoteOp(Node->getOperand(0));
2419 if (Node->getOpcode() == ISD::SINT_TO_FP)
2420 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002421 Result,
2422 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002423 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002424 Result = DAG.getZeroExtendInReg(Result,
2425 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002426 // No extra round required here.
2427 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002428 break;
2429 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002430 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2431 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002432 // Round if we cannot tolerate excess precision.
2433 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002434 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2435 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002436 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002437 }
Chris Lattner4d978642005-01-15 22:16:26 +00002438 break;
2439
Chris Lattner268d4572005-12-09 17:32:47 +00002440 case ISD::SIGN_EXTEND_INREG:
2441 Result = PromoteOp(Node->getOperand(0));
2442 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2443 Node->getOperand(1));
2444 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002445 case ISD::FP_TO_SINT:
2446 case ISD::FP_TO_UINT:
2447 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2448 case Legal:
2449 Tmp1 = LegalizeOp(Node->getOperand(0));
2450 break;
2451 case Promote:
2452 // The input result is prerounded, so we don't have to do anything
2453 // special.
2454 Tmp1 = PromoteOp(Node->getOperand(0));
2455 break;
2456 case Expand:
2457 assert(0 && "not implemented");
2458 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002459 // If we're promoting a UINT to a larger size, check to see if the new node
2460 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2461 // we can use that instead. This allows us to generate better code for
2462 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2463 // legal, such as PowerPC.
2464 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002465 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002466 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2467 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002468 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2469 } else {
2470 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2471 }
Chris Lattner4d978642005-01-15 22:16:26 +00002472 break;
2473
Chris Lattner13fe99c2005-04-02 05:00:07 +00002474 case ISD::FABS:
2475 case ISD::FNEG:
2476 Tmp1 = PromoteOp(Node->getOperand(0));
2477 assert(Tmp1.getValueType() == NVT);
2478 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2479 // NOTE: we do not have to do any extra rounding here for
2480 // NoExcessFPPrecision, because we know the input will have the appropriate
2481 // precision, and these operations don't modify precision at all.
2482 break;
2483
Chris Lattner9d6fa982005-04-28 21:44:33 +00002484 case ISD::FSQRT:
2485 case ISD::FSIN:
2486 case ISD::FCOS:
2487 Tmp1 = PromoteOp(Node->getOperand(0));
2488 assert(Tmp1.getValueType() == NVT);
2489 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2490 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002491 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2492 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002493 break;
2494
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002495 case ISD::AND:
2496 case ISD::OR:
2497 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002498 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002499 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002500 case ISD::MUL:
2501 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002502 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002503 // that too is okay if they are integer operations.
2504 Tmp1 = PromoteOp(Node->getOperand(0));
2505 Tmp2 = PromoteOp(Node->getOperand(1));
2506 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2507 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002508 break;
2509 case ISD::FADD:
2510 case ISD::FSUB:
2511 case ISD::FMUL:
2512 // The input may have strange things in the top bits of the registers, but
2513 // these operations don't care.
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);
2518
2519 // Floating point operations will give excess precision that we may not be
2520 // able to tolerate. If we DO allow excess precision, just leave it,
2521 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002522 // FIXME: Why would we need to round FP ops more than integer ones?
2523 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002524 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002525 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2526 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002527 break;
2528
Chris Lattner4d978642005-01-15 22:16:26 +00002529 case ISD::SDIV:
2530 case ISD::SREM:
2531 // These operators require that their input be sign extended.
2532 Tmp1 = PromoteOp(Node->getOperand(0));
2533 Tmp2 = PromoteOp(Node->getOperand(1));
2534 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002535 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2536 DAG.getValueType(VT));
2537 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2538 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002539 }
2540 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2541
2542 // Perform FP_ROUND: this is probably overly pessimistic.
2543 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002544 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2545 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002546 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002547 case ISD::FDIV:
2548 case ISD::FREM:
2549 // These operators require that their input be fp extended.
2550 Tmp1 = PromoteOp(Node->getOperand(0));
2551 Tmp2 = PromoteOp(Node->getOperand(1));
2552 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2553
2554 // Perform FP_ROUND: this is probably overly pessimistic.
2555 if (NoExcessFPPrecision)
2556 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2557 DAG.getValueType(VT));
2558 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002559
2560 case ISD::UDIV:
2561 case ISD::UREM:
2562 // These operators require that their input be zero extended.
2563 Tmp1 = PromoteOp(Node->getOperand(0));
2564 Tmp2 = PromoteOp(Node->getOperand(1));
2565 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002566 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2567 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002568 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2569 break;
2570
2571 case ISD::SHL:
2572 Tmp1 = PromoteOp(Node->getOperand(0));
2573 Tmp2 = LegalizeOp(Node->getOperand(1));
2574 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2575 break;
2576 case ISD::SRA:
2577 // The input value must be properly sign extended.
2578 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002579 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2580 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002581 Tmp2 = LegalizeOp(Node->getOperand(1));
2582 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2583 break;
2584 case ISD::SRL:
2585 // The input value must be properly zero extended.
2586 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002587 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002588 Tmp2 = LegalizeOp(Node->getOperand(1));
2589 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2590 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002591 case ISD::LOAD:
2592 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2593 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002594 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2595 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002596 // Remember that we legalized the chain.
2597 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2598 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002599 case ISD::SEXTLOAD:
2600 case ISD::ZEXTLOAD:
2601 case ISD::EXTLOAD:
2602 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2603 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002604 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2605 Node->getOperand(2),
2606 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002607 // Remember that we legalized the chain.
2608 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2609 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002610 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002611 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2612 case Expand: assert(0 && "It's impossible to expand bools");
2613 case Legal:
2614 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2615 break;
2616 case Promote:
2617 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2618 break;
2619 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002620 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2621 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2622 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2623 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002624 case ISD::SELECT_CC:
2625 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2626 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2627 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2628 Node->getOperand(1), Tmp2, Tmp3,
2629 Node->getOperand(4));
2630 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002631 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002632 case ISD::CALL: {
2633 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2635
Chris Lattner3d95c142005-01-19 20:24:35 +00002636 std::vector<SDOperand> Ops;
2637 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2638 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2639
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002640 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2641 "Can only promote single result calls");
2642 std::vector<MVT::ValueType> RetTyVTs;
2643 RetTyVTs.reserve(2);
2644 RetTyVTs.push_back(NVT);
2645 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002646 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2647 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002648 Result = SDOperand(NC, 0);
2649
2650 // Insert the new chain mapping.
2651 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2652 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002653 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002654 case ISD::CTPOP:
2655 case ISD::CTTZ:
2656 case ISD::CTLZ:
2657 Tmp1 = Node->getOperand(0);
2658 //Zero extend the argument
2659 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2660 // Perform the larger operation, then subtract if needed.
2661 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2662 switch(Node->getOpcode())
2663 {
2664 case ISD::CTPOP:
2665 Result = Tmp1;
2666 break;
2667 case ISD::CTTZ:
2668 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002669 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002670 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002671 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002672 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2673 break;
2674 case ISD::CTLZ:
2675 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002676 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2677 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002678 getSizeInBits(VT), NVT));
2679 break;
2680 }
2681 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002682 }
2683
2684 assert(Result.Val && "Didn't set a result!");
2685 AddPromotedOperand(Op, Result);
2686 return Result;
2687}
Chris Lattnerdc750592005-01-07 07:47:09 +00002688
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002689/// ExpandAddSub - Find a clever way to expand this add operation into
2690/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002691void SelectionDAGLegalize::
2692ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2693 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002694 // Expand the subcomponents.
2695 SDOperand LHSL, LHSH, RHSL, RHSH;
2696 ExpandOp(LHS, LHSL, LHSH);
2697 ExpandOp(RHS, RHSL, RHSH);
2698
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002699 std::vector<SDOperand> Ops;
2700 Ops.push_back(LHSL);
2701 Ops.push_back(LHSH);
2702 Ops.push_back(RHSL);
2703 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002704 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2705 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002706 Hi = Lo.getValue(1);
2707}
2708
Chris Lattner4157c412005-04-02 04:00:59 +00002709void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2710 SDOperand Op, SDOperand Amt,
2711 SDOperand &Lo, SDOperand &Hi) {
2712 // Expand the subcomponents.
2713 SDOperand LHSL, LHSH;
2714 ExpandOp(Op, LHSL, LHSH);
2715
2716 std::vector<SDOperand> Ops;
2717 Ops.push_back(LHSL);
2718 Ops.push_back(LHSH);
2719 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002720 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002721 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002722 Hi = Lo.getValue(1);
2723}
2724
2725
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002726/// ExpandShift - Try to find a clever way to expand this shift operation out to
2727/// smaller elements. If we can't find a way that is more efficient than a
2728/// libcall on this target, return false. Otherwise, return true with the
2729/// low-parts expanded into Lo and Hi.
2730bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2731 SDOperand &Lo, SDOperand &Hi) {
2732 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2733 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002734
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002735 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002736 SDOperand ShAmt = LegalizeOp(Amt);
2737 MVT::ValueType ShTy = ShAmt.getValueType();
2738 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2739 unsigned NVTBits = MVT::getSizeInBits(NVT);
2740
2741 // Handle the case when Amt is an immediate. Other cases are currently broken
2742 // and are disabled.
2743 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2744 unsigned Cst = CN->getValue();
2745 // Expand the incoming operand to be shifted, so that we have its parts
2746 SDOperand InL, InH;
2747 ExpandOp(Op, InL, InH);
2748 switch(Opc) {
2749 case ISD::SHL:
2750 if (Cst > VTBits) {
2751 Lo = DAG.getConstant(0, NVT);
2752 Hi = DAG.getConstant(0, NVT);
2753 } else if (Cst > NVTBits) {
2754 Lo = DAG.getConstant(0, NVT);
2755 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002756 } else if (Cst == NVTBits) {
2757 Lo = DAG.getConstant(0, NVT);
2758 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002759 } else {
2760 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2761 Hi = DAG.getNode(ISD::OR, NVT,
2762 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2763 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2764 }
2765 return true;
2766 case ISD::SRL:
2767 if (Cst > VTBits) {
2768 Lo = DAG.getConstant(0, NVT);
2769 Hi = DAG.getConstant(0, NVT);
2770 } else if (Cst > NVTBits) {
2771 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2772 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002773 } else if (Cst == NVTBits) {
2774 Lo = InH;
2775 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002776 } else {
2777 Lo = DAG.getNode(ISD::OR, NVT,
2778 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2779 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2780 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2781 }
2782 return true;
2783 case ISD::SRA:
2784 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002785 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002786 DAG.getConstant(NVTBits-1, ShTy));
2787 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002788 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002789 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002790 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002791 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002792 } else if (Cst == NVTBits) {
2793 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002794 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002795 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002796 } else {
2797 Lo = DAG.getNode(ISD::OR, NVT,
2798 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2799 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2800 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2801 }
2802 return true;
2803 }
2804 }
2805 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2806 // so disable it for now. Currently targets are handling this via SHL_PARTS
2807 // and friends.
2808 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002809
2810 // If we have an efficient select operation (or if the selects will all fold
2811 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002812 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002813 return false;
2814
2815 SDOperand InL, InH;
2816 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002817 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2818 DAG.getConstant(NVTBits, ShTy), ShAmt);
2819
Chris Lattner4d25c042005-01-20 20:29:23 +00002820 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002821 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2822 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002823
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002824 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2825 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2826 DAG.getConstant(NVTBits-1, ShTy));
2827 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2828 DAG.getConstant(NVTBits-1, ShTy));
2829 }
2830
2831 if (Opc == ISD::SHL) {
2832 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2833 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2834 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002835 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002836
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002837 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2838 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2839 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002840 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002841 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2842 DAG.getConstant(32, ShTy),
2843 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002844 DAG.getConstant(0, NVT),
2845 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002846 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002847 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002848 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002849 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002850
2851 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002852 if (Opc == ISD::SRA)
2853 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2854 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002855 else
2856 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002857 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002858 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002859 }
2860 return true;
2861}
Chris Lattneraac464e2005-01-21 06:05:23 +00002862
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002863/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2864/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002865/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002866static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002867 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002868
Chris Lattner2dce7032005-05-12 23:24:06 +00002869 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002870 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002871 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002872 Found = Node;
2873 return;
2874 }
2875
2876 // Otherwise, scan the operands of Node to see if any of them is a call.
2877 assert(Node->getNumOperands() != 0 &&
2878 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002879 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002880 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002881
2882 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002883 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002884 Found);
2885}
2886
2887
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002888/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2889/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002890/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002891static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2892 std::set<SDNode*> &Visited) {
2893 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2894 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002895
Chris Lattner2dce7032005-05-12 23:24:06 +00002896 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002897 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002898 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002899 Found = Node;
2900 return;
2901 }
2902
2903 // Otherwise, scan the operands of Node to see if any of them is a call.
2904 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2905 if (UI == E) return;
2906 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002907 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002908
2909 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002910 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002911}
2912
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002913/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002914/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002915static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002916 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002917 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002918 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002919 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002920
Chris Lattner4add7e32005-01-23 04:42:50 +00002921 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002922 if (TheChain.getValueType() != MVT::Other)
2923 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002924 if (TheChain.getValueType() != MVT::Other)
2925 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002926
2927 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002928 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002929
Chris Lattner4add7e32005-01-23 04:42:50 +00002930 // Make sure to only follow users of our token chain.
2931 SDNode *User = *UI;
2932 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2933 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002934 if (SDNode *Result = FindCallSeqEnd(User))
2935 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002936 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002937 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002938}
2939
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002940/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002941/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002942static SDNode *FindCallSeqStart(SDNode *Node) {
2943 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002944 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002945
2946 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2947 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002948 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002949}
2950
2951
Chris Lattner4add7e32005-01-23 04:42:50 +00002952/// FindInputOutputChains - If we are replacing an operation with a call we need
2953/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002954/// properly serialize the calls in the block. The returned operand is the
2955/// input chain value for the new call (e.g. the entry node or the previous
2956/// call), and OutChain is set to be the chain node to update to point to the
2957/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002958static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2959 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002960 SDNode *LatestCallSeqStart = Entry.Val;
2961 SDNode *LatestCallSeqEnd = 0;
2962 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2963 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002964
Chris Lattner2dce7032005-05-12 23:24:06 +00002965 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002966 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002967 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2968 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002969 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002970 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002971 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2972 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002973 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002974 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002975 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002976
Chris Lattner06bbeb62005-05-11 19:02:11 +00002977 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002978 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002979 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002980 std::set<SDNode*> Visited;
2981 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002982
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002983 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002984 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002985 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002986
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002987 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002988}
2989
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002990/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002991void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2992 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002993 // Nothing to splice it into?
2994 if (OutChain == 0) return;
2995
2996 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2997 //OutChain->dump();
2998
2999 // Form a token factor node merging the old inval and the new inval.
3000 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3001 OutChain->getOperand(0));
3002 // Change the node to refer to the new token.
3003 OutChain->setAdjCallChain(InToken);
3004}
Chris Lattner4add7e32005-01-23 04:42:50 +00003005
3006
Chris Lattneraac464e2005-01-21 06:05:23 +00003007// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3008// does not fit into a register, return the lo part and set the hi part to the
3009// by-reg argument. If it does fit into a single register, return the result
3010// and leave the Hi part unset.
3011SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3012 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003013 SDNode *OutChain;
3014 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3015 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003016 if (InChain.Val == 0)
3017 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003018
Chris Lattneraac464e2005-01-21 06:05:23 +00003019 TargetLowering::ArgListTy Args;
3020 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3021 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3022 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3023 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3024 }
3025 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003026
Chris Lattner06bbeb62005-05-11 19:02:11 +00003027 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003028 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003029 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003030 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3031 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003032
Chris Lattner63022662005-09-02 20:26:58 +00003033 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003034 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003035 default: assert(0 && "Unknown thing");
3036 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003037 Result = CallInfo.first;
3038 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003039 case Promote:
3040 assert(0 && "Cannot promote this yet!");
3041 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003042 ExpandOp(CallInfo.first, Result, Hi);
3043 CallInfo.second = LegalizeOp(CallInfo.second);
3044 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003045 }
Chris Lattner63022662005-09-02 20:26:58 +00003046
3047 SpliceCallInto(CallInfo.second, OutChain);
3048 NeedsAnotherIteration = true;
3049 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003050}
3051
Chris Lattner4add7e32005-01-23 04:42:50 +00003052
Chris Lattneraac464e2005-01-21 06:05:23 +00003053/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3054/// destination type is legal.
3055SDOperand SelectionDAGLegalize::
3056ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003057 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003058 assert(getTypeAction(Source.getValueType()) == Expand &&
3059 "This is not an expansion!");
3060 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3061
Chris Lattner06bbeb62005-05-11 19:02:11 +00003062 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003063 assert(Source.getValueType() == MVT::i64 &&
3064 "This only works for 64-bit -> FP");
3065 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3066 // incoming integer is set. To handle this, we dynamically test to see if
3067 // it is set, and, if so, add a fudge factor.
3068 SDOperand Lo, Hi;
3069 ExpandOp(Source, Lo, Hi);
3070
Chris Lattner2a4f7312005-05-13 04:45:13 +00003071 // If this is unsigned, and not supported, first perform the conversion to
3072 // signed, then adjust the result if the sign bit is set.
3073 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3074 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3075
Chris Lattnerd47675e2005-08-09 20:20:18 +00003076 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3077 DAG.getConstant(0, Hi.getValueType()),
3078 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003079 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3080 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3081 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003082 uint64_t FF = 0x5f800000ULL;
3083 if (TLI.isLittleEndian()) FF <<= 32;
3084 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003085
Chris Lattnerc30405e2005-08-26 17:15:30 +00003086 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003087 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3088 SDOperand FudgeInReg;
3089 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003090 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3091 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003092 else {
3093 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003094 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3095 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003096 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003097 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003098 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003099
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003100 // Check to see if the target has a custom way to lower this. If so, use it.
3101 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3102 default: assert(0 && "This action not implemented for this operation!");
3103 case TargetLowering::Legal:
3104 case TargetLowering::Expand:
3105 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003106 case TargetLowering::Custom: {
3107 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3108 Source), DAG);
3109 if (NV.Val)
3110 return LegalizeOp(NV);
3111 break; // The target decided this was legal after all
3112 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003113 }
3114
Chris Lattner153587e2005-05-12 07:00:44 +00003115 // Expand the source, then glue it back together for the call. We must expand
3116 // the source in case it is shared (this pass of legalize must traverse it).
3117 SDOperand SrcLo, SrcHi;
3118 ExpandOp(Source, SrcLo, SrcHi);
3119 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3120
Chris Lattner06bbeb62005-05-11 19:02:11 +00003121 SDNode *OutChain = 0;
3122 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3123 DAG.getEntryNode());
3124 const char *FnName = 0;
3125 if (DestTy == MVT::f32)
3126 FnName = "__floatdisf";
3127 else {
3128 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3129 FnName = "__floatdidf";
3130 }
3131
Chris Lattneraac464e2005-01-21 06:05:23 +00003132 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3133
3134 TargetLowering::ArgListTy Args;
3135 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003136
Chris Lattneraac464e2005-01-21 06:05:23 +00003137 Args.push_back(std::make_pair(Source, ArgTy));
3138
3139 // We don't care about token chains for libcalls. We just use the entry
3140 // node as our input and ignore the output chain. This allows us to place
3141 // calls wherever we need them to satisfy data dependences.
3142 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003143
3144 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003145 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3146 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003147
Chris Lattnera5bf1032005-05-12 04:49:08 +00003148 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003149 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003150}
Misha Brukman835702a2005-04-21 22:36:52 +00003151
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003152
3153
Chris Lattnerdc750592005-01-07 07:47:09 +00003154/// ExpandOp - Expand the specified SDOperand into its two component pieces
3155/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3156/// LegalizeNodes map is filled in for any results that are not expanded, the
3157/// ExpandedNodes map is filled in for any results that are expanded, and the
3158/// Lo/Hi values are returned.
3159void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3160 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003161 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003162 SDNode *Node = Op.Val;
3163 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003164 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3165 "Cannot expand FP values!");
3166 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003167 "Cannot expand to FP value or to larger int value!");
3168
Chris Lattner1a570f12005-09-02 20:32:45 +00003169 // See if we already expanded it.
3170 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3171 = ExpandedNodes.find(Op);
3172 if (I != ExpandedNodes.end()) {
3173 Lo = I->second.first;
3174 Hi = I->second.second;
3175 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003176 }
3177
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003178 // Expanding to multiple registers needs to perform an optimization step, and
3179 // is not careful to avoid operations the target does not support. Make sure
3180 // that all generated operations are legalized in the next iteration.
3181 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003182
Chris Lattnerdc750592005-01-07 07:47:09 +00003183 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003184 case ISD::CopyFromReg:
3185 assert(0 && "CopyFromReg must be legal!");
3186 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003187 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3188 assert(0 && "Do not know how to expand this operator!");
3189 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003190 case ISD::UNDEF:
3191 Lo = DAG.getNode(ISD::UNDEF, NVT);
3192 Hi = DAG.getNode(ISD::UNDEF, NVT);
3193 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003194 case ISD::Constant: {
3195 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3196 Lo = DAG.getConstant(Cst, NVT);
3197 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3198 break;
3199 }
Nate Begemanae89d862005-12-07 19:48:11 +00003200 case ISD::ConstantVec: {
3201 unsigned NumElements = Node->getNumOperands();
3202 // If we only have two elements left in the constant vector, just break it
3203 // apart into the two scalar constants it contains. Otherwise, bisect the
3204 // ConstantVec, and return each half as a new ConstantVec.
3205 // FIXME: this is hard coded as big endian, it may have to change to support
3206 // SSE and Alpha MVI
3207 if (NumElements == 2) {
3208 Hi = Node->getOperand(0);
3209 Lo = Node->getOperand(1);
3210 } else {
3211 NumElements /= 2;
3212 std::vector<SDOperand> LoOps, HiOps;
3213 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3214 HiOps.push_back(Node->getOperand(I));
3215 LoOps.push_back(Node->getOperand(I+NumElements));
3216 }
3217 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3218 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3219 }
3220 break;
3221 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003222
Chris Lattner32e08b72005-03-28 22:03:13 +00003223 case ISD::BUILD_PAIR:
3224 // Legalize both operands. FIXME: in the future we should handle the case
3225 // where the two elements are not legal.
3226 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3227 Lo = LegalizeOp(Node->getOperand(0));
3228 Hi = LegalizeOp(Node->getOperand(1));
3229 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003230
3231 case ISD::SIGN_EXTEND_INREG:
3232 ExpandOp(Node->getOperand(0), Lo, Hi);
3233 // Sign extend the lo-part.
3234 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3235 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3236 TLI.getShiftAmountTy()));
3237 // sext_inreg the low part if needed.
3238 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3239 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003240
Chris Lattner55e9cde2005-05-11 04:51:16 +00003241 case ISD::CTPOP:
3242 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003243 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3244 DAG.getNode(ISD::CTPOP, NVT, Lo),
3245 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003246 Hi = DAG.getConstant(0, NVT);
3247 break;
3248
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003249 case ISD::CTLZ: {
3250 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003251 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003252 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3253 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003254 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3255 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003256 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3257 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3258
3259 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3260 Hi = DAG.getConstant(0, NVT);
3261 break;
3262 }
3263
3264 case ISD::CTTZ: {
3265 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003266 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003267 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3268 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003269 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3270 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003271 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3272 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3273
3274 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3275 Hi = DAG.getConstant(0, NVT);
3276 break;
3277 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003278
Chris Lattnerdc750592005-01-07 07:47:09 +00003279 case ISD::LOAD: {
3280 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3281 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003282 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003283
3284 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003285 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003286 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3287 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003288 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003289 //are from the same instruction?
3290 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003291
3292 // Build a factor node to remember that this load is independent of the
3293 // other one.
3294 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3295 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003296
Chris Lattnerdc750592005-01-07 07:47:09 +00003297 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003298 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003299 if (!TLI.isLittleEndian())
3300 std::swap(Lo, Hi);
3301 break;
3302 }
Nate Begemand37c1312005-11-22 18:16:00 +00003303 case ISD::VLOAD: {
3304 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3305 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3306 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3307 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3308
3309 // If we only have two elements, turn into a pair of scalar loads.
3310 // FIXME: handle case where a vector of two elements is fine, such as
3311 // 2 x double on SSE2.
3312 if (NumElements == 2) {
3313 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3314 // Increment the pointer to the other half.
3315 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3316 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3317 getIntPtrConstant(IncrementSize));
3318 //Is this safe? declaring that the two parts of the split load
3319 //are from the same instruction?
3320 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3321 } else {
3322 NumElements /= 2; // Split the vector in half
3323 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3324 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3325 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3326 getIntPtrConstant(IncrementSize));
3327 //Is this safe? declaring that the two parts of the split load
3328 //are from the same instruction?
3329 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3330 }
3331
3332 // Build a factor node to remember that this load is independent of the
3333 // other one.
3334 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3335 Hi.getValue(1));
3336
3337 // Remember that we legalized the chain.
3338 AddLegalizedOperand(Op.getValue(1), TF);
3339 if (!TLI.isLittleEndian())
3340 std::swap(Lo, Hi);
3341 break;
3342 }
3343 case ISD::VADD:
3344 case ISD::VSUB:
3345 case ISD::VMUL: {
3346 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3347 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3348 SDOperand LL, LH, RL, RH;
3349
3350 ExpandOp(Node->getOperand(0), LL, LH);
3351 ExpandOp(Node->getOperand(1), RL, RH);
3352
3353 // If we only have two elements, turn into a pair of scalar loads.
3354 // FIXME: handle case where a vector of two elements is fine, such as
3355 // 2 x double on SSE2.
3356 if (NumElements == 2) {
3357 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3358 Lo = DAG.getNode(Opc, EVT, LL, RL);
3359 Hi = DAG.getNode(Opc, EVT, LH, RH);
3360 } else {
3361 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3362 LL.getOperand(3));
3363 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3364 LH.getOperand(3));
3365 }
3366 break;
3367 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003368 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003369 case ISD::CALL: {
3370 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3371 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3372
Chris Lattner3d95c142005-01-19 20:24:35 +00003373 bool Changed = false;
3374 std::vector<SDOperand> Ops;
3375 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3376 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3377 Changed |= Ops.back() != Node->getOperand(i);
3378 }
3379
Chris Lattnerdc750592005-01-07 07:47:09 +00003380 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3381 "Can only expand a call once so far, not i64 -> i16!");
3382
3383 std::vector<MVT::ValueType> RetTyVTs;
3384 RetTyVTs.reserve(3);
3385 RetTyVTs.push_back(NVT);
3386 RetTyVTs.push_back(NVT);
3387 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003388 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3389 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003390 Lo = SDOperand(NC, 0);
3391 Hi = SDOperand(NC, 1);
3392
3393 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003394 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003395 break;
3396 }
3397 case ISD::AND:
3398 case ISD::OR:
3399 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3400 SDOperand LL, LH, RL, RH;
3401 ExpandOp(Node->getOperand(0), LL, LH);
3402 ExpandOp(Node->getOperand(1), RL, RH);
3403 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3404 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3405 break;
3406 }
3407 case ISD::SELECT: {
3408 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003409
3410 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3411 case Expand: assert(0 && "It's impossible to expand bools");
3412 case Legal:
3413 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3414 break;
3415 case Promote:
3416 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3417 break;
3418 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003419 ExpandOp(Node->getOperand(1), LL, LH);
3420 ExpandOp(Node->getOperand(2), RL, RH);
3421 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3422 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3423 break;
3424 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003425 case ISD::SELECT_CC: {
3426 SDOperand TL, TH, FL, FH;
3427 ExpandOp(Node->getOperand(2), TL, TH);
3428 ExpandOp(Node->getOperand(3), FL, FH);
3429 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3430 Node->getOperand(1), TL, FL, Node->getOperand(4));
3431 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3432 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003433 Lo = LegalizeOp(Lo);
3434 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003435 break;
3436 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003437 case ISD::SEXTLOAD: {
3438 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3439 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3440 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3441
3442 if (EVT == NVT)
3443 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3444 else
3445 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3446 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003447
3448 // Remember that we legalized the chain.
3449 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3450
Nate Begemanc3a89c52005-10-13 17:15:37 +00003451 // The high part is obtained by SRA'ing all but one of the bits of the lo
3452 // part.
3453 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3454 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3455 TLI.getShiftAmountTy()));
3456 Lo = LegalizeOp(Lo);
3457 Hi = LegalizeOp(Hi);
3458 break;
3459 }
3460 case ISD::ZEXTLOAD: {
3461 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3462 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3463 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3464
3465 if (EVT == NVT)
3466 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3467 else
3468 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3469 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003470
3471 // Remember that we legalized the chain.
3472 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3473
Nate Begemanc3a89c52005-10-13 17:15:37 +00003474 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003475 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003476 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003477 break;
3478 }
3479 case ISD::EXTLOAD: {
3480 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3481 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3482 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3483
3484 if (EVT == NVT)
3485 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3486 else
3487 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3488 EVT);
3489
3490 // Remember that we legalized the chain.
3491 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3492
3493 // The high part is undefined.
3494 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3495 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003496 break;
3497 }
Chris Lattner7753f172005-09-02 00:18:10 +00003498 case ISD::ANY_EXTEND: {
3499 SDOperand In;
3500 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3501 case Expand: assert(0 && "expand-expand not implemented yet!");
3502 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3503 case Promote:
3504 In = PromoteOp(Node->getOperand(0));
3505 break;
3506 }
3507
3508 // The low part is any extension of the input (which degenerates to a copy).
3509 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3510 // The high part is undefined.
3511 Hi = DAG.getNode(ISD::UNDEF, NVT);
3512 break;
3513 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003514 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003515 SDOperand In;
3516 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3517 case Expand: assert(0 && "expand-expand not implemented yet!");
3518 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3519 case Promote:
3520 In = PromoteOp(Node->getOperand(0));
3521 // Emit the appropriate sign_extend_inreg to get the value we want.
3522 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003523 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003524 break;
3525 }
3526
Chris Lattnerdc750592005-01-07 07:47:09 +00003527 // The low part is just a sign extension of the input (which degenerates to
3528 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003529 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003530
Chris Lattnerdc750592005-01-07 07:47:09 +00003531 // The high part is obtained by SRA'ing all but one of the bits of the lo
3532 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003533 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003534 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3535 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003536 break;
3537 }
Chris Lattner47844892005-04-03 23:41:52 +00003538 case ISD::ZERO_EXTEND: {
3539 SDOperand In;
3540 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3541 case Expand: assert(0 && "expand-expand not implemented yet!");
3542 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3543 case Promote:
3544 In = PromoteOp(Node->getOperand(0));
3545 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003546 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003547 break;
3548 }
3549
Chris Lattnerdc750592005-01-07 07:47:09 +00003550 // The low part is just a zero extension of the input (which degenerates to
3551 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003552 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003553
Chris Lattnerdc750592005-01-07 07:47:09 +00003554 // The high part is just a zero.
3555 Hi = DAG.getConstant(0, NVT);
3556 break;
Chris Lattner47844892005-04-03 23:41:52 +00003557 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003558
Chris Lattner44c28c22005-11-20 22:56:56 +00003559 case ISD::READCYCLECOUNTER: {
3560 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3561 TargetLowering::Custom &&
3562 "Must custom expand ReadCycleCounter");
3563 SDOperand T = TLI.LowerOperation(Op, DAG);
3564 assert(T.Val && "Node must be custom expanded!");
3565 Lo = LegalizeOp(T.getValue(0));
3566 Hi = LegalizeOp(T.getValue(1));
3567 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3568 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003569 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003570 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003571
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003572 // These operators cannot be expanded directly, emit them as calls to
3573 // library functions.
3574 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003575 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003576 SDOperand Op;
3577 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3578 case Expand: assert(0 && "cannot expand FP!");
3579 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3580 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3581 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003582
Chris Lattner941d84a2005-07-30 01:40:57 +00003583 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3584
Chris Lattnerfe68d752005-07-29 00:33:32 +00003585 // Now that the custom expander is done, expand the result, which is still
3586 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003587 if (Op.Val) {
3588 ExpandOp(Op, Lo, Hi);
3589 break;
3590 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003591 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003592
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003593 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003594 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003595 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003596 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003597 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003598
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003599 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003600 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3601 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3602 LegalizeOp(Node->getOperand(0)));
3603 // Now that the custom expander is done, expand the result, which is still
3604 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003605 Op = TLI.LowerOperation(Op, DAG);
3606 if (Op.Val) {
3607 ExpandOp(Op, Lo, Hi);
3608 break;
3609 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003610 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003611
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003612 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003613 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003614 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003615 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003616 break;
3617
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003618 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003619 // If the target wants custom lowering, do so.
3620 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3621 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3622 LegalizeOp(Node->getOperand(1)));
3623 Op = TLI.LowerOperation(Op, DAG);
3624 if (Op.Val) {
3625 // Now that the custom expander is done, expand the result, which is
3626 // still VT.
3627 ExpandOp(Op, Lo, Hi);
3628 break;
3629 }
3630 }
3631
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003632 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003633 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003634 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003635
3636 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003637 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003638 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3639 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003640 break;
3641 }
3642
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003643 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003644 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003645 break;
3646
3647 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003648 // If the target wants custom lowering, do so.
3649 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3650 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3651 LegalizeOp(Node->getOperand(1)));
3652 Op = TLI.LowerOperation(Op, DAG);
3653 if (Op.Val) {
3654 // Now that the custom expander is done, expand the result, which is
3655 // still VT.
3656 ExpandOp(Op, Lo, Hi);
3657 break;
3658 }
3659 }
3660
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003661 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003662 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003663 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003664
3665 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003666 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003667 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3668 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003669 break;
3670 }
3671
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003672 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003673 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003674 break;
3675 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003676 // If the target wants custom lowering, do so.
3677 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3678 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3679 LegalizeOp(Node->getOperand(1)));
3680 Op = TLI.LowerOperation(Op, DAG);
3681 if (Op.Val) {
3682 // Now that the custom expander is done, expand the result, which is
3683 // still VT.
3684 ExpandOp(Op, Lo, Hi);
3685 break;
3686 }
3687 }
3688
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003689 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003690 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003691 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003692
3693 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003694 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003695 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3696 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003697 break;
3698 }
3699
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003700 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003701 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003702 break;
3703
Misha Brukman835702a2005-04-21 22:36:52 +00003704 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003705 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3706 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003707 break;
3708 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003709 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3710 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003711 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003712 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003713 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003714 SDOperand LL, LH, RL, RH;
3715 ExpandOp(Node->getOperand(0), LL, LH);
3716 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003717 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3718 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3719 // extended the sign bit of the low half through the upper half, and if so
3720 // emit a MULHS instead of the alternate sequence that is valid for any
3721 // i64 x i64 multiply.
3722 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3723 // is RH an extension of the sign bit of RL?
3724 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3725 RH.getOperand(1).getOpcode() == ISD::Constant &&
3726 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3727 // is LH an extension of the sign bit of LL?
3728 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3729 LH.getOperand(1).getOpcode() == ISD::Constant &&
3730 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3731 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3732 } else {
3733 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3734 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3735 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3736 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3737 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3738 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003739 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3740 } else {
3741 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3742 }
3743 break;
3744 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003745 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3746 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3747 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3748 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003749 }
3750
3751 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003752 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3753 std::make_pair(Lo, Hi))).second;
3754 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003755}
3756
3757
3758// SelectionDAG::Legalize - This is the entry point for the file.
3759//
Chris Lattner4add7e32005-01-23 04:42:50 +00003760void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003761 /// run - This is the main entry point to this class.
3762 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003763 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003764}
3765