blob: bdc49513eba01cb19c3836cf998b8f400e424baa [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Jim Laskeyf2516a92005-08-17 00:39:29 +0000168/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
169/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000170/// we expand it. At this point, we know that the result and operand types are
171/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand Op0,
174 MVT::ValueType DestVT) {
175 if (Op0.getValueType() == MVT::i32) {
176 // simple 32-bit [signed|unsigned] integer to float/double expansion
177
178 // get the stack frame index of a 8 byte buffer
179 MachineFunction &MF = DAG.getMachineFunction();
180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
181 // get address of 8 byte buffer
182 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
183 // word offset constant for Hi/Lo address computation
184 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
185 // set up Hi and Lo (into buffer) address based on endian
186 SDOperand Hi, Lo;
187 if (TLI.isLittleEndian()) {
188 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
189 Lo = StackSlot;
190 } else {
191 Hi = StackSlot;
192 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 }
194 // if signed map to unsigned space
195 SDOperand Op0Mapped;
196 if (isSigned) {
197 // constant used to invert sign bit (signed to unsigned mapping)
198 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
199 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
200 } else {
201 Op0Mapped = Op0;
202 }
203 // store the lo of the constructed double - based on integer input
204 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
205 Op0Mapped, Lo, DAG.getSrcValue(NULL));
206 // initial hi portion of constructed double
207 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
208 // store the hi of the constructed double - biased exponent
209 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
210 InitialHi, Hi, DAG.getSrcValue(NULL));
211 // load the constructed double
212 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
213 DAG.getSrcValue(NULL));
214 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000221 // final result
222 SDOperand Result;
223 // handle final rounding
224 if (DestVT == MVT::f64) {
225 // do nothing
226 Result = Sub;
227 } else {
228 // if f32 then cast to f32
229 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
230 }
231 NeedsAnotherIteration = true;
232 return Result;
233 }
234 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattnerd47675e2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000240 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
241 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
242 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Jim Laskeyf2516a92005-08-17 00:39:29 +0000244 // If the sign bit of the integer is set, the large number will be treated
245 // as a negative number. To counteract this, the dynamic code adds an
246 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000247 uint64_t FF;
248 switch (Op0.getValueType()) {
249 default: assert(0 && "Unsupported integer type!");
250 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
251 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
252 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
253 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
254 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnerc30405e2005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
260 SDOperand FudgeInReg;
261 if (DestVT == MVT::f32)
262 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL));
264 else {
265 assert(DestVT == MVT::f64 && "Unexpected conversion");
266 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
267 DAG.getEntryNode(), CPIdx,
268 DAG.getSrcValue(NULL), MVT::f32));
269 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000273}
274
Chris Lattner19732782005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000277/// we promote it. At this point, we know that the result and operand types are
278/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
279/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // First step, figure out the appropriate *INT_TO_FP operation to use.
284 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000285
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // Scan for the appropriate larger type to use.
289 while (1) {
290 NewInTy = (MVT::ValueType)(NewInTy+1);
291 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Chris Lattnere3e847b2005-07-16 00:19:57 +0000293 // If the target supports SINT_TO_FP of this type, use it.
294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
295 default: break;
296 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000298 break; // Can't use this datatype.
299 // FALL THROUGH.
300 case TargetLowering::Custom:
301 OpToUse = ISD::SINT_TO_FP;
302 break;
303 }
304 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Chris Lattnere3e847b2005-07-16 00:19:57 +0000307 // If the target supports UINT_TO_FP of this type, use it.
308 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
309 default: break;
310 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000312 break; // Can't use this datatype.
313 // FALL THROUGH.
314 case TargetLowering::Custom:
315 OpToUse = ISD::UINT_TO_FP;
316 break;
317 }
318 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000319
Chris Lattnere3e847b2005-07-16 00:19:57 +0000320 // Otherwise, try a larger type.
321 }
322
323 // Make sure to legalize any nodes we create here in the next pass.
324 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000325
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
328 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000331}
332
Chris Lattner44fe26f2005-07-29 00:11:56 +0000333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
334/// FP_TO_*INT operation of the specified operand when the target requests that
335/// we promote it. At this point, we know that the result and operand types are
336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
337/// operation that returns a larger result.
338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
339 MVT::ValueType DestVT,
340 bool isSigned) {
341 // First step, figure out the appropriate FP_TO*INT operation to use.
342 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000343
Chris Lattner44fe26f2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // Scan for the appropriate larger type to use.
347 while (1) {
348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000350
Chris Lattner44fe26f2005-07-29 00:11:56 +0000351 // If the target supports FP_TO_SINT returning this type, use it.
352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
353 default: break;
354 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000356 break; // Can't use this datatype.
357 // FALL THROUGH.
358 case TargetLowering::Custom:
359 OpToUse = ISD::FP_TO_SINT;
360 break;
361 }
362 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-07-29 00:11:56 +0000364 // If the target supports FP_TO_UINT of this type, use it.
365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
366 default: break;
367 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 break; // Can't use this datatype.
370 // FALL THROUGH.
371 case TargetLowering::Custom:
372 OpToUse = ISD::FP_TO_UINT;
373 break;
374 }
375 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000376
Chris Lattner44fe26f2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000379
Chris Lattner44fe26f2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000382
Chris Lattner44fe26f2005-07-29 00:11:56 +0000383 // Okay, we found the operation and type to use. Truncate the result of the
384 // extended FP_TO_*INT operation to the desired size.
385 return DAG.getNode(ISD::TRUNCATE, DestVT,
386 DAG.getNode(OpToUse, NewOutTy, LegalOp));
387}
388
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000535 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000536 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000537 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000538 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000539 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000540 case ISD::BasicBlock:
541 case ISD::CONDCODE:
542 case ISD::VALUETYPE:
543 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000544 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000545 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
546 default: assert(0 && "This action is not supported yet!");
547 case TargetLowering::Custom: {
548 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
549 if (Tmp.Val) {
550 Result = LegalizeOp(Tmp);
551 break;
552 }
553 } // FALLTHROUGH if the target doesn't want to lower this op after all.
554 case TargetLowering::Legal:
555 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
556 break;
557 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000558 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000559 case ISD::AssertSext:
560 case ISD::AssertZext:
561 Tmp1 = LegalizeOp(Node->getOperand(0));
562 if (Tmp1 != Node->getOperand(0))
563 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
564 Node->getOperand(1));
565 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000566 case ISD::MERGE_VALUES:
567 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000568 case ISD::CopyFromReg:
569 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000570 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000571 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000572 if (Tmp1 != Node->getOperand(0))
573 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000574 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000575 Node->getValueType(0));
576 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000577 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
578 if (Node->getNumOperands() == 3)
579 Tmp2 = LegalizeOp(Node->getOperand(2));
580 if (Tmp1 != Node->getOperand(0) ||
581 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattnere3c67e92005-12-18 15:27:43 +0000582 Result = DAG.getCopyFromReg(Tmp1,
583 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
584 Node->getValueType(0), Tmp2);
585 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
586 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000587 // Since CopyFromReg produces two values, make sure to remember that we
588 // legalized both of them.
589 AddLegalizedOperand(Op.getValue(0), Result);
590 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
591 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000592 case ISD::ImplicitDef:
593 Tmp1 = LegalizeOp(Node->getOperand(0));
594 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000595 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
596 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000597 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000598 case ISD::UNDEF: {
599 MVT::ValueType VT = Op.getValueType();
600 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000601 default: assert(0 && "This action is not supported yet!");
602 case TargetLowering::Expand:
603 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000604 if (MVT::isInteger(VT))
605 Result = DAG.getConstant(0, VT);
606 else if (MVT::isFloatingPoint(VT))
607 Result = DAG.getConstantFP(0, VT);
608 else
609 assert(0 && "Unknown value type!");
610 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000611 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000612 break;
613 }
614 break;
615 }
Chris Lattner435b4022005-11-29 06:21:05 +0000616
617 case ISD::LOCATION:
618 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
620
621 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
622 case TargetLowering::Promote:
623 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000624 case TargetLowering::Expand: {
Chris Lattnerc06da622005-12-18 23:54:29 +0000625 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
626 std::vector<SDOperand> Ops;
627 Ops.push_back(Tmp1); // chain
628 Ops.push_back(Node->getOperand(1)); // line #
629 Ops.push_back(Node->getOperand(2)); // col #
630 const std::string &fname =
631 cast<StringSDNode>(Node->getOperand(3))->getValue();
632 const std::string &dirname =
633 cast<StringSDNode>(Node->getOperand(4))->getValue();
634 unsigned id = DebugInfo.RecordSource(fname, dirname);
635 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
636 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
637 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner435b4022005-11-29 06:21:05 +0000638 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000639 }
Chris Lattner435b4022005-11-29 06:21:05 +0000640 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000641 if (Tmp1 != Node->getOperand(0) ||
642 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000643 std::vector<SDOperand> Ops;
644 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000645 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
646 Ops.push_back(Node->getOperand(1)); // line # must be legal.
647 Ops.push_back(Node->getOperand(2)); // col # must be legal.
648 } else {
649 // Otherwise promote them.
650 Ops.push_back(PromoteOp(Node->getOperand(1)));
651 Ops.push_back(PromoteOp(Node->getOperand(2)));
652 }
Chris Lattner435b4022005-11-29 06:21:05 +0000653 Ops.push_back(Node->getOperand(3)); // filename must be legal.
654 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
655 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
656 }
657 break;
658 }
659 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000660
661 case ISD::DEBUG_LOC:
662 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
663 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
664 case TargetLowering::Promote:
665 case TargetLowering::Expand:
666 default: assert(0 && "This action is not supported yet!");
667 case TargetLowering::Legal:
668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
669 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
670 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
671 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
672
673 if (Tmp1 != Node->getOperand(0) ||
674 Tmp2 != Node->getOperand(1) ||
675 Tmp3 != Node->getOperand(2) ||
676 Tmp4 != Node->getOperand(3)) {
677 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
678 }
679 break;
680 }
681 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000682
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 case ISD::Constant:
684 // We know we don't need to expand constants here, constants only have one
685 // value and we check that it is fine above.
686
687 // FIXME: Maybe we should handle things like targets that don't support full
688 // 32-bit immediates?
689 break;
690 case ISD::ConstantFP: {
691 // Spill FP immediates to the constant pool if the target cannot directly
692 // codegen them. Targets often have some immediate values that can be
693 // efficiently generated into an FP register without a load. We explicitly
694 // leave these constants as ConstantFP nodes for the target to deal with.
695
696 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
697
698 // Check to see if this FP immediate is already legal.
699 bool isLegal = false;
700 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
701 E = TLI.legal_fpimm_end(); I != E; ++I)
702 if (CFP->isExactlyValue(*I)) {
703 isLegal = true;
704 break;
705 }
706
707 if (!isLegal) {
708 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000709 bool Extend = false;
710
711 // If a FP immediate is precise when represented as a float, we put it
712 // into the constant pool as a float, even if it's is statically typed
713 // as a double.
714 MVT::ValueType VT = CFP->getValueType(0);
715 bool isDouble = VT == MVT::f64;
716 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
717 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000718 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
719 // Only do this if the target has a native EXTLOAD instruction from
720 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000721 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000722 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
723 VT = MVT::f32;
724 Extend = true;
725 }
Misha Brukman835702a2005-04-21 22:36:52 +0000726
Nate Begeman4e56db62005-12-10 02:36:00 +0000727 SDOperand CPIdx =
728 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000729 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000730 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
731 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000732 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000733 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
734 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000735 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 }
737 break;
738 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000739 case ISD::ConstantVec: {
740 // We assume that vector constants are not legal, and will be immediately
741 // spilled to the constant pool.
742 //
743 // FIXME: revisit this when we have some kind of mechanism by which targets
744 // can decided legality of vector constants, of which there may be very
745 // many.
746 //
747 // Create a ConstantPacked, and put it in the constant pool.
748 std::vector<Constant*> CV;
749 MVT::ValueType VT = Node->getValueType(0);
750 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
751 SDOperand OpN = Node->getOperand(I);
752 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
753 if (MVT::isFloatingPoint(VT))
754 CV.push_back(ConstantFP::get(OpNTy,
755 cast<ConstantFPSDNode>(OpN)->getValue()));
756 else
757 CV.push_back(ConstantUInt::get(OpNTy,
758 cast<ConstantSDNode>(OpN)->getValue()));
759 }
760 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000761 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000762 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
763 break;
764 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000765 case ISD::TokenFactor:
766 if (Node->getNumOperands() == 2) {
767 bool Changed = false;
768 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
769 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
770 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
771 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
772 } else {
773 std::vector<SDOperand> Ops;
774 bool Changed = false;
775 // Legalize the operands.
776 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
777 SDOperand Op = Node->getOperand(i);
778 Ops.push_back(LegalizeOp(Op));
779 Changed |= Ops[i] != Op;
780 }
781 if (Changed)
782 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000783 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000784 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000785
Chris Lattner2dce7032005-05-12 23:24:06 +0000786 case ISD::CALLSEQ_START:
787 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000789 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000790 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000791 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000792 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000793
Chris Lattner2dce7032005-05-12 23:24:06 +0000794 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000795 // nodes are treated specially and are mutated in place. This makes the dag
796 // legalization process more efficient and also makes libcall insertion
797 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000798 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000799 case ISD::DYNAMIC_STACKALLOC:
800 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
801 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
802 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
803 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000804 Tmp3 != Node->getOperand(2)) {
805 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
806 std::vector<SDOperand> Ops;
807 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
808 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
809 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000810 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000811
812 // Since this op produces two values, make sure to remember that we
813 // legalized both of them.
814 AddLegalizedOperand(SDOperand(Node, 0), Result);
815 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
816 return Result.getValue(Op.ResNo);
817
Chris Lattnerd0feb642005-05-13 18:43:43 +0000818 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000819 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
821 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000822
823 bool Changed = false;
824 std::vector<SDOperand> Ops;
825 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
826 Ops.push_back(LegalizeOp(Node->getOperand(i)));
827 Changed |= Ops.back() != Node->getOperand(i);
828 }
829
830 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000831 std::vector<MVT::ValueType> RetTyVTs;
832 RetTyVTs.reserve(Node->getNumValues());
833 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000834 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000835 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
836 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000837 } else {
838 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000839 }
Chris Lattner9242c502005-01-09 19:43:23 +0000840 // Since calls produce multiple values, make sure to remember that we
841 // legalized all of them.
842 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
843 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
844 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000845 }
Chris Lattner68a12142005-01-07 22:12:08 +0000846 case ISD::BR:
847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
848 if (Tmp1 != Node->getOperand(0))
849 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
850 break;
851
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000852 case ISD::BRCOND:
853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000854
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000855 switch (getTypeAction(Node->getOperand(1).getValueType())) {
856 case Expand: assert(0 && "It's impossible to expand bools");
857 case Legal:
858 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
859 break;
860 case Promote:
861 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
862 break;
863 }
Nate Begeman371e4952005-08-16 19:49:35 +0000864
865 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
866 default: assert(0 && "This action is not supported yet!");
867 case TargetLowering::Expand:
868 // Expand brcond's setcc into its constituent parts and create a BR_CC
869 // Node.
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(2));
874 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000875 // Make sure the condition is either zero or one. It may have been
876 // promoted from something else.
877 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
878
Nate Begeman371e4952005-08-16 19:49:35 +0000879 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
880 DAG.getCondCode(ISD::SETNE), Tmp2,
881 DAG.getConstant(0, Tmp2.getValueType()),
882 Node->getOperand(2));
883 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000884 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +0000885 break;
Evan Cheng6fc31042005-12-19 23:12:38 +0000886 case TargetLowering::Custom: {
887 SDOperand Tmp =
888 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
889 Tmp1, Tmp2, Node->getOperand(2)), DAG);
890 if (Tmp.Val) {
891 Result = LegalizeOp(Tmp);
892 break;
893 }
894 // FALLTHROUGH if the target thinks it is legal.
895 }
Nate Begeman371e4952005-08-16 19:49:35 +0000896 case TargetLowering::Legal:
897 // Basic block destination (Op#2) is always legal.
898 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
899 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
900 Node->getOperand(2));
901 break;
902 }
903 break;
904 case ISD::BR_CC:
905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000906 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000907 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
908 Node->getOperand(2), // LHS
909 Node->getOperand(3), // RHS
910 Node->getOperand(1)));
911 // If we get a SETCC back from legalizing the SETCC node we just
912 // created, then use its LHS, RHS, and CC directly in creating a new
913 // node. Otherwise, select between the true and false value based on
914 // comparing the result of the legalized with zero.
915 if (Tmp2.getOpcode() == ISD::SETCC) {
916 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
917 Tmp2.getOperand(0), Tmp2.getOperand(1),
918 Node->getOperand(4));
919 } else {
920 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
921 DAG.getCondCode(ISD::SETNE),
922 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
923 Node->getOperand(4));
924 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000925 break;
926 }
927
928 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
929 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
930
931 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
932 default: assert(0 && "Unexpected action for BR_CC!");
933 case TargetLowering::Custom: {
934 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
935 Tmp2, Tmp3, Node->getOperand(4));
936 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
937 if (Tmp4.Val) {
938 Result = LegalizeOp(Tmp4);
939 break;
940 }
941 } // FALLTHROUGH if the target doesn't want to lower this op after all.
942 case TargetLowering::Legal:
943 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
944 Tmp3 != Node->getOperand(3)) {
945 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
946 Tmp2, Tmp3, Node->getOperand(4));
947 }
948 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000949 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000950 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000951 case ISD::BRCONDTWOWAY:
952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
953 switch (getTypeAction(Node->getOperand(1).getValueType())) {
954 case Expand: assert(0 && "It's impossible to expand bools");
955 case Legal:
956 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
957 break;
958 case Promote:
959 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
960 break;
961 }
962 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
963 // pair.
964 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
965 case TargetLowering::Promote:
966 default: assert(0 && "This action is not supported yet!");
967 case TargetLowering::Legal:
968 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
969 std::vector<SDOperand> Ops;
970 Ops.push_back(Tmp1);
971 Ops.push_back(Tmp2);
972 Ops.push_back(Node->getOperand(2));
973 Ops.push_back(Node->getOperand(3));
974 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
975 }
976 break;
977 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000978 // If BRTWOWAY_CC is legal for this target, then simply expand this node
979 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
980 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000981 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000982 if (Tmp2.getOpcode() == ISD::SETCC) {
983 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
984 Tmp2.getOperand(0), Tmp2.getOperand(1),
985 Node->getOperand(2), Node->getOperand(3));
986 } else {
987 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
988 DAG.getConstant(0, Tmp2.getValueType()),
989 Node->getOperand(2), Node->getOperand(3));
990 }
991 } else {
992 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000993 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000994 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
995 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000996 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattnerfd986782005-04-09 03:30:19 +0000997 break;
998 }
999 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001000 case ISD::BRTWOWAY_CC:
1001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001002 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +00001003 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1004 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1005 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1006 Tmp3 != Node->getOperand(3)) {
1007 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1008 Node->getOperand(4), Node->getOperand(5));
1009 }
1010 break;
1011 } else {
1012 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1013 Node->getOperand(2), // LHS
1014 Node->getOperand(3), // RHS
1015 Node->getOperand(1)));
1016 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1017 // pair.
1018 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1019 default: assert(0 && "This action is not supported yet!");
1020 case TargetLowering::Legal:
1021 // If we get a SETCC back from legalizing the SETCC node we just
1022 // created, then use its LHS, RHS, and CC directly in creating a new
1023 // node. Otherwise, select between the true and false value based on
1024 // comparing the result of the legalized with zero.
1025 if (Tmp2.getOpcode() == ISD::SETCC) {
1026 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1027 Tmp2.getOperand(0), Tmp2.getOperand(1),
1028 Node->getOperand(4), Node->getOperand(5));
1029 } else {
1030 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1031 DAG.getConstant(0, Tmp2.getValueType()),
1032 Node->getOperand(4), Node->getOperand(5));
1033 }
1034 break;
1035 case TargetLowering::Expand:
1036 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1037 Node->getOperand(4));
1038 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
Chris Lattnerc06da622005-12-18 23:54:29 +00001039 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +00001040 break;
1041 }
1042 }
1043 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001044 case ISD::LOAD:
1045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001047
Chris Lattnerdc750592005-01-07 07:47:09 +00001048 if (Tmp1 != Node->getOperand(0) ||
1049 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +00001050 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1051 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001052 else
1053 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001054
Chris Lattnerea4ca942005-01-07 22:28:47 +00001055 // Since loads produce two values, make sure to remember that we legalized
1056 // both of them.
1057 AddLegalizedOperand(SDOperand(Node, 0), Result);
1058 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1059 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +00001060
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001061 case ISD::EXTLOAD:
1062 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001063 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001064 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1065 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001066
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001067 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001068 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001069 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001070 case TargetLowering::Promote:
1071 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001072 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1073 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001074 // Since loads produce two values, make sure to remember that we legalized
1075 // both of them.
1076 AddLegalizedOperand(SDOperand(Node, 0), Result);
1077 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1078 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001079
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001080 case TargetLowering::Legal:
1081 if (Tmp1 != Node->getOperand(0) ||
1082 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001083 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1084 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001085 else
1086 Result = SDOperand(Node, 0);
1087
1088 // Since loads produce two values, make sure to remember that we legalized
1089 // both of them.
1090 AddLegalizedOperand(SDOperand(Node, 0), Result);
1091 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1092 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001093 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001094 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1095 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1096 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001097 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001098 Result = LegalizeOp(Result); // Relegalize new nodes.
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001099 if (Op.ResNo)
1100 return Load.getValue(1);
1101 return Result;
1102 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001103 assert(Node->getOpcode() != ISD::EXTLOAD &&
1104 "EXTLOAD should always be supported!");
1105 // Turn the unsupported load into an EXTLOAD followed by an explicit
1106 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001107 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1108 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001109 SDOperand ValRes;
1110 if (Node->getOpcode() == ISD::SEXTLOAD)
1111 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001112 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001113 else
1114 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001115 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1116 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc06da622005-12-18 23:54:29 +00001117 Result = LegalizeOp(Result); // Relegalize new nodes.
1118 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001119 if (Op.ResNo)
1120 return Result.getValue(1);
1121 return ValRes;
1122 }
1123 assert(0 && "Unreachable");
1124 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001125 case ISD::EXTRACT_ELEMENT: {
1126 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1127 switch (getTypeAction(OpTy)) {
1128 default:
1129 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1130 break;
1131 case Legal:
1132 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1133 // 1 -> Hi
1134 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1135 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1136 TLI.getShiftAmountTy()));
1137 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1138 } else {
1139 // 0 -> Lo
1140 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1141 Node->getOperand(0));
1142 }
1143 Result = LegalizeOp(Result);
1144 break;
1145 case Expand:
1146 // Get both the low and high parts.
1147 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1148 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1149 Result = Tmp2; // 1 -> Hi
1150 else
1151 Result = Tmp1; // 0 -> Lo
1152 break;
1153 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001154 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001155 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001156
1157 case ISD::CopyToReg:
1158 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001159
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001160 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001161 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001162 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001163 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001164 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001165 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1166 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1167 Node->getOperand(1), Tmp2);
1168 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001169 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1170 if (Node->getNumOperands() == 4)
1171 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001172 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001173 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001174 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1175 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1176 }
1177
1178 // Since this produces two values, make sure to remember that we legalized
1179 // both of them.
1180 AddLegalizedOperand(SDOperand(Node, 0), Result);
1181 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1182 return Result.getValue(Op.ResNo);
1183 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001184 break;
1185
1186 case ISD::RET:
1187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1188 switch (Node->getNumOperands()) {
1189 case 2: // ret val
1190 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1191 case Legal:
1192 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001193 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001194 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1195 break;
1196 case Expand: {
1197 SDOperand Lo, Hi;
1198 ExpandOp(Node->getOperand(1), Lo, Hi);
1199 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001200 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001201 }
1202 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001203 Tmp2 = PromoteOp(Node->getOperand(1));
1204 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1205 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001206 }
1207 break;
1208 case 1: // ret void
1209 if (Tmp1 != Node->getOperand(0))
1210 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1211 break;
1212 default: { // ret <values>
1213 std::vector<SDOperand> NewValues;
1214 NewValues.push_back(Tmp1);
1215 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1216 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1217 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001218 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001219 break;
1220 case Expand: {
1221 SDOperand Lo, Hi;
1222 ExpandOp(Node->getOperand(i), Lo, Hi);
1223 NewValues.push_back(Lo);
1224 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001225 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 }
1227 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001228 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001229 }
1230 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1231 break;
1232 }
1233 }
1234 break;
1235 case ISD::STORE:
1236 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1237 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1238
Chris Lattnere69daaf2005-01-08 06:25:56 +00001239 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001240 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001241 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001242 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001243 DAG.getConstant(FloatToBits(CFP->getValue()),
1244 MVT::i32),
1245 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001246 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001247 } else {
1248 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001249 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001250 DAG.getConstant(DoubleToBits(CFP->getValue()),
1251 MVT::i64),
1252 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001253 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001254 }
Chris Lattnera4743132005-02-22 07:23:39 +00001255 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001256 }
1257
Chris Lattnerdc750592005-01-07 07:47:09 +00001258 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1259 case Legal: {
1260 SDOperand Val = LegalizeOp(Node->getOperand(1));
1261 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1262 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001263 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1264 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001265 break;
1266 }
1267 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001268 // Truncate the value and store the result.
1269 Tmp3 = PromoteOp(Node->getOperand(1));
1270 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001271 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001272 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001273 break;
1274
Chris Lattnerdc750592005-01-07 07:47:09 +00001275 case Expand:
1276 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001277 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001278 ExpandOp(Node->getOperand(1), Lo, Hi);
1279
1280 if (!TLI.isLittleEndian())
1281 std::swap(Lo, Hi);
1282
Chris Lattner55e9cde2005-05-11 04:51:16 +00001283 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1284 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001285 // If this is a vector type, then we have to calculate the increment as
1286 // the product of the element size in bytes, and the number of elements
1287 // in the high half of the vector.
1288 if (MVT::Vector == Hi.getValueType()) {
1289 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1290 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1291 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1292 } else {
1293 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1294 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001295 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1296 getIntPtrConstant(IncrementSize));
1297 assert(isTypeLegal(Tmp2.getValueType()) &&
1298 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001299 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001300 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1301 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001302 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1303 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001304 }
1305 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001306 case ISD::PCMARKER:
1307 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001308 if (Tmp1 != Node->getOperand(0))
1309 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001310 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001311 case ISD::READCYCLECOUNTER:
1312 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001313 if (Tmp1 != Node->getOperand(0)) {
1314 std::vector<MVT::ValueType> rtypes;
1315 std::vector<SDOperand> rvals;
1316 rtypes.push_back(MVT::i64);
1317 rtypes.push_back(MVT::Other);
1318 rvals.push_back(Tmp1);
1319 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1320 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001321
1322 // Since rdcc produce two values, make sure to remember that we legalized
1323 // both of them.
1324 AddLegalizedOperand(SDOperand(Node, 0), Result);
1325 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1326 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001327
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001328 case ISD::TRUNCSTORE:
1329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1330 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1331
1332 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1333 case Legal:
1334 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001335
1336 // The only promote case we handle is TRUNCSTORE:i1 X into
1337 // -> TRUNCSTORE:i8 (and X, 1)
1338 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1339 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1340 TargetLowering::Promote) {
1341 // Promote the bool to a mask then store.
1342 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1343 DAG.getConstant(1, Tmp2.getValueType()));
1344 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1345 Node->getOperand(3), DAG.getValueType(MVT::i8));
1346
1347 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1348 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001349 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001350 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001351 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001352 break;
1353 case Promote:
1354 case Expand:
1355 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1356 }
1357 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001358 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001359 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1360 case Expand: assert(0 && "It's impossible to expand bools");
1361 case Legal:
1362 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1363 break;
1364 case Promote:
1365 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1366 break;
1367 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001368 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001369 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001370
Nate Begeman987121a2005-08-23 04:29:48 +00001371 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001372 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001373 case TargetLowering::Expand:
1374 if (Tmp1.getOpcode() == ISD::SETCC) {
1375 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1376 Tmp2, Tmp3,
1377 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1378 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001379 // Make sure the condition is either zero or one. It may have been
1380 // promoted from something else.
1381 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001382 Result = DAG.getSelectCC(Tmp1,
1383 DAG.getConstant(0, Tmp1.getValueType()),
1384 Tmp2, Tmp3, ISD::SETNE);
1385 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001386 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begemane5b86d72005-08-10 20:51:12 +00001387 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001388 case TargetLowering::Custom: {
1389 SDOperand Tmp =
1390 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1391 Tmp1, Tmp2, Tmp3), DAG);
1392 if (Tmp.Val) {
1393 Result = LegalizeOp(Tmp);
1394 break;
1395 }
1396 // FALLTHROUGH if the target thinks it is legal.
1397 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001398 case TargetLowering::Legal:
1399 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1400 Tmp3 != Node->getOperand(2))
1401 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1402 Tmp1, Tmp2, Tmp3);
1403 break;
1404 case TargetLowering::Promote: {
1405 MVT::ValueType NVT =
1406 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1407 unsigned ExtOp, TruncOp;
1408 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001409 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001410 TruncOp = ISD::TRUNCATE;
1411 } else {
1412 ExtOp = ISD::FP_EXTEND;
1413 TruncOp = ISD::FP_ROUND;
1414 }
1415 // Promote each of the values to the new type.
1416 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1417 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1418 // Perform the larger operation, then round down.
1419 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1420 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1421 break;
1422 }
1423 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001424 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001425 case ISD::SELECT_CC:
1426 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1427 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1428
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001429 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001430 // Everything is legal, see if we should expand this op or something.
1431 switch (TLI.getOperationAction(ISD::SELECT_CC,
1432 Node->getOperand(0).getValueType())) {
1433 default: assert(0 && "This action is not supported yet!");
1434 case TargetLowering::Custom: {
1435 SDOperand Tmp =
1436 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1437 Node->getOperand(0),
1438 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001439 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001440 if (Tmp.Val) {
1441 Result = LegalizeOp(Tmp);
1442 break;
1443 }
1444 } // FALLTHROUGH if the target can't lower this operation after all.
1445 case TargetLowering::Legal:
1446 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1447 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1448 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1449 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1450 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1451 Tmp3, Tmp4, Node->getOperand(4));
1452 }
1453 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001454 }
1455 break;
1456 } else {
1457 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1458 Node->getOperand(0), // LHS
1459 Node->getOperand(1), // RHS
1460 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001461 // If we get a SETCC back from legalizing the SETCC node we just
1462 // created, then use its LHS, RHS, and CC directly in creating a new
1463 // node. Otherwise, select between the true and false value based on
1464 // comparing the result of the legalized with zero.
1465 if (Tmp1.getOpcode() == ISD::SETCC) {
1466 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1467 Tmp1.getOperand(0), Tmp1.getOperand(1),
1468 Tmp3, Tmp4, Tmp1.getOperand(2));
1469 } else {
1470 Result = DAG.getSelectCC(Tmp1,
1471 DAG.getConstant(0, Tmp1.getValueType()),
1472 Tmp3, Tmp4, ISD::SETNE);
1473 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001474 }
1475 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001476 case ISD::SETCC:
1477 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1478 case Legal:
1479 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1480 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001481 break;
1482 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001483 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1484 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1485
1486 // If this is an FP compare, the operands have already been extended.
1487 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1488 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001489 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001490
1491 // Otherwise, we have to insert explicit sign or zero extends. Note
1492 // that we could insert sign extends for ALL conditions, but zero extend
1493 // is cheaper on many machines (an AND instead of two shifts), so prefer
1494 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001495 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001496 default: assert(0 && "Unknown integer comparison!");
1497 case ISD::SETEQ:
1498 case ISD::SETNE:
1499 case ISD::SETUGE:
1500 case ISD::SETUGT:
1501 case ISD::SETULE:
1502 case ISD::SETULT:
1503 // ALL of these operations will work if we either sign or zero extend
1504 // the operands (including the unsigned comparisons!). Zero extend is
1505 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001506 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1507 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001508 break;
1509 case ISD::SETGE:
1510 case ISD::SETGT:
1511 case ISD::SETLT:
1512 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001513 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1514 DAG.getValueType(VT));
1515 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1516 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001517 break;
1518 }
Chris Lattner4d978642005-01-15 22:16:26 +00001519 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001520 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001521 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001522 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1523 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1524 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001525 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001526 case ISD::SETEQ:
1527 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001528 if (RHSLo == RHSHi)
1529 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1530 if (RHSCST->isAllOnesValue()) {
1531 // Comparison to -1.
1532 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001533 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001534 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001535 }
1536
Chris Lattnerdc750592005-01-07 07:47:09 +00001537 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1538 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1539 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001540 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001541 break;
1542 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001543 // If this is a comparison of the sign bit, just look at the top part.
1544 // X > -1, x < 0
1545 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001546 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001547 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001548 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001549 (CST->isAllOnesValue()))) { // X > -1
1550 Tmp1 = LHSHi;
1551 Tmp2 = RHSHi;
1552 break;
1553 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001554
Chris Lattnerdc750592005-01-07 07:47:09 +00001555 // FIXME: This generated code sucks.
1556 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001557 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001558 default: assert(0 && "Unknown integer setcc!");
1559 case ISD::SETLT:
1560 case ISD::SETULT: LowCC = ISD::SETULT; break;
1561 case ISD::SETGT:
1562 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1563 case ISD::SETLE:
1564 case ISD::SETULE: LowCC = ISD::SETULE; break;
1565 case ISD::SETGE:
1566 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1567 }
Misha Brukman835702a2005-04-21 22:36:52 +00001568
Chris Lattnerdc750592005-01-07 07:47:09 +00001569 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1570 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1571 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1572
1573 // NOTE: on targets without efficient SELECT of bools, we can always use
1574 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001575 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1576 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1577 Node->getOperand(2));
1578 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001579 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1580 Result, Tmp1, Tmp2));
1581 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001582 }
1583 }
Nate Begeman987121a2005-08-23 04:29:48 +00001584
1585 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1586 default:
1587 assert(0 && "Cannot handle this action for SETCC yet!");
1588 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001589 case TargetLowering::Promote: {
1590 // First step, figure out the appropriate operation to use.
1591 // Allow SETCC to not be supported for all legal data types
1592 // Mostly this targets FP
1593 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1594 MVT::ValueType OldVT = NewInTy;
1595
1596 // Scan for the appropriate larger type to use.
1597 while (1) {
1598 NewInTy = (MVT::ValueType)(NewInTy+1);
1599
1600 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1601 "Fell off of the edge of the integer world");
1602 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1603 "Fell off of the edge of the floating point world");
1604
1605 // If the target supports SETCC of this type, use it.
1606 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1607 break;
1608 }
1609 if (MVT::isInteger(NewInTy))
1610 assert(0 && "Cannot promote Legal Integer SETCC yet");
1611 else {
1612 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1613 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1614 }
1615
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001616 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1617 Node->getOperand(2));
1618 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001619 }
Nate Begeman987121a2005-08-23 04:29:48 +00001620 case TargetLowering::Legal:
1621 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1622 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1623 Node->getOperand(2));
1624 break;
1625 case TargetLowering::Expand:
1626 // Expand a setcc node into a select_cc of the same condition, lhs, and
1627 // rhs that selects between const 1 (true) and const 0 (false).
1628 MVT::ValueType VT = Node->getValueType(0);
1629 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1630 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1631 Node->getOperand(2));
1632 Result = LegalizeOp(Result);
1633 break;
1634 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001635 break;
1636
Chris Lattner85d70c62005-01-11 05:57:22 +00001637 case ISD::MEMSET:
1638 case ISD::MEMCPY:
1639 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1642
1643 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1644 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1645 case Expand: assert(0 && "Cannot expand a byte!");
1646 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001647 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001648 break;
1649 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001650 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001651 break;
1652 }
1653 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001654 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001655 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001656
1657 SDOperand Tmp4;
1658 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001659 case Expand: {
1660 // Length is too big, just take the lo-part of the length.
1661 SDOperand HiPart;
1662 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1663 break;
1664 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001665 case Legal:
1666 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001667 break;
1668 case Promote:
1669 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001670 break;
1671 }
1672
1673 SDOperand Tmp5;
1674 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1675 case Expand: assert(0 && "Cannot expand this yet!");
1676 case Legal:
1677 Tmp5 = LegalizeOp(Node->getOperand(4));
1678 break;
1679 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001680 Tmp5 = PromoteOp(Node->getOperand(4));
1681 break;
1682 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001683
1684 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1685 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001686 case TargetLowering::Custom: {
1687 SDOperand Tmp =
1688 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1689 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1690 if (Tmp.Val) {
1691 Result = LegalizeOp(Tmp);
1692 break;
1693 }
1694 // FALLTHROUGH if the target thinks it is legal.
1695 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001696 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001697 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1698 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1699 Tmp5 != Node->getOperand(4)) {
1700 std::vector<SDOperand> Ops;
1701 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1702 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1703 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1704 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001705 break;
1706 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001707 // Otherwise, the target does not support this operation. Lower the
1708 // operation to an explicit libcall as appropriate.
1709 MVT::ValueType IntPtr = TLI.getPointerTy();
1710 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1711 std::vector<std::pair<SDOperand, const Type*> > Args;
1712
Reid Spencer6dced922005-01-12 14:53:45 +00001713 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001714 if (Node->getOpcode() == ISD::MEMSET) {
1715 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1716 // Extend the ubyte argument to be an int value for the call.
1717 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1718 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1719 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1720
1721 FnName = "memset";
1722 } else if (Node->getOpcode() == ISD::MEMCPY ||
1723 Node->getOpcode() == ISD::MEMMOVE) {
1724 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1725 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1726 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1727 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1728 } else {
1729 assert(0 && "Unknown op!");
1730 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001731
Chris Lattner85d70c62005-01-11 05:57:22 +00001732 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001733 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001734 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001735 Result = CallResult.second;
1736 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001737 break;
1738 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001739 }
1740 break;
1741 }
Chris Lattner5385db52005-05-09 20:23:03 +00001742
1743 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001744 Tmp1 = LegalizeOp(Node->getOperand(0));
1745 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001746
Chris Lattner86535992005-05-14 07:45:46 +00001747 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1748 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1749 std::vector<SDOperand> Ops;
1750 Ops.push_back(Tmp1);
1751 Ops.push_back(Tmp2);
1752 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1753 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001754 Result = SDOperand(Node, 0);
1755 // Since these produce two values, make sure to remember that we legalized
1756 // both of them.
1757 AddLegalizedOperand(SDOperand(Node, 0), Result);
1758 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1759 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001760 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001761 Tmp1 = LegalizeOp(Node->getOperand(0));
1762 Tmp2 = LegalizeOp(Node->getOperand(1));
1763 Tmp3 = LegalizeOp(Node->getOperand(2));
1764 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1765 Tmp3 != Node->getOperand(2))
1766 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1767 break;
1768
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001769 case ISD::READIO:
1770 Tmp1 = LegalizeOp(Node->getOperand(0));
1771 Tmp2 = LegalizeOp(Node->getOperand(1));
1772
1773 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1774 case TargetLowering::Custom:
1775 default: assert(0 && "This action not implemented for this operation!");
1776 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001777 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1778 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1779 std::vector<SDOperand> Ops;
1780 Ops.push_back(Tmp1);
1781 Ops.push_back(Tmp2);
1782 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1783 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001784 Result = SDOperand(Node, 0);
1785 break;
1786 case TargetLowering::Expand:
1787 // Replace this with a load from memory.
1788 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1789 Node->getOperand(1), DAG.getSrcValue(NULL));
1790 Result = LegalizeOp(Result);
1791 break;
1792 }
1793
1794 // Since these produce two values, make sure to remember that we legalized
1795 // both of them.
1796 AddLegalizedOperand(SDOperand(Node, 0), Result);
1797 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1798 return Result.getValue(Op.ResNo);
1799
1800 case ISD::WRITEIO:
1801 Tmp1 = LegalizeOp(Node->getOperand(0));
1802 Tmp2 = LegalizeOp(Node->getOperand(1));
1803 Tmp3 = LegalizeOp(Node->getOperand(2));
1804
1805 switch (TLI.getOperationAction(Node->getOpcode(),
1806 Node->getOperand(1).getValueType())) {
1807 case TargetLowering::Custom:
1808 default: assert(0 && "This action not implemented for this operation!");
1809 case TargetLowering::Legal:
1810 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1811 Tmp3 != Node->getOperand(2))
1812 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1813 break;
1814 case TargetLowering::Expand:
1815 // Replace this with a store to memory.
1816 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1817 Node->getOperand(1), Node->getOperand(2),
1818 DAG.getSrcValue(NULL));
1819 Result = LegalizeOp(Result);
1820 break;
1821 }
1822 break;
1823
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001824 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001825 case ISD::SUB_PARTS:
1826 case ISD::SHL_PARTS:
1827 case ISD::SRA_PARTS:
1828 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001829 std::vector<SDOperand> Ops;
1830 bool Changed = false;
1831 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1832 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1833 Changed |= Ops.back() != Node->getOperand(i);
1834 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001835 if (Changed) {
1836 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1837 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1838 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001839
1840 // Since these produce multiple values, make sure to remember that we
1841 // legalized all of them.
1842 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1843 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1844 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001845 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001846
1847 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001848 case ISD::ADD:
1849 case ISD::SUB:
1850 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001851 case ISD::MULHS:
1852 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001853 case ISD::UDIV:
1854 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001855 case ISD::AND:
1856 case ISD::OR:
1857 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001858 case ISD::SHL:
1859 case ISD::SRL:
1860 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001861 case ISD::FADD:
1862 case ISD::FSUB:
1863 case ISD::FMUL:
1864 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001865 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001866 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1867 case Expand: assert(0 && "Not possible");
1868 case Legal:
1869 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1870 break;
1871 case Promote:
1872 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1873 break;
1874 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001875 if (Tmp1 != Node->getOperand(0) ||
1876 Tmp2 != Node->getOperand(1))
1877 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1878 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001879
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001880 case ISD::BUILD_PAIR: {
1881 MVT::ValueType PairTy = Node->getValueType(0);
1882 // TODO: handle the case where the Lo and Hi operands are not of legal type
1883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1885 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1886 case TargetLowering::Legal:
1887 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1888 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1889 break;
1890 case TargetLowering::Promote:
1891 case TargetLowering::Custom:
1892 assert(0 && "Cannot promote/custom this yet!");
1893 case TargetLowering::Expand:
1894 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1895 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1896 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1897 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1898 TLI.getShiftAmountTy()));
1899 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1900 break;
1901 }
1902 break;
1903 }
1904
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001905 case ISD::UREM:
1906 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001907 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001908 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1909 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1910 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1911 case TargetLowering::Legal:
1912 if (Tmp1 != Node->getOperand(0) ||
1913 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001914 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001915 Tmp2);
1916 break;
1917 case TargetLowering::Promote:
1918 case TargetLowering::Custom:
1919 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001920 case TargetLowering::Expand:
1921 if (MVT::isInteger(Node->getValueType(0))) {
1922 MVT::ValueType VT = Node->getValueType(0);
1923 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1924 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1925 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1926 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1927 } else {
1928 // Floating point mod -> fmod libcall.
1929 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1930 SDOperand Dummy;
1931 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001932 }
1933 break;
1934 }
1935 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001936
Andrew Lenharth5e177822005-05-03 17:19:30 +00001937 case ISD::CTPOP:
1938 case ISD::CTTZ:
1939 case ISD::CTLZ:
1940 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1941 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1942 case TargetLowering::Legal:
1943 if (Tmp1 != Node->getOperand(0))
1944 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1945 break;
1946 case TargetLowering::Promote: {
1947 MVT::ValueType OVT = Tmp1.getValueType();
1948 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001949
1950 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001951 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1952 // Perform the larger operation, then subtract if needed.
1953 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1954 switch(Node->getOpcode())
1955 {
1956 case ISD::CTPOP:
1957 Result = Tmp1;
1958 break;
1959 case ISD::CTTZ:
1960 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001961 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1962 DAG.getConstant(getSizeInBits(NVT), NVT),
1963 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001964 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001965 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1966 break;
1967 case ISD::CTLZ:
1968 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001969 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1970 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001971 getSizeInBits(OVT), NVT));
1972 break;
1973 }
1974 break;
1975 }
1976 case TargetLowering::Custom:
1977 assert(0 && "Cannot custom handle this yet!");
1978 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001979 switch(Node->getOpcode())
1980 {
1981 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001982 static const uint64_t mask[6] = {
1983 0x5555555555555555ULL, 0x3333333333333333ULL,
1984 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1985 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1986 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001987 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001988 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1989 unsigned len = getSizeInBits(VT);
1990 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001991 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001992 Tmp2 = DAG.getConstant(mask[i], VT);
1993 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001994 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001995 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1996 DAG.getNode(ISD::AND, VT,
1997 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1998 Tmp2));
1999 }
2000 Result = Tmp1;
2001 break;
2002 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002003 case ISD::CTLZ: {
2004 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00002005 x = x | (x >> 1);
2006 x = x | (x >> 2);
2007 ...
2008 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002009 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00002010 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002011
Chris Lattner56add052005-05-11 18:35:21 +00002012 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2013 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002014 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2015 unsigned len = getSizeInBits(VT);
2016 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2017 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002018 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002019 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2020 }
2021 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002022 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002023 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002024 }
2025 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002026 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002027 // unless the target has ctlz but not ctpop, in which case we use:
2028 // { return 32 - nlz(~x & (x-1)); }
2029 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002030 MVT::ValueType VT = Tmp1.getValueType();
2031 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002032 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002033 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2034 DAG.getNode(ISD::SUB, VT, Tmp1,
2035 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002036 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002037 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2038 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002039 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002040 DAG.getConstant(getSizeInBits(VT), VT),
2041 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2042 } else {
2043 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2044 }
Chris Lattner72473242005-05-11 05:27:09 +00002045 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002046 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002047 default:
2048 assert(0 && "Cannot expand this yet!");
2049 break;
2050 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002051 break;
2052 }
2053 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002054
Chris Lattner13fe99c2005-04-02 05:00:07 +00002055 // Unary operators
2056 case ISD::FABS:
2057 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002058 case ISD::FSQRT:
2059 case ISD::FSIN:
2060 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002061 Tmp1 = LegalizeOp(Node->getOperand(0));
2062 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2063 case TargetLowering::Legal:
2064 if (Tmp1 != Node->getOperand(0))
2065 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2066 break;
2067 case TargetLowering::Promote:
2068 case TargetLowering::Custom:
2069 assert(0 && "Cannot promote/custom handle this yet!");
2070 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002071 switch(Node->getOpcode()) {
2072 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002073 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2074 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002075 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002076 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002077 break;
2078 }
2079 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002080 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2081 MVT::ValueType VT = Node->getValueType(0);
2082 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002083 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002084 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2085 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2086 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002087 break;
2088 }
2089 case ISD::FSQRT:
2090 case ISD::FSIN:
2091 case ISD::FCOS: {
2092 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002093 const char *FnName = 0;
2094 switch(Node->getOpcode()) {
2095 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2096 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2097 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2098 default: assert(0 && "Unreachable!");
2099 }
Nate Begeman77558da2005-08-04 21:43:28 +00002100 SDOperand Dummy;
2101 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002102 break;
2103 }
2104 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002105 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002106 }
2107 break;
2108 }
2109 break;
2110
2111 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002112 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002113 case ISD::UINT_TO_FP: {
2114 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2116 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002117 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002118 Node->getOperand(0).getValueType())) {
2119 default: assert(0 && "Unknown operation action!");
2120 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002121 Result = ExpandLegalINT_TO_FP(isSigned,
2122 LegalizeOp(Node->getOperand(0)),
2123 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002124 AddLegalizedOperand(Op, Result);
2125 return Result;
2126 case TargetLowering::Promote:
2127 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2128 Node->getValueType(0),
2129 isSigned);
2130 AddLegalizedOperand(Op, Result);
2131 return Result;
2132 case TargetLowering::Legal:
2133 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002134 case TargetLowering::Custom: {
2135 Tmp1 = LegalizeOp(Node->getOperand(0));
2136 SDOperand Tmp =
2137 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2138 Tmp = TLI.LowerOperation(Tmp, DAG);
2139 if (Tmp.Val) {
2140 AddLegalizedOperand(Op, Tmp);
2141 NeedsAnotherIteration = true;
2142 return Tmp;
2143 } else {
2144 assert(0 && "Target Must Lower this");
2145 }
2146 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002147 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002148
Chris Lattnerdc750592005-01-07 07:47:09 +00002149 Tmp1 = LegalizeOp(Node->getOperand(0));
2150 if (Tmp1 != Node->getOperand(0))
2151 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2152 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002153 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002154 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2155 Node->getValueType(0), Node->getOperand(0));
2156 break;
2157 case Promote:
2158 if (isSigned) {
2159 Result = PromoteOp(Node->getOperand(0));
2160 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2161 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2162 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2163 } else {
2164 Result = PromoteOp(Node->getOperand(0));
2165 Result = DAG.getZeroExtendInReg(Result,
2166 Node->getOperand(0).getValueType());
2167 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002168 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002169 break;
2170 }
2171 break;
2172 }
2173 case ISD::TRUNCATE:
2174 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2175 case Legal:
2176 Tmp1 = LegalizeOp(Node->getOperand(0));
2177 if (Tmp1 != Node->getOperand(0))
2178 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2179 break;
2180 case Expand:
2181 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2182
2183 // Since the result is legal, we should just be able to truncate the low
2184 // part of the source.
2185 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2186 break;
2187 case Promote:
2188 Result = PromoteOp(Node->getOperand(0));
2189 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2190 break;
2191 }
2192 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002193
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002194 case ISD::FP_TO_SINT:
2195 case ISD::FP_TO_UINT:
2196 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2197 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002198 Tmp1 = LegalizeOp(Node->getOperand(0));
2199
Chris Lattner44fe26f2005-07-29 00:11:56 +00002200 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2201 default: assert(0 && "Unknown operation action!");
2202 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002203 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2204 SDOperand True, False;
2205 MVT::ValueType VT = Node->getOperand(0).getValueType();
2206 MVT::ValueType NVT = Node->getValueType(0);
2207 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2208 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2209 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2210 Node->getOperand(0), Tmp2, ISD::SETLT);
2211 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2212 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002213 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002214 Tmp2));
2215 False = DAG.getNode(ISD::XOR, NVT, False,
2216 DAG.getConstant(1ULL << ShiftAmt, NVT));
2217 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002218 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002219 } else {
2220 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2221 }
2222 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002223 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002224 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002225 Node->getOpcode() == ISD::FP_TO_SINT);
2226 AddLegalizedOperand(Op, Result);
2227 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002228 case TargetLowering::Custom: {
2229 SDOperand Tmp =
2230 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2231 Tmp = TLI.LowerOperation(Tmp, DAG);
2232 if (Tmp.Val) {
2233 AddLegalizedOperand(Op, Tmp);
2234 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002235 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002236 } else {
2237 // The target thinks this is legal afterall.
2238 break;
2239 }
2240 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002241 case TargetLowering::Legal:
2242 break;
2243 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002244
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002245 if (Tmp1 != Node->getOperand(0))
2246 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2247 break;
2248 case Expand:
2249 assert(0 && "Shouldn't need to expand other operators here!");
2250 case Promote:
2251 Result = PromoteOp(Node->getOperand(0));
2252 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2253 break;
2254 }
2255 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002256
Chris Lattner7753f172005-09-02 00:18:10 +00002257 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002258 case ISD::ZERO_EXTEND:
2259 case ISD::SIGN_EXTEND:
2260 case ISD::FP_EXTEND:
2261 case ISD::FP_ROUND:
2262 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2263 case Legal:
2264 Tmp1 = LegalizeOp(Node->getOperand(0));
2265 if (Tmp1 != Node->getOperand(0))
2266 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2267 break;
2268 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002269 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002270
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002271 case Promote:
2272 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002273 case ISD::ANY_EXTEND:
2274 Result = PromoteOp(Node->getOperand(0));
2275 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2276 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002277 case ISD::ZERO_EXTEND:
2278 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002279 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002280 Result = DAG.getZeroExtendInReg(Result,
2281 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002282 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002283 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002284 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002285 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002286 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002287 Result,
2288 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002289 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002290 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002291 Result = PromoteOp(Node->getOperand(0));
2292 if (Result.getValueType() != Op.getValueType())
2293 // Dynamically dead while we have only 2 FP types.
2294 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2295 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002296 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002297 Result = PromoteOp(Node->getOperand(0));
2298 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2299 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002300 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002301 }
2302 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002303 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002304 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002305 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002306 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002307
2308 // If this operation is not supported, convert it to a shl/shr or load/store
2309 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002310 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2311 default: assert(0 && "This action not supported for this op yet!");
2312 case TargetLowering::Legal:
2313 if (Tmp1 != Node->getOperand(0))
2314 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002315 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002316 break;
2317 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002318 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002319 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002320 // NOTE: we could fall back on load/store here too for targets without
2321 // SAR. However, it is doubtful that any exist.
2322 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2323 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002324 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002325 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2326 Node->getOperand(0), ShiftCst);
2327 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2328 Result, ShiftCst);
2329 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2330 // The only way we can lower this is to turn it into a STORETRUNC,
2331 // EXTLOAD pair, targetting a temporary location (a stack slot).
2332
2333 // NOTE: there is a choice here between constantly creating new stack
2334 // slots and always reusing the same one. We currently always create
2335 // new ones, as reuse may inhibit scheduling.
2336 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2337 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2338 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2339 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002340 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002341 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2342 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2343 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002344 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002345 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002346 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2347 Result, StackSlot, DAG.getSrcValue(NULL),
2348 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002349 } else {
2350 assert(0 && "Unknown op");
2351 }
2352 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002353 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002354 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002355 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002356 }
Chris Lattner99222f72005-01-15 07:15:18 +00002357 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002358
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002359 // Note that LegalizeOp may be reentered even from single-use nodes, which
2360 // means that we always must cache transformed nodes.
2361 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002362 return Result;
2363}
2364
Chris Lattner4d978642005-01-15 22:16:26 +00002365/// PromoteOp - Given an operation that produces a value in an invalid type,
2366/// promote it to compute the value into a larger type. The produced value will
2367/// have the correct bits for the low portion of the register, but no guarantee
2368/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002369SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2370 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002371 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002372 assert(getTypeAction(VT) == Promote &&
2373 "Caller should expand or legalize operands that are not promotable!");
2374 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2375 "Cannot promote to smaller type!");
2376
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002377 SDOperand Tmp1, Tmp2, Tmp3;
2378
2379 SDOperand Result;
2380 SDNode *Node = Op.Val;
2381
Chris Lattner1a570f12005-09-02 20:32:45 +00002382 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2383 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002384
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002385 // Promotion needs an optimization step to clean up after it, and is not
2386 // careful to avoid operations the target does not support. Make sure that
2387 // all generated operations are legalized in the next iteration.
2388 NeedsAnotherIteration = true;
2389
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002390 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002391 case ISD::CopyFromReg:
2392 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002393 default:
2394 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2395 assert(0 && "Do not know how to promote this operator!");
2396 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002397 case ISD::UNDEF:
2398 Result = DAG.getNode(ISD::UNDEF, NVT);
2399 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002400 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002401 if (VT != MVT::i1)
2402 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2403 else
2404 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002405 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2406 break;
2407 case ISD::ConstantFP:
2408 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2409 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2410 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002411
Chris Lattner2cb338d2005-01-18 02:59:52 +00002412 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002413 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002414 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2415 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002416 Result = LegalizeOp(Result);
2417 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002418
2419 case ISD::TRUNCATE:
2420 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2421 case Legal:
2422 Result = LegalizeOp(Node->getOperand(0));
2423 assert(Result.getValueType() >= NVT &&
2424 "This truncation doesn't make sense!");
2425 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2426 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2427 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002428 case Promote:
2429 // The truncation is not required, because we don't guarantee anything
2430 // about high bits anyway.
2431 Result = PromoteOp(Node->getOperand(0));
2432 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002433 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002434 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2435 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002436 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002437 }
2438 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002439 case ISD::SIGN_EXTEND:
2440 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002441 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002442 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2443 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2444 case Legal:
2445 // Input is legal? Just do extend all the way to the larger type.
2446 Result = LegalizeOp(Node->getOperand(0));
2447 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2448 break;
2449 case Promote:
2450 // Promote the reg if it's smaller.
2451 Result = PromoteOp(Node->getOperand(0));
2452 // The high bits are not guaranteed to be anything. Insert an extend.
2453 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002454 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002455 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002456 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002457 Result = DAG.getZeroExtendInReg(Result,
2458 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002459 break;
2460 }
2461 break;
2462
2463 case ISD::FP_EXTEND:
2464 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2465 case ISD::FP_ROUND:
2466 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2467 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2468 case Promote: assert(0 && "Unreachable with 2 FP types!");
2469 case Legal:
2470 // Input is legal? Do an FP_ROUND_INREG.
2471 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002472 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2473 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002474 break;
2475 }
2476 break;
2477
2478 case ISD::SINT_TO_FP:
2479 case ISD::UINT_TO_FP:
2480 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2481 case Legal:
2482 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002483 // No extra round required here.
2484 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002485 break;
2486
2487 case Promote:
2488 Result = PromoteOp(Node->getOperand(0));
2489 if (Node->getOpcode() == ISD::SINT_TO_FP)
2490 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002491 Result,
2492 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002493 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002494 Result = DAG.getZeroExtendInReg(Result,
2495 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002496 // No extra round required here.
2497 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002498 break;
2499 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002500 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2501 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002502 // Round if we cannot tolerate excess precision.
2503 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002504 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2505 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002506 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002507 }
Chris Lattner4d978642005-01-15 22:16:26 +00002508 break;
2509
Chris Lattner268d4572005-12-09 17:32:47 +00002510 case ISD::SIGN_EXTEND_INREG:
2511 Result = PromoteOp(Node->getOperand(0));
2512 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2513 Node->getOperand(1));
2514 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002515 case ISD::FP_TO_SINT:
2516 case ISD::FP_TO_UINT:
2517 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2518 case Legal:
2519 Tmp1 = LegalizeOp(Node->getOperand(0));
2520 break;
2521 case Promote:
2522 // The input result is prerounded, so we don't have to do anything
2523 // special.
2524 Tmp1 = PromoteOp(Node->getOperand(0));
2525 break;
2526 case Expand:
2527 assert(0 && "not implemented");
2528 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002529 // If we're promoting a UINT to a larger size, check to see if the new node
2530 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2531 // we can use that instead. This allows us to generate better code for
2532 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2533 // legal, such as PowerPC.
2534 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002535 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002536 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2537 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002538 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2539 } else {
2540 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2541 }
Chris Lattner4d978642005-01-15 22:16:26 +00002542 break;
2543
Chris Lattner13fe99c2005-04-02 05:00:07 +00002544 case ISD::FABS:
2545 case ISD::FNEG:
2546 Tmp1 = PromoteOp(Node->getOperand(0));
2547 assert(Tmp1.getValueType() == NVT);
2548 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2549 // NOTE: we do not have to do any extra rounding here for
2550 // NoExcessFPPrecision, because we know the input will have the appropriate
2551 // precision, and these operations don't modify precision at all.
2552 break;
2553
Chris Lattner9d6fa982005-04-28 21:44:33 +00002554 case ISD::FSQRT:
2555 case ISD::FSIN:
2556 case ISD::FCOS:
2557 Tmp1 = PromoteOp(Node->getOperand(0));
2558 assert(Tmp1.getValueType() == NVT);
2559 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2560 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002561 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2562 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002563 break;
2564
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002565 case ISD::AND:
2566 case ISD::OR:
2567 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002568 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002569 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002570 case ISD::MUL:
2571 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002572 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002573 // that too is okay if they are integer operations.
2574 Tmp1 = PromoteOp(Node->getOperand(0));
2575 Tmp2 = PromoteOp(Node->getOperand(1));
2576 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2577 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002578 break;
2579 case ISD::FADD:
2580 case ISD::FSUB:
2581 case ISD::FMUL:
2582 // The input may have strange things in the top bits of the registers, but
2583 // these operations don't care.
2584 Tmp1 = PromoteOp(Node->getOperand(0));
2585 Tmp2 = PromoteOp(Node->getOperand(1));
2586 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2587 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2588
2589 // Floating point operations will give excess precision that we may not be
2590 // able to tolerate. If we DO allow excess precision, just leave it,
2591 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002592 // FIXME: Why would we need to round FP ops more than integer ones?
2593 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002594 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002595 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2596 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002597 break;
2598
Chris Lattner4d978642005-01-15 22:16:26 +00002599 case ISD::SDIV:
2600 case ISD::SREM:
2601 // These operators require that their input be sign extended.
2602 Tmp1 = PromoteOp(Node->getOperand(0));
2603 Tmp2 = PromoteOp(Node->getOperand(1));
2604 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002605 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2606 DAG.getValueType(VT));
2607 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2608 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002609 }
2610 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2611
2612 // Perform FP_ROUND: this is probably overly pessimistic.
2613 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002614 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2615 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002616 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002617 case ISD::FDIV:
2618 case ISD::FREM:
2619 // These operators require that their input be fp extended.
2620 Tmp1 = PromoteOp(Node->getOperand(0));
2621 Tmp2 = PromoteOp(Node->getOperand(1));
2622 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2623
2624 // Perform FP_ROUND: this is probably overly pessimistic.
2625 if (NoExcessFPPrecision)
2626 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2627 DAG.getValueType(VT));
2628 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002629
2630 case ISD::UDIV:
2631 case ISD::UREM:
2632 // These operators require that their input be zero extended.
2633 Tmp1 = PromoteOp(Node->getOperand(0));
2634 Tmp2 = PromoteOp(Node->getOperand(1));
2635 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002636 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2637 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002638 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2639 break;
2640
2641 case ISD::SHL:
2642 Tmp1 = PromoteOp(Node->getOperand(0));
2643 Tmp2 = LegalizeOp(Node->getOperand(1));
2644 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2645 break;
2646 case ISD::SRA:
2647 // The input value must be properly sign extended.
2648 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002649 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2650 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002651 Tmp2 = LegalizeOp(Node->getOperand(1));
2652 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2653 break;
2654 case ISD::SRL:
2655 // The input value must be properly zero extended.
2656 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002657 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002658 Tmp2 = LegalizeOp(Node->getOperand(1));
2659 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2660 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002661 case ISD::LOAD:
2662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2663 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002664 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2665 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002666 // Remember that we legalized the chain.
2667 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2668 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002669 case ISD::SEXTLOAD:
2670 case ISD::ZEXTLOAD:
2671 case ISD::EXTLOAD:
2672 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2673 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002674 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2675 Node->getOperand(2),
2676 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002677 // Remember that we legalized the chain.
2678 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2679 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002680 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002681 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2682 case Expand: assert(0 && "It's impossible to expand bools");
2683 case Legal:
2684 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2685 break;
2686 case Promote:
2687 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2688 break;
2689 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002690 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2691 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2692 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2693 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002694 case ISD::SELECT_CC:
2695 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2696 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2697 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2698 Node->getOperand(1), Tmp2, Tmp3,
2699 Node->getOperand(4));
2700 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002701 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002702 case ISD::CALL: {
2703 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2704 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2705
Chris Lattner3d95c142005-01-19 20:24:35 +00002706 std::vector<SDOperand> Ops;
2707 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2708 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2709
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002710 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2711 "Can only promote single result calls");
2712 std::vector<MVT::ValueType> RetTyVTs;
2713 RetTyVTs.reserve(2);
2714 RetTyVTs.push_back(NVT);
2715 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002716 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2717 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002718 Result = SDOperand(NC, 0);
2719
2720 // Insert the new chain mapping.
2721 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2722 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002723 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002724 case ISD::CTPOP:
2725 case ISD::CTTZ:
2726 case ISD::CTLZ:
2727 Tmp1 = Node->getOperand(0);
2728 //Zero extend the argument
2729 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2730 // Perform the larger operation, then subtract if needed.
2731 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2732 switch(Node->getOpcode())
2733 {
2734 case ISD::CTPOP:
2735 Result = Tmp1;
2736 break;
2737 case ISD::CTTZ:
2738 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002739 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002740 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002741 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002742 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2743 break;
2744 case ISD::CTLZ:
2745 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002746 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2747 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002748 getSizeInBits(VT), NVT));
2749 break;
2750 }
2751 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002752 }
2753
2754 assert(Result.Val && "Didn't set a result!");
2755 AddPromotedOperand(Op, Result);
2756 return Result;
2757}
Chris Lattnerdc750592005-01-07 07:47:09 +00002758
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002759/// ExpandAddSub - Find a clever way to expand this add operation into
2760/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002761void SelectionDAGLegalize::
2762ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2763 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002764 // Expand the subcomponents.
2765 SDOperand LHSL, LHSH, RHSL, RHSH;
2766 ExpandOp(LHS, LHSL, LHSH);
2767 ExpandOp(RHS, RHSL, RHSH);
2768
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002769 std::vector<SDOperand> Ops;
2770 Ops.push_back(LHSL);
2771 Ops.push_back(LHSH);
2772 Ops.push_back(RHSL);
2773 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002774 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2775 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002776 Hi = Lo.getValue(1);
2777}
2778
Chris Lattner4157c412005-04-02 04:00:59 +00002779void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2780 SDOperand Op, SDOperand Amt,
2781 SDOperand &Lo, SDOperand &Hi) {
2782 // Expand the subcomponents.
2783 SDOperand LHSL, LHSH;
2784 ExpandOp(Op, LHSL, LHSH);
2785
2786 std::vector<SDOperand> Ops;
2787 Ops.push_back(LHSL);
2788 Ops.push_back(LHSH);
2789 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002790 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002791 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002792 Hi = Lo.getValue(1);
2793}
2794
2795
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002796/// ExpandShift - Try to find a clever way to expand this shift operation out to
2797/// smaller elements. If we can't find a way that is more efficient than a
2798/// libcall on this target, return false. Otherwise, return true with the
2799/// low-parts expanded into Lo and Hi.
2800bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2801 SDOperand &Lo, SDOperand &Hi) {
2802 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2803 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002804
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002805 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002806 SDOperand ShAmt = LegalizeOp(Amt);
2807 MVT::ValueType ShTy = ShAmt.getValueType();
2808 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2809 unsigned NVTBits = MVT::getSizeInBits(NVT);
2810
2811 // Handle the case when Amt is an immediate. Other cases are currently broken
2812 // and are disabled.
2813 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2814 unsigned Cst = CN->getValue();
2815 // Expand the incoming operand to be shifted, so that we have its parts
2816 SDOperand InL, InH;
2817 ExpandOp(Op, InL, InH);
2818 switch(Opc) {
2819 case ISD::SHL:
2820 if (Cst > VTBits) {
2821 Lo = DAG.getConstant(0, NVT);
2822 Hi = DAG.getConstant(0, NVT);
2823 } else if (Cst > NVTBits) {
2824 Lo = DAG.getConstant(0, NVT);
2825 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002826 } else if (Cst == NVTBits) {
2827 Lo = DAG.getConstant(0, NVT);
2828 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002829 } else {
2830 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2831 Hi = DAG.getNode(ISD::OR, NVT,
2832 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2833 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2834 }
2835 return true;
2836 case ISD::SRL:
2837 if (Cst > VTBits) {
2838 Lo = DAG.getConstant(0, NVT);
2839 Hi = DAG.getConstant(0, NVT);
2840 } else if (Cst > NVTBits) {
2841 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2842 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002843 } else if (Cst == NVTBits) {
2844 Lo = InH;
2845 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002846 } else {
2847 Lo = DAG.getNode(ISD::OR, NVT,
2848 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2849 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2850 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2851 }
2852 return true;
2853 case ISD::SRA:
2854 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002855 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002856 DAG.getConstant(NVTBits-1, ShTy));
2857 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002858 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002859 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002860 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002861 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002862 } else if (Cst == NVTBits) {
2863 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002864 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002865 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002866 } else {
2867 Lo = DAG.getNode(ISD::OR, NVT,
2868 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2869 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2870 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2871 }
2872 return true;
2873 }
2874 }
2875 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2876 // so disable it for now. Currently targets are handling this via SHL_PARTS
2877 // and friends.
2878 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002879
2880 // If we have an efficient select operation (or if the selects will all fold
2881 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002882 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002883 return false;
2884
2885 SDOperand InL, InH;
2886 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002887 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2888 DAG.getConstant(NVTBits, ShTy), ShAmt);
2889
Chris Lattner4d25c042005-01-20 20:29:23 +00002890 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002891 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2892 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002893
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002894 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2895 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2896 DAG.getConstant(NVTBits-1, ShTy));
2897 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2898 DAG.getConstant(NVTBits-1, ShTy));
2899 }
2900
2901 if (Opc == ISD::SHL) {
2902 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2903 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2904 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002905 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002906
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002907 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2908 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2909 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002910 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002911 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2912 DAG.getConstant(32, ShTy),
2913 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002914 DAG.getConstant(0, NVT),
2915 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002916 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002917 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002918 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002919 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002920
2921 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002922 if (Opc == ISD::SRA)
2923 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2924 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002925 else
2926 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002927 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002928 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002929 }
2930 return true;
2931}
Chris Lattneraac464e2005-01-21 06:05:23 +00002932
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002933/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2934/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002935/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002936static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002937 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002938
Chris Lattner2dce7032005-05-12 23:24:06 +00002939 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002940 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002941 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002942 Found = Node;
2943 return;
2944 }
2945
2946 // Otherwise, scan the operands of Node to see if any of them is a call.
2947 assert(Node->getNumOperands() != 0 &&
2948 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002949 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002950 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002951
2952 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002953 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002954 Found);
2955}
2956
2957
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002958/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2959/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002960/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002961static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2962 std::set<SDNode*> &Visited) {
2963 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2964 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002965
Chris Lattner2dce7032005-05-12 23:24:06 +00002966 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002967 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002968 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002969 Found = Node;
2970 return;
2971 }
2972
2973 // Otherwise, scan the operands of Node to see if any of them is a call.
2974 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2975 if (UI == E) return;
2976 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002977 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002978
2979 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002980 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002981}
2982
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002983/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002984/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002985static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002986 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002987 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002988 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002989 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002990
Chris Lattner4add7e32005-01-23 04:42:50 +00002991 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002992 if (TheChain.getValueType() != MVT::Other)
2993 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002994 if (TheChain.getValueType() != MVT::Other)
2995 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002996
2997 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002998 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002999
Chris Lattner4add7e32005-01-23 04:42:50 +00003000 // Make sure to only follow users of our token chain.
3001 SDNode *User = *UI;
3002 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3003 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003004 if (SDNode *Result = FindCallSeqEnd(User))
3005 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003006 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003007 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003008}
3009
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003010/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003011/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003012static SDNode *FindCallSeqStart(SDNode *Node) {
3013 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003014 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003015
3016 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3017 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003018 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003019}
3020
3021
Chris Lattner4add7e32005-01-23 04:42:50 +00003022/// FindInputOutputChains - If we are replacing an operation with a call we need
3023/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003024/// properly serialize the calls in the block. The returned operand is the
3025/// input chain value for the new call (e.g. the entry node or the previous
3026/// call), and OutChain is set to be the chain node to update to point to the
3027/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003028static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3029 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003030 SDNode *LatestCallSeqStart = Entry.Val;
3031 SDNode *LatestCallSeqEnd = 0;
3032 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3033 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003034
Chris Lattner2dce7032005-05-12 23:24:06 +00003035 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003036 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003037 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3038 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003039 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003040 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003041 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3042 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003043 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003044 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003045 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003046
Chris Lattner06bbeb62005-05-11 19:02:11 +00003047 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003048 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003049 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003050 std::set<SDNode*> Visited;
3051 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003052
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003053 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003054 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003055 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003056
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003057 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003058}
3059
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003060/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003061void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3062 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003063 // Nothing to splice it into?
3064 if (OutChain == 0) return;
3065
3066 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3067 //OutChain->dump();
3068
3069 // Form a token factor node merging the old inval and the new inval.
3070 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3071 OutChain->getOperand(0));
3072 // Change the node to refer to the new token.
3073 OutChain->setAdjCallChain(InToken);
3074}
Chris Lattner4add7e32005-01-23 04:42:50 +00003075
3076
Chris Lattneraac464e2005-01-21 06:05:23 +00003077// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3078// does not fit into a register, return the lo part and set the hi part to the
3079// by-reg argument. If it does fit into a single register, return the result
3080// and leave the Hi part unset.
3081SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3082 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003083 SDNode *OutChain;
3084 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3085 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003086 if (InChain.Val == 0)
3087 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003088
Chris Lattneraac464e2005-01-21 06:05:23 +00003089 TargetLowering::ArgListTy Args;
3090 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3091 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3092 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3093 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3094 }
3095 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003096
Chris Lattner06bbeb62005-05-11 19:02:11 +00003097 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003098 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003099 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003100 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3101 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003102
Chris Lattner63022662005-09-02 20:26:58 +00003103 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003104 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003105 default: assert(0 && "Unknown thing");
3106 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003107 Result = CallInfo.first;
3108 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003109 case Promote:
3110 assert(0 && "Cannot promote this yet!");
3111 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003112 ExpandOp(CallInfo.first, Result, Hi);
3113 CallInfo.second = LegalizeOp(CallInfo.second);
3114 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003115 }
Chris Lattner63022662005-09-02 20:26:58 +00003116
3117 SpliceCallInto(CallInfo.second, OutChain);
3118 NeedsAnotherIteration = true;
3119 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003120}
3121
Chris Lattner4add7e32005-01-23 04:42:50 +00003122
Chris Lattneraac464e2005-01-21 06:05:23 +00003123/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3124/// destination type is legal.
3125SDOperand SelectionDAGLegalize::
3126ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003127 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003128 assert(getTypeAction(Source.getValueType()) == Expand &&
3129 "This is not an expansion!");
3130 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3131
Chris Lattner06bbeb62005-05-11 19:02:11 +00003132 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003133 assert(Source.getValueType() == MVT::i64 &&
3134 "This only works for 64-bit -> FP");
3135 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3136 // incoming integer is set. To handle this, we dynamically test to see if
3137 // it is set, and, if so, add a fudge factor.
3138 SDOperand Lo, Hi;
3139 ExpandOp(Source, Lo, Hi);
3140
Chris Lattner2a4f7312005-05-13 04:45:13 +00003141 // If this is unsigned, and not supported, first perform the conversion to
3142 // signed, then adjust the result if the sign bit is set.
3143 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3144 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3145
Chris Lattnerd47675e2005-08-09 20:20:18 +00003146 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3147 DAG.getConstant(0, Hi.getValueType()),
3148 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003149 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3150 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3151 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003152 uint64_t FF = 0x5f800000ULL;
3153 if (TLI.isLittleEndian()) FF <<= 32;
3154 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003155
Chris Lattnerc30405e2005-08-26 17:15:30 +00003156 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003157 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3158 SDOperand FudgeInReg;
3159 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003160 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3161 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003162 else {
3163 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003164 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3165 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003166 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003167 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003168 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003169
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003170 // Check to see if the target has a custom way to lower this. If so, use it.
3171 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3172 default: assert(0 && "This action not implemented for this operation!");
3173 case TargetLowering::Legal:
3174 case TargetLowering::Expand:
3175 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003176 case TargetLowering::Custom: {
3177 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3178 Source), DAG);
3179 if (NV.Val)
3180 return LegalizeOp(NV);
3181 break; // The target decided this was legal after all
3182 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003183 }
3184
Chris Lattner153587e2005-05-12 07:00:44 +00003185 // Expand the source, then glue it back together for the call. We must expand
3186 // the source in case it is shared (this pass of legalize must traverse it).
3187 SDOperand SrcLo, SrcHi;
3188 ExpandOp(Source, SrcLo, SrcHi);
3189 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3190
Chris Lattner06bbeb62005-05-11 19:02:11 +00003191 SDNode *OutChain = 0;
3192 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3193 DAG.getEntryNode());
3194 const char *FnName = 0;
3195 if (DestTy == MVT::f32)
3196 FnName = "__floatdisf";
3197 else {
3198 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3199 FnName = "__floatdidf";
3200 }
3201
Chris Lattneraac464e2005-01-21 06:05:23 +00003202 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3203
3204 TargetLowering::ArgListTy Args;
3205 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003206
Chris Lattneraac464e2005-01-21 06:05:23 +00003207 Args.push_back(std::make_pair(Source, ArgTy));
3208
3209 // We don't care about token chains for libcalls. We just use the entry
3210 // node as our input and ignore the output chain. This allows us to place
3211 // calls wherever we need them to satisfy data dependences.
3212 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003213
3214 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003215 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3216 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003217
Chris Lattnera5bf1032005-05-12 04:49:08 +00003218 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003219 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003220}
Misha Brukman835702a2005-04-21 22:36:52 +00003221
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003222
3223
Chris Lattnerdc750592005-01-07 07:47:09 +00003224/// ExpandOp - Expand the specified SDOperand into its two component pieces
3225/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3226/// LegalizeNodes map is filled in for any results that are not expanded, the
3227/// ExpandedNodes map is filled in for any results that are expanded, and the
3228/// Lo/Hi values are returned.
3229void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3230 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003231 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003232 SDNode *Node = Op.Val;
3233 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003234 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3235 "Cannot expand FP values!");
3236 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003237 "Cannot expand to FP value or to larger int value!");
3238
Chris Lattner1a570f12005-09-02 20:32:45 +00003239 // See if we already expanded it.
3240 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3241 = ExpandedNodes.find(Op);
3242 if (I != ExpandedNodes.end()) {
3243 Lo = I->second.first;
3244 Hi = I->second.second;
3245 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003246 }
3247
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003248 // Expanding to multiple registers needs to perform an optimization step, and
3249 // is not careful to avoid operations the target does not support. Make sure
3250 // that all generated operations are legalized in the next iteration.
3251 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003252
Chris Lattnerdc750592005-01-07 07:47:09 +00003253 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003254 case ISD::CopyFromReg:
3255 assert(0 && "CopyFromReg must be legal!");
3256 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003257 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3258 assert(0 && "Do not know how to expand this operator!");
3259 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003260 case ISD::UNDEF:
3261 Lo = DAG.getNode(ISD::UNDEF, NVT);
3262 Hi = DAG.getNode(ISD::UNDEF, NVT);
3263 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003264 case ISD::Constant: {
3265 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3266 Lo = DAG.getConstant(Cst, NVT);
3267 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3268 break;
3269 }
Nate Begemanae89d862005-12-07 19:48:11 +00003270 case ISD::ConstantVec: {
3271 unsigned NumElements = Node->getNumOperands();
3272 // If we only have two elements left in the constant vector, just break it
3273 // apart into the two scalar constants it contains. Otherwise, bisect the
3274 // ConstantVec, and return each half as a new ConstantVec.
3275 // FIXME: this is hard coded as big endian, it may have to change to support
3276 // SSE and Alpha MVI
3277 if (NumElements == 2) {
3278 Hi = Node->getOperand(0);
3279 Lo = Node->getOperand(1);
3280 } else {
3281 NumElements /= 2;
3282 std::vector<SDOperand> LoOps, HiOps;
3283 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3284 HiOps.push_back(Node->getOperand(I));
3285 LoOps.push_back(Node->getOperand(I+NumElements));
3286 }
3287 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3288 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3289 }
3290 break;
3291 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003292
Chris Lattner32e08b72005-03-28 22:03:13 +00003293 case ISD::BUILD_PAIR:
3294 // Legalize both operands. FIXME: in the future we should handle the case
3295 // where the two elements are not legal.
3296 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3297 Lo = LegalizeOp(Node->getOperand(0));
3298 Hi = LegalizeOp(Node->getOperand(1));
3299 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003300
3301 case ISD::SIGN_EXTEND_INREG:
3302 ExpandOp(Node->getOperand(0), Lo, Hi);
3303 // Sign extend the lo-part.
3304 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3305 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3306 TLI.getShiftAmountTy()));
3307 // sext_inreg the low part if needed.
3308 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3309 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003310
Chris Lattner55e9cde2005-05-11 04:51:16 +00003311 case ISD::CTPOP:
3312 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003313 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3314 DAG.getNode(ISD::CTPOP, NVT, Lo),
3315 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003316 Hi = DAG.getConstant(0, NVT);
3317 break;
3318
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003319 case ISD::CTLZ: {
3320 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003321 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003322 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3323 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003324 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3325 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003326 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3327 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3328
3329 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3330 Hi = DAG.getConstant(0, NVT);
3331 break;
3332 }
3333
3334 case ISD::CTTZ: {
3335 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003336 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003337 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3338 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003339 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3340 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003341 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3342 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3343
3344 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3345 Hi = DAG.getConstant(0, NVT);
3346 break;
3347 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003348
Chris Lattnerdc750592005-01-07 07:47:09 +00003349 case ISD::LOAD: {
3350 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3351 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003352 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003353
3354 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003355 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003356 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3357 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003358 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003359 //are from the same instruction?
3360 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003361
3362 // Build a factor node to remember that this load is independent of the
3363 // other one.
3364 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3365 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003366
Chris Lattnerdc750592005-01-07 07:47:09 +00003367 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003368 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003369 if (!TLI.isLittleEndian())
3370 std::swap(Lo, Hi);
3371 break;
3372 }
Nate Begemand37c1312005-11-22 18:16:00 +00003373 case ISD::VLOAD: {
3374 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3375 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3376 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3377 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3378
3379 // If we only have two elements, turn into a pair of scalar loads.
3380 // FIXME: handle case where a vector of two elements is fine, such as
3381 // 2 x double on SSE2.
3382 if (NumElements == 2) {
3383 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3384 // Increment the pointer to the other half.
3385 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3386 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3387 getIntPtrConstant(IncrementSize));
3388 //Is this safe? declaring that the two parts of the split load
3389 //are from the same instruction?
3390 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3391 } else {
3392 NumElements /= 2; // Split the vector in half
3393 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3394 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3395 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3396 getIntPtrConstant(IncrementSize));
3397 //Is this safe? declaring that the two parts of the split load
3398 //are from the same instruction?
3399 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3400 }
3401
3402 // Build a factor node to remember that this load is independent of the
3403 // other one.
3404 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3405 Hi.getValue(1));
3406
3407 // Remember that we legalized the chain.
3408 AddLegalizedOperand(Op.getValue(1), TF);
3409 if (!TLI.isLittleEndian())
3410 std::swap(Lo, Hi);
3411 break;
3412 }
3413 case ISD::VADD:
3414 case ISD::VSUB:
3415 case ISD::VMUL: {
3416 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3417 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3418 SDOperand LL, LH, RL, RH;
3419
3420 ExpandOp(Node->getOperand(0), LL, LH);
3421 ExpandOp(Node->getOperand(1), RL, RH);
3422
3423 // If we only have two elements, turn into a pair of scalar loads.
3424 // FIXME: handle case where a vector of two elements is fine, such as
3425 // 2 x double on SSE2.
3426 if (NumElements == 2) {
3427 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3428 Lo = DAG.getNode(Opc, EVT, LL, RL);
3429 Hi = DAG.getNode(Opc, EVT, LH, RH);
3430 } else {
3431 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3432 LL.getOperand(3));
3433 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3434 LH.getOperand(3));
3435 }
3436 break;
3437 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003438 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003439 case ISD::CALL: {
3440 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3441 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3442
Chris Lattner3d95c142005-01-19 20:24:35 +00003443 bool Changed = false;
3444 std::vector<SDOperand> Ops;
3445 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3446 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3447 Changed |= Ops.back() != Node->getOperand(i);
3448 }
3449
Chris Lattnerdc750592005-01-07 07:47:09 +00003450 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3451 "Can only expand a call once so far, not i64 -> i16!");
3452
3453 std::vector<MVT::ValueType> RetTyVTs;
3454 RetTyVTs.reserve(3);
3455 RetTyVTs.push_back(NVT);
3456 RetTyVTs.push_back(NVT);
3457 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003458 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3459 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003460 Lo = SDOperand(NC, 0);
3461 Hi = SDOperand(NC, 1);
3462
3463 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003464 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003465 break;
3466 }
3467 case ISD::AND:
3468 case ISD::OR:
3469 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3470 SDOperand LL, LH, RL, RH;
3471 ExpandOp(Node->getOperand(0), LL, LH);
3472 ExpandOp(Node->getOperand(1), RL, RH);
3473 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3474 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3475 break;
3476 }
3477 case ISD::SELECT: {
3478 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003479
3480 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3481 case Expand: assert(0 && "It's impossible to expand bools");
3482 case Legal:
3483 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3484 break;
3485 case Promote:
3486 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3487 break;
3488 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003489 ExpandOp(Node->getOperand(1), LL, LH);
3490 ExpandOp(Node->getOperand(2), RL, RH);
3491 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3492 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3493 break;
3494 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003495 case ISD::SELECT_CC: {
3496 SDOperand TL, TH, FL, FH;
3497 ExpandOp(Node->getOperand(2), TL, TH);
3498 ExpandOp(Node->getOperand(3), FL, FH);
3499 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3500 Node->getOperand(1), TL, FL, Node->getOperand(4));
3501 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3502 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003503 Lo = LegalizeOp(Lo);
3504 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003505 break;
3506 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003507 case ISD::SEXTLOAD: {
3508 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3509 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3510 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3511
3512 if (EVT == NVT)
3513 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3514 else
3515 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3516 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003517
3518 // Remember that we legalized the chain.
3519 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3520
Nate Begemanc3a89c52005-10-13 17:15:37 +00003521 // The high part is obtained by SRA'ing all but one of the bits of the lo
3522 // part.
3523 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3524 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3525 TLI.getShiftAmountTy()));
3526 Lo = LegalizeOp(Lo);
3527 Hi = LegalizeOp(Hi);
3528 break;
3529 }
3530 case ISD::ZEXTLOAD: {
3531 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3532 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3533 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3534
3535 if (EVT == NVT)
3536 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3537 else
3538 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3539 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003540
3541 // Remember that we legalized the chain.
3542 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3543
Nate Begemanc3a89c52005-10-13 17:15:37 +00003544 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003545 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003546 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003547 break;
3548 }
3549 case ISD::EXTLOAD: {
3550 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3551 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3552 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3553
3554 if (EVT == NVT)
3555 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3556 else
3557 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3558 EVT);
3559
3560 // Remember that we legalized the chain.
3561 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3562
3563 // The high part is undefined.
3564 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3565 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003566 break;
3567 }
Chris Lattner7753f172005-09-02 00:18:10 +00003568 case ISD::ANY_EXTEND: {
3569 SDOperand In;
3570 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3571 case Expand: assert(0 && "expand-expand not implemented yet!");
3572 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3573 case Promote:
3574 In = PromoteOp(Node->getOperand(0));
3575 break;
3576 }
3577
3578 // The low part is any extension of the input (which degenerates to a copy).
3579 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3580 // The high part is undefined.
3581 Hi = DAG.getNode(ISD::UNDEF, NVT);
3582 break;
3583 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003584 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003585 SDOperand In;
3586 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3587 case Expand: assert(0 && "expand-expand not implemented yet!");
3588 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3589 case Promote:
3590 In = PromoteOp(Node->getOperand(0));
3591 // Emit the appropriate sign_extend_inreg to get the value we want.
3592 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003593 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003594 break;
3595 }
3596
Chris Lattnerdc750592005-01-07 07:47:09 +00003597 // The low part is just a sign extension of the input (which degenerates to
3598 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003599 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003600
Chris Lattnerdc750592005-01-07 07:47:09 +00003601 // The high part is obtained by SRA'ing all but one of the bits of the lo
3602 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003603 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003604 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3605 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003606 break;
3607 }
Chris Lattner47844892005-04-03 23:41:52 +00003608 case ISD::ZERO_EXTEND: {
3609 SDOperand In;
3610 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3611 case Expand: assert(0 && "expand-expand not implemented yet!");
3612 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3613 case Promote:
3614 In = PromoteOp(Node->getOperand(0));
3615 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003616 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003617 break;
3618 }
3619
Chris Lattnerdc750592005-01-07 07:47:09 +00003620 // The low part is just a zero extension of the input (which degenerates to
3621 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003622 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003623
Chris Lattnerdc750592005-01-07 07:47:09 +00003624 // The high part is just a zero.
3625 Hi = DAG.getConstant(0, NVT);
3626 break;
Chris Lattner47844892005-04-03 23:41:52 +00003627 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003628
Chris Lattner44c28c22005-11-20 22:56:56 +00003629 case ISD::READCYCLECOUNTER: {
3630 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3631 TargetLowering::Custom &&
3632 "Must custom expand ReadCycleCounter");
3633 SDOperand T = TLI.LowerOperation(Op, DAG);
3634 assert(T.Val && "Node must be custom expanded!");
3635 Lo = LegalizeOp(T.getValue(0));
3636 Hi = LegalizeOp(T.getValue(1));
3637 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3638 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003639 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003640 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003641
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003642 // These operators cannot be expanded directly, emit them as calls to
3643 // library functions.
3644 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003645 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003646 SDOperand Op;
3647 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3648 case Expand: assert(0 && "cannot expand FP!");
3649 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3650 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3651 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003652
Chris Lattner941d84a2005-07-30 01:40:57 +00003653 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3654
Chris Lattnerfe68d752005-07-29 00:33:32 +00003655 // Now that the custom expander is done, expand the result, which is still
3656 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003657 if (Op.Val) {
3658 ExpandOp(Op, Lo, Hi);
3659 break;
3660 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003661 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003662
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003663 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003664 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003665 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003666 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003667 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003668
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003669 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003670 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3671 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3672 LegalizeOp(Node->getOperand(0)));
3673 // Now that the custom expander is done, expand the result, which is still
3674 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003675 Op = TLI.LowerOperation(Op, DAG);
3676 if (Op.Val) {
3677 ExpandOp(Op, Lo, Hi);
3678 break;
3679 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003680 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003681
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003682 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003683 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003684 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003685 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003686 break;
3687
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003688 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003689 // If the target wants custom lowering, do so.
3690 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3691 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3692 LegalizeOp(Node->getOperand(1)));
3693 Op = TLI.LowerOperation(Op, DAG);
3694 if (Op.Val) {
3695 // Now that the custom expander is done, expand the result, which is
3696 // still VT.
3697 ExpandOp(Op, Lo, Hi);
3698 break;
3699 }
3700 }
3701
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003702 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003703 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003704 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003705
3706 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003707 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003708 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3709 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003710 break;
3711 }
3712
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003713 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003714 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003715 break;
3716
3717 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003718 // If the target wants custom lowering, do so.
3719 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3720 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3721 LegalizeOp(Node->getOperand(1)));
3722 Op = TLI.LowerOperation(Op, DAG);
3723 if (Op.Val) {
3724 // Now that the custom expander is done, expand the result, which is
3725 // still VT.
3726 ExpandOp(Op, Lo, Hi);
3727 break;
3728 }
3729 }
3730
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003731 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003732 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003733 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003734
3735 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003736 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003737 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3738 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003739 break;
3740 }
3741
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003742 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003743 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003744 break;
3745 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003746 // If the target wants custom lowering, do so.
3747 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3748 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3749 LegalizeOp(Node->getOperand(1)));
3750 Op = TLI.LowerOperation(Op, DAG);
3751 if (Op.Val) {
3752 // Now that the custom expander is done, expand the result, which is
3753 // still VT.
3754 ExpandOp(Op, Lo, Hi);
3755 break;
3756 }
3757 }
3758
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003759 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003760 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003761 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003762
3763 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003764 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003765 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3766 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003767 break;
3768 }
3769
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003770 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003771 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003772 break;
3773
Misha Brukman835702a2005-04-21 22:36:52 +00003774 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003775 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3776 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003777 break;
3778 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003779 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3780 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003781 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003782 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003783 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003784 SDOperand LL, LH, RL, RH;
3785 ExpandOp(Node->getOperand(0), LL, LH);
3786 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003787 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3788 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3789 // extended the sign bit of the low half through the upper half, and if so
3790 // emit a MULHS instead of the alternate sequence that is valid for any
3791 // i64 x i64 multiply.
3792 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3793 // is RH an extension of the sign bit of RL?
3794 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3795 RH.getOperand(1).getOpcode() == ISD::Constant &&
3796 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3797 // is LH an extension of the sign bit of LL?
3798 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3799 LH.getOperand(1).getOpcode() == ISD::Constant &&
3800 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3801 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3802 } else {
3803 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3804 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3805 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3806 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3807 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3808 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003809 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3810 } else {
3811 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3812 }
3813 break;
3814 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003815 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3816 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3817 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3818 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003819 }
3820
3821 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003822 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3823 std::make_pair(Lo, Hi))).second;
3824 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003825}
3826
3827
3828// SelectionDAG::Legalize - This is the entry point for the file.
3829//
Chris Lattner4add7e32005-01-23 04:42:50 +00003830void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003831 /// run - This is the main entry point to this class.
3832 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003833 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003834}
3835