blob: 6896fb39e261b0c7600719b0e928f0632cdef236 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Jim Laskeyf2516a92005-08-17 00:39:29 +0000168/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
169/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000170/// we expand it. At this point, we know that the result and operand types are
171/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand Op0,
174 MVT::ValueType DestVT) {
175 if (Op0.getValueType() == MVT::i32) {
176 // simple 32-bit [signed|unsigned] integer to float/double expansion
177
178 // get the stack frame index of a 8 byte buffer
179 MachineFunction &MF = DAG.getMachineFunction();
180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
181 // get address of 8 byte buffer
182 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
183 // word offset constant for Hi/Lo address computation
184 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
185 // set up Hi and Lo (into buffer) address based on endian
186 SDOperand Hi, Lo;
187 if (TLI.isLittleEndian()) {
188 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
189 Lo = StackSlot;
190 } else {
191 Hi = StackSlot;
192 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 }
194 // if signed map to unsigned space
195 SDOperand Op0Mapped;
196 if (isSigned) {
197 // constant used to invert sign bit (signed to unsigned mapping)
198 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
199 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
200 } else {
201 Op0Mapped = Op0;
202 }
203 // store the lo of the constructed double - based on integer input
204 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
205 Op0Mapped, Lo, DAG.getSrcValue(NULL));
206 // initial hi portion of constructed double
207 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
208 // store the hi of the constructed double - biased exponent
209 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
210 InitialHi, Hi, DAG.getSrcValue(NULL));
211 // load the constructed double
212 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
213 DAG.getSrcValue(NULL));
214 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000221 // final result
222 SDOperand Result;
223 // handle final rounding
224 if (DestVT == MVT::f64) {
225 // do nothing
226 Result = Sub;
227 } else {
228 // if f32 then cast to f32
229 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
230 }
231 NeedsAnotherIteration = true;
232 return Result;
233 }
234 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattnerd47675e2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000240 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
241 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
242 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Jim Laskeyf2516a92005-08-17 00:39:29 +0000244 // If the sign bit of the integer is set, the large number will be treated
245 // as a negative number. To counteract this, the dynamic code adds an
246 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000247 uint64_t FF;
248 switch (Op0.getValueType()) {
249 default: assert(0 && "Unsupported integer type!");
250 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
251 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
252 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
253 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
254 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnerc30405e2005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
260 SDOperand FudgeInReg;
261 if (DestVT == MVT::f32)
262 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL));
264 else {
265 assert(DestVT == MVT::f64 && "Unexpected conversion");
266 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
267 DAG.getEntryNode(), CPIdx,
268 DAG.getSrcValue(NULL), MVT::f32));
269 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000273}
274
Chris Lattner19732782005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000277/// we promote it. At this point, we know that the result and operand types are
278/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
279/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // First step, figure out the appropriate *INT_TO_FP operation to use.
284 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000285
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // Scan for the appropriate larger type to use.
289 while (1) {
290 NewInTy = (MVT::ValueType)(NewInTy+1);
291 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Chris Lattnere3e847b2005-07-16 00:19:57 +0000293 // If the target supports SINT_TO_FP of this type, use it.
294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
295 default: break;
296 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000298 break; // Can't use this datatype.
299 // FALL THROUGH.
300 case TargetLowering::Custom:
301 OpToUse = ISD::SINT_TO_FP;
302 break;
303 }
304 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Chris Lattnere3e847b2005-07-16 00:19:57 +0000307 // If the target supports UINT_TO_FP of this type, use it.
308 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
309 default: break;
310 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000312 break; // Can't use this datatype.
313 // FALL THROUGH.
314 case TargetLowering::Custom:
315 OpToUse = ISD::UINT_TO_FP;
316 break;
317 }
318 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000319
Chris Lattnere3e847b2005-07-16 00:19:57 +0000320 // Otherwise, try a larger type.
321 }
322
323 // Make sure to legalize any nodes we create here in the next pass.
324 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000325
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
328 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000331}
332
Chris Lattner44fe26f2005-07-29 00:11:56 +0000333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
334/// FP_TO_*INT operation of the specified operand when the target requests that
335/// we promote it. At this point, we know that the result and operand types are
336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
337/// operation that returns a larger result.
338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
339 MVT::ValueType DestVT,
340 bool isSigned) {
341 // First step, figure out the appropriate FP_TO*INT operation to use.
342 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000343
Chris Lattner44fe26f2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // Scan for the appropriate larger type to use.
347 while (1) {
348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000350
Chris Lattner44fe26f2005-07-29 00:11:56 +0000351 // If the target supports FP_TO_SINT returning this type, use it.
352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
353 default: break;
354 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000356 break; // Can't use this datatype.
357 // FALL THROUGH.
358 case TargetLowering::Custom:
359 OpToUse = ISD::FP_TO_SINT;
360 break;
361 }
362 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-07-29 00:11:56 +0000364 // If the target supports FP_TO_UINT of this type, use it.
365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
366 default: break;
367 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 break; // Can't use this datatype.
370 // FALL THROUGH.
371 case TargetLowering::Custom:
372 OpToUse = ISD::FP_TO_UINT;
373 break;
374 }
375 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000376
Chris Lattner44fe26f2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000379
Chris Lattner44fe26f2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000382
Chris Lattner44fe26f2005-07-29 00:11:56 +0000383 // Okay, we found the operation and type to use. Truncate the result of the
384 // extended FP_TO_*INT operation to the desired size.
385 return DAG.getNode(ISD::TRUNCATE, DestVT,
386 DAG.getNode(OpToUse, NewOutTy, LegalOp));
387}
388
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000535 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000536 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000537 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000538 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000539 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000540 case ISD::BasicBlock:
541 case ISD::CONDCODE:
542 case ISD::VALUETYPE:
543 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000544 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000545 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
546 default: assert(0 && "This action is not supported yet!");
547 case TargetLowering::Custom: {
548 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
549 if (Tmp.Val) {
550 Result = LegalizeOp(Tmp);
551 break;
552 }
553 } // FALLTHROUGH if the target doesn't want to lower this op after all.
554 case TargetLowering::Legal:
555 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
556 break;
557 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000558 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000559 case ISD::AssertSext:
560 case ISD::AssertZext:
561 Tmp1 = LegalizeOp(Node->getOperand(0));
562 if (Tmp1 != Node->getOperand(0))
563 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
564 Node->getOperand(1));
565 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000566 case ISD::MERGE_VALUES:
567 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
570 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000571 Result = DAG.getCopyFromReg(Tmp1,
572 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
573 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000574 else
575 Result = Op.getValue(0);
576
577 // Since CopyFromReg produces two values, make sure to remember that we
578 // legalized both of them.
579 AddLegalizedOperand(Op.getValue(0), Result);
580 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
581 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000582 case ISD::ImplicitDef:
583 Tmp1 = LegalizeOp(Node->getOperand(0));
584 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000585 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
586 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000587 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000588 case ISD::UNDEF: {
589 MVT::ValueType VT = Op.getValueType();
590 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000591 default: assert(0 && "This action is not supported yet!");
592 case TargetLowering::Expand:
593 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000594 if (MVT::isInteger(VT))
595 Result = DAG.getConstant(0, VT);
596 else if (MVT::isFloatingPoint(VT))
597 Result = DAG.getConstantFP(0, VT);
598 else
599 assert(0 && "Unknown value type!");
600 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000601 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000602 break;
603 }
604 break;
605 }
Chris Lattner435b4022005-11-29 06:21:05 +0000606
607 case ISD::LOCATION:
608 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
610
611 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
612 case TargetLowering::Promote:
613 default: assert(0 && "This action is not supported yet!");
614 case TargetLowering::Expand:
615 // If the target doesn't support line numbers, ignore this node.
616 Result = Tmp1;
617 break;
618 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000619 if (Tmp1 != Node->getOperand(0) ||
620 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000621 std::vector<SDOperand> Ops;
622 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000623 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
624 Ops.push_back(Node->getOperand(1)); // line # must be legal.
625 Ops.push_back(Node->getOperand(2)); // col # must be legal.
626 } else {
627 // Otherwise promote them.
628 Ops.push_back(PromoteOp(Node->getOperand(1)));
629 Ops.push_back(PromoteOp(Node->getOperand(2)));
630 }
Chris Lattner435b4022005-11-29 06:21:05 +0000631 Ops.push_back(Node->getOperand(3)); // filename must be legal.
632 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
633 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
634 }
635 break;
636 }
637 break;
638
Chris Lattnerdc750592005-01-07 07:47:09 +0000639 case ISD::Constant:
640 // We know we don't need to expand constants here, constants only have one
641 // value and we check that it is fine above.
642
643 // FIXME: Maybe we should handle things like targets that don't support full
644 // 32-bit immediates?
645 break;
646 case ISD::ConstantFP: {
647 // Spill FP immediates to the constant pool if the target cannot directly
648 // codegen them. Targets often have some immediate values that can be
649 // efficiently generated into an FP register without a load. We explicitly
650 // leave these constants as ConstantFP nodes for the target to deal with.
651
652 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
653
654 // Check to see if this FP immediate is already legal.
655 bool isLegal = false;
656 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
657 E = TLI.legal_fpimm_end(); I != E; ++I)
658 if (CFP->isExactlyValue(*I)) {
659 isLegal = true;
660 break;
661 }
662
663 if (!isLegal) {
664 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000665 bool Extend = false;
666
667 // If a FP immediate is precise when represented as a float, we put it
668 // into the constant pool as a float, even if it's is statically typed
669 // as a double.
670 MVT::ValueType VT = CFP->getValueType(0);
671 bool isDouble = VT == MVT::f64;
672 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
673 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000674 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
675 // Only do this if the target has a native EXTLOAD instruction from
676 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000677 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000678 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
679 VT = MVT::f32;
680 Extend = true;
681 }
Misha Brukman835702a2005-04-21 22:36:52 +0000682
Nate Begeman4e56db62005-12-10 02:36:00 +0000683 SDOperand CPIdx =
684 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000685 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000686 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
687 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000688 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000689 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
690 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000691 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000692 }
693 break;
694 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000695 case ISD::ConstantVec: {
696 // We assume that vector constants are not legal, and will be immediately
697 // spilled to the constant pool.
698 //
699 // FIXME: revisit this when we have some kind of mechanism by which targets
700 // can decided legality of vector constants, of which there may be very
701 // many.
702 //
703 // Create a ConstantPacked, and put it in the constant pool.
704 std::vector<Constant*> CV;
705 MVT::ValueType VT = Node->getValueType(0);
706 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
707 SDOperand OpN = Node->getOperand(I);
708 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
709 if (MVT::isFloatingPoint(VT))
710 CV.push_back(ConstantFP::get(OpNTy,
711 cast<ConstantFPSDNode>(OpN)->getValue()));
712 else
713 CV.push_back(ConstantUInt::get(OpNTy,
714 cast<ConstantSDNode>(OpN)->getValue()));
715 }
716 Constant *CP = ConstantPacked::get(CV);
717 SDOperand CPIdx = DAG.getConstantPool(CP, Node->getValueType(0));
718 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
719 break;
720 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000721 case ISD::TokenFactor:
722 if (Node->getNumOperands() == 2) {
723 bool Changed = false;
724 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
725 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
726 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
727 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
728 } else {
729 std::vector<SDOperand> Ops;
730 bool Changed = false;
731 // Legalize the operands.
732 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
733 SDOperand Op = Node->getOperand(i);
734 Ops.push_back(LegalizeOp(Op));
735 Changed |= Ops[i] != Op;
736 }
737 if (Changed)
738 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000739 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000740 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000741
Chris Lattner2dce7032005-05-12 23:24:06 +0000742 case ISD::CALLSEQ_START:
743 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000744 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000745 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000746 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000747 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000748 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000749
Chris Lattner2dce7032005-05-12 23:24:06 +0000750 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000751 // nodes are treated specially and are mutated in place. This makes the dag
752 // legalization process more efficient and also makes libcall insertion
753 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000754 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000755 case ISD::DYNAMIC_STACKALLOC:
756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
757 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
758 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
759 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000760 Tmp3 != Node->getOperand(2)) {
761 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
762 std::vector<SDOperand> Ops;
763 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
764 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
765 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000766 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000767
768 // Since this op produces two values, make sure to remember that we
769 // legalized both of them.
770 AddLegalizedOperand(SDOperand(Node, 0), Result);
771 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
772 return Result.getValue(Op.ResNo);
773
Chris Lattnerd0feb642005-05-13 18:43:43 +0000774 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000775 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000776 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
777 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000778
779 bool Changed = false;
780 std::vector<SDOperand> Ops;
781 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
782 Ops.push_back(LegalizeOp(Node->getOperand(i)));
783 Changed |= Ops.back() != Node->getOperand(i);
784 }
785
786 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000787 std::vector<MVT::ValueType> RetTyVTs;
788 RetTyVTs.reserve(Node->getNumValues());
789 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000790 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000791 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
792 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000793 } else {
794 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000795 }
Chris Lattner9242c502005-01-09 19:43:23 +0000796 // Since calls produce multiple values, make sure to remember that we
797 // legalized all of them.
798 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
799 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
800 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000801 }
Chris Lattner68a12142005-01-07 22:12:08 +0000802 case ISD::BR:
803 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
804 if (Tmp1 != Node->getOperand(0))
805 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
806 break;
807
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000808 case ISD::BRCOND:
809 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000810
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000811 switch (getTypeAction(Node->getOperand(1).getValueType())) {
812 case Expand: assert(0 && "It's impossible to expand bools");
813 case Legal:
814 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
815 break;
816 case Promote:
817 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
818 break;
819 }
Nate Begeman371e4952005-08-16 19:49:35 +0000820
821 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
822 default: assert(0 && "This action is not supported yet!");
823 case TargetLowering::Expand:
824 // Expand brcond's setcc into its constituent parts and create a BR_CC
825 // Node.
826 if (Tmp2.getOpcode() == ISD::SETCC) {
827 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
828 Tmp2.getOperand(0), Tmp2.getOperand(1),
829 Node->getOperand(2));
830 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000831 // Make sure the condition is either zero or one. It may have been
832 // promoted from something else.
833 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
834
Nate Begeman371e4952005-08-16 19:49:35 +0000835 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
836 DAG.getCondCode(ISD::SETNE), Tmp2,
837 DAG.getConstant(0, Tmp2.getValueType()),
838 Node->getOperand(2));
839 }
840 break;
841 case TargetLowering::Legal:
842 // Basic block destination (Op#2) is always legal.
843 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
844 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
845 Node->getOperand(2));
846 break;
847 }
848 break;
849 case ISD::BR_CC:
850 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
851
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000852 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000853 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
854 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
855 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
856 Tmp3 != Node->getOperand(3)) {
857 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
858 Tmp2, Tmp3, Node->getOperand(4));
859 }
860 break;
861 } else {
862 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
863 Node->getOperand(2), // LHS
864 Node->getOperand(3), // RHS
865 Node->getOperand(1)));
866 // If we get a SETCC back from legalizing the SETCC node we just
867 // created, then use its LHS, RHS, and CC directly in creating a new
868 // node. Otherwise, select between the true and false value based on
869 // comparing the result of the legalized with zero.
870 if (Tmp2.getOpcode() == ISD::SETCC) {
871 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
872 Tmp2.getOperand(0), Tmp2.getOperand(1),
873 Node->getOperand(4));
874 } else {
875 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
876 DAG.getCondCode(ISD::SETNE),
877 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
878 Node->getOperand(4));
879 }
880 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000881 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000882 case ISD::BRCONDTWOWAY:
883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
884 switch (getTypeAction(Node->getOperand(1).getValueType())) {
885 case Expand: assert(0 && "It's impossible to expand bools");
886 case Legal:
887 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
888 break;
889 case Promote:
890 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
891 break;
892 }
893 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
894 // pair.
895 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
896 case TargetLowering::Promote:
897 default: assert(0 && "This action is not supported yet!");
898 case TargetLowering::Legal:
899 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
900 std::vector<SDOperand> Ops;
901 Ops.push_back(Tmp1);
902 Ops.push_back(Tmp2);
903 Ops.push_back(Node->getOperand(2));
904 Ops.push_back(Node->getOperand(3));
905 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
906 }
907 break;
908 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000909 // If BRTWOWAY_CC is legal for this target, then simply expand this node
910 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
911 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000912 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000913 if (Tmp2.getOpcode() == ISD::SETCC) {
914 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
915 Tmp2.getOperand(0), Tmp2.getOperand(1),
916 Node->getOperand(2), Node->getOperand(3));
917 } else {
918 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
919 DAG.getConstant(0, Tmp2.getValueType()),
920 Node->getOperand(2), Node->getOperand(3));
921 }
922 } else {
923 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000924 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000925 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
926 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000927 break;
928 }
929 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000930 case ISD::BRTWOWAY_CC:
931 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000932 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000933 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
934 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
935 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
936 Tmp3 != Node->getOperand(3)) {
937 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
938 Node->getOperand(4), Node->getOperand(5));
939 }
940 break;
941 } else {
942 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
943 Node->getOperand(2), // LHS
944 Node->getOperand(3), // RHS
945 Node->getOperand(1)));
946 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
947 // pair.
948 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
949 default: assert(0 && "This action is not supported yet!");
950 case TargetLowering::Legal:
951 // If we get a SETCC back from legalizing the SETCC node we just
952 // created, then use its LHS, RHS, and CC directly in creating a new
953 // node. Otherwise, select between the true and false value based on
954 // comparing the result of the legalized with zero.
955 if (Tmp2.getOpcode() == ISD::SETCC) {
956 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
957 Tmp2.getOperand(0), Tmp2.getOperand(1),
958 Node->getOperand(4), Node->getOperand(5));
959 } else {
960 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
961 DAG.getConstant(0, Tmp2.getValueType()),
962 Node->getOperand(4), Node->getOperand(5));
963 }
964 break;
965 case TargetLowering::Expand:
966 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
967 Node->getOperand(4));
968 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
969 break;
970 }
971 }
972 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000973 case ISD::LOAD:
974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
975 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000976
Chris Lattnerdc750592005-01-07 07:47:09 +0000977 if (Tmp1 != Node->getOperand(0) ||
978 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000979 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
980 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000981 else
982 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000983
Chris Lattnerea4ca942005-01-07 22:28:47 +0000984 // Since loads produce two values, make sure to remember that we legalized
985 // both of them.
986 AddLegalizedOperand(SDOperand(Node, 0), Result);
987 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
988 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000989
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000990 case ISD::EXTLOAD:
991 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000992 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000995
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000996 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000997 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000998 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000999 case TargetLowering::Promote:
1000 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001001 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1002 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001003 // Since loads produce two values, make sure to remember that we legalized
1004 // both of them.
1005 AddLegalizedOperand(SDOperand(Node, 0), Result);
1006 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1007 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001008
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001009 case TargetLowering::Legal:
1010 if (Tmp1 != Node->getOperand(0) ||
1011 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001012 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1013 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001014 else
1015 Result = SDOperand(Node, 0);
1016
1017 // Since loads produce two values, make sure to remember that we legalized
1018 // both of them.
1019 AddLegalizedOperand(SDOperand(Node, 0), Result);
1020 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1021 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001022 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001023 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1024 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1025 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001026 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001027 if (Op.ResNo)
1028 return Load.getValue(1);
1029 return Result;
1030 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001031 assert(Node->getOpcode() != ISD::EXTLOAD &&
1032 "EXTLOAD should always be supported!");
1033 // Turn the unsupported load into an EXTLOAD followed by an explicit
1034 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001035 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1036 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001037 SDOperand ValRes;
1038 if (Node->getOpcode() == ISD::SEXTLOAD)
1039 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001040 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001041 else
1042 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001043 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1044 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1045 if (Op.ResNo)
1046 return Result.getValue(1);
1047 return ValRes;
1048 }
1049 assert(0 && "Unreachable");
1050 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001051 case ISD::EXTRACT_ELEMENT: {
1052 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1053 switch (getTypeAction(OpTy)) {
1054 default:
1055 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1056 break;
1057 case Legal:
1058 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1059 // 1 -> Hi
1060 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1061 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1062 TLI.getShiftAmountTy()));
1063 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1064 } else {
1065 // 0 -> Lo
1066 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1067 Node->getOperand(0));
1068 }
1069 Result = LegalizeOp(Result);
1070 break;
1071 case Expand:
1072 // Get both the low and high parts.
1073 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1074 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1075 Result = Tmp2; // 1 -> Hi
1076 else
1077 Result = Tmp1; // 0 -> Lo
1078 break;
1079 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001080 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001081 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001082
1083 case ISD::CopyToReg:
1084 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001085
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001086 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001087 "Register type must be legal!");
1088 // Legalize the incoming value (must be legal).
1089 Tmp2 = LegalizeOp(Node->getOperand(2));
1090 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1091 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1092 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001093 break;
1094
1095 case ISD::RET:
1096 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1097 switch (Node->getNumOperands()) {
1098 case 2: // ret val
1099 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1100 case Legal:
1101 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001102 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001103 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1104 break;
1105 case Expand: {
1106 SDOperand Lo, Hi;
1107 ExpandOp(Node->getOperand(1), Lo, Hi);
1108 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001109 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001110 }
1111 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001112 Tmp2 = PromoteOp(Node->getOperand(1));
1113 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1114 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001115 }
1116 break;
1117 case 1: // ret void
1118 if (Tmp1 != Node->getOperand(0))
1119 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1120 break;
1121 default: { // ret <values>
1122 std::vector<SDOperand> NewValues;
1123 NewValues.push_back(Tmp1);
1124 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1125 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1126 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001127 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001128 break;
1129 case Expand: {
1130 SDOperand Lo, Hi;
1131 ExpandOp(Node->getOperand(i), Lo, Hi);
1132 NewValues.push_back(Lo);
1133 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001134 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001135 }
1136 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001137 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001138 }
1139 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1140 break;
1141 }
1142 }
1143 break;
1144 case ISD::STORE:
1145 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1146 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1147
Chris Lattnere69daaf2005-01-08 06:25:56 +00001148 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001149 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001150 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001151 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001152 DAG.getConstant(FloatToBits(CFP->getValue()),
1153 MVT::i32),
1154 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001155 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001156 } else {
1157 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001158 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001159 DAG.getConstant(DoubleToBits(CFP->getValue()),
1160 MVT::i64),
1161 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001162 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001163 }
Chris Lattnera4743132005-02-22 07:23:39 +00001164 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001165 }
1166
Chris Lattnerdc750592005-01-07 07:47:09 +00001167 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1168 case Legal: {
1169 SDOperand Val = LegalizeOp(Node->getOperand(1));
1170 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1171 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001172 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1173 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001174 break;
1175 }
1176 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001177 // Truncate the value and store the result.
1178 Tmp3 = PromoteOp(Node->getOperand(1));
1179 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001180 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001181 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001182 break;
1183
Chris Lattnerdc750592005-01-07 07:47:09 +00001184 case Expand:
1185 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001186 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001187 ExpandOp(Node->getOperand(1), Lo, Hi);
1188
1189 if (!TLI.isLittleEndian())
1190 std::swap(Lo, Hi);
1191
Chris Lattner55e9cde2005-05-11 04:51:16 +00001192 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1193 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001194 // If this is a vector type, then we have to calculate the increment as
1195 // the product of the element size in bytes, and the number of elements
1196 // in the high half of the vector.
1197 if (MVT::Vector == Hi.getValueType()) {
1198 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1199 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1200 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1201 } else {
1202 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1203 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001204 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1205 getIntPtrConstant(IncrementSize));
1206 assert(isTypeLegal(Tmp2.getValueType()) &&
1207 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001208 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001209 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1210 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001211 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1212 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001213 }
1214 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001215 case ISD::PCMARKER:
1216 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001217 if (Tmp1 != Node->getOperand(0))
1218 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001219 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001220 case ISD::READCYCLECOUNTER:
1221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001222 if (Tmp1 != Node->getOperand(0)) {
1223 std::vector<MVT::ValueType> rtypes;
1224 std::vector<SDOperand> rvals;
1225 rtypes.push_back(MVT::i64);
1226 rtypes.push_back(MVT::Other);
1227 rvals.push_back(Tmp1);
1228 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1229 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001230
1231 // Since rdcc produce two values, make sure to remember that we legalized
1232 // both of them.
1233 AddLegalizedOperand(SDOperand(Node, 0), Result);
1234 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1235 return Result.getValue(Op.ResNo);
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001236 break;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001237
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001238 case ISD::TRUNCSTORE:
1239 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1240 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1241
1242 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1243 case Legal:
1244 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001245
1246 // The only promote case we handle is TRUNCSTORE:i1 X into
1247 // -> TRUNCSTORE:i8 (and X, 1)
1248 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1249 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1250 TargetLowering::Promote) {
1251 // Promote the bool to a mask then store.
1252 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1253 DAG.getConstant(1, Tmp2.getValueType()));
1254 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1255 Node->getOperand(3), DAG.getValueType(MVT::i8));
1256
1257 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1258 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001259 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001260 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001261 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001262 break;
1263 case Promote:
1264 case Expand:
1265 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1266 }
1267 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001268 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001269 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1270 case Expand: assert(0 && "It's impossible to expand bools");
1271 case Legal:
1272 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1273 break;
1274 case Promote:
1275 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1276 break;
1277 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001278 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001279 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001280
Nate Begeman987121a2005-08-23 04:29:48 +00001281 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001282 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001283 case TargetLowering::Expand:
1284 if (Tmp1.getOpcode() == ISD::SETCC) {
1285 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1286 Tmp2, Tmp3,
1287 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1288 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001289 // Make sure the condition is either zero or one. It may have been
1290 // promoted from something else.
1291 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001292 Result = DAG.getSelectCC(Tmp1,
1293 DAG.getConstant(0, Tmp1.getValueType()),
1294 Tmp2, Tmp3, ISD::SETNE);
1295 }
1296 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001297 case TargetLowering::Legal:
1298 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1299 Tmp3 != Node->getOperand(2))
1300 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1301 Tmp1, Tmp2, Tmp3);
1302 break;
1303 case TargetLowering::Promote: {
1304 MVT::ValueType NVT =
1305 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1306 unsigned ExtOp, TruncOp;
1307 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001308 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001309 TruncOp = ISD::TRUNCATE;
1310 } else {
1311 ExtOp = ISD::FP_EXTEND;
1312 TruncOp = ISD::FP_ROUND;
1313 }
1314 // Promote each of the values to the new type.
1315 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1316 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1317 // Perform the larger operation, then round down.
1318 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1319 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1320 break;
1321 }
1322 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001323 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001324 case ISD::SELECT_CC:
1325 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1326 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1327
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001328 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001329 // Everything is legal, see if we should expand this op or something.
1330 switch (TLI.getOperationAction(ISD::SELECT_CC,
1331 Node->getOperand(0).getValueType())) {
1332 default: assert(0 && "This action is not supported yet!");
1333 case TargetLowering::Custom: {
1334 SDOperand Tmp =
1335 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1336 Node->getOperand(0),
1337 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001338 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001339 if (Tmp.Val) {
1340 Result = LegalizeOp(Tmp);
1341 break;
1342 }
1343 } // FALLTHROUGH if the target can't lower this operation after all.
1344 case TargetLowering::Legal:
1345 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1346 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1347 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1348 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1349 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1350 Tmp3, Tmp4, Node->getOperand(4));
1351 }
1352 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001353 }
1354 break;
1355 } else {
1356 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1357 Node->getOperand(0), // LHS
1358 Node->getOperand(1), // RHS
1359 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001360 // If we get a SETCC back from legalizing the SETCC node we just
1361 // created, then use its LHS, RHS, and CC directly in creating a new
1362 // node. Otherwise, select between the true and false value based on
1363 // comparing the result of the legalized with zero.
1364 if (Tmp1.getOpcode() == ISD::SETCC) {
1365 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1366 Tmp1.getOperand(0), Tmp1.getOperand(1),
1367 Tmp3, Tmp4, Tmp1.getOperand(2));
1368 } else {
1369 Result = DAG.getSelectCC(Tmp1,
1370 DAG.getConstant(0, Tmp1.getValueType()),
1371 Tmp3, Tmp4, ISD::SETNE);
1372 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001373 }
1374 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001375 case ISD::SETCC:
1376 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1377 case Legal:
1378 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1379 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001380 break;
1381 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001382 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1383 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1384
1385 // If this is an FP compare, the operands have already been extended.
1386 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1387 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001388 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001389
1390 // Otherwise, we have to insert explicit sign or zero extends. Note
1391 // that we could insert sign extends for ALL conditions, but zero extend
1392 // is cheaper on many machines (an AND instead of two shifts), so prefer
1393 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001394 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001395 default: assert(0 && "Unknown integer comparison!");
1396 case ISD::SETEQ:
1397 case ISD::SETNE:
1398 case ISD::SETUGE:
1399 case ISD::SETUGT:
1400 case ISD::SETULE:
1401 case ISD::SETULT:
1402 // ALL of these operations will work if we either sign or zero extend
1403 // the operands (including the unsigned comparisons!). Zero extend is
1404 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001405 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1406 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001407 break;
1408 case ISD::SETGE:
1409 case ISD::SETGT:
1410 case ISD::SETLT:
1411 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001412 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1413 DAG.getValueType(VT));
1414 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1415 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001416 break;
1417 }
Chris Lattner4d978642005-01-15 22:16:26 +00001418 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001419 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001420 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001421 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1422 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1423 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001424 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001425 case ISD::SETEQ:
1426 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001427 if (RHSLo == RHSHi)
1428 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1429 if (RHSCST->isAllOnesValue()) {
1430 // Comparison to -1.
1431 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001432 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001433 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001434 }
1435
Chris Lattnerdc750592005-01-07 07:47:09 +00001436 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1437 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1438 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001439 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001440 break;
1441 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001442 // If this is a comparison of the sign bit, just look at the top part.
1443 // X > -1, x < 0
1444 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001445 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001446 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001447 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001448 (CST->isAllOnesValue()))) { // X > -1
1449 Tmp1 = LHSHi;
1450 Tmp2 = RHSHi;
1451 break;
1452 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001453
Chris Lattnerdc750592005-01-07 07:47:09 +00001454 // FIXME: This generated code sucks.
1455 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001456 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001457 default: assert(0 && "Unknown integer setcc!");
1458 case ISD::SETLT:
1459 case ISD::SETULT: LowCC = ISD::SETULT; break;
1460 case ISD::SETGT:
1461 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1462 case ISD::SETLE:
1463 case ISD::SETULE: LowCC = ISD::SETULE; break;
1464 case ISD::SETGE:
1465 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1466 }
Misha Brukman835702a2005-04-21 22:36:52 +00001467
Chris Lattnerdc750592005-01-07 07:47:09 +00001468 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1469 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1470 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1471
1472 // NOTE: on targets without efficient SELECT of bools, we can always use
1473 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001474 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1475 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1476 Node->getOperand(2));
1477 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001478 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1479 Result, Tmp1, Tmp2));
1480 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001481 }
1482 }
Nate Begeman987121a2005-08-23 04:29:48 +00001483
1484 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1485 default:
1486 assert(0 && "Cannot handle this action for SETCC yet!");
1487 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001488 case TargetLowering::Promote: {
1489 // First step, figure out the appropriate operation to use.
1490 // Allow SETCC to not be supported for all legal data types
1491 // Mostly this targets FP
1492 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1493 MVT::ValueType OldVT = NewInTy;
1494
1495 // Scan for the appropriate larger type to use.
1496 while (1) {
1497 NewInTy = (MVT::ValueType)(NewInTy+1);
1498
1499 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1500 "Fell off of the edge of the integer world");
1501 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1502 "Fell off of the edge of the floating point world");
1503
1504 // If the target supports SETCC of this type, use it.
1505 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1506 break;
1507 }
1508 if (MVT::isInteger(NewInTy))
1509 assert(0 && "Cannot promote Legal Integer SETCC yet");
1510 else {
1511 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1512 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1513 }
1514
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001515 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1516 Node->getOperand(2));
1517 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001518 }
Nate Begeman987121a2005-08-23 04:29:48 +00001519 case TargetLowering::Legal:
1520 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1521 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1522 Node->getOperand(2));
1523 break;
1524 case TargetLowering::Expand:
1525 // Expand a setcc node into a select_cc of the same condition, lhs, and
1526 // rhs that selects between const 1 (true) and const 0 (false).
1527 MVT::ValueType VT = Node->getValueType(0);
1528 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1529 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1530 Node->getOperand(2));
1531 Result = LegalizeOp(Result);
1532 break;
1533 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001534 break;
1535
Chris Lattner85d70c62005-01-11 05:57:22 +00001536 case ISD::MEMSET:
1537 case ISD::MEMCPY:
1538 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001539 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001540 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1541
1542 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1543 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1544 case Expand: assert(0 && "Cannot expand a byte!");
1545 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001546 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001547 break;
1548 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001549 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001550 break;
1551 }
1552 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001553 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001554 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001555
1556 SDOperand Tmp4;
1557 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001558 case Expand: {
1559 // Length is too big, just take the lo-part of the length.
1560 SDOperand HiPart;
1561 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1562 break;
1563 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001564 case Legal:
1565 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001566 break;
1567 case Promote:
1568 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001569 break;
1570 }
1571
1572 SDOperand Tmp5;
1573 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1574 case Expand: assert(0 && "Cannot expand this yet!");
1575 case Legal:
1576 Tmp5 = LegalizeOp(Node->getOperand(4));
1577 break;
1578 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001579 Tmp5 = PromoteOp(Node->getOperand(4));
1580 break;
1581 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001582
1583 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1584 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001585 case TargetLowering::Custom: {
1586 SDOperand Tmp =
1587 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1588 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1589 if (Tmp.Val) {
1590 Result = LegalizeOp(Tmp);
1591 break;
1592 }
1593 // FALLTHROUGH if the target thinks it is legal.
1594 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001595 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001596 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1597 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1598 Tmp5 != Node->getOperand(4)) {
1599 std::vector<SDOperand> Ops;
1600 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1601 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1602 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1603 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001604 break;
1605 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001606 // Otherwise, the target does not support this operation. Lower the
1607 // operation to an explicit libcall as appropriate.
1608 MVT::ValueType IntPtr = TLI.getPointerTy();
1609 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1610 std::vector<std::pair<SDOperand, const Type*> > Args;
1611
Reid Spencer6dced922005-01-12 14:53:45 +00001612 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001613 if (Node->getOpcode() == ISD::MEMSET) {
1614 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1615 // Extend the ubyte argument to be an int value for the call.
1616 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1617 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1618 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1619
1620 FnName = "memset";
1621 } else if (Node->getOpcode() == ISD::MEMCPY ||
1622 Node->getOpcode() == ISD::MEMMOVE) {
1623 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1624 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1625 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1626 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1627 } else {
1628 assert(0 && "Unknown op!");
1629 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001630
Chris Lattner85d70c62005-01-11 05:57:22 +00001631 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001632 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001633 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001634 Result = CallResult.second;
1635 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001636 break;
1637 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001638 }
1639 break;
1640 }
Chris Lattner5385db52005-05-09 20:23:03 +00001641
1642 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001643 Tmp1 = LegalizeOp(Node->getOperand(0));
1644 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001645
Chris Lattner86535992005-05-14 07:45:46 +00001646 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1647 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1648 std::vector<SDOperand> Ops;
1649 Ops.push_back(Tmp1);
1650 Ops.push_back(Tmp2);
1651 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1652 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001653 Result = SDOperand(Node, 0);
1654 // Since these produce two values, make sure to remember that we legalized
1655 // both of them.
1656 AddLegalizedOperand(SDOperand(Node, 0), Result);
1657 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1658 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001659 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001660 Tmp1 = LegalizeOp(Node->getOperand(0));
1661 Tmp2 = LegalizeOp(Node->getOperand(1));
1662 Tmp3 = LegalizeOp(Node->getOperand(2));
1663 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1664 Tmp3 != Node->getOperand(2))
1665 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1666 break;
1667
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001668 case ISD::READIO:
1669 Tmp1 = LegalizeOp(Node->getOperand(0));
1670 Tmp2 = LegalizeOp(Node->getOperand(1));
1671
1672 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1673 case TargetLowering::Custom:
1674 default: assert(0 && "This action not implemented for this operation!");
1675 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001676 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1677 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1678 std::vector<SDOperand> Ops;
1679 Ops.push_back(Tmp1);
1680 Ops.push_back(Tmp2);
1681 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1682 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001683 Result = SDOperand(Node, 0);
1684 break;
1685 case TargetLowering::Expand:
1686 // Replace this with a load from memory.
1687 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1688 Node->getOperand(1), DAG.getSrcValue(NULL));
1689 Result = LegalizeOp(Result);
1690 break;
1691 }
1692
1693 // Since these produce two values, make sure to remember that we legalized
1694 // both of them.
1695 AddLegalizedOperand(SDOperand(Node, 0), Result);
1696 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1697 return Result.getValue(Op.ResNo);
1698
1699 case ISD::WRITEIO:
1700 Tmp1 = LegalizeOp(Node->getOperand(0));
1701 Tmp2 = LegalizeOp(Node->getOperand(1));
1702 Tmp3 = LegalizeOp(Node->getOperand(2));
1703
1704 switch (TLI.getOperationAction(Node->getOpcode(),
1705 Node->getOperand(1).getValueType())) {
1706 case TargetLowering::Custom:
1707 default: assert(0 && "This action not implemented for this operation!");
1708 case TargetLowering::Legal:
1709 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1710 Tmp3 != Node->getOperand(2))
1711 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1712 break;
1713 case TargetLowering::Expand:
1714 // Replace this with a store to memory.
1715 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1716 Node->getOperand(1), Node->getOperand(2),
1717 DAG.getSrcValue(NULL));
1718 Result = LegalizeOp(Result);
1719 break;
1720 }
1721 break;
1722
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001723 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001724 case ISD::SUB_PARTS:
1725 case ISD::SHL_PARTS:
1726 case ISD::SRA_PARTS:
1727 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001728 std::vector<SDOperand> Ops;
1729 bool Changed = false;
1730 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1731 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1732 Changed |= Ops.back() != Node->getOperand(i);
1733 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001734 if (Changed) {
1735 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1736 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1737 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001738
1739 // Since these produce multiple values, make sure to remember that we
1740 // legalized all of them.
1741 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1742 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1743 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001744 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001745
1746 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001747 case ISD::ADD:
1748 case ISD::SUB:
1749 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001750 case ISD::MULHS:
1751 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001752 case ISD::UDIV:
1753 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001754 case ISD::AND:
1755 case ISD::OR:
1756 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001757 case ISD::SHL:
1758 case ISD::SRL:
1759 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001760 case ISD::FADD:
1761 case ISD::FSUB:
1762 case ISD::FMUL:
1763 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001764 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001765 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1766 case Expand: assert(0 && "Not possible");
1767 case Legal:
1768 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1769 break;
1770 case Promote:
1771 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1772 break;
1773 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001774 if (Tmp1 != Node->getOperand(0) ||
1775 Tmp2 != Node->getOperand(1))
1776 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1777 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001778
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001779 case ISD::BUILD_PAIR: {
1780 MVT::ValueType PairTy = Node->getValueType(0);
1781 // TODO: handle the case where the Lo and Hi operands are not of legal type
1782 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1783 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1784 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1785 case TargetLowering::Legal:
1786 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1787 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1788 break;
1789 case TargetLowering::Promote:
1790 case TargetLowering::Custom:
1791 assert(0 && "Cannot promote/custom this yet!");
1792 case TargetLowering::Expand:
1793 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1794 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1795 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1796 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1797 TLI.getShiftAmountTy()));
1798 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1799 break;
1800 }
1801 break;
1802 }
1803
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001804 case ISD::UREM:
1805 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001806 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001807 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1808 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1809 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1810 case TargetLowering::Legal:
1811 if (Tmp1 != Node->getOperand(0) ||
1812 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001813 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001814 Tmp2);
1815 break;
1816 case TargetLowering::Promote:
1817 case TargetLowering::Custom:
1818 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001819 case TargetLowering::Expand:
1820 if (MVT::isInteger(Node->getValueType(0))) {
1821 MVT::ValueType VT = Node->getValueType(0);
1822 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1823 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1824 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1825 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1826 } else {
1827 // Floating point mod -> fmod libcall.
1828 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1829 SDOperand Dummy;
1830 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001831 }
1832 break;
1833 }
1834 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001835
Andrew Lenharth5e177822005-05-03 17:19:30 +00001836 case ISD::CTPOP:
1837 case ISD::CTTZ:
1838 case ISD::CTLZ:
1839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1840 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1841 case TargetLowering::Legal:
1842 if (Tmp1 != Node->getOperand(0))
1843 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1844 break;
1845 case TargetLowering::Promote: {
1846 MVT::ValueType OVT = Tmp1.getValueType();
1847 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001848
1849 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001850 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1851 // Perform the larger operation, then subtract if needed.
1852 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1853 switch(Node->getOpcode())
1854 {
1855 case ISD::CTPOP:
1856 Result = Tmp1;
1857 break;
1858 case ISD::CTTZ:
1859 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001860 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1861 DAG.getConstant(getSizeInBits(NVT), NVT),
1862 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001863 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001864 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1865 break;
1866 case ISD::CTLZ:
1867 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001868 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1869 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001870 getSizeInBits(OVT), NVT));
1871 break;
1872 }
1873 break;
1874 }
1875 case TargetLowering::Custom:
1876 assert(0 && "Cannot custom handle this yet!");
1877 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001878 switch(Node->getOpcode())
1879 {
1880 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001881 static const uint64_t mask[6] = {
1882 0x5555555555555555ULL, 0x3333333333333333ULL,
1883 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1884 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1885 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001886 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001887 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1888 unsigned len = getSizeInBits(VT);
1889 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001890 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001891 Tmp2 = DAG.getConstant(mask[i], VT);
1892 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001893 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001894 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1895 DAG.getNode(ISD::AND, VT,
1896 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1897 Tmp2));
1898 }
1899 Result = Tmp1;
1900 break;
1901 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001902 case ISD::CTLZ: {
1903 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001904 x = x | (x >> 1);
1905 x = x | (x >> 2);
1906 ...
1907 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001908 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001909 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001910
Chris Lattner56add052005-05-11 18:35:21 +00001911 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1912 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001913 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1914 unsigned len = getSizeInBits(VT);
1915 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1916 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001917 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001918 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1919 }
1920 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001921 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001922 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001923 }
1924 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001925 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001926 // unless the target has ctlz but not ctpop, in which case we use:
1927 // { return 32 - nlz(~x & (x-1)); }
1928 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001929 MVT::ValueType VT = Tmp1.getValueType();
1930 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001931 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001932 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1933 DAG.getNode(ISD::SUB, VT, Tmp1,
1934 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001935 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001936 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1937 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001938 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001939 DAG.getConstant(getSizeInBits(VT), VT),
1940 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1941 } else {
1942 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1943 }
Chris Lattner72473242005-05-11 05:27:09 +00001944 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001945 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001946 default:
1947 assert(0 && "Cannot expand this yet!");
1948 break;
1949 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001950 break;
1951 }
1952 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001953
Chris Lattner13fe99c2005-04-02 05:00:07 +00001954 // Unary operators
1955 case ISD::FABS:
1956 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001957 case ISD::FSQRT:
1958 case ISD::FSIN:
1959 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001960 Tmp1 = LegalizeOp(Node->getOperand(0));
1961 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1962 case TargetLowering::Legal:
1963 if (Tmp1 != Node->getOperand(0))
1964 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1965 break;
1966 case TargetLowering::Promote:
1967 case TargetLowering::Custom:
1968 assert(0 && "Cannot promote/custom handle this yet!");
1969 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001970 switch(Node->getOpcode()) {
1971 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001972 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1973 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001974 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00001975 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001976 break;
1977 }
1978 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001979 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1980 MVT::ValueType VT = Node->getValueType(0);
1981 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001982 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001983 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1984 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1985 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001986 break;
1987 }
1988 case ISD::FSQRT:
1989 case ISD::FSIN:
1990 case ISD::FCOS: {
1991 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001992 const char *FnName = 0;
1993 switch(Node->getOpcode()) {
1994 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1995 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1996 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1997 default: assert(0 && "Unreachable!");
1998 }
Nate Begeman77558da2005-08-04 21:43:28 +00001999 SDOperand Dummy;
2000 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002001 break;
2002 }
2003 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002004 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002005 }
2006 break;
2007 }
2008 break;
2009
2010 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002011 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002012 case ISD::UINT_TO_FP: {
2013 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002014 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2015 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002016 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002017 Node->getOperand(0).getValueType())) {
2018 default: assert(0 && "Unknown operation action!");
2019 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002020 Result = ExpandLegalINT_TO_FP(isSigned,
2021 LegalizeOp(Node->getOperand(0)),
2022 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002023 AddLegalizedOperand(Op, Result);
2024 return Result;
2025 case TargetLowering::Promote:
2026 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2027 Node->getValueType(0),
2028 isSigned);
2029 AddLegalizedOperand(Op, Result);
2030 return Result;
2031 case TargetLowering::Legal:
2032 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002033 case TargetLowering::Custom: {
2034 Tmp1 = LegalizeOp(Node->getOperand(0));
2035 SDOperand Tmp =
2036 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2037 Tmp = TLI.LowerOperation(Tmp, DAG);
2038 if (Tmp.Val) {
2039 AddLegalizedOperand(Op, Tmp);
2040 NeedsAnotherIteration = true;
2041 return Tmp;
2042 } else {
2043 assert(0 && "Target Must Lower this");
2044 }
2045 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002046 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002047
Chris Lattnerdc750592005-01-07 07:47:09 +00002048 Tmp1 = LegalizeOp(Node->getOperand(0));
2049 if (Tmp1 != Node->getOperand(0))
2050 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2051 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002052 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002053 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2054 Node->getValueType(0), Node->getOperand(0));
2055 break;
2056 case Promote:
2057 if (isSigned) {
2058 Result = PromoteOp(Node->getOperand(0));
2059 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2060 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2061 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2062 } else {
2063 Result = PromoteOp(Node->getOperand(0));
2064 Result = DAG.getZeroExtendInReg(Result,
2065 Node->getOperand(0).getValueType());
2066 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002067 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002068 break;
2069 }
2070 break;
2071 }
2072 case ISD::TRUNCATE:
2073 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2074 case Legal:
2075 Tmp1 = LegalizeOp(Node->getOperand(0));
2076 if (Tmp1 != Node->getOperand(0))
2077 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2078 break;
2079 case Expand:
2080 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2081
2082 // Since the result is legal, we should just be able to truncate the low
2083 // part of the source.
2084 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2085 break;
2086 case Promote:
2087 Result = PromoteOp(Node->getOperand(0));
2088 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2089 break;
2090 }
2091 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002092
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002093 case ISD::FP_TO_SINT:
2094 case ISD::FP_TO_UINT:
2095 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2096 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002097 Tmp1 = LegalizeOp(Node->getOperand(0));
2098
Chris Lattner44fe26f2005-07-29 00:11:56 +00002099 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2100 default: assert(0 && "Unknown operation action!");
2101 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002102 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2103 SDOperand True, False;
2104 MVT::ValueType VT = Node->getOperand(0).getValueType();
2105 MVT::ValueType NVT = Node->getValueType(0);
2106 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2107 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2108 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2109 Node->getOperand(0), Tmp2, ISD::SETLT);
2110 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2111 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002112 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002113 Tmp2));
2114 False = DAG.getNode(ISD::XOR, NVT, False,
2115 DAG.getConstant(1ULL << ShiftAmt, NVT));
2116 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002117 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002118 } else {
2119 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2120 }
2121 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002122 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002123 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002124 Node->getOpcode() == ISD::FP_TO_SINT);
2125 AddLegalizedOperand(Op, Result);
2126 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002127 case TargetLowering::Custom: {
2128 SDOperand Tmp =
2129 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2130 Tmp = TLI.LowerOperation(Tmp, DAG);
2131 if (Tmp.Val) {
2132 AddLegalizedOperand(Op, Tmp);
2133 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002134 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002135 } else {
2136 // The target thinks this is legal afterall.
2137 break;
2138 }
2139 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002140 case TargetLowering::Legal:
2141 break;
2142 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002143
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002144 if (Tmp1 != Node->getOperand(0))
2145 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2146 break;
2147 case Expand:
2148 assert(0 && "Shouldn't need to expand other operators here!");
2149 case Promote:
2150 Result = PromoteOp(Node->getOperand(0));
2151 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2152 break;
2153 }
2154 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002155
Chris Lattner7753f172005-09-02 00:18:10 +00002156 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002157 case ISD::ZERO_EXTEND:
2158 case ISD::SIGN_EXTEND:
2159 case ISD::FP_EXTEND:
2160 case ISD::FP_ROUND:
2161 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2162 case Legal:
2163 Tmp1 = LegalizeOp(Node->getOperand(0));
2164 if (Tmp1 != Node->getOperand(0))
2165 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2166 break;
2167 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002168 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002169
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002170 case Promote:
2171 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002172 case ISD::ANY_EXTEND:
2173 Result = PromoteOp(Node->getOperand(0));
2174 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2175 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002176 case ISD::ZERO_EXTEND:
2177 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002178 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002179 Result = DAG.getZeroExtendInReg(Result,
2180 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002181 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002182 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002183 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002184 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002185 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002186 Result,
2187 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002188 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002189 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002190 Result = PromoteOp(Node->getOperand(0));
2191 if (Result.getValueType() != Op.getValueType())
2192 // Dynamically dead while we have only 2 FP types.
2193 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2194 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002195 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002196 Result = PromoteOp(Node->getOperand(0));
2197 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2198 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002199 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002200 }
2201 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002202 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002203 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002204 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002205 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002206
2207 // If this operation is not supported, convert it to a shl/shr or load/store
2208 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002209 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2210 default: assert(0 && "This action not supported for this op yet!");
2211 case TargetLowering::Legal:
2212 if (Tmp1 != Node->getOperand(0))
2213 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002214 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002215 break;
2216 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002217 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002218 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002219 // NOTE: we could fall back on load/store here too for targets without
2220 // SAR. However, it is doubtful that any exist.
2221 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2222 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002223 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002224 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2225 Node->getOperand(0), ShiftCst);
2226 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2227 Result, ShiftCst);
2228 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2229 // The only way we can lower this is to turn it into a STORETRUNC,
2230 // EXTLOAD pair, targetting a temporary location (a stack slot).
2231
2232 // NOTE: there is a choice here between constantly creating new stack
2233 // slots and always reusing the same one. We currently always create
2234 // new ones, as reuse may inhibit scheduling.
2235 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2236 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2237 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2238 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002239 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002240 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2241 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2242 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002243 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002244 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002245 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2246 Result, StackSlot, DAG.getSrcValue(NULL),
2247 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002248 } else {
2249 assert(0 && "Unknown op");
2250 }
2251 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002252 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002253 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002254 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002255 }
Chris Lattner99222f72005-01-15 07:15:18 +00002256 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002257
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002258 // Note that LegalizeOp may be reentered even from single-use nodes, which
2259 // means that we always must cache transformed nodes.
2260 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002261 return Result;
2262}
2263
Chris Lattner4d978642005-01-15 22:16:26 +00002264/// PromoteOp - Given an operation that produces a value in an invalid type,
2265/// promote it to compute the value into a larger type. The produced value will
2266/// have the correct bits for the low portion of the register, but no guarantee
2267/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002268SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2269 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002270 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002271 assert(getTypeAction(VT) == Promote &&
2272 "Caller should expand or legalize operands that are not promotable!");
2273 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2274 "Cannot promote to smaller type!");
2275
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002276 SDOperand Tmp1, Tmp2, Tmp3;
2277
2278 SDOperand Result;
2279 SDNode *Node = Op.Val;
2280
Chris Lattner1a570f12005-09-02 20:32:45 +00002281 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2282 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002283
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002284 // Promotion needs an optimization step to clean up after it, and is not
2285 // careful to avoid operations the target does not support. Make sure that
2286 // all generated operations are legalized in the next iteration.
2287 NeedsAnotherIteration = true;
2288
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002289 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002290 case ISD::CopyFromReg:
2291 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002292 default:
2293 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2294 assert(0 && "Do not know how to promote this operator!");
2295 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002296 case ISD::UNDEF:
2297 Result = DAG.getNode(ISD::UNDEF, NVT);
2298 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002299 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002300 if (VT != MVT::i1)
2301 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2302 else
2303 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002304 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2305 break;
2306 case ISD::ConstantFP:
2307 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2308 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2309 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002310
Chris Lattner2cb338d2005-01-18 02:59:52 +00002311 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002312 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002313 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2314 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002315 Result = LegalizeOp(Result);
2316 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002317
2318 case ISD::TRUNCATE:
2319 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2320 case Legal:
2321 Result = LegalizeOp(Node->getOperand(0));
2322 assert(Result.getValueType() >= NVT &&
2323 "This truncation doesn't make sense!");
2324 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2325 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2326 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002327 case Promote:
2328 // The truncation is not required, because we don't guarantee anything
2329 // about high bits anyway.
2330 Result = PromoteOp(Node->getOperand(0));
2331 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002332 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002333 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2334 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002335 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002336 }
2337 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002338 case ISD::SIGN_EXTEND:
2339 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002340 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002341 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2342 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2343 case Legal:
2344 // Input is legal? Just do extend all the way to the larger type.
2345 Result = LegalizeOp(Node->getOperand(0));
2346 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2347 break;
2348 case Promote:
2349 // Promote the reg if it's smaller.
2350 Result = PromoteOp(Node->getOperand(0));
2351 // The high bits are not guaranteed to be anything. Insert an extend.
2352 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002353 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002354 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002355 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002356 Result = DAG.getZeroExtendInReg(Result,
2357 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002358 break;
2359 }
2360 break;
2361
2362 case ISD::FP_EXTEND:
2363 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2364 case ISD::FP_ROUND:
2365 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2366 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2367 case Promote: assert(0 && "Unreachable with 2 FP types!");
2368 case Legal:
2369 // Input is legal? Do an FP_ROUND_INREG.
2370 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002371 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2372 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002373 break;
2374 }
2375 break;
2376
2377 case ISD::SINT_TO_FP:
2378 case ISD::UINT_TO_FP:
2379 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2380 case Legal:
2381 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002382 // No extra round required here.
2383 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002384 break;
2385
2386 case Promote:
2387 Result = PromoteOp(Node->getOperand(0));
2388 if (Node->getOpcode() == ISD::SINT_TO_FP)
2389 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002390 Result,
2391 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002392 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002393 Result = DAG.getZeroExtendInReg(Result,
2394 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002395 // No extra round required here.
2396 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002397 break;
2398 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002399 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2400 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002401 // Round if we cannot tolerate excess precision.
2402 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002403 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2404 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002405 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002406 }
Chris Lattner4d978642005-01-15 22:16:26 +00002407 break;
2408
Chris Lattner268d4572005-12-09 17:32:47 +00002409 case ISD::SIGN_EXTEND_INREG:
2410 Result = PromoteOp(Node->getOperand(0));
2411 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2412 Node->getOperand(1));
2413 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002414 case ISD::FP_TO_SINT:
2415 case ISD::FP_TO_UINT:
2416 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2417 case Legal:
2418 Tmp1 = LegalizeOp(Node->getOperand(0));
2419 break;
2420 case Promote:
2421 // The input result is prerounded, so we don't have to do anything
2422 // special.
2423 Tmp1 = PromoteOp(Node->getOperand(0));
2424 break;
2425 case Expand:
2426 assert(0 && "not implemented");
2427 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002428 // If we're promoting a UINT to a larger size, check to see if the new node
2429 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2430 // we can use that instead. This allows us to generate better code for
2431 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2432 // legal, such as PowerPC.
2433 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002434 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002435 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2436 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002437 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2438 } else {
2439 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2440 }
Chris Lattner4d978642005-01-15 22:16:26 +00002441 break;
2442
Chris Lattner13fe99c2005-04-02 05:00:07 +00002443 case ISD::FABS:
2444 case ISD::FNEG:
2445 Tmp1 = PromoteOp(Node->getOperand(0));
2446 assert(Tmp1.getValueType() == NVT);
2447 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2448 // NOTE: we do not have to do any extra rounding here for
2449 // NoExcessFPPrecision, because we know the input will have the appropriate
2450 // precision, and these operations don't modify precision at all.
2451 break;
2452
Chris Lattner9d6fa982005-04-28 21:44:33 +00002453 case ISD::FSQRT:
2454 case ISD::FSIN:
2455 case ISD::FCOS:
2456 Tmp1 = PromoteOp(Node->getOperand(0));
2457 assert(Tmp1.getValueType() == NVT);
2458 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2459 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002460 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2461 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002462 break;
2463
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002464 case ISD::AND:
2465 case ISD::OR:
2466 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002467 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002468 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002469 case ISD::MUL:
2470 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002471 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002472 // that too is okay if they are integer operations.
2473 Tmp1 = PromoteOp(Node->getOperand(0));
2474 Tmp2 = PromoteOp(Node->getOperand(1));
2475 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2476 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002477 break;
2478 case ISD::FADD:
2479 case ISD::FSUB:
2480 case ISD::FMUL:
2481 // The input may have strange things in the top bits of the registers, but
2482 // these operations don't care.
2483 Tmp1 = PromoteOp(Node->getOperand(0));
2484 Tmp2 = PromoteOp(Node->getOperand(1));
2485 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2486 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2487
2488 // Floating point operations will give excess precision that we may not be
2489 // able to tolerate. If we DO allow excess precision, just leave it,
2490 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002491 // FIXME: Why would we need to round FP ops more than integer ones?
2492 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002493 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002494 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2495 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002496 break;
2497
Chris Lattner4d978642005-01-15 22:16:26 +00002498 case ISD::SDIV:
2499 case ISD::SREM:
2500 // These operators require that their input be sign extended.
2501 Tmp1 = PromoteOp(Node->getOperand(0));
2502 Tmp2 = PromoteOp(Node->getOperand(1));
2503 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002504 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2505 DAG.getValueType(VT));
2506 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2507 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002508 }
2509 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2510
2511 // Perform FP_ROUND: this is probably overly pessimistic.
2512 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002513 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2514 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002515 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002516 case ISD::FDIV:
2517 case ISD::FREM:
2518 // These operators require that their input be fp extended.
2519 Tmp1 = PromoteOp(Node->getOperand(0));
2520 Tmp2 = PromoteOp(Node->getOperand(1));
2521 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2522
2523 // Perform FP_ROUND: this is probably overly pessimistic.
2524 if (NoExcessFPPrecision)
2525 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2526 DAG.getValueType(VT));
2527 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002528
2529 case ISD::UDIV:
2530 case ISD::UREM:
2531 // These operators require that their input be zero extended.
2532 Tmp1 = PromoteOp(Node->getOperand(0));
2533 Tmp2 = PromoteOp(Node->getOperand(1));
2534 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002535 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2536 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002537 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2538 break;
2539
2540 case ISD::SHL:
2541 Tmp1 = PromoteOp(Node->getOperand(0));
2542 Tmp2 = LegalizeOp(Node->getOperand(1));
2543 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2544 break;
2545 case ISD::SRA:
2546 // The input value must be properly sign extended.
2547 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002548 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2549 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002550 Tmp2 = LegalizeOp(Node->getOperand(1));
2551 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2552 break;
2553 case ISD::SRL:
2554 // The input value must be properly zero extended.
2555 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002556 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002557 Tmp2 = LegalizeOp(Node->getOperand(1));
2558 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2559 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002560 case ISD::LOAD:
2561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2562 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002563 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2564 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002565 // Remember that we legalized the chain.
2566 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2567 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002568 case ISD::SEXTLOAD:
2569 case ISD::ZEXTLOAD:
2570 case ISD::EXTLOAD:
2571 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2572 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002573 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2574 Node->getOperand(2),
2575 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002576 // Remember that we legalized the chain.
2577 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2578 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002579 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002580 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2581 case Expand: assert(0 && "It's impossible to expand bools");
2582 case Legal:
2583 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2584 break;
2585 case Promote:
2586 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2587 break;
2588 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002589 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2590 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2591 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2592 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002593 case ISD::SELECT_CC:
2594 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2595 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2596 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2597 Node->getOperand(1), Tmp2, Tmp3,
2598 Node->getOperand(4));
2599 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002600 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002601 case ISD::CALL: {
2602 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2603 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2604
Chris Lattner3d95c142005-01-19 20:24:35 +00002605 std::vector<SDOperand> Ops;
2606 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2607 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2608
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002609 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2610 "Can only promote single result calls");
2611 std::vector<MVT::ValueType> RetTyVTs;
2612 RetTyVTs.reserve(2);
2613 RetTyVTs.push_back(NVT);
2614 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002615 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2616 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002617 Result = SDOperand(NC, 0);
2618
2619 // Insert the new chain mapping.
2620 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2621 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002622 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002623 case ISD::CTPOP:
2624 case ISD::CTTZ:
2625 case ISD::CTLZ:
2626 Tmp1 = Node->getOperand(0);
2627 //Zero extend the argument
2628 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2629 // Perform the larger operation, then subtract if needed.
2630 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2631 switch(Node->getOpcode())
2632 {
2633 case ISD::CTPOP:
2634 Result = Tmp1;
2635 break;
2636 case ISD::CTTZ:
2637 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002638 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002639 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002640 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002641 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2642 break;
2643 case ISD::CTLZ:
2644 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002645 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2646 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002647 getSizeInBits(VT), NVT));
2648 break;
2649 }
2650 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002651 }
2652
2653 assert(Result.Val && "Didn't set a result!");
2654 AddPromotedOperand(Op, Result);
2655 return Result;
2656}
Chris Lattnerdc750592005-01-07 07:47:09 +00002657
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002658/// ExpandAddSub - Find a clever way to expand this add operation into
2659/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002660void SelectionDAGLegalize::
2661ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2662 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002663 // Expand the subcomponents.
2664 SDOperand LHSL, LHSH, RHSL, RHSH;
2665 ExpandOp(LHS, LHSL, LHSH);
2666 ExpandOp(RHS, RHSL, RHSH);
2667
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002668 std::vector<SDOperand> Ops;
2669 Ops.push_back(LHSL);
2670 Ops.push_back(LHSH);
2671 Ops.push_back(RHSL);
2672 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002673 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2674 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002675 Hi = Lo.getValue(1);
2676}
2677
Chris Lattner4157c412005-04-02 04:00:59 +00002678void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2679 SDOperand Op, SDOperand Amt,
2680 SDOperand &Lo, SDOperand &Hi) {
2681 // Expand the subcomponents.
2682 SDOperand LHSL, LHSH;
2683 ExpandOp(Op, LHSL, LHSH);
2684
2685 std::vector<SDOperand> Ops;
2686 Ops.push_back(LHSL);
2687 Ops.push_back(LHSH);
2688 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002689 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002690 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002691 Hi = Lo.getValue(1);
2692}
2693
2694
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002695/// ExpandShift - Try to find a clever way to expand this shift operation out to
2696/// smaller elements. If we can't find a way that is more efficient than a
2697/// libcall on this target, return false. Otherwise, return true with the
2698/// low-parts expanded into Lo and Hi.
2699bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2700 SDOperand &Lo, SDOperand &Hi) {
2701 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2702 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002703
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002704 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002705 SDOperand ShAmt = LegalizeOp(Amt);
2706 MVT::ValueType ShTy = ShAmt.getValueType();
2707 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2708 unsigned NVTBits = MVT::getSizeInBits(NVT);
2709
2710 // Handle the case when Amt is an immediate. Other cases are currently broken
2711 // and are disabled.
2712 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2713 unsigned Cst = CN->getValue();
2714 // Expand the incoming operand to be shifted, so that we have its parts
2715 SDOperand InL, InH;
2716 ExpandOp(Op, InL, InH);
2717 switch(Opc) {
2718 case ISD::SHL:
2719 if (Cst > VTBits) {
2720 Lo = DAG.getConstant(0, NVT);
2721 Hi = DAG.getConstant(0, NVT);
2722 } else if (Cst > NVTBits) {
2723 Lo = DAG.getConstant(0, NVT);
2724 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002725 } else if (Cst == NVTBits) {
2726 Lo = DAG.getConstant(0, NVT);
2727 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002728 } else {
2729 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2730 Hi = DAG.getNode(ISD::OR, NVT,
2731 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2732 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2733 }
2734 return true;
2735 case ISD::SRL:
2736 if (Cst > VTBits) {
2737 Lo = DAG.getConstant(0, NVT);
2738 Hi = DAG.getConstant(0, NVT);
2739 } else if (Cst > NVTBits) {
2740 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2741 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002742 } else if (Cst == NVTBits) {
2743 Lo = InH;
2744 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002745 } else {
2746 Lo = DAG.getNode(ISD::OR, NVT,
2747 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2748 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2749 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2750 }
2751 return true;
2752 case ISD::SRA:
2753 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002754 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002755 DAG.getConstant(NVTBits-1, ShTy));
2756 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002757 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002758 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002759 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002760 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002761 } else if (Cst == NVTBits) {
2762 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002763 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002764 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002765 } else {
2766 Lo = DAG.getNode(ISD::OR, NVT,
2767 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2768 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2769 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2770 }
2771 return true;
2772 }
2773 }
2774 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2775 // so disable it for now. Currently targets are handling this via SHL_PARTS
2776 // and friends.
2777 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002778
2779 // If we have an efficient select operation (or if the selects will all fold
2780 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002781 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002782 return false;
2783
2784 SDOperand InL, InH;
2785 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002786 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2787 DAG.getConstant(NVTBits, ShTy), ShAmt);
2788
Chris Lattner4d25c042005-01-20 20:29:23 +00002789 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002790 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2791 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002792
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002793 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2794 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2795 DAG.getConstant(NVTBits-1, ShTy));
2796 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2797 DAG.getConstant(NVTBits-1, ShTy));
2798 }
2799
2800 if (Opc == ISD::SHL) {
2801 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2802 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2803 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002804 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002805
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002806 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2807 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2808 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002809 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002810 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2811 DAG.getConstant(32, ShTy),
2812 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002813 DAG.getConstant(0, NVT),
2814 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002815 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002816 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002817 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002818 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002819
2820 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002821 if (Opc == ISD::SRA)
2822 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2823 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002824 else
2825 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002826 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002827 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002828 }
2829 return true;
2830}
Chris Lattneraac464e2005-01-21 06:05:23 +00002831
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002832/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2833/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002834/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002835static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002836 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002837
Chris Lattner2dce7032005-05-12 23:24:06 +00002838 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002839 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002840 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002841 Found = Node;
2842 return;
2843 }
2844
2845 // Otherwise, scan the operands of Node to see if any of them is a call.
2846 assert(Node->getNumOperands() != 0 &&
2847 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002848 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002849 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002850
2851 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002852 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002853 Found);
2854}
2855
2856
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002857/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2858/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002859/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002860static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2861 std::set<SDNode*> &Visited) {
2862 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2863 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002864
Chris Lattner2dce7032005-05-12 23:24:06 +00002865 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002866 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002867 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002868 Found = Node;
2869 return;
2870 }
2871
2872 // Otherwise, scan the operands of Node to see if any of them is a call.
2873 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2874 if (UI == E) return;
2875 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002876 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002877
2878 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002879 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002880}
2881
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002882/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002883/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002884static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002885 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002886 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002887 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002888 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002889
Chris Lattner4add7e32005-01-23 04:42:50 +00002890 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002891 if (TheChain.getValueType() != MVT::Other)
2892 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002893 if (TheChain.getValueType() != MVT::Other)
2894 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002895
2896 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002897 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002898
Chris Lattner4add7e32005-01-23 04:42:50 +00002899 // Make sure to only follow users of our token chain.
2900 SDNode *User = *UI;
2901 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2902 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002903 if (SDNode *Result = FindCallSeqEnd(User))
2904 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002905 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002906 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002907}
2908
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002909/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002910/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002911static SDNode *FindCallSeqStart(SDNode *Node) {
2912 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002913 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002914
2915 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2916 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002917 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002918}
2919
2920
Chris Lattner4add7e32005-01-23 04:42:50 +00002921/// FindInputOutputChains - If we are replacing an operation with a call we need
2922/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002923/// properly serialize the calls in the block. The returned operand is the
2924/// input chain value for the new call (e.g. the entry node or the previous
2925/// call), and OutChain is set to be the chain node to update to point to the
2926/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002927static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2928 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002929 SDNode *LatestCallSeqStart = Entry.Val;
2930 SDNode *LatestCallSeqEnd = 0;
2931 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2932 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002933
Chris Lattner2dce7032005-05-12 23:24:06 +00002934 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002935 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002936 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2937 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002938 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002939 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002940 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2941 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002942 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002943 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002944 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002945
Chris Lattner06bbeb62005-05-11 19:02:11 +00002946 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002947 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002948 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002949 std::set<SDNode*> Visited;
2950 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002951
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002952 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002953 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002954 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002955
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002956 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002957}
2958
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002959/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002960void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2961 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002962 // Nothing to splice it into?
2963 if (OutChain == 0) return;
2964
2965 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2966 //OutChain->dump();
2967
2968 // Form a token factor node merging the old inval and the new inval.
2969 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2970 OutChain->getOperand(0));
2971 // Change the node to refer to the new token.
2972 OutChain->setAdjCallChain(InToken);
2973}
Chris Lattner4add7e32005-01-23 04:42:50 +00002974
2975
Chris Lattneraac464e2005-01-21 06:05:23 +00002976// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2977// does not fit into a register, return the lo part and set the hi part to the
2978// by-reg argument. If it does fit into a single register, return the result
2979// and leave the Hi part unset.
2980SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2981 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002982 SDNode *OutChain;
2983 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2984 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002985 if (InChain.Val == 0)
2986 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002987
Chris Lattneraac464e2005-01-21 06:05:23 +00002988 TargetLowering::ArgListTy Args;
2989 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2990 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2991 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2992 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2993 }
2994 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002995
Chris Lattner06bbeb62005-05-11 19:02:11 +00002996 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002997 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002998 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002999 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3000 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003001
Chris Lattner63022662005-09-02 20:26:58 +00003002 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003003 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003004 default: assert(0 && "Unknown thing");
3005 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003006 Result = CallInfo.first;
3007 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003008 case Promote:
3009 assert(0 && "Cannot promote this yet!");
3010 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003011 ExpandOp(CallInfo.first, Result, Hi);
3012 CallInfo.second = LegalizeOp(CallInfo.second);
3013 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003014 }
Chris Lattner63022662005-09-02 20:26:58 +00003015
3016 SpliceCallInto(CallInfo.second, OutChain);
3017 NeedsAnotherIteration = true;
3018 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003019}
3020
Chris Lattner4add7e32005-01-23 04:42:50 +00003021
Chris Lattneraac464e2005-01-21 06:05:23 +00003022/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3023/// destination type is legal.
3024SDOperand SelectionDAGLegalize::
3025ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003026 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003027 assert(getTypeAction(Source.getValueType()) == Expand &&
3028 "This is not an expansion!");
3029 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3030
Chris Lattner06bbeb62005-05-11 19:02:11 +00003031 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003032 assert(Source.getValueType() == MVT::i64 &&
3033 "This only works for 64-bit -> FP");
3034 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3035 // incoming integer is set. To handle this, we dynamically test to see if
3036 // it is set, and, if so, add a fudge factor.
3037 SDOperand Lo, Hi;
3038 ExpandOp(Source, Lo, Hi);
3039
Chris Lattner2a4f7312005-05-13 04:45:13 +00003040 // If this is unsigned, and not supported, first perform the conversion to
3041 // signed, then adjust the result if the sign bit is set.
3042 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3043 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3044
Chris Lattnerd47675e2005-08-09 20:20:18 +00003045 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3046 DAG.getConstant(0, Hi.getValueType()),
3047 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003048 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3049 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3050 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003051 uint64_t FF = 0x5f800000ULL;
3052 if (TLI.isLittleEndian()) FF <<= 32;
3053 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003054
Chris Lattnerc30405e2005-08-26 17:15:30 +00003055 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003056 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3057 SDOperand FudgeInReg;
3058 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003059 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3060 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003061 else {
3062 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003063 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3064 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003065 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003066 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003067 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003068
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003069 // Check to see if the target has a custom way to lower this. If so, use it.
3070 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3071 default: assert(0 && "This action not implemented for this operation!");
3072 case TargetLowering::Legal:
3073 case TargetLowering::Expand:
3074 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003075 case TargetLowering::Custom: {
3076 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3077 Source), DAG);
3078 if (NV.Val)
3079 return LegalizeOp(NV);
3080 break; // The target decided this was legal after all
3081 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003082 }
3083
Chris Lattner153587e2005-05-12 07:00:44 +00003084 // Expand the source, then glue it back together for the call. We must expand
3085 // the source in case it is shared (this pass of legalize must traverse it).
3086 SDOperand SrcLo, SrcHi;
3087 ExpandOp(Source, SrcLo, SrcHi);
3088 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3089
Chris Lattner06bbeb62005-05-11 19:02:11 +00003090 SDNode *OutChain = 0;
3091 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3092 DAG.getEntryNode());
3093 const char *FnName = 0;
3094 if (DestTy == MVT::f32)
3095 FnName = "__floatdisf";
3096 else {
3097 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3098 FnName = "__floatdidf";
3099 }
3100
Chris Lattneraac464e2005-01-21 06:05:23 +00003101 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3102
3103 TargetLowering::ArgListTy Args;
3104 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003105
Chris Lattneraac464e2005-01-21 06:05:23 +00003106 Args.push_back(std::make_pair(Source, ArgTy));
3107
3108 // We don't care about token chains for libcalls. We just use the entry
3109 // node as our input and ignore the output chain. This allows us to place
3110 // calls wherever we need them to satisfy data dependences.
3111 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003112
3113 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003114 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3115 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003116
Chris Lattnera5bf1032005-05-12 04:49:08 +00003117 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003118 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003119}
Misha Brukman835702a2005-04-21 22:36:52 +00003120
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003121
3122
Chris Lattnerdc750592005-01-07 07:47:09 +00003123/// ExpandOp - Expand the specified SDOperand into its two component pieces
3124/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3125/// LegalizeNodes map is filled in for any results that are not expanded, the
3126/// ExpandedNodes map is filled in for any results that are expanded, and the
3127/// Lo/Hi values are returned.
3128void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3129 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003130 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003131 SDNode *Node = Op.Val;
3132 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003133 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3134 "Cannot expand FP values!");
3135 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003136 "Cannot expand to FP value or to larger int value!");
3137
Chris Lattner1a570f12005-09-02 20:32:45 +00003138 // See if we already expanded it.
3139 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3140 = ExpandedNodes.find(Op);
3141 if (I != ExpandedNodes.end()) {
3142 Lo = I->second.first;
3143 Hi = I->second.second;
3144 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003145 }
3146
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003147 // Expanding to multiple registers needs to perform an optimization step, and
3148 // is not careful to avoid operations the target does not support. Make sure
3149 // that all generated operations are legalized in the next iteration.
3150 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003151
Chris Lattnerdc750592005-01-07 07:47:09 +00003152 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003153 case ISD::CopyFromReg:
3154 assert(0 && "CopyFromReg must be legal!");
3155 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003156 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3157 assert(0 && "Do not know how to expand this operator!");
3158 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003159 case ISD::UNDEF:
3160 Lo = DAG.getNode(ISD::UNDEF, NVT);
3161 Hi = DAG.getNode(ISD::UNDEF, NVT);
3162 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003163 case ISD::Constant: {
3164 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3165 Lo = DAG.getConstant(Cst, NVT);
3166 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3167 break;
3168 }
Nate Begemanae89d862005-12-07 19:48:11 +00003169 case ISD::ConstantVec: {
3170 unsigned NumElements = Node->getNumOperands();
3171 // If we only have two elements left in the constant vector, just break it
3172 // apart into the two scalar constants it contains. Otherwise, bisect the
3173 // ConstantVec, and return each half as a new ConstantVec.
3174 // FIXME: this is hard coded as big endian, it may have to change to support
3175 // SSE and Alpha MVI
3176 if (NumElements == 2) {
3177 Hi = Node->getOperand(0);
3178 Lo = Node->getOperand(1);
3179 } else {
3180 NumElements /= 2;
3181 std::vector<SDOperand> LoOps, HiOps;
3182 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3183 HiOps.push_back(Node->getOperand(I));
3184 LoOps.push_back(Node->getOperand(I+NumElements));
3185 }
3186 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3187 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3188 }
3189 break;
3190 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003191
Chris Lattner32e08b72005-03-28 22:03:13 +00003192 case ISD::BUILD_PAIR:
3193 // Legalize both operands. FIXME: in the future we should handle the case
3194 // where the two elements are not legal.
3195 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3196 Lo = LegalizeOp(Node->getOperand(0));
3197 Hi = LegalizeOp(Node->getOperand(1));
3198 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003199
3200 case ISD::SIGN_EXTEND_INREG:
3201 ExpandOp(Node->getOperand(0), Lo, Hi);
3202 // Sign extend the lo-part.
3203 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3204 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3205 TLI.getShiftAmountTy()));
3206 // sext_inreg the low part if needed.
3207 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3208 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003209
Chris Lattner55e9cde2005-05-11 04:51:16 +00003210 case ISD::CTPOP:
3211 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003212 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3213 DAG.getNode(ISD::CTPOP, NVT, Lo),
3214 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003215 Hi = DAG.getConstant(0, NVT);
3216 break;
3217
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003218 case ISD::CTLZ: {
3219 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003220 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003221 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3222 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003223 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3224 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003225 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3226 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3227
3228 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3229 Hi = DAG.getConstant(0, NVT);
3230 break;
3231 }
3232
3233 case ISD::CTTZ: {
3234 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003235 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003236 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3237 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003238 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3239 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003240 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3241 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3242
3243 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3244 Hi = DAG.getConstant(0, NVT);
3245 break;
3246 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003247
Chris Lattnerdc750592005-01-07 07:47:09 +00003248 case ISD::LOAD: {
3249 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3250 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003251 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003252
3253 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003254 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003255 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3256 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003257 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003258 //are from the same instruction?
3259 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003260
3261 // Build a factor node to remember that this load is independent of the
3262 // other one.
3263 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3264 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003265
Chris Lattnerdc750592005-01-07 07:47:09 +00003266 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003267 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003268 if (!TLI.isLittleEndian())
3269 std::swap(Lo, Hi);
3270 break;
3271 }
Nate Begemand37c1312005-11-22 18:16:00 +00003272 case ISD::VLOAD: {
3273 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3274 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3275 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3276 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3277
3278 // If we only have two elements, turn into a pair of scalar loads.
3279 // FIXME: handle case where a vector of two elements is fine, such as
3280 // 2 x double on SSE2.
3281 if (NumElements == 2) {
3282 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3283 // Increment the pointer to the other half.
3284 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3285 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3286 getIntPtrConstant(IncrementSize));
3287 //Is this safe? declaring that the two parts of the split load
3288 //are from the same instruction?
3289 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3290 } else {
3291 NumElements /= 2; // Split the vector in half
3292 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3293 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3294 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3295 getIntPtrConstant(IncrementSize));
3296 //Is this safe? declaring that the two parts of the split load
3297 //are from the same instruction?
3298 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3299 }
3300
3301 // Build a factor node to remember that this load is independent of the
3302 // other one.
3303 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3304 Hi.getValue(1));
3305
3306 // Remember that we legalized the chain.
3307 AddLegalizedOperand(Op.getValue(1), TF);
3308 if (!TLI.isLittleEndian())
3309 std::swap(Lo, Hi);
3310 break;
3311 }
3312 case ISD::VADD:
3313 case ISD::VSUB:
3314 case ISD::VMUL: {
3315 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3316 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3317 SDOperand LL, LH, RL, RH;
3318
3319 ExpandOp(Node->getOperand(0), LL, LH);
3320 ExpandOp(Node->getOperand(1), RL, RH);
3321
3322 // If we only have two elements, turn into a pair of scalar loads.
3323 // FIXME: handle case where a vector of two elements is fine, such as
3324 // 2 x double on SSE2.
3325 if (NumElements == 2) {
3326 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3327 Lo = DAG.getNode(Opc, EVT, LL, RL);
3328 Hi = DAG.getNode(Opc, EVT, LH, RH);
3329 } else {
3330 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3331 LL.getOperand(3));
3332 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3333 LH.getOperand(3));
3334 }
3335 break;
3336 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003337 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003338 case ISD::CALL: {
3339 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3340 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3341
Chris Lattner3d95c142005-01-19 20:24:35 +00003342 bool Changed = false;
3343 std::vector<SDOperand> Ops;
3344 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3345 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3346 Changed |= Ops.back() != Node->getOperand(i);
3347 }
3348
Chris Lattnerdc750592005-01-07 07:47:09 +00003349 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3350 "Can only expand a call once so far, not i64 -> i16!");
3351
3352 std::vector<MVT::ValueType> RetTyVTs;
3353 RetTyVTs.reserve(3);
3354 RetTyVTs.push_back(NVT);
3355 RetTyVTs.push_back(NVT);
3356 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003357 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3358 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003359 Lo = SDOperand(NC, 0);
3360 Hi = SDOperand(NC, 1);
3361
3362 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003363 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003364 break;
3365 }
3366 case ISD::AND:
3367 case ISD::OR:
3368 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3369 SDOperand LL, LH, RL, RH;
3370 ExpandOp(Node->getOperand(0), LL, LH);
3371 ExpandOp(Node->getOperand(1), RL, RH);
3372 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3373 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3374 break;
3375 }
3376 case ISD::SELECT: {
3377 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003378
3379 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3380 case Expand: assert(0 && "It's impossible to expand bools");
3381 case Legal:
3382 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3383 break;
3384 case Promote:
3385 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3386 break;
3387 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003388 ExpandOp(Node->getOperand(1), LL, LH);
3389 ExpandOp(Node->getOperand(2), RL, RH);
3390 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3391 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3392 break;
3393 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003394 case ISD::SELECT_CC: {
3395 SDOperand TL, TH, FL, FH;
3396 ExpandOp(Node->getOperand(2), TL, TH);
3397 ExpandOp(Node->getOperand(3), FL, FH);
3398 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3399 Node->getOperand(1), TL, FL, Node->getOperand(4));
3400 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3401 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003402 Lo = LegalizeOp(Lo);
3403 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003404 break;
3405 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003406 case ISD::SEXTLOAD: {
3407 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3408 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3409 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3410
3411 if (EVT == NVT)
3412 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3413 else
3414 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3415 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003416
3417 // Remember that we legalized the chain.
3418 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3419
Nate Begemanc3a89c52005-10-13 17:15:37 +00003420 // The high part is obtained by SRA'ing all but one of the bits of the lo
3421 // part.
3422 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3423 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3424 TLI.getShiftAmountTy()));
3425 Lo = LegalizeOp(Lo);
3426 Hi = LegalizeOp(Hi);
3427 break;
3428 }
3429 case ISD::ZEXTLOAD: {
3430 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3431 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3432 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3433
3434 if (EVT == NVT)
3435 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3436 else
3437 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3438 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003439
3440 // Remember that we legalized the chain.
3441 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3442
Nate Begemanc3a89c52005-10-13 17:15:37 +00003443 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003444 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003445 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003446 break;
3447 }
3448 case ISD::EXTLOAD: {
3449 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3450 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3451 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3452
3453 if (EVT == NVT)
3454 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3455 else
3456 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3457 EVT);
3458
3459 // Remember that we legalized the chain.
3460 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3461
3462 // The high part is undefined.
3463 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3464 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003465 break;
3466 }
Chris Lattner7753f172005-09-02 00:18:10 +00003467 case ISD::ANY_EXTEND: {
3468 SDOperand In;
3469 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3470 case Expand: assert(0 && "expand-expand not implemented yet!");
3471 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3472 case Promote:
3473 In = PromoteOp(Node->getOperand(0));
3474 break;
3475 }
3476
3477 // The low part is any extension of the input (which degenerates to a copy).
3478 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3479 // The high part is undefined.
3480 Hi = DAG.getNode(ISD::UNDEF, NVT);
3481 break;
3482 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003483 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003484 SDOperand In;
3485 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3486 case Expand: assert(0 && "expand-expand not implemented yet!");
3487 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3488 case Promote:
3489 In = PromoteOp(Node->getOperand(0));
3490 // Emit the appropriate sign_extend_inreg to get the value we want.
3491 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003492 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003493 break;
3494 }
3495
Chris Lattnerdc750592005-01-07 07:47:09 +00003496 // The low part is just a sign extension of the input (which degenerates to
3497 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003498 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003499
Chris Lattnerdc750592005-01-07 07:47:09 +00003500 // The high part is obtained by SRA'ing all but one of the bits of the lo
3501 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003502 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003503 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3504 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003505 break;
3506 }
Chris Lattner47844892005-04-03 23:41:52 +00003507 case ISD::ZERO_EXTEND: {
3508 SDOperand In;
3509 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3510 case Expand: assert(0 && "expand-expand not implemented yet!");
3511 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3512 case Promote:
3513 In = PromoteOp(Node->getOperand(0));
3514 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003515 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003516 break;
3517 }
3518
Chris Lattnerdc750592005-01-07 07:47:09 +00003519 // The low part is just a zero extension of the input (which degenerates to
3520 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003521 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003522
Chris Lattnerdc750592005-01-07 07:47:09 +00003523 // The high part is just a zero.
3524 Hi = DAG.getConstant(0, NVT);
3525 break;
Chris Lattner47844892005-04-03 23:41:52 +00003526 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003527
Chris Lattner44c28c22005-11-20 22:56:56 +00003528 case ISD::READCYCLECOUNTER: {
3529 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3530 TargetLowering::Custom &&
3531 "Must custom expand ReadCycleCounter");
3532 SDOperand T = TLI.LowerOperation(Op, DAG);
3533 assert(T.Val && "Node must be custom expanded!");
3534 Lo = LegalizeOp(T.getValue(0));
3535 Hi = LegalizeOp(T.getValue(1));
3536 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3537 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003538 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003539 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003540
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003541 // These operators cannot be expanded directly, emit them as calls to
3542 // library functions.
3543 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003544 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003545 SDOperand Op;
3546 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3547 case Expand: assert(0 && "cannot expand FP!");
3548 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3549 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3550 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003551
Chris Lattner941d84a2005-07-30 01:40:57 +00003552 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3553
Chris Lattnerfe68d752005-07-29 00:33:32 +00003554 // Now that the custom expander is done, expand the result, which is still
3555 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003556 if (Op.Val) {
3557 ExpandOp(Op, Lo, Hi);
3558 break;
3559 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003560 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003561
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003562 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003563 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003564 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003565 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003566 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003567
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003568 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003569 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3570 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3571 LegalizeOp(Node->getOperand(0)));
3572 // Now that the custom expander is done, expand the result, which is still
3573 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003574 Op = TLI.LowerOperation(Op, DAG);
3575 if (Op.Val) {
3576 ExpandOp(Op, Lo, Hi);
3577 break;
3578 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003579 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003580
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003581 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003582 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003583 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003584 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003585 break;
3586
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003587 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003588 // If the target wants custom lowering, do so.
3589 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3590 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3591 LegalizeOp(Node->getOperand(1)));
3592 Op = TLI.LowerOperation(Op, DAG);
3593 if (Op.Val) {
3594 // Now that the custom expander is done, expand the result, which is
3595 // still VT.
3596 ExpandOp(Op, Lo, Hi);
3597 break;
3598 }
3599 }
3600
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003601 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003602 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003603 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003604
3605 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003606 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003607 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3608 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003609 break;
3610 }
3611
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003612 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003613 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003614 break;
3615
3616 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003617 // If the target wants custom lowering, do so.
3618 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3619 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3620 LegalizeOp(Node->getOperand(1)));
3621 Op = TLI.LowerOperation(Op, DAG);
3622 if (Op.Val) {
3623 // Now that the custom expander is done, expand the result, which is
3624 // still VT.
3625 ExpandOp(Op, Lo, Hi);
3626 break;
3627 }
3628 }
3629
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003630 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003631 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003632 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003633
3634 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003635 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003636 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3637 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003638 break;
3639 }
3640
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003641 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003642 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003643 break;
3644 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003645 // If the target wants custom lowering, do so.
3646 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3647 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3648 LegalizeOp(Node->getOperand(1)));
3649 Op = TLI.LowerOperation(Op, DAG);
3650 if (Op.Val) {
3651 // Now that the custom expander is done, expand the result, which is
3652 // still VT.
3653 ExpandOp(Op, Lo, Hi);
3654 break;
3655 }
3656 }
3657
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003658 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003659 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003660 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003661
3662 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003663 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003664 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3665 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003666 break;
3667 }
3668
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003669 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003670 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003671 break;
3672
Misha Brukman835702a2005-04-21 22:36:52 +00003673 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003674 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3675 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003676 break;
3677 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003678 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3679 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003680 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003681 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003682 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003683 SDOperand LL, LH, RL, RH;
3684 ExpandOp(Node->getOperand(0), LL, LH);
3685 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003686 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3687 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3688 // extended the sign bit of the low half through the upper half, and if so
3689 // emit a MULHS instead of the alternate sequence that is valid for any
3690 // i64 x i64 multiply.
3691 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3692 // is RH an extension of the sign bit of RL?
3693 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3694 RH.getOperand(1).getOpcode() == ISD::Constant &&
3695 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3696 // is LH an extension of the sign bit of LL?
3697 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3698 LH.getOperand(1).getOpcode() == ISD::Constant &&
3699 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3700 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3701 } else {
3702 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3703 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3704 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3705 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3706 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3707 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003708 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3709 } else {
3710 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3711 }
3712 break;
3713 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003714 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3715 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3716 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3717 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003718 }
3719
3720 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003721 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3722 std::make_pair(Lo, Hi))).second;
3723 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003724}
3725
3726
3727// SelectionDAG::Legalize - This is the entry point for the file.
3728//
Chris Lattner4add7e32005-01-23 04:42:50 +00003729void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003730 /// run - This is the main entry point to this class.
3731 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003732 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003733}
3734