blob: 7b13db8258bba89f1fb25620499837912ab7b7bd [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Jim Laskeyf2516a92005-08-17 00:39:29 +0000168/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
169/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000170/// we expand it. At this point, we know that the result and operand types are
171/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand Op0,
174 MVT::ValueType DestVT) {
175 if (Op0.getValueType() == MVT::i32) {
176 // simple 32-bit [signed|unsigned] integer to float/double expansion
177
178 // get the stack frame index of a 8 byte buffer
179 MachineFunction &MF = DAG.getMachineFunction();
180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
181 // get address of 8 byte buffer
182 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
183 // word offset constant for Hi/Lo address computation
184 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
185 // set up Hi and Lo (into buffer) address based on endian
186 SDOperand Hi, Lo;
187 if (TLI.isLittleEndian()) {
188 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
189 Lo = StackSlot;
190 } else {
191 Hi = StackSlot;
192 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 }
194 // if signed map to unsigned space
195 SDOperand Op0Mapped;
196 if (isSigned) {
197 // constant used to invert sign bit (signed to unsigned mapping)
198 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
199 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
200 } else {
201 Op0Mapped = Op0;
202 }
203 // store the lo of the constructed double - based on integer input
204 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
205 Op0Mapped, Lo, DAG.getSrcValue(NULL));
206 // initial hi portion of constructed double
207 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
208 // store the hi of the constructed double - biased exponent
209 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
210 InitialHi, Hi, DAG.getSrcValue(NULL));
211 // load the constructed double
212 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
213 DAG.getSrcValue(NULL));
214 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000221 // final result
222 SDOperand Result;
223 // handle final rounding
224 if (DestVT == MVT::f64) {
225 // do nothing
226 Result = Sub;
227 } else {
228 // if f32 then cast to f32
229 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
230 }
231 NeedsAnotherIteration = true;
232 return Result;
233 }
234 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattnerd47675e2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000240 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
241 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
242 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Jim Laskeyf2516a92005-08-17 00:39:29 +0000244 // If the sign bit of the integer is set, the large number will be treated
245 // as a negative number. To counteract this, the dynamic code adds an
246 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000247 uint64_t FF;
248 switch (Op0.getValueType()) {
249 default: assert(0 && "Unsupported integer type!");
250 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
251 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
252 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
253 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
254 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnerc30405e2005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
260 SDOperand FudgeInReg;
261 if (DestVT == MVT::f32)
262 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL));
264 else {
265 assert(DestVT == MVT::f64 && "Unexpected conversion");
266 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
267 DAG.getEntryNode(), CPIdx,
268 DAG.getSrcValue(NULL), MVT::f32));
269 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000273}
274
Chris Lattner19732782005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000277/// we promote it. At this point, we know that the result and operand types are
278/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
279/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // First step, figure out the appropriate *INT_TO_FP operation to use.
284 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000285
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // Scan for the appropriate larger type to use.
289 while (1) {
290 NewInTy = (MVT::ValueType)(NewInTy+1);
291 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Chris Lattnere3e847b2005-07-16 00:19:57 +0000293 // If the target supports SINT_TO_FP of this type, use it.
294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
295 default: break;
296 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000298 break; // Can't use this datatype.
299 // FALL THROUGH.
300 case TargetLowering::Custom:
301 OpToUse = ISD::SINT_TO_FP;
302 break;
303 }
304 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Chris Lattnere3e847b2005-07-16 00:19:57 +0000307 // If the target supports UINT_TO_FP of this type, use it.
308 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
309 default: break;
310 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000312 break; // Can't use this datatype.
313 // FALL THROUGH.
314 case TargetLowering::Custom:
315 OpToUse = ISD::UINT_TO_FP;
316 break;
317 }
318 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000319
Chris Lattnere3e847b2005-07-16 00:19:57 +0000320 // Otherwise, try a larger type.
321 }
322
323 // Make sure to legalize any nodes we create here in the next pass.
324 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000325
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
328 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000331}
332
Chris Lattner44fe26f2005-07-29 00:11:56 +0000333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
334/// FP_TO_*INT operation of the specified operand when the target requests that
335/// we promote it. At this point, we know that the result and operand types are
336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
337/// operation that returns a larger result.
338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
339 MVT::ValueType DestVT,
340 bool isSigned) {
341 // First step, figure out the appropriate FP_TO*INT operation to use.
342 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000343
Chris Lattner44fe26f2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // Scan for the appropriate larger type to use.
347 while (1) {
348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000350
Chris Lattner44fe26f2005-07-29 00:11:56 +0000351 // If the target supports FP_TO_SINT returning this type, use it.
352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
353 default: break;
354 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000356 break; // Can't use this datatype.
357 // FALL THROUGH.
358 case TargetLowering::Custom:
359 OpToUse = ISD::FP_TO_SINT;
360 break;
361 }
362 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-07-29 00:11:56 +0000364 // If the target supports FP_TO_UINT of this type, use it.
365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
366 default: break;
367 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 break; // Can't use this datatype.
370 // FALL THROUGH.
371 case TargetLowering::Custom:
372 OpToUse = ISD::FP_TO_UINT;
373 break;
374 }
375 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000376
Chris Lattner44fe26f2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000379
Chris Lattner44fe26f2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000382
Chris Lattner44fe26f2005-07-29 00:11:56 +0000383 // Okay, we found the operation and type to use. Truncate the result of the
384 // extended FP_TO_*INT operation to the desired size.
385 return DAG.getNode(ISD::TRUNCATE, DestVT,
386 DAG.getNode(OpToUse, NewOutTy, LegalOp));
387}
388
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000535 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000536 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000537 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000538 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000539 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000540 case ISD::BasicBlock:
541 case ISD::CONDCODE:
542 case ISD::VALUETYPE:
543 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000544 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000545 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
546 default: assert(0 && "This action is not supported yet!");
547 case TargetLowering::Custom: {
548 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
549 if (Tmp.Val) {
550 Result = LegalizeOp(Tmp);
551 break;
552 }
553 } // FALLTHROUGH if the target doesn't want to lower this op after all.
554 case TargetLowering::Legal:
555 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
556 break;
557 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000558 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000559 case ISD::AssertSext:
560 case ISD::AssertZext:
561 Tmp1 = LegalizeOp(Node->getOperand(0));
562 if (Tmp1 != Node->getOperand(0))
563 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
564 Node->getOperand(1));
565 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000566 case ISD::MERGE_VALUES:
567 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000570 Result = Op.getValue(0);
571 if (Node->getNumOperands() == 2) {
572 if (Tmp1 != Node->getOperand(0))
573 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000574 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000575 Node->getValueType(0));
576 } else {
577 assert(Node->getNumOperands() == 3 && "Invalid copyfromreg!");
578 Tmp2 = LegalizeOp(Node->getOperand(2));
579 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
580 Result = DAG.getCopyFromReg(Tmp1,
581 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
582 Node->getValueType(0), Tmp2);
583 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
584 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000585 // Since CopyFromReg produces two values, make sure to remember that we
586 // legalized both of them.
587 AddLegalizedOperand(Op.getValue(0), Result);
588 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
589 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000590 case ISD::ImplicitDef:
591 Tmp1 = LegalizeOp(Node->getOperand(0));
592 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000593 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
594 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000595 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000596 case ISD::UNDEF: {
597 MVT::ValueType VT = Op.getValueType();
598 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000599 default: assert(0 && "This action is not supported yet!");
600 case TargetLowering::Expand:
601 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000602 if (MVT::isInteger(VT))
603 Result = DAG.getConstant(0, VT);
604 else if (MVT::isFloatingPoint(VT))
605 Result = DAG.getConstantFP(0, VT);
606 else
607 assert(0 && "Unknown value type!");
608 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000609 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000610 break;
611 }
612 break;
613 }
Chris Lattner435b4022005-11-29 06:21:05 +0000614
615 case ISD::LOCATION:
616 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
617 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
618
619 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
620 case TargetLowering::Promote:
621 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000622 case TargetLowering::Expand: {
623 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
624 std::vector<SDOperand> Ops;
625 Ops.push_back(Tmp1); // chain
626 Ops.push_back(Node->getOperand(1)); // line #
627 Ops.push_back(Node->getOperand(2)); // col #
628 const std::string &fname = cast<StringSDNode>(Node->getOperand(3))->getValue();
629 const std::string &dirname=cast<StringSDNode>(Node->getOperand(4))->getValue();
630 unsigned id = DebugInfo.RecordSource(fname, dirname);
631 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
632 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
633 }
Chris Lattner435b4022005-11-29 06:21:05 +0000634 break;
635 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000636 if (Tmp1 != Node->getOperand(0) ||
637 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000638 std::vector<SDOperand> Ops;
639 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000640 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
641 Ops.push_back(Node->getOperand(1)); // line # must be legal.
642 Ops.push_back(Node->getOperand(2)); // col # must be legal.
643 } else {
644 // Otherwise promote them.
645 Ops.push_back(PromoteOp(Node->getOperand(1)));
646 Ops.push_back(PromoteOp(Node->getOperand(2)));
647 }
Chris Lattner435b4022005-11-29 06:21:05 +0000648 Ops.push_back(Node->getOperand(3)); // filename must be legal.
649 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
650 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
651 }
652 break;
653 }
654 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000655
656 case ISD::DEBUG_LOC:
657 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
658 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
659 case TargetLowering::Promote:
660 case TargetLowering::Expand:
661 default: assert(0 && "This action is not supported yet!");
662 case TargetLowering::Legal:
663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
665 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
666 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
667
668 if (Tmp1 != Node->getOperand(0) ||
669 Tmp2 != Node->getOperand(1) ||
670 Tmp3 != Node->getOperand(2) ||
671 Tmp4 != Node->getOperand(3)) {
672 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
673 }
674 break;
675 }
676 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000677
Chris Lattnerdc750592005-01-07 07:47:09 +0000678 case ISD::Constant:
679 // We know we don't need to expand constants here, constants only have one
680 // value and we check that it is fine above.
681
682 // FIXME: Maybe we should handle things like targets that don't support full
683 // 32-bit immediates?
684 break;
685 case ISD::ConstantFP: {
686 // Spill FP immediates to the constant pool if the target cannot directly
687 // codegen them. Targets often have some immediate values that can be
688 // efficiently generated into an FP register without a load. We explicitly
689 // leave these constants as ConstantFP nodes for the target to deal with.
690
691 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
692
693 // Check to see if this FP immediate is already legal.
694 bool isLegal = false;
695 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
696 E = TLI.legal_fpimm_end(); I != E; ++I)
697 if (CFP->isExactlyValue(*I)) {
698 isLegal = true;
699 break;
700 }
701
702 if (!isLegal) {
703 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000704 bool Extend = false;
705
706 // If a FP immediate is precise when represented as a float, we put it
707 // into the constant pool as a float, even if it's is statically typed
708 // as a double.
709 MVT::ValueType VT = CFP->getValueType(0);
710 bool isDouble = VT == MVT::f64;
711 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
712 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000713 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
714 // Only do this if the target has a native EXTLOAD instruction from
715 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000716 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000717 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
718 VT = MVT::f32;
719 Extend = true;
720 }
Misha Brukman835702a2005-04-21 22:36:52 +0000721
Nate Begeman4e56db62005-12-10 02:36:00 +0000722 SDOperand CPIdx =
723 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000724 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000725 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
726 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000727 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000728 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
729 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000730 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000731 }
732 break;
733 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000734 case ISD::ConstantVec: {
735 // We assume that vector constants are not legal, and will be immediately
736 // spilled to the constant pool.
737 //
738 // FIXME: revisit this when we have some kind of mechanism by which targets
739 // can decided legality of vector constants, of which there may be very
740 // many.
741 //
742 // Create a ConstantPacked, and put it in the constant pool.
743 std::vector<Constant*> CV;
744 MVT::ValueType VT = Node->getValueType(0);
745 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
746 SDOperand OpN = Node->getOperand(I);
747 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
748 if (MVT::isFloatingPoint(VT))
749 CV.push_back(ConstantFP::get(OpNTy,
750 cast<ConstantFPSDNode>(OpN)->getValue()));
751 else
752 CV.push_back(ConstantUInt::get(OpNTy,
753 cast<ConstantSDNode>(OpN)->getValue()));
754 }
755 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000756 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000757 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
758 break;
759 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000760 case ISD::TokenFactor:
761 if (Node->getNumOperands() == 2) {
762 bool Changed = false;
763 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
764 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
765 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
766 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
767 } else {
768 std::vector<SDOperand> Ops;
769 bool Changed = false;
770 // Legalize the operands.
771 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
772 SDOperand Op = Node->getOperand(i);
773 Ops.push_back(LegalizeOp(Op));
774 Changed |= Ops[i] != Op;
775 }
776 if (Changed)
777 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000778 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000779 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000780
Chris Lattner2dce7032005-05-12 23:24:06 +0000781 case ISD::CALLSEQ_START:
782 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000784 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000785 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000786 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000787 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000788
Chris Lattner2dce7032005-05-12 23:24:06 +0000789 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000790 // nodes are treated specially and are mutated in place. This makes the dag
791 // legalization process more efficient and also makes libcall insertion
792 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000793 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000794 case ISD::DYNAMIC_STACKALLOC:
795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
797 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
798 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000799 Tmp3 != Node->getOperand(2)) {
800 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
801 std::vector<SDOperand> Ops;
802 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
803 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
804 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000805 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000806
807 // Since this op produces two values, make sure to remember that we
808 // legalized both of them.
809 AddLegalizedOperand(SDOperand(Node, 0), Result);
810 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
811 return Result.getValue(Op.ResNo);
812
Chris Lattnerd0feb642005-05-13 18:43:43 +0000813 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000814 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000815 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
816 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000817
818 bool Changed = false;
819 std::vector<SDOperand> Ops;
820 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
821 Ops.push_back(LegalizeOp(Node->getOperand(i)));
822 Changed |= Ops.back() != Node->getOperand(i);
823 }
824
825 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000826 std::vector<MVT::ValueType> RetTyVTs;
827 RetTyVTs.reserve(Node->getNumValues());
828 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000829 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000830 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
831 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000832 } else {
833 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000834 }
Chris Lattner9242c502005-01-09 19:43:23 +0000835 // Since calls produce multiple values, make sure to remember that we
836 // legalized all of them.
837 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
838 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
839 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000840 }
Chris Lattner68a12142005-01-07 22:12:08 +0000841 case ISD::BR:
842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
843 if (Tmp1 != Node->getOperand(0))
844 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
845 break;
846
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000847 case ISD::BRCOND:
848 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000849
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000850 switch (getTypeAction(Node->getOperand(1).getValueType())) {
851 case Expand: assert(0 && "It's impossible to expand bools");
852 case Legal:
853 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
854 break;
855 case Promote:
856 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
857 break;
858 }
Nate Begeman371e4952005-08-16 19:49:35 +0000859
860 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
861 default: assert(0 && "This action is not supported yet!");
862 case TargetLowering::Expand:
863 // Expand brcond's setcc into its constituent parts and create a BR_CC
864 // Node.
865 if (Tmp2.getOpcode() == ISD::SETCC) {
866 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
867 Tmp2.getOperand(0), Tmp2.getOperand(1),
868 Node->getOperand(2));
869 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000870 // Make sure the condition is either zero or one. It may have been
871 // promoted from something else.
872 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
873
Nate Begeman371e4952005-08-16 19:49:35 +0000874 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
875 DAG.getCondCode(ISD::SETNE), Tmp2,
876 DAG.getConstant(0, Tmp2.getValueType()),
877 Node->getOperand(2));
878 }
879 break;
880 case TargetLowering::Legal:
881 // Basic block destination (Op#2) is always legal.
882 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
883 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
884 Node->getOperand(2));
885 break;
886 }
887 break;
888 case ISD::BR_CC:
889 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000890 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000891 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
892 Node->getOperand(2), // LHS
893 Node->getOperand(3), // RHS
894 Node->getOperand(1)));
895 // If we get a SETCC back from legalizing the SETCC node we just
896 // created, then use its LHS, RHS, and CC directly in creating a new
897 // node. Otherwise, select between the true and false value based on
898 // comparing the result of the legalized with zero.
899 if (Tmp2.getOpcode() == ISD::SETCC) {
900 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
901 Tmp2.getOperand(0), Tmp2.getOperand(1),
902 Node->getOperand(4));
903 } else {
904 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
905 DAG.getCondCode(ISD::SETNE),
906 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
907 Node->getOperand(4));
908 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000909 break;
910 }
911
912 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
913 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
914
915 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
916 default: assert(0 && "Unexpected action for BR_CC!");
917 case TargetLowering::Custom: {
918 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
919 Tmp2, Tmp3, Node->getOperand(4));
920 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
921 if (Tmp4.Val) {
922 Result = LegalizeOp(Tmp4);
923 break;
924 }
925 } // FALLTHROUGH if the target doesn't want to lower this op after all.
926 case TargetLowering::Legal:
927 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
928 Tmp3 != Node->getOperand(3)) {
929 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
930 Tmp2, Tmp3, Node->getOperand(4));
931 }
932 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000933 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000934 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000935 case ISD::BRCONDTWOWAY:
936 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
937 switch (getTypeAction(Node->getOperand(1).getValueType())) {
938 case Expand: assert(0 && "It's impossible to expand bools");
939 case Legal:
940 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
941 break;
942 case Promote:
943 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
944 break;
945 }
946 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
947 // pair.
948 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
949 case TargetLowering::Promote:
950 default: assert(0 && "This action is not supported yet!");
951 case TargetLowering::Legal:
952 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
953 std::vector<SDOperand> Ops;
954 Ops.push_back(Tmp1);
955 Ops.push_back(Tmp2);
956 Ops.push_back(Node->getOperand(2));
957 Ops.push_back(Node->getOperand(3));
958 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
959 }
960 break;
961 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000962 // If BRTWOWAY_CC is legal for this target, then simply expand this node
963 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
964 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000965 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000966 if (Tmp2.getOpcode() == ISD::SETCC) {
967 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
968 Tmp2.getOperand(0), Tmp2.getOperand(1),
969 Node->getOperand(2), Node->getOperand(3));
970 } else {
971 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
972 DAG.getConstant(0, Tmp2.getValueType()),
973 Node->getOperand(2), Node->getOperand(3));
974 }
975 } else {
976 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000977 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000978 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
979 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000980 break;
981 }
982 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000983 case ISD::BRTWOWAY_CC:
984 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000985 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000986 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
987 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
988 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
989 Tmp3 != Node->getOperand(3)) {
990 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
991 Node->getOperand(4), Node->getOperand(5));
992 }
993 break;
994 } else {
995 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
996 Node->getOperand(2), // LHS
997 Node->getOperand(3), // RHS
998 Node->getOperand(1)));
999 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1000 // pair.
1001 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1002 default: assert(0 && "This action is not supported yet!");
1003 case TargetLowering::Legal:
1004 // If we get a SETCC back from legalizing the SETCC node we just
1005 // created, then use its LHS, RHS, and CC directly in creating a new
1006 // node. Otherwise, select between the true and false value based on
1007 // comparing the result of the legalized with zero.
1008 if (Tmp2.getOpcode() == ISD::SETCC) {
1009 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1010 Tmp2.getOperand(0), Tmp2.getOperand(1),
1011 Node->getOperand(4), Node->getOperand(5));
1012 } else {
1013 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1014 DAG.getConstant(0, Tmp2.getValueType()),
1015 Node->getOperand(4), Node->getOperand(5));
1016 }
1017 break;
1018 case TargetLowering::Expand:
1019 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1020 Node->getOperand(4));
1021 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1022 break;
1023 }
1024 }
1025 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001026 case ISD::LOAD:
1027 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1028 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001029
Chris Lattnerdc750592005-01-07 07:47:09 +00001030 if (Tmp1 != Node->getOperand(0) ||
1031 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +00001032 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1033 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001034 else
1035 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001036
Chris Lattnerea4ca942005-01-07 22:28:47 +00001037 // Since loads produce two values, make sure to remember that we legalized
1038 // both of them.
1039 AddLegalizedOperand(SDOperand(Node, 0), Result);
1040 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1041 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +00001042
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001043 case ISD::EXTLOAD:
1044 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001045 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001046 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1047 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001048
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001049 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001050 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001051 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001052 case TargetLowering::Promote:
1053 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001054 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1055 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001056 // Since loads produce two values, make sure to remember that we legalized
1057 // both of them.
1058 AddLegalizedOperand(SDOperand(Node, 0), Result);
1059 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1060 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001061
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001062 case TargetLowering::Legal:
1063 if (Tmp1 != Node->getOperand(0) ||
1064 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001065 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1066 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001067 else
1068 Result = SDOperand(Node, 0);
1069
1070 // Since loads produce two values, make sure to remember that we legalized
1071 // both of them.
1072 AddLegalizedOperand(SDOperand(Node, 0), Result);
1073 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1074 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001075 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001076 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1077 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1078 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001079 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001080 if (Op.ResNo)
1081 return Load.getValue(1);
1082 return Result;
1083 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001084 assert(Node->getOpcode() != ISD::EXTLOAD &&
1085 "EXTLOAD should always be supported!");
1086 // Turn the unsupported load into an EXTLOAD followed by an explicit
1087 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001088 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1089 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001090 SDOperand ValRes;
1091 if (Node->getOpcode() == ISD::SEXTLOAD)
1092 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001093 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001094 else
1095 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001096 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1097 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1098 if (Op.ResNo)
1099 return Result.getValue(1);
1100 return ValRes;
1101 }
1102 assert(0 && "Unreachable");
1103 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001104 case ISD::EXTRACT_ELEMENT: {
1105 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1106 switch (getTypeAction(OpTy)) {
1107 default:
1108 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1109 break;
1110 case Legal:
1111 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1112 // 1 -> Hi
1113 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1114 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1115 TLI.getShiftAmountTy()));
1116 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1117 } else {
1118 // 0 -> Lo
1119 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1120 Node->getOperand(0));
1121 }
1122 Result = LegalizeOp(Result);
1123 break;
1124 case Expand:
1125 // Get both the low and high parts.
1126 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1127 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1128 Result = Tmp2; // 1 -> Hi
1129 else
1130 Result = Tmp1; // 0 -> Lo
1131 break;
1132 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001133 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001134 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001135
1136 case ISD::CopyToReg:
1137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001138
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001139 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001140 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001141 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001142 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001143 if (Node->getNumOperands() == 3) {
1144 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1145 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1146 Node->getOperand(1), Tmp2);
1147 } else {
1148 assert(Node->getNumOperands() == 4 && "Unknown CopyToReg");
1149 Tmp3 = LegalizeOp(Node->getOperand(3));
1150 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1151 Tmp3 != Node->getOperand(3)) {
1152 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1153 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1154 }
1155
1156 // Since this produces two values, make sure to remember that we legalized
1157 // both of them.
1158 AddLegalizedOperand(SDOperand(Node, 0), Result);
1159 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1160 return Result.getValue(Op.ResNo);
1161 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001162 break;
1163
1164 case ISD::RET:
1165 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1166 switch (Node->getNumOperands()) {
1167 case 2: // ret val
1168 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1169 case Legal:
1170 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001171 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001172 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1173 break;
1174 case Expand: {
1175 SDOperand Lo, Hi;
1176 ExpandOp(Node->getOperand(1), Lo, Hi);
1177 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001178 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001179 }
1180 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001181 Tmp2 = PromoteOp(Node->getOperand(1));
1182 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1183 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001184 }
1185 break;
1186 case 1: // ret void
1187 if (Tmp1 != Node->getOperand(0))
1188 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1189 break;
1190 default: { // ret <values>
1191 std::vector<SDOperand> NewValues;
1192 NewValues.push_back(Tmp1);
1193 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1194 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1195 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001196 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001197 break;
1198 case Expand: {
1199 SDOperand Lo, Hi;
1200 ExpandOp(Node->getOperand(i), Lo, Hi);
1201 NewValues.push_back(Lo);
1202 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001203 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001204 }
1205 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001206 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001207 }
1208 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1209 break;
1210 }
1211 }
1212 break;
1213 case ISD::STORE:
1214 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1215 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1216
Chris Lattnere69daaf2005-01-08 06:25:56 +00001217 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001218 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001219 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001220 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001221 DAG.getConstant(FloatToBits(CFP->getValue()),
1222 MVT::i32),
1223 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001224 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001225 } else {
1226 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001227 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001228 DAG.getConstant(DoubleToBits(CFP->getValue()),
1229 MVT::i64),
1230 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001231 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001232 }
Chris Lattnera4743132005-02-22 07:23:39 +00001233 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001234 }
1235
Chris Lattnerdc750592005-01-07 07:47:09 +00001236 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1237 case Legal: {
1238 SDOperand Val = LegalizeOp(Node->getOperand(1));
1239 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1240 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001241 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1242 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001243 break;
1244 }
1245 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001246 // Truncate the value and store the result.
1247 Tmp3 = PromoteOp(Node->getOperand(1));
1248 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001249 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001250 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001251 break;
1252
Chris Lattnerdc750592005-01-07 07:47:09 +00001253 case Expand:
1254 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001255 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001256 ExpandOp(Node->getOperand(1), Lo, Hi);
1257
1258 if (!TLI.isLittleEndian())
1259 std::swap(Lo, Hi);
1260
Chris Lattner55e9cde2005-05-11 04:51:16 +00001261 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1262 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001263 // If this is a vector type, then we have to calculate the increment as
1264 // the product of the element size in bytes, and the number of elements
1265 // in the high half of the vector.
1266 if (MVT::Vector == Hi.getValueType()) {
1267 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1268 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1269 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1270 } else {
1271 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1272 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001273 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1274 getIntPtrConstant(IncrementSize));
1275 assert(isTypeLegal(Tmp2.getValueType()) &&
1276 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001277 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001278 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1279 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001280 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1281 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001282 }
1283 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001284 case ISD::PCMARKER:
1285 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001286 if (Tmp1 != Node->getOperand(0))
1287 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001288 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001289 case ISD::READCYCLECOUNTER:
1290 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001291 if (Tmp1 != Node->getOperand(0)) {
1292 std::vector<MVT::ValueType> rtypes;
1293 std::vector<SDOperand> rvals;
1294 rtypes.push_back(MVT::i64);
1295 rtypes.push_back(MVT::Other);
1296 rvals.push_back(Tmp1);
1297 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1298 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001299
1300 // Since rdcc produce two values, make sure to remember that we legalized
1301 // both of them.
1302 AddLegalizedOperand(SDOperand(Node, 0), Result);
1303 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1304 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001305
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001306 case ISD::TRUNCSTORE:
1307 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1308 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1309
1310 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1311 case Legal:
1312 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001313
1314 // The only promote case we handle is TRUNCSTORE:i1 X into
1315 // -> TRUNCSTORE:i8 (and X, 1)
1316 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1317 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1318 TargetLowering::Promote) {
1319 // Promote the bool to a mask then store.
1320 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1321 DAG.getConstant(1, Tmp2.getValueType()));
1322 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1323 Node->getOperand(3), DAG.getValueType(MVT::i8));
1324
1325 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1326 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001327 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001328 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001329 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001330 break;
1331 case Promote:
1332 case Expand:
1333 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1334 }
1335 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001336 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001337 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1338 case Expand: assert(0 && "It's impossible to expand bools");
1339 case Legal:
1340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1341 break;
1342 case Promote:
1343 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1344 break;
1345 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001346 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001347 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001348
Nate Begeman987121a2005-08-23 04:29:48 +00001349 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001350 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001351 case TargetLowering::Expand:
1352 if (Tmp1.getOpcode() == ISD::SETCC) {
1353 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1354 Tmp2, Tmp3,
1355 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1356 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001357 // Make sure the condition is either zero or one. It may have been
1358 // promoted from something else.
1359 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001360 Result = DAG.getSelectCC(Tmp1,
1361 DAG.getConstant(0, Tmp1.getValueType()),
1362 Tmp2, Tmp3, ISD::SETNE);
1363 }
1364 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001365 case TargetLowering::Custom: {
1366 SDOperand Tmp =
1367 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1368 Tmp1, Tmp2, Tmp3), DAG);
1369 if (Tmp.Val) {
1370 Result = LegalizeOp(Tmp);
1371 break;
1372 }
1373 // FALLTHROUGH if the target thinks it is legal.
1374 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001375 case TargetLowering::Legal:
1376 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1377 Tmp3 != Node->getOperand(2))
1378 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1379 Tmp1, Tmp2, Tmp3);
1380 break;
1381 case TargetLowering::Promote: {
1382 MVT::ValueType NVT =
1383 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1384 unsigned ExtOp, TruncOp;
1385 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001386 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001387 TruncOp = ISD::TRUNCATE;
1388 } else {
1389 ExtOp = ISD::FP_EXTEND;
1390 TruncOp = ISD::FP_ROUND;
1391 }
1392 // Promote each of the values to the new type.
1393 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1394 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1395 // Perform the larger operation, then round down.
1396 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1397 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1398 break;
1399 }
1400 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001401 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001402 case ISD::SELECT_CC:
1403 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1404 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1405
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001406 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001407 // Everything is legal, see if we should expand this op or something.
1408 switch (TLI.getOperationAction(ISD::SELECT_CC,
1409 Node->getOperand(0).getValueType())) {
1410 default: assert(0 && "This action is not supported yet!");
1411 case TargetLowering::Custom: {
1412 SDOperand Tmp =
1413 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1414 Node->getOperand(0),
1415 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001416 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001417 if (Tmp.Val) {
1418 Result = LegalizeOp(Tmp);
1419 break;
1420 }
1421 } // FALLTHROUGH if the target can't lower this operation after all.
1422 case TargetLowering::Legal:
1423 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1424 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1425 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1426 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1427 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1428 Tmp3, Tmp4, Node->getOperand(4));
1429 }
1430 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001431 }
1432 break;
1433 } else {
1434 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1435 Node->getOperand(0), // LHS
1436 Node->getOperand(1), // RHS
1437 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001438 // If we get a SETCC back from legalizing the SETCC node we just
1439 // created, then use its LHS, RHS, and CC directly in creating a new
1440 // node. Otherwise, select between the true and false value based on
1441 // comparing the result of the legalized with zero.
1442 if (Tmp1.getOpcode() == ISD::SETCC) {
1443 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1444 Tmp1.getOperand(0), Tmp1.getOperand(1),
1445 Tmp3, Tmp4, Tmp1.getOperand(2));
1446 } else {
1447 Result = DAG.getSelectCC(Tmp1,
1448 DAG.getConstant(0, Tmp1.getValueType()),
1449 Tmp3, Tmp4, ISD::SETNE);
1450 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001451 }
1452 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001453 case ISD::SETCC:
1454 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1455 case Legal:
1456 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1457 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001458 break;
1459 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001460 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1461 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1462
1463 // If this is an FP compare, the operands have already been extended.
1464 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1465 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001466 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001467
1468 // Otherwise, we have to insert explicit sign or zero extends. Note
1469 // that we could insert sign extends for ALL conditions, but zero extend
1470 // is cheaper on many machines (an AND instead of two shifts), so prefer
1471 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001472 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001473 default: assert(0 && "Unknown integer comparison!");
1474 case ISD::SETEQ:
1475 case ISD::SETNE:
1476 case ISD::SETUGE:
1477 case ISD::SETUGT:
1478 case ISD::SETULE:
1479 case ISD::SETULT:
1480 // ALL of these operations will work if we either sign or zero extend
1481 // the operands (including the unsigned comparisons!). Zero extend is
1482 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001483 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1484 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001485 break;
1486 case ISD::SETGE:
1487 case ISD::SETGT:
1488 case ISD::SETLT:
1489 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001490 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1491 DAG.getValueType(VT));
1492 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1493 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001494 break;
1495 }
Chris Lattner4d978642005-01-15 22:16:26 +00001496 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001497 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001498 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001499 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1500 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1501 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001502 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001503 case ISD::SETEQ:
1504 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001505 if (RHSLo == RHSHi)
1506 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1507 if (RHSCST->isAllOnesValue()) {
1508 // Comparison to -1.
1509 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001510 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001511 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001512 }
1513
Chris Lattnerdc750592005-01-07 07:47:09 +00001514 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1515 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1516 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001517 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001518 break;
1519 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001520 // If this is a comparison of the sign bit, just look at the top part.
1521 // X > -1, x < 0
1522 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001523 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001524 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001525 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001526 (CST->isAllOnesValue()))) { // X > -1
1527 Tmp1 = LHSHi;
1528 Tmp2 = RHSHi;
1529 break;
1530 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001531
Chris Lattnerdc750592005-01-07 07:47:09 +00001532 // FIXME: This generated code sucks.
1533 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001534 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001535 default: assert(0 && "Unknown integer setcc!");
1536 case ISD::SETLT:
1537 case ISD::SETULT: LowCC = ISD::SETULT; break;
1538 case ISD::SETGT:
1539 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1540 case ISD::SETLE:
1541 case ISD::SETULE: LowCC = ISD::SETULE; break;
1542 case ISD::SETGE:
1543 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1544 }
Misha Brukman835702a2005-04-21 22:36:52 +00001545
Chris Lattnerdc750592005-01-07 07:47:09 +00001546 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1547 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1548 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1549
1550 // NOTE: on targets without efficient SELECT of bools, we can always use
1551 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001552 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1553 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1554 Node->getOperand(2));
1555 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001556 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1557 Result, Tmp1, Tmp2));
1558 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001559 }
1560 }
Nate Begeman987121a2005-08-23 04:29:48 +00001561
1562 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1563 default:
1564 assert(0 && "Cannot handle this action for SETCC yet!");
1565 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001566 case TargetLowering::Promote: {
1567 // First step, figure out the appropriate operation to use.
1568 // Allow SETCC to not be supported for all legal data types
1569 // Mostly this targets FP
1570 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1571 MVT::ValueType OldVT = NewInTy;
1572
1573 // Scan for the appropriate larger type to use.
1574 while (1) {
1575 NewInTy = (MVT::ValueType)(NewInTy+1);
1576
1577 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1578 "Fell off of the edge of the integer world");
1579 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1580 "Fell off of the edge of the floating point world");
1581
1582 // If the target supports SETCC of this type, use it.
1583 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1584 break;
1585 }
1586 if (MVT::isInteger(NewInTy))
1587 assert(0 && "Cannot promote Legal Integer SETCC yet");
1588 else {
1589 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1590 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1591 }
1592
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001593 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1594 Node->getOperand(2));
1595 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001596 }
Nate Begeman987121a2005-08-23 04:29:48 +00001597 case TargetLowering::Legal:
1598 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1599 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1600 Node->getOperand(2));
1601 break;
1602 case TargetLowering::Expand:
1603 // Expand a setcc node into a select_cc of the same condition, lhs, and
1604 // rhs that selects between const 1 (true) and const 0 (false).
1605 MVT::ValueType VT = Node->getValueType(0);
1606 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1607 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1608 Node->getOperand(2));
1609 Result = LegalizeOp(Result);
1610 break;
1611 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001612 break;
1613
Chris Lattner85d70c62005-01-11 05:57:22 +00001614 case ISD::MEMSET:
1615 case ISD::MEMCPY:
1616 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001617 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001618 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1619
1620 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1621 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1622 case Expand: assert(0 && "Cannot expand a byte!");
1623 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001624 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001625 break;
1626 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001627 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001628 break;
1629 }
1630 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001631 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001632 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001633
1634 SDOperand Tmp4;
1635 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001636 case Expand: {
1637 // Length is too big, just take the lo-part of the length.
1638 SDOperand HiPart;
1639 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1640 break;
1641 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001642 case Legal:
1643 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001644 break;
1645 case Promote:
1646 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001647 break;
1648 }
1649
1650 SDOperand Tmp5;
1651 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1652 case Expand: assert(0 && "Cannot expand this yet!");
1653 case Legal:
1654 Tmp5 = LegalizeOp(Node->getOperand(4));
1655 break;
1656 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001657 Tmp5 = PromoteOp(Node->getOperand(4));
1658 break;
1659 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001660
1661 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1662 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001663 case TargetLowering::Custom: {
1664 SDOperand Tmp =
1665 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1666 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1667 if (Tmp.Val) {
1668 Result = LegalizeOp(Tmp);
1669 break;
1670 }
1671 // FALLTHROUGH if the target thinks it is legal.
1672 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001673 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001674 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1675 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1676 Tmp5 != Node->getOperand(4)) {
1677 std::vector<SDOperand> Ops;
1678 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1679 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1680 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1681 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001682 break;
1683 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001684 // Otherwise, the target does not support this operation. Lower the
1685 // operation to an explicit libcall as appropriate.
1686 MVT::ValueType IntPtr = TLI.getPointerTy();
1687 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1688 std::vector<std::pair<SDOperand, const Type*> > Args;
1689
Reid Spencer6dced922005-01-12 14:53:45 +00001690 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001691 if (Node->getOpcode() == ISD::MEMSET) {
1692 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1693 // Extend the ubyte argument to be an int value for the call.
1694 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1695 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1696 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1697
1698 FnName = "memset";
1699 } else if (Node->getOpcode() == ISD::MEMCPY ||
1700 Node->getOpcode() == ISD::MEMMOVE) {
1701 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1702 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1703 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1704 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1705 } else {
1706 assert(0 && "Unknown op!");
1707 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001708
Chris Lattner85d70c62005-01-11 05:57:22 +00001709 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001710 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001711 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001712 Result = CallResult.second;
1713 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001714 break;
1715 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001716 }
1717 break;
1718 }
Chris Lattner5385db52005-05-09 20:23:03 +00001719
1720 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001721 Tmp1 = LegalizeOp(Node->getOperand(0));
1722 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001723
Chris Lattner86535992005-05-14 07:45:46 +00001724 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1725 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1726 std::vector<SDOperand> Ops;
1727 Ops.push_back(Tmp1);
1728 Ops.push_back(Tmp2);
1729 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1730 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001731 Result = SDOperand(Node, 0);
1732 // Since these produce two values, make sure to remember that we legalized
1733 // both of them.
1734 AddLegalizedOperand(SDOperand(Node, 0), Result);
1735 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1736 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001737 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001738 Tmp1 = LegalizeOp(Node->getOperand(0));
1739 Tmp2 = LegalizeOp(Node->getOperand(1));
1740 Tmp3 = LegalizeOp(Node->getOperand(2));
1741 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1742 Tmp3 != Node->getOperand(2))
1743 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1744 break;
1745
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001746 case ISD::READIO:
1747 Tmp1 = LegalizeOp(Node->getOperand(0));
1748 Tmp2 = LegalizeOp(Node->getOperand(1));
1749
1750 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1751 case TargetLowering::Custom:
1752 default: assert(0 && "This action not implemented for this operation!");
1753 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001754 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1755 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1756 std::vector<SDOperand> Ops;
1757 Ops.push_back(Tmp1);
1758 Ops.push_back(Tmp2);
1759 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1760 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001761 Result = SDOperand(Node, 0);
1762 break;
1763 case TargetLowering::Expand:
1764 // Replace this with a load from memory.
1765 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1766 Node->getOperand(1), DAG.getSrcValue(NULL));
1767 Result = LegalizeOp(Result);
1768 break;
1769 }
1770
1771 // Since these produce two values, make sure to remember that we legalized
1772 // both of them.
1773 AddLegalizedOperand(SDOperand(Node, 0), Result);
1774 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1775 return Result.getValue(Op.ResNo);
1776
1777 case ISD::WRITEIO:
1778 Tmp1 = LegalizeOp(Node->getOperand(0));
1779 Tmp2 = LegalizeOp(Node->getOperand(1));
1780 Tmp3 = LegalizeOp(Node->getOperand(2));
1781
1782 switch (TLI.getOperationAction(Node->getOpcode(),
1783 Node->getOperand(1).getValueType())) {
1784 case TargetLowering::Custom:
1785 default: assert(0 && "This action not implemented for this operation!");
1786 case TargetLowering::Legal:
1787 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1788 Tmp3 != Node->getOperand(2))
1789 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1790 break;
1791 case TargetLowering::Expand:
1792 // Replace this with a store to memory.
1793 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1794 Node->getOperand(1), Node->getOperand(2),
1795 DAG.getSrcValue(NULL));
1796 Result = LegalizeOp(Result);
1797 break;
1798 }
1799 break;
1800
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001801 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001802 case ISD::SUB_PARTS:
1803 case ISD::SHL_PARTS:
1804 case ISD::SRA_PARTS:
1805 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001806 std::vector<SDOperand> Ops;
1807 bool Changed = false;
1808 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1809 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1810 Changed |= Ops.back() != Node->getOperand(i);
1811 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001812 if (Changed) {
1813 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1814 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1815 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001816
1817 // Since these produce multiple values, make sure to remember that we
1818 // legalized all of them.
1819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1820 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1821 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001822 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001823
1824 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001825 case ISD::ADD:
1826 case ISD::SUB:
1827 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001828 case ISD::MULHS:
1829 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001830 case ISD::UDIV:
1831 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001832 case ISD::AND:
1833 case ISD::OR:
1834 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001835 case ISD::SHL:
1836 case ISD::SRL:
1837 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001838 case ISD::FADD:
1839 case ISD::FSUB:
1840 case ISD::FMUL:
1841 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001842 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001843 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1844 case Expand: assert(0 && "Not possible");
1845 case Legal:
1846 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1847 break;
1848 case Promote:
1849 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1850 break;
1851 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001852 if (Tmp1 != Node->getOperand(0) ||
1853 Tmp2 != Node->getOperand(1))
1854 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1855 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001856
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001857 case ISD::BUILD_PAIR: {
1858 MVT::ValueType PairTy = Node->getValueType(0);
1859 // TODO: handle the case where the Lo and Hi operands are not of legal type
1860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1861 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1862 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1863 case TargetLowering::Legal:
1864 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1865 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1866 break;
1867 case TargetLowering::Promote:
1868 case TargetLowering::Custom:
1869 assert(0 && "Cannot promote/custom this yet!");
1870 case TargetLowering::Expand:
1871 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1872 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1873 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1874 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1875 TLI.getShiftAmountTy()));
1876 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1877 break;
1878 }
1879 break;
1880 }
1881
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001882 case ISD::UREM:
1883 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001884 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001885 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1886 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1887 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1888 case TargetLowering::Legal:
1889 if (Tmp1 != Node->getOperand(0) ||
1890 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001891 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001892 Tmp2);
1893 break;
1894 case TargetLowering::Promote:
1895 case TargetLowering::Custom:
1896 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001897 case TargetLowering::Expand:
1898 if (MVT::isInteger(Node->getValueType(0))) {
1899 MVT::ValueType VT = Node->getValueType(0);
1900 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1901 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1902 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1903 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1904 } else {
1905 // Floating point mod -> fmod libcall.
1906 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1907 SDOperand Dummy;
1908 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001909 }
1910 break;
1911 }
1912 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001913
Andrew Lenharth5e177822005-05-03 17:19:30 +00001914 case ISD::CTPOP:
1915 case ISD::CTTZ:
1916 case ISD::CTLZ:
1917 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1918 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1919 case TargetLowering::Legal:
1920 if (Tmp1 != Node->getOperand(0))
1921 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1922 break;
1923 case TargetLowering::Promote: {
1924 MVT::ValueType OVT = Tmp1.getValueType();
1925 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001926
1927 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001928 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1929 // Perform the larger operation, then subtract if needed.
1930 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1931 switch(Node->getOpcode())
1932 {
1933 case ISD::CTPOP:
1934 Result = Tmp1;
1935 break;
1936 case ISD::CTTZ:
1937 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001938 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1939 DAG.getConstant(getSizeInBits(NVT), NVT),
1940 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001941 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001942 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1943 break;
1944 case ISD::CTLZ:
1945 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001946 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1947 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001948 getSizeInBits(OVT), NVT));
1949 break;
1950 }
1951 break;
1952 }
1953 case TargetLowering::Custom:
1954 assert(0 && "Cannot custom handle this yet!");
1955 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001956 switch(Node->getOpcode())
1957 {
1958 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001959 static const uint64_t mask[6] = {
1960 0x5555555555555555ULL, 0x3333333333333333ULL,
1961 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1962 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1963 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001964 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001965 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1966 unsigned len = getSizeInBits(VT);
1967 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001968 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001969 Tmp2 = DAG.getConstant(mask[i], VT);
1970 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001971 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001972 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1973 DAG.getNode(ISD::AND, VT,
1974 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1975 Tmp2));
1976 }
1977 Result = Tmp1;
1978 break;
1979 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001980 case ISD::CTLZ: {
1981 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001982 x = x | (x >> 1);
1983 x = x | (x >> 2);
1984 ...
1985 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001986 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001987 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001988
Chris Lattner56add052005-05-11 18:35:21 +00001989 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1990 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001991 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1992 unsigned len = getSizeInBits(VT);
1993 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1994 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001995 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001996 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1997 }
1998 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001999 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002000 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002001 }
2002 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002003 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002004 // unless the target has ctlz but not ctpop, in which case we use:
2005 // { return 32 - nlz(~x & (x-1)); }
2006 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002007 MVT::ValueType VT = Tmp1.getValueType();
2008 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002009 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002010 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2011 DAG.getNode(ISD::SUB, VT, Tmp1,
2012 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002013 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002014 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2015 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002016 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002017 DAG.getConstant(getSizeInBits(VT), VT),
2018 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2019 } else {
2020 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2021 }
Chris Lattner72473242005-05-11 05:27:09 +00002022 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002023 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002024 default:
2025 assert(0 && "Cannot expand this yet!");
2026 break;
2027 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002028 break;
2029 }
2030 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002031
Chris Lattner13fe99c2005-04-02 05:00:07 +00002032 // Unary operators
2033 case ISD::FABS:
2034 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002035 case ISD::FSQRT:
2036 case ISD::FSIN:
2037 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002038 Tmp1 = LegalizeOp(Node->getOperand(0));
2039 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2040 case TargetLowering::Legal:
2041 if (Tmp1 != Node->getOperand(0))
2042 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2043 break;
2044 case TargetLowering::Promote:
2045 case TargetLowering::Custom:
2046 assert(0 && "Cannot promote/custom handle this yet!");
2047 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002048 switch(Node->getOpcode()) {
2049 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002050 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2051 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002052 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002053 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002054 break;
2055 }
2056 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002057 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2058 MVT::ValueType VT = Node->getValueType(0);
2059 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002060 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002061 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2062 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2063 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002064 break;
2065 }
2066 case ISD::FSQRT:
2067 case ISD::FSIN:
2068 case ISD::FCOS: {
2069 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002070 const char *FnName = 0;
2071 switch(Node->getOpcode()) {
2072 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2073 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2074 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2075 default: assert(0 && "Unreachable!");
2076 }
Nate Begeman77558da2005-08-04 21:43:28 +00002077 SDOperand Dummy;
2078 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002079 break;
2080 }
2081 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002082 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002083 }
2084 break;
2085 }
2086 break;
2087
2088 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002089 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002090 case ISD::UINT_TO_FP: {
2091 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002092 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2093 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002094 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002095 Node->getOperand(0).getValueType())) {
2096 default: assert(0 && "Unknown operation action!");
2097 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002098 Result = ExpandLegalINT_TO_FP(isSigned,
2099 LegalizeOp(Node->getOperand(0)),
2100 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002101 AddLegalizedOperand(Op, Result);
2102 return Result;
2103 case TargetLowering::Promote:
2104 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2105 Node->getValueType(0),
2106 isSigned);
2107 AddLegalizedOperand(Op, Result);
2108 return Result;
2109 case TargetLowering::Legal:
2110 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002111 case TargetLowering::Custom: {
2112 Tmp1 = LegalizeOp(Node->getOperand(0));
2113 SDOperand Tmp =
2114 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2115 Tmp = TLI.LowerOperation(Tmp, DAG);
2116 if (Tmp.Val) {
2117 AddLegalizedOperand(Op, Tmp);
2118 NeedsAnotherIteration = true;
2119 return Tmp;
2120 } else {
2121 assert(0 && "Target Must Lower this");
2122 }
2123 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002124 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002125
Chris Lattnerdc750592005-01-07 07:47:09 +00002126 Tmp1 = LegalizeOp(Node->getOperand(0));
2127 if (Tmp1 != Node->getOperand(0))
2128 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2129 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002130 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002131 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2132 Node->getValueType(0), Node->getOperand(0));
2133 break;
2134 case Promote:
2135 if (isSigned) {
2136 Result = PromoteOp(Node->getOperand(0));
2137 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2138 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2139 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2140 } else {
2141 Result = PromoteOp(Node->getOperand(0));
2142 Result = DAG.getZeroExtendInReg(Result,
2143 Node->getOperand(0).getValueType());
2144 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002145 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002146 break;
2147 }
2148 break;
2149 }
2150 case ISD::TRUNCATE:
2151 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2152 case Legal:
2153 Tmp1 = LegalizeOp(Node->getOperand(0));
2154 if (Tmp1 != Node->getOperand(0))
2155 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2156 break;
2157 case Expand:
2158 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2159
2160 // Since the result is legal, we should just be able to truncate the low
2161 // part of the source.
2162 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2163 break;
2164 case Promote:
2165 Result = PromoteOp(Node->getOperand(0));
2166 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2167 break;
2168 }
2169 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002170
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002171 case ISD::FP_TO_SINT:
2172 case ISD::FP_TO_UINT:
2173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2174 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002175 Tmp1 = LegalizeOp(Node->getOperand(0));
2176
Chris Lattner44fe26f2005-07-29 00:11:56 +00002177 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2178 default: assert(0 && "Unknown operation action!");
2179 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002180 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2181 SDOperand True, False;
2182 MVT::ValueType VT = Node->getOperand(0).getValueType();
2183 MVT::ValueType NVT = Node->getValueType(0);
2184 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2185 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2186 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2187 Node->getOperand(0), Tmp2, ISD::SETLT);
2188 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2189 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002190 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002191 Tmp2));
2192 False = DAG.getNode(ISD::XOR, NVT, False,
2193 DAG.getConstant(1ULL << ShiftAmt, NVT));
2194 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002195 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002196 } else {
2197 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2198 }
2199 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002200 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002201 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002202 Node->getOpcode() == ISD::FP_TO_SINT);
2203 AddLegalizedOperand(Op, Result);
2204 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002205 case TargetLowering::Custom: {
2206 SDOperand Tmp =
2207 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2208 Tmp = TLI.LowerOperation(Tmp, DAG);
2209 if (Tmp.Val) {
2210 AddLegalizedOperand(Op, Tmp);
2211 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002212 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002213 } else {
2214 // The target thinks this is legal afterall.
2215 break;
2216 }
2217 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002218 case TargetLowering::Legal:
2219 break;
2220 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002221
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002222 if (Tmp1 != Node->getOperand(0))
2223 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2224 break;
2225 case Expand:
2226 assert(0 && "Shouldn't need to expand other operators here!");
2227 case Promote:
2228 Result = PromoteOp(Node->getOperand(0));
2229 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2230 break;
2231 }
2232 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002233
Chris Lattner7753f172005-09-02 00:18:10 +00002234 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002235 case ISD::ZERO_EXTEND:
2236 case ISD::SIGN_EXTEND:
2237 case ISD::FP_EXTEND:
2238 case ISD::FP_ROUND:
2239 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2240 case Legal:
2241 Tmp1 = LegalizeOp(Node->getOperand(0));
2242 if (Tmp1 != Node->getOperand(0))
2243 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2244 break;
2245 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002246 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002247
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002248 case Promote:
2249 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002250 case ISD::ANY_EXTEND:
2251 Result = PromoteOp(Node->getOperand(0));
2252 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2253 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002254 case ISD::ZERO_EXTEND:
2255 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002256 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002257 Result = DAG.getZeroExtendInReg(Result,
2258 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002259 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002260 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002261 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002262 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002263 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002264 Result,
2265 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002266 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002267 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002268 Result = PromoteOp(Node->getOperand(0));
2269 if (Result.getValueType() != Op.getValueType())
2270 // Dynamically dead while we have only 2 FP types.
2271 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2272 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002273 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002274 Result = PromoteOp(Node->getOperand(0));
2275 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2276 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002277 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002278 }
2279 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002280 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002281 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002282 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002283 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002284
2285 // If this operation is not supported, convert it to a shl/shr or load/store
2286 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002287 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2288 default: assert(0 && "This action not supported for this op yet!");
2289 case TargetLowering::Legal:
2290 if (Tmp1 != Node->getOperand(0))
2291 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002292 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002293 break;
2294 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002295 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002296 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002297 // NOTE: we could fall back on load/store here too for targets without
2298 // SAR. However, it is doubtful that any exist.
2299 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2300 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002301 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002302 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2303 Node->getOperand(0), ShiftCst);
2304 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2305 Result, ShiftCst);
2306 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2307 // The only way we can lower this is to turn it into a STORETRUNC,
2308 // EXTLOAD pair, targetting a temporary location (a stack slot).
2309
2310 // NOTE: there is a choice here between constantly creating new stack
2311 // slots and always reusing the same one. We currently always create
2312 // new ones, as reuse may inhibit scheduling.
2313 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2314 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2315 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2316 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002317 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002318 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2319 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2320 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002321 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002322 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002323 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2324 Result, StackSlot, DAG.getSrcValue(NULL),
2325 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002326 } else {
2327 assert(0 && "Unknown op");
2328 }
2329 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002330 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002331 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002332 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002333 }
Chris Lattner99222f72005-01-15 07:15:18 +00002334 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002335
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002336 // Note that LegalizeOp may be reentered even from single-use nodes, which
2337 // means that we always must cache transformed nodes.
2338 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002339 return Result;
2340}
2341
Chris Lattner4d978642005-01-15 22:16:26 +00002342/// PromoteOp - Given an operation that produces a value in an invalid type,
2343/// promote it to compute the value into a larger type. The produced value will
2344/// have the correct bits for the low portion of the register, but no guarantee
2345/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002346SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2347 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002348 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002349 assert(getTypeAction(VT) == Promote &&
2350 "Caller should expand or legalize operands that are not promotable!");
2351 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2352 "Cannot promote to smaller type!");
2353
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002354 SDOperand Tmp1, Tmp2, Tmp3;
2355
2356 SDOperand Result;
2357 SDNode *Node = Op.Val;
2358
Chris Lattner1a570f12005-09-02 20:32:45 +00002359 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2360 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002361
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002362 // Promotion needs an optimization step to clean up after it, and is not
2363 // careful to avoid operations the target does not support. Make sure that
2364 // all generated operations are legalized in the next iteration.
2365 NeedsAnotherIteration = true;
2366
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002367 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002368 case ISD::CopyFromReg:
2369 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002370 default:
2371 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2372 assert(0 && "Do not know how to promote this operator!");
2373 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002374 case ISD::UNDEF:
2375 Result = DAG.getNode(ISD::UNDEF, NVT);
2376 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002377 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002378 if (VT != MVT::i1)
2379 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2380 else
2381 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002382 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2383 break;
2384 case ISD::ConstantFP:
2385 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2386 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2387 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002388
Chris Lattner2cb338d2005-01-18 02:59:52 +00002389 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002390 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002391 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2392 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002393 Result = LegalizeOp(Result);
2394 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002395
2396 case ISD::TRUNCATE:
2397 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2398 case Legal:
2399 Result = LegalizeOp(Node->getOperand(0));
2400 assert(Result.getValueType() >= NVT &&
2401 "This truncation doesn't make sense!");
2402 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2403 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2404 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002405 case Promote:
2406 // The truncation is not required, because we don't guarantee anything
2407 // about high bits anyway.
2408 Result = PromoteOp(Node->getOperand(0));
2409 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002410 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002411 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2412 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002413 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002414 }
2415 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002416 case ISD::SIGN_EXTEND:
2417 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002418 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002419 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2420 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2421 case Legal:
2422 // Input is legal? Just do extend all the way to the larger type.
2423 Result = LegalizeOp(Node->getOperand(0));
2424 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2425 break;
2426 case Promote:
2427 // Promote the reg if it's smaller.
2428 Result = PromoteOp(Node->getOperand(0));
2429 // The high bits are not guaranteed to be anything. Insert an extend.
2430 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002431 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002432 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002433 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002434 Result = DAG.getZeroExtendInReg(Result,
2435 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002436 break;
2437 }
2438 break;
2439
2440 case ISD::FP_EXTEND:
2441 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2442 case ISD::FP_ROUND:
2443 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2444 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2445 case Promote: assert(0 && "Unreachable with 2 FP types!");
2446 case Legal:
2447 // Input is legal? Do an FP_ROUND_INREG.
2448 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002449 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2450 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002451 break;
2452 }
2453 break;
2454
2455 case ISD::SINT_TO_FP:
2456 case ISD::UINT_TO_FP:
2457 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2458 case Legal:
2459 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002460 // No extra round required here.
2461 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002462 break;
2463
2464 case Promote:
2465 Result = PromoteOp(Node->getOperand(0));
2466 if (Node->getOpcode() == ISD::SINT_TO_FP)
2467 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002468 Result,
2469 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002470 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002471 Result = DAG.getZeroExtendInReg(Result,
2472 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002473 // No extra round required here.
2474 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002475 break;
2476 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002477 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2478 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002479 // Round if we cannot tolerate excess precision.
2480 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002481 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2482 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002483 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002484 }
Chris Lattner4d978642005-01-15 22:16:26 +00002485 break;
2486
Chris Lattner268d4572005-12-09 17:32:47 +00002487 case ISD::SIGN_EXTEND_INREG:
2488 Result = PromoteOp(Node->getOperand(0));
2489 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2490 Node->getOperand(1));
2491 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002492 case ISD::FP_TO_SINT:
2493 case ISD::FP_TO_UINT:
2494 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2495 case Legal:
2496 Tmp1 = LegalizeOp(Node->getOperand(0));
2497 break;
2498 case Promote:
2499 // The input result is prerounded, so we don't have to do anything
2500 // special.
2501 Tmp1 = PromoteOp(Node->getOperand(0));
2502 break;
2503 case Expand:
2504 assert(0 && "not implemented");
2505 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002506 // If we're promoting a UINT to a larger size, check to see if the new node
2507 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2508 // we can use that instead. This allows us to generate better code for
2509 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2510 // legal, such as PowerPC.
2511 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002512 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002513 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2514 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002515 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2516 } else {
2517 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2518 }
Chris Lattner4d978642005-01-15 22:16:26 +00002519 break;
2520
Chris Lattner13fe99c2005-04-02 05:00:07 +00002521 case ISD::FABS:
2522 case ISD::FNEG:
2523 Tmp1 = PromoteOp(Node->getOperand(0));
2524 assert(Tmp1.getValueType() == NVT);
2525 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2526 // NOTE: we do not have to do any extra rounding here for
2527 // NoExcessFPPrecision, because we know the input will have the appropriate
2528 // precision, and these operations don't modify precision at all.
2529 break;
2530
Chris Lattner9d6fa982005-04-28 21:44:33 +00002531 case ISD::FSQRT:
2532 case ISD::FSIN:
2533 case ISD::FCOS:
2534 Tmp1 = PromoteOp(Node->getOperand(0));
2535 assert(Tmp1.getValueType() == NVT);
2536 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2537 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002538 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2539 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002540 break;
2541
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002542 case ISD::AND:
2543 case ISD::OR:
2544 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002545 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002546 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002547 case ISD::MUL:
2548 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002549 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002550 // that too is okay if they are integer operations.
2551 Tmp1 = PromoteOp(Node->getOperand(0));
2552 Tmp2 = PromoteOp(Node->getOperand(1));
2553 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2554 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002555 break;
2556 case ISD::FADD:
2557 case ISD::FSUB:
2558 case ISD::FMUL:
2559 // The input may have strange things in the top bits of the registers, but
2560 // these operations don't care.
2561 Tmp1 = PromoteOp(Node->getOperand(0));
2562 Tmp2 = PromoteOp(Node->getOperand(1));
2563 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2564 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2565
2566 // Floating point operations will give excess precision that we may not be
2567 // able to tolerate. If we DO allow excess precision, just leave it,
2568 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002569 // FIXME: Why would we need to round FP ops more than integer ones?
2570 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002571 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002572 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2573 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002574 break;
2575
Chris Lattner4d978642005-01-15 22:16:26 +00002576 case ISD::SDIV:
2577 case ISD::SREM:
2578 // These operators require that their input be sign extended.
2579 Tmp1 = PromoteOp(Node->getOperand(0));
2580 Tmp2 = PromoteOp(Node->getOperand(1));
2581 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002582 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2583 DAG.getValueType(VT));
2584 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2585 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002586 }
2587 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2588
2589 // Perform FP_ROUND: this is probably overly pessimistic.
2590 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002591 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2592 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002593 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002594 case ISD::FDIV:
2595 case ISD::FREM:
2596 // These operators require that their input be fp extended.
2597 Tmp1 = PromoteOp(Node->getOperand(0));
2598 Tmp2 = PromoteOp(Node->getOperand(1));
2599 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2600
2601 // Perform FP_ROUND: this is probably overly pessimistic.
2602 if (NoExcessFPPrecision)
2603 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2604 DAG.getValueType(VT));
2605 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002606
2607 case ISD::UDIV:
2608 case ISD::UREM:
2609 // These operators require that their input be zero extended.
2610 Tmp1 = PromoteOp(Node->getOperand(0));
2611 Tmp2 = PromoteOp(Node->getOperand(1));
2612 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002613 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2614 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002615 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2616 break;
2617
2618 case ISD::SHL:
2619 Tmp1 = PromoteOp(Node->getOperand(0));
2620 Tmp2 = LegalizeOp(Node->getOperand(1));
2621 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2622 break;
2623 case ISD::SRA:
2624 // The input value must be properly sign extended.
2625 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002626 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2627 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002628 Tmp2 = LegalizeOp(Node->getOperand(1));
2629 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2630 break;
2631 case ISD::SRL:
2632 // The input value must be properly zero extended.
2633 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002634 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002635 Tmp2 = LegalizeOp(Node->getOperand(1));
2636 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2637 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002638 case ISD::LOAD:
2639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2640 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002641 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2642 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002643 // Remember that we legalized the chain.
2644 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2645 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002646 case ISD::SEXTLOAD:
2647 case ISD::ZEXTLOAD:
2648 case ISD::EXTLOAD:
2649 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2650 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002651 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2652 Node->getOperand(2),
2653 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002654 // Remember that we legalized the chain.
2655 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2656 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002657 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002658 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2659 case Expand: assert(0 && "It's impossible to expand bools");
2660 case Legal:
2661 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2662 break;
2663 case Promote:
2664 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2665 break;
2666 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002667 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2668 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2669 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2670 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002671 case ISD::SELECT_CC:
2672 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2673 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2674 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2675 Node->getOperand(1), Tmp2, Tmp3,
2676 Node->getOperand(4));
2677 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002678 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002679 case ISD::CALL: {
2680 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2681 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2682
Chris Lattner3d95c142005-01-19 20:24:35 +00002683 std::vector<SDOperand> Ops;
2684 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2685 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2686
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002687 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2688 "Can only promote single result calls");
2689 std::vector<MVT::ValueType> RetTyVTs;
2690 RetTyVTs.reserve(2);
2691 RetTyVTs.push_back(NVT);
2692 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002693 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2694 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002695 Result = SDOperand(NC, 0);
2696
2697 // Insert the new chain mapping.
2698 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2699 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002700 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002701 case ISD::CTPOP:
2702 case ISD::CTTZ:
2703 case ISD::CTLZ:
2704 Tmp1 = Node->getOperand(0);
2705 //Zero extend the argument
2706 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2707 // Perform the larger operation, then subtract if needed.
2708 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2709 switch(Node->getOpcode())
2710 {
2711 case ISD::CTPOP:
2712 Result = Tmp1;
2713 break;
2714 case ISD::CTTZ:
2715 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002716 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002717 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002718 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002719 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2720 break;
2721 case ISD::CTLZ:
2722 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002723 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2724 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002725 getSizeInBits(VT), NVT));
2726 break;
2727 }
2728 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002729 }
2730
2731 assert(Result.Val && "Didn't set a result!");
2732 AddPromotedOperand(Op, Result);
2733 return Result;
2734}
Chris Lattnerdc750592005-01-07 07:47:09 +00002735
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002736/// ExpandAddSub - Find a clever way to expand this add operation into
2737/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002738void SelectionDAGLegalize::
2739ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2740 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002741 // Expand the subcomponents.
2742 SDOperand LHSL, LHSH, RHSL, RHSH;
2743 ExpandOp(LHS, LHSL, LHSH);
2744 ExpandOp(RHS, RHSL, RHSH);
2745
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002746 std::vector<SDOperand> Ops;
2747 Ops.push_back(LHSL);
2748 Ops.push_back(LHSH);
2749 Ops.push_back(RHSL);
2750 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002751 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2752 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002753 Hi = Lo.getValue(1);
2754}
2755
Chris Lattner4157c412005-04-02 04:00:59 +00002756void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2757 SDOperand Op, SDOperand Amt,
2758 SDOperand &Lo, SDOperand &Hi) {
2759 // Expand the subcomponents.
2760 SDOperand LHSL, LHSH;
2761 ExpandOp(Op, LHSL, LHSH);
2762
2763 std::vector<SDOperand> Ops;
2764 Ops.push_back(LHSL);
2765 Ops.push_back(LHSH);
2766 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002767 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002768 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002769 Hi = Lo.getValue(1);
2770}
2771
2772
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002773/// ExpandShift - Try to find a clever way to expand this shift operation out to
2774/// smaller elements. If we can't find a way that is more efficient than a
2775/// libcall on this target, return false. Otherwise, return true with the
2776/// low-parts expanded into Lo and Hi.
2777bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2778 SDOperand &Lo, SDOperand &Hi) {
2779 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2780 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002781
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002782 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002783 SDOperand ShAmt = LegalizeOp(Amt);
2784 MVT::ValueType ShTy = ShAmt.getValueType();
2785 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2786 unsigned NVTBits = MVT::getSizeInBits(NVT);
2787
2788 // Handle the case when Amt is an immediate. Other cases are currently broken
2789 // and are disabled.
2790 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2791 unsigned Cst = CN->getValue();
2792 // Expand the incoming operand to be shifted, so that we have its parts
2793 SDOperand InL, InH;
2794 ExpandOp(Op, InL, InH);
2795 switch(Opc) {
2796 case ISD::SHL:
2797 if (Cst > VTBits) {
2798 Lo = DAG.getConstant(0, NVT);
2799 Hi = DAG.getConstant(0, NVT);
2800 } else if (Cst > NVTBits) {
2801 Lo = DAG.getConstant(0, NVT);
2802 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002803 } else if (Cst == NVTBits) {
2804 Lo = DAG.getConstant(0, NVT);
2805 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002806 } else {
2807 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2808 Hi = DAG.getNode(ISD::OR, NVT,
2809 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2810 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2811 }
2812 return true;
2813 case ISD::SRL:
2814 if (Cst > VTBits) {
2815 Lo = DAG.getConstant(0, NVT);
2816 Hi = DAG.getConstant(0, NVT);
2817 } else if (Cst > NVTBits) {
2818 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2819 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002820 } else if (Cst == NVTBits) {
2821 Lo = InH;
2822 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002823 } else {
2824 Lo = DAG.getNode(ISD::OR, NVT,
2825 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2826 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2827 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2828 }
2829 return true;
2830 case ISD::SRA:
2831 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002832 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002833 DAG.getConstant(NVTBits-1, ShTy));
2834 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002835 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002836 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002837 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002838 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002839 } else if (Cst == NVTBits) {
2840 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002841 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002842 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002843 } else {
2844 Lo = DAG.getNode(ISD::OR, NVT,
2845 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2846 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2847 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2848 }
2849 return true;
2850 }
2851 }
2852 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2853 // so disable it for now. Currently targets are handling this via SHL_PARTS
2854 // and friends.
2855 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002856
2857 // If we have an efficient select operation (or if the selects will all fold
2858 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002859 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002860 return false;
2861
2862 SDOperand InL, InH;
2863 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002864 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2865 DAG.getConstant(NVTBits, ShTy), ShAmt);
2866
Chris Lattner4d25c042005-01-20 20:29:23 +00002867 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002868 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2869 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002870
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002871 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2872 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2873 DAG.getConstant(NVTBits-1, ShTy));
2874 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2875 DAG.getConstant(NVTBits-1, ShTy));
2876 }
2877
2878 if (Opc == ISD::SHL) {
2879 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2880 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2881 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002882 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002883
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002884 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2885 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2886 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002887 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002888 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2889 DAG.getConstant(32, ShTy),
2890 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002891 DAG.getConstant(0, NVT),
2892 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002893 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002894 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002895 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002896 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002897
2898 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002899 if (Opc == ISD::SRA)
2900 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2901 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002902 else
2903 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002904 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002905 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002906 }
2907 return true;
2908}
Chris Lattneraac464e2005-01-21 06:05:23 +00002909
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002910/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2911/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002912/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002913static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002914 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002915
Chris Lattner2dce7032005-05-12 23:24:06 +00002916 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002917 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002918 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002919 Found = Node;
2920 return;
2921 }
2922
2923 // Otherwise, scan the operands of Node to see if any of them is a call.
2924 assert(Node->getNumOperands() != 0 &&
2925 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002926 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002927 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002928
2929 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002930 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002931 Found);
2932}
2933
2934
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002935/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2936/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002937/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002938static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2939 std::set<SDNode*> &Visited) {
2940 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2941 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002942
Chris Lattner2dce7032005-05-12 23:24:06 +00002943 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002944 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002945 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002946 Found = Node;
2947 return;
2948 }
2949
2950 // Otherwise, scan the operands of Node to see if any of them is a call.
2951 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2952 if (UI == E) return;
2953 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002954 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002955
2956 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002957 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002958}
2959
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002960/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002961/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002962static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002963 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002964 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002965 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002966 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002967
Chris Lattner4add7e32005-01-23 04:42:50 +00002968 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002969 if (TheChain.getValueType() != MVT::Other)
2970 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002971 if (TheChain.getValueType() != MVT::Other)
2972 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002973
2974 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002975 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002976
Chris Lattner4add7e32005-01-23 04:42:50 +00002977 // Make sure to only follow users of our token chain.
2978 SDNode *User = *UI;
2979 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2980 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002981 if (SDNode *Result = FindCallSeqEnd(User))
2982 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002983 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002984 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002985}
2986
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002987/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002988/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002989static SDNode *FindCallSeqStart(SDNode *Node) {
2990 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002991 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002992
2993 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2994 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002995 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002996}
2997
2998
Chris Lattner4add7e32005-01-23 04:42:50 +00002999/// FindInputOutputChains - If we are replacing an operation with a call we need
3000/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003001/// properly serialize the calls in the block. The returned operand is the
3002/// input chain value for the new call (e.g. the entry node or the previous
3003/// call), and OutChain is set to be the chain node to update to point to the
3004/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003005static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3006 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003007 SDNode *LatestCallSeqStart = Entry.Val;
3008 SDNode *LatestCallSeqEnd = 0;
3009 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3010 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003011
Chris Lattner2dce7032005-05-12 23:24:06 +00003012 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003013 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003014 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3015 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003016 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003017 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003018 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3019 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003020 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003021 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003022 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003023
Chris Lattner06bbeb62005-05-11 19:02:11 +00003024 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003025 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003026 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003027 std::set<SDNode*> Visited;
3028 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003029
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003030 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003031 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003032 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003033
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003034 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003035}
3036
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003037/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003038void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3039 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003040 // Nothing to splice it into?
3041 if (OutChain == 0) return;
3042
3043 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3044 //OutChain->dump();
3045
3046 // Form a token factor node merging the old inval and the new inval.
3047 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3048 OutChain->getOperand(0));
3049 // Change the node to refer to the new token.
3050 OutChain->setAdjCallChain(InToken);
3051}
Chris Lattner4add7e32005-01-23 04:42:50 +00003052
3053
Chris Lattneraac464e2005-01-21 06:05:23 +00003054// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3055// does not fit into a register, return the lo part and set the hi part to the
3056// by-reg argument. If it does fit into a single register, return the result
3057// and leave the Hi part unset.
3058SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3059 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003060 SDNode *OutChain;
3061 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3062 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003063 if (InChain.Val == 0)
3064 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003065
Chris Lattneraac464e2005-01-21 06:05:23 +00003066 TargetLowering::ArgListTy Args;
3067 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3068 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3069 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3070 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3071 }
3072 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003073
Chris Lattner06bbeb62005-05-11 19:02:11 +00003074 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003075 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003076 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003077 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3078 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003079
Chris Lattner63022662005-09-02 20:26:58 +00003080 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003081 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003082 default: assert(0 && "Unknown thing");
3083 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003084 Result = CallInfo.first;
3085 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003086 case Promote:
3087 assert(0 && "Cannot promote this yet!");
3088 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003089 ExpandOp(CallInfo.first, Result, Hi);
3090 CallInfo.second = LegalizeOp(CallInfo.second);
3091 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003092 }
Chris Lattner63022662005-09-02 20:26:58 +00003093
3094 SpliceCallInto(CallInfo.second, OutChain);
3095 NeedsAnotherIteration = true;
3096 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003097}
3098
Chris Lattner4add7e32005-01-23 04:42:50 +00003099
Chris Lattneraac464e2005-01-21 06:05:23 +00003100/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3101/// destination type is legal.
3102SDOperand SelectionDAGLegalize::
3103ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003104 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003105 assert(getTypeAction(Source.getValueType()) == Expand &&
3106 "This is not an expansion!");
3107 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3108
Chris Lattner06bbeb62005-05-11 19:02:11 +00003109 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003110 assert(Source.getValueType() == MVT::i64 &&
3111 "This only works for 64-bit -> FP");
3112 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3113 // incoming integer is set. To handle this, we dynamically test to see if
3114 // it is set, and, if so, add a fudge factor.
3115 SDOperand Lo, Hi;
3116 ExpandOp(Source, Lo, Hi);
3117
Chris Lattner2a4f7312005-05-13 04:45:13 +00003118 // If this is unsigned, and not supported, first perform the conversion to
3119 // signed, then adjust the result if the sign bit is set.
3120 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3121 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3122
Chris Lattnerd47675e2005-08-09 20:20:18 +00003123 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3124 DAG.getConstant(0, Hi.getValueType()),
3125 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003126 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3127 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3128 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003129 uint64_t FF = 0x5f800000ULL;
3130 if (TLI.isLittleEndian()) FF <<= 32;
3131 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003132
Chris Lattnerc30405e2005-08-26 17:15:30 +00003133 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003134 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3135 SDOperand FudgeInReg;
3136 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003137 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3138 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003139 else {
3140 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003141 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3142 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003143 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003144 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003145 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003146
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003147 // Check to see if the target has a custom way to lower this. If so, use it.
3148 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3149 default: assert(0 && "This action not implemented for this operation!");
3150 case TargetLowering::Legal:
3151 case TargetLowering::Expand:
3152 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003153 case TargetLowering::Custom: {
3154 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3155 Source), DAG);
3156 if (NV.Val)
3157 return LegalizeOp(NV);
3158 break; // The target decided this was legal after all
3159 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003160 }
3161
Chris Lattner153587e2005-05-12 07:00:44 +00003162 // Expand the source, then glue it back together for the call. We must expand
3163 // the source in case it is shared (this pass of legalize must traverse it).
3164 SDOperand SrcLo, SrcHi;
3165 ExpandOp(Source, SrcLo, SrcHi);
3166 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3167
Chris Lattner06bbeb62005-05-11 19:02:11 +00003168 SDNode *OutChain = 0;
3169 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3170 DAG.getEntryNode());
3171 const char *FnName = 0;
3172 if (DestTy == MVT::f32)
3173 FnName = "__floatdisf";
3174 else {
3175 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3176 FnName = "__floatdidf";
3177 }
3178
Chris Lattneraac464e2005-01-21 06:05:23 +00003179 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3180
3181 TargetLowering::ArgListTy Args;
3182 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003183
Chris Lattneraac464e2005-01-21 06:05:23 +00003184 Args.push_back(std::make_pair(Source, ArgTy));
3185
3186 // We don't care about token chains for libcalls. We just use the entry
3187 // node as our input and ignore the output chain. This allows us to place
3188 // calls wherever we need them to satisfy data dependences.
3189 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003190
3191 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003192 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3193 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003194
Chris Lattnera5bf1032005-05-12 04:49:08 +00003195 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003196 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003197}
Misha Brukman835702a2005-04-21 22:36:52 +00003198
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003199
3200
Chris Lattnerdc750592005-01-07 07:47:09 +00003201/// ExpandOp - Expand the specified SDOperand into its two component pieces
3202/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3203/// LegalizeNodes map is filled in for any results that are not expanded, the
3204/// ExpandedNodes map is filled in for any results that are expanded, and the
3205/// Lo/Hi values are returned.
3206void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3207 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003208 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003209 SDNode *Node = Op.Val;
3210 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003211 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3212 "Cannot expand FP values!");
3213 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003214 "Cannot expand to FP value or to larger int value!");
3215
Chris Lattner1a570f12005-09-02 20:32:45 +00003216 // See if we already expanded it.
3217 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3218 = ExpandedNodes.find(Op);
3219 if (I != ExpandedNodes.end()) {
3220 Lo = I->second.first;
3221 Hi = I->second.second;
3222 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003223 }
3224
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003225 // Expanding to multiple registers needs to perform an optimization step, and
3226 // is not careful to avoid operations the target does not support. Make sure
3227 // that all generated operations are legalized in the next iteration.
3228 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003229
Chris Lattnerdc750592005-01-07 07:47:09 +00003230 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003231 case ISD::CopyFromReg:
3232 assert(0 && "CopyFromReg must be legal!");
3233 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003234 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3235 assert(0 && "Do not know how to expand this operator!");
3236 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003237 case ISD::UNDEF:
3238 Lo = DAG.getNode(ISD::UNDEF, NVT);
3239 Hi = DAG.getNode(ISD::UNDEF, NVT);
3240 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003241 case ISD::Constant: {
3242 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3243 Lo = DAG.getConstant(Cst, NVT);
3244 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3245 break;
3246 }
Nate Begemanae89d862005-12-07 19:48:11 +00003247 case ISD::ConstantVec: {
3248 unsigned NumElements = Node->getNumOperands();
3249 // If we only have two elements left in the constant vector, just break it
3250 // apart into the two scalar constants it contains. Otherwise, bisect the
3251 // ConstantVec, and return each half as a new ConstantVec.
3252 // FIXME: this is hard coded as big endian, it may have to change to support
3253 // SSE and Alpha MVI
3254 if (NumElements == 2) {
3255 Hi = Node->getOperand(0);
3256 Lo = Node->getOperand(1);
3257 } else {
3258 NumElements /= 2;
3259 std::vector<SDOperand> LoOps, HiOps;
3260 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3261 HiOps.push_back(Node->getOperand(I));
3262 LoOps.push_back(Node->getOperand(I+NumElements));
3263 }
3264 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3265 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3266 }
3267 break;
3268 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003269
Chris Lattner32e08b72005-03-28 22:03:13 +00003270 case ISD::BUILD_PAIR:
3271 // Legalize both operands. FIXME: in the future we should handle the case
3272 // where the two elements are not legal.
3273 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3274 Lo = LegalizeOp(Node->getOperand(0));
3275 Hi = LegalizeOp(Node->getOperand(1));
3276 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003277
3278 case ISD::SIGN_EXTEND_INREG:
3279 ExpandOp(Node->getOperand(0), Lo, Hi);
3280 // Sign extend the lo-part.
3281 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3282 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3283 TLI.getShiftAmountTy()));
3284 // sext_inreg the low part if needed.
3285 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3286 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003287
Chris Lattner55e9cde2005-05-11 04:51:16 +00003288 case ISD::CTPOP:
3289 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003290 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3291 DAG.getNode(ISD::CTPOP, NVT, Lo),
3292 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003293 Hi = DAG.getConstant(0, NVT);
3294 break;
3295
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003296 case ISD::CTLZ: {
3297 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003298 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003299 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3300 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003301 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3302 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003303 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3304 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3305
3306 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3307 Hi = DAG.getConstant(0, NVT);
3308 break;
3309 }
3310
3311 case ISD::CTTZ: {
3312 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003313 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003314 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3315 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003316 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3317 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003318 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3319 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3320
3321 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3322 Hi = DAG.getConstant(0, NVT);
3323 break;
3324 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003325
Chris Lattnerdc750592005-01-07 07:47:09 +00003326 case ISD::LOAD: {
3327 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3328 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003329 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003330
3331 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003332 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003333 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3334 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003335 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003336 //are from the same instruction?
3337 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003338
3339 // Build a factor node to remember that this load is independent of the
3340 // other one.
3341 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3342 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003343
Chris Lattnerdc750592005-01-07 07:47:09 +00003344 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003345 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003346 if (!TLI.isLittleEndian())
3347 std::swap(Lo, Hi);
3348 break;
3349 }
Nate Begemand37c1312005-11-22 18:16:00 +00003350 case ISD::VLOAD: {
3351 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3352 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3353 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3354 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3355
3356 // If we only have two elements, turn into a pair of scalar loads.
3357 // FIXME: handle case where a vector of two elements is fine, such as
3358 // 2 x double on SSE2.
3359 if (NumElements == 2) {
3360 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3361 // Increment the pointer to the other half.
3362 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3363 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3364 getIntPtrConstant(IncrementSize));
3365 //Is this safe? declaring that the two parts of the split load
3366 //are from the same instruction?
3367 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3368 } else {
3369 NumElements /= 2; // Split the vector in half
3370 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3371 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3372 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3373 getIntPtrConstant(IncrementSize));
3374 //Is this safe? declaring that the two parts of the split load
3375 //are from the same instruction?
3376 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3377 }
3378
3379 // Build a factor node to remember that this load is independent of the
3380 // other one.
3381 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3382 Hi.getValue(1));
3383
3384 // Remember that we legalized the chain.
3385 AddLegalizedOperand(Op.getValue(1), TF);
3386 if (!TLI.isLittleEndian())
3387 std::swap(Lo, Hi);
3388 break;
3389 }
3390 case ISD::VADD:
3391 case ISD::VSUB:
3392 case ISD::VMUL: {
3393 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3394 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3395 SDOperand LL, LH, RL, RH;
3396
3397 ExpandOp(Node->getOperand(0), LL, LH);
3398 ExpandOp(Node->getOperand(1), RL, RH);
3399
3400 // If we only have two elements, turn into a pair of scalar loads.
3401 // FIXME: handle case where a vector of two elements is fine, such as
3402 // 2 x double on SSE2.
3403 if (NumElements == 2) {
3404 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3405 Lo = DAG.getNode(Opc, EVT, LL, RL);
3406 Hi = DAG.getNode(Opc, EVT, LH, RH);
3407 } else {
3408 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3409 LL.getOperand(3));
3410 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3411 LH.getOperand(3));
3412 }
3413 break;
3414 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003415 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003416 case ISD::CALL: {
3417 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3418 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3419
Chris Lattner3d95c142005-01-19 20:24:35 +00003420 bool Changed = false;
3421 std::vector<SDOperand> Ops;
3422 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3423 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3424 Changed |= Ops.back() != Node->getOperand(i);
3425 }
3426
Chris Lattnerdc750592005-01-07 07:47:09 +00003427 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3428 "Can only expand a call once so far, not i64 -> i16!");
3429
3430 std::vector<MVT::ValueType> RetTyVTs;
3431 RetTyVTs.reserve(3);
3432 RetTyVTs.push_back(NVT);
3433 RetTyVTs.push_back(NVT);
3434 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003435 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3436 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003437 Lo = SDOperand(NC, 0);
3438 Hi = SDOperand(NC, 1);
3439
3440 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003441 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003442 break;
3443 }
3444 case ISD::AND:
3445 case ISD::OR:
3446 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3447 SDOperand LL, LH, RL, RH;
3448 ExpandOp(Node->getOperand(0), LL, LH);
3449 ExpandOp(Node->getOperand(1), RL, RH);
3450 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3451 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3452 break;
3453 }
3454 case ISD::SELECT: {
3455 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003456
3457 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3458 case Expand: assert(0 && "It's impossible to expand bools");
3459 case Legal:
3460 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3461 break;
3462 case Promote:
3463 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3464 break;
3465 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003466 ExpandOp(Node->getOperand(1), LL, LH);
3467 ExpandOp(Node->getOperand(2), RL, RH);
3468 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3469 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3470 break;
3471 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003472 case ISD::SELECT_CC: {
3473 SDOperand TL, TH, FL, FH;
3474 ExpandOp(Node->getOperand(2), TL, TH);
3475 ExpandOp(Node->getOperand(3), FL, FH);
3476 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3477 Node->getOperand(1), TL, FL, Node->getOperand(4));
3478 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3479 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003480 Lo = LegalizeOp(Lo);
3481 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003482 break;
3483 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003484 case ISD::SEXTLOAD: {
3485 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3486 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3487 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3488
3489 if (EVT == NVT)
3490 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3491 else
3492 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3493 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003494
3495 // Remember that we legalized the chain.
3496 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3497
Nate Begemanc3a89c52005-10-13 17:15:37 +00003498 // The high part is obtained by SRA'ing all but one of the bits of the lo
3499 // part.
3500 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3501 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3502 TLI.getShiftAmountTy()));
3503 Lo = LegalizeOp(Lo);
3504 Hi = LegalizeOp(Hi);
3505 break;
3506 }
3507 case ISD::ZEXTLOAD: {
3508 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3509 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3510 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3511
3512 if (EVT == NVT)
3513 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3514 else
3515 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3516 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003517
3518 // Remember that we legalized the chain.
3519 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3520
Nate Begemanc3a89c52005-10-13 17:15:37 +00003521 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003522 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003523 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003524 break;
3525 }
3526 case ISD::EXTLOAD: {
3527 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3528 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3529 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3530
3531 if (EVT == NVT)
3532 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3533 else
3534 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3535 EVT);
3536
3537 // Remember that we legalized the chain.
3538 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3539
3540 // The high part is undefined.
3541 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3542 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003543 break;
3544 }
Chris Lattner7753f172005-09-02 00:18:10 +00003545 case ISD::ANY_EXTEND: {
3546 SDOperand In;
3547 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3548 case Expand: assert(0 && "expand-expand not implemented yet!");
3549 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3550 case Promote:
3551 In = PromoteOp(Node->getOperand(0));
3552 break;
3553 }
3554
3555 // The low part is any extension of the input (which degenerates to a copy).
3556 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3557 // The high part is undefined.
3558 Hi = DAG.getNode(ISD::UNDEF, NVT);
3559 break;
3560 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003561 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003562 SDOperand In;
3563 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3564 case Expand: assert(0 && "expand-expand not implemented yet!");
3565 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3566 case Promote:
3567 In = PromoteOp(Node->getOperand(0));
3568 // Emit the appropriate sign_extend_inreg to get the value we want.
3569 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003570 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003571 break;
3572 }
3573
Chris Lattnerdc750592005-01-07 07:47:09 +00003574 // The low part is just a sign extension of the input (which degenerates to
3575 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003576 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003577
Chris Lattnerdc750592005-01-07 07:47:09 +00003578 // The high part is obtained by SRA'ing all but one of the bits of the lo
3579 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003580 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003581 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3582 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003583 break;
3584 }
Chris Lattner47844892005-04-03 23:41:52 +00003585 case ISD::ZERO_EXTEND: {
3586 SDOperand In;
3587 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3588 case Expand: assert(0 && "expand-expand not implemented yet!");
3589 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3590 case Promote:
3591 In = PromoteOp(Node->getOperand(0));
3592 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003593 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003594 break;
3595 }
3596
Chris Lattnerdc750592005-01-07 07:47:09 +00003597 // The low part is just a zero extension of the input (which degenerates to
3598 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003599 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003600
Chris Lattnerdc750592005-01-07 07:47:09 +00003601 // The high part is just a zero.
3602 Hi = DAG.getConstant(0, NVT);
3603 break;
Chris Lattner47844892005-04-03 23:41:52 +00003604 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003605
Chris Lattner44c28c22005-11-20 22:56:56 +00003606 case ISD::READCYCLECOUNTER: {
3607 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3608 TargetLowering::Custom &&
3609 "Must custom expand ReadCycleCounter");
3610 SDOperand T = TLI.LowerOperation(Op, DAG);
3611 assert(T.Val && "Node must be custom expanded!");
3612 Lo = LegalizeOp(T.getValue(0));
3613 Hi = LegalizeOp(T.getValue(1));
3614 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3615 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003616 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003617 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003618
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003619 // These operators cannot be expanded directly, emit them as calls to
3620 // library functions.
3621 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003622 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003623 SDOperand Op;
3624 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3625 case Expand: assert(0 && "cannot expand FP!");
3626 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3627 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3628 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003629
Chris Lattner941d84a2005-07-30 01:40:57 +00003630 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3631
Chris Lattnerfe68d752005-07-29 00:33:32 +00003632 // Now that the custom expander is done, expand the result, which is still
3633 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003634 if (Op.Val) {
3635 ExpandOp(Op, Lo, Hi);
3636 break;
3637 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003638 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003639
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003640 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003641 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003642 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003643 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003644 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003645
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003646 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003647 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3648 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3649 LegalizeOp(Node->getOperand(0)));
3650 // Now that the custom expander is done, expand the result, which is still
3651 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003652 Op = TLI.LowerOperation(Op, DAG);
3653 if (Op.Val) {
3654 ExpandOp(Op, Lo, Hi);
3655 break;
3656 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003657 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003658
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003659 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003660 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003661 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003662 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003663 break;
3664
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003665 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003666 // If the target wants custom lowering, do so.
3667 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3668 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3669 LegalizeOp(Node->getOperand(1)));
3670 Op = TLI.LowerOperation(Op, DAG);
3671 if (Op.Val) {
3672 // Now that the custom expander is done, expand the result, which is
3673 // still VT.
3674 ExpandOp(Op, Lo, Hi);
3675 break;
3676 }
3677 }
3678
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003679 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003680 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003681 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003682
3683 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003684 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003685 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3686 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003687 break;
3688 }
3689
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003690 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003691 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003692 break;
3693
3694 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003695 // If the target wants custom lowering, do so.
3696 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3697 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3698 LegalizeOp(Node->getOperand(1)));
3699 Op = TLI.LowerOperation(Op, DAG);
3700 if (Op.Val) {
3701 // Now that the custom expander is done, expand the result, which is
3702 // still VT.
3703 ExpandOp(Op, Lo, Hi);
3704 break;
3705 }
3706 }
3707
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003708 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003709 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003710 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003711
3712 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003713 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003714 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3715 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003716 break;
3717 }
3718
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003719 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003720 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003721 break;
3722 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003723 // If the target wants custom lowering, do so.
3724 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3725 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3726 LegalizeOp(Node->getOperand(1)));
3727 Op = TLI.LowerOperation(Op, DAG);
3728 if (Op.Val) {
3729 // Now that the custom expander is done, expand the result, which is
3730 // still VT.
3731 ExpandOp(Op, Lo, Hi);
3732 break;
3733 }
3734 }
3735
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003736 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003737 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003738 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003739
3740 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003741 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003742 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3743 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003744 break;
3745 }
3746
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003747 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003748 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003749 break;
3750
Misha Brukman835702a2005-04-21 22:36:52 +00003751 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003752 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3753 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003754 break;
3755 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003756 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3757 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003758 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003759 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003760 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003761 SDOperand LL, LH, RL, RH;
3762 ExpandOp(Node->getOperand(0), LL, LH);
3763 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003764 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3765 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3766 // extended the sign bit of the low half through the upper half, and if so
3767 // emit a MULHS instead of the alternate sequence that is valid for any
3768 // i64 x i64 multiply.
3769 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3770 // is RH an extension of the sign bit of RL?
3771 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3772 RH.getOperand(1).getOpcode() == ISD::Constant &&
3773 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3774 // is LH an extension of the sign bit of LL?
3775 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3776 LH.getOperand(1).getOpcode() == ISD::Constant &&
3777 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3778 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3779 } else {
3780 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3781 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3782 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3783 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3784 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3785 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003786 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3787 } else {
3788 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3789 }
3790 break;
3791 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003792 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3793 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3794 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3795 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003796 }
3797
3798 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003799 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3800 std::make_pair(Lo, Hi))).second;
3801 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003802}
3803
3804
3805// SelectionDAG::Legalize - This is the entry point for the file.
3806//
Chris Lattner4add7e32005-01-23 04:42:50 +00003807void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003808 /// run - This is the main entry point to this class.
3809 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003810 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003811}
3812