blob: e3d20e480f0fed5ce5102e84823d92058a3a718c [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:
Chris Lattnerdc750592005-01-07 07:47:09 +0000535 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000536 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000537 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000538 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000539 case ISD::BasicBlock:
540 case ISD::CONDCODE:
541 case ISD::VALUETYPE:
542 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000543 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000544 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
545 default: assert(0 && "This action is not supported yet!");
546 case TargetLowering::Custom: {
547 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
548 if (Tmp.Val) {
549 Result = LegalizeOp(Tmp);
550 break;
551 }
552 } // FALLTHROUGH if the target doesn't want to lower this op after all.
553 case TargetLowering::Legal:
554 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
555 break;
556 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000557 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000558 case ISD::AssertSext:
559 case ISD::AssertZext:
560 Tmp1 = LegalizeOp(Node->getOperand(0));
561 if (Tmp1 != Node->getOperand(0))
562 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
563 Node->getOperand(1));
564 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000565 case ISD::MERGE_VALUES:
566 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000567 case ISD::CopyFromReg:
568 Tmp1 = LegalizeOp(Node->getOperand(0));
569 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000570 Result = DAG.getCopyFromReg(Tmp1,
571 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
572 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000573 else
574 Result = Op.getValue(0);
575
576 // Since CopyFromReg produces two values, make sure to remember that we
577 // legalized both of them.
578 AddLegalizedOperand(Op.getValue(0), Result);
579 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
580 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000581 case ISD::ImplicitDef:
582 Tmp1 = LegalizeOp(Node->getOperand(0));
583 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000584 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
585 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000586 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000587 case ISD::UNDEF: {
588 MVT::ValueType VT = Op.getValueType();
589 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000590 default: assert(0 && "This action is not supported yet!");
591 case TargetLowering::Expand:
592 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000593 if (MVT::isInteger(VT))
594 Result = DAG.getConstant(0, VT);
595 else if (MVT::isFloatingPoint(VT))
596 Result = DAG.getConstantFP(0, VT);
597 else
598 assert(0 && "Unknown value type!");
599 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000600 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000601 break;
602 }
603 break;
604 }
Chris Lattner435b4022005-11-29 06:21:05 +0000605
606 case ISD::LOCATION:
607 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
608 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
609
610 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
611 case TargetLowering::Promote:
612 default: assert(0 && "This action is not supported yet!");
613 case TargetLowering::Expand:
614 // If the target doesn't support line numbers, ignore this node.
615 Result = Tmp1;
616 break;
617 case TargetLowering::Legal:
618 if (Tmp1 != Node->getOperand(0)) {
619 std::vector<SDOperand> Ops;
620 Ops.push_back(Tmp1);
621 Ops.push_back(Node->getOperand(1)); // line # must be legal.
622 Ops.push_back(Node->getOperand(2)); // col # must be legal.
623 Ops.push_back(Node->getOperand(3)); // filename must be legal.
624 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
625 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
626 }
627 break;
628 }
629 break;
630
Chris Lattnerdc750592005-01-07 07:47:09 +0000631 case ISD::Constant:
632 // We know we don't need to expand constants here, constants only have one
633 // value and we check that it is fine above.
634
635 // FIXME: Maybe we should handle things like targets that don't support full
636 // 32-bit immediates?
637 break;
638 case ISD::ConstantFP: {
639 // Spill FP immediates to the constant pool if the target cannot directly
640 // codegen them. Targets often have some immediate values that can be
641 // efficiently generated into an FP register without a load. We explicitly
642 // leave these constants as ConstantFP nodes for the target to deal with.
643
644 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
645
646 // Check to see if this FP immediate is already legal.
647 bool isLegal = false;
648 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
649 E = TLI.legal_fpimm_end(); I != E; ++I)
650 if (CFP->isExactlyValue(*I)) {
651 isLegal = true;
652 break;
653 }
654
655 if (!isLegal) {
656 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000657 bool Extend = false;
658
659 // If a FP immediate is precise when represented as a float, we put it
660 // into the constant pool as a float, even if it's is statically typed
661 // as a double.
662 MVT::ValueType VT = CFP->getValueType(0);
663 bool isDouble = VT == MVT::f64;
664 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
665 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000666 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
667 // Only do this if the target has a native EXTLOAD instruction from
668 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000669 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000670 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
671 VT = MVT::f32;
672 Extend = true;
673 }
Misha Brukman835702a2005-04-21 22:36:52 +0000674
Chris Lattnerc30405e2005-08-26 17:15:30 +0000675 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000676 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000677 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
678 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000679 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000680 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
681 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000682 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 }
684 break;
685 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000686 case ISD::TokenFactor:
687 if (Node->getNumOperands() == 2) {
688 bool Changed = false;
689 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
690 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
691 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
692 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
693 } else {
694 std::vector<SDOperand> Ops;
695 bool Changed = false;
696 // Legalize the operands.
697 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
698 SDOperand Op = Node->getOperand(i);
699 Ops.push_back(LegalizeOp(Op));
700 Changed |= Ops[i] != Op;
701 }
702 if (Changed)
703 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000704 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000705 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000706
Chris Lattner2dce7032005-05-12 23:24:06 +0000707 case ISD::CALLSEQ_START:
708 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000709 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000710 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000711 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000712 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000713 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000714
Chris Lattner2dce7032005-05-12 23:24:06 +0000715 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000716 // nodes are treated specially and are mutated in place. This makes the dag
717 // legalization process more efficient and also makes libcall insertion
718 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000719 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000720 case ISD::DYNAMIC_STACKALLOC:
721 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
722 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
723 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
724 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000725 Tmp3 != Node->getOperand(2)) {
726 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
727 std::vector<SDOperand> Ops;
728 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
729 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
730 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000731 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000732
733 // Since this op produces two values, make sure to remember that we
734 // legalized both of them.
735 AddLegalizedOperand(SDOperand(Node, 0), Result);
736 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
737 return Result.getValue(Op.ResNo);
738
Chris Lattnerd0feb642005-05-13 18:43:43 +0000739 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000740 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000741 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
742 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000743
744 bool Changed = false;
745 std::vector<SDOperand> Ops;
746 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
747 Ops.push_back(LegalizeOp(Node->getOperand(i)));
748 Changed |= Ops.back() != Node->getOperand(i);
749 }
750
751 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000752 std::vector<MVT::ValueType> RetTyVTs;
753 RetTyVTs.reserve(Node->getNumValues());
754 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000755 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000756 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
757 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000758 } else {
759 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000760 }
Chris Lattner9242c502005-01-09 19:43:23 +0000761 // Since calls produce multiple values, make sure to remember that we
762 // legalized all of them.
763 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
764 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
765 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000766 }
Chris Lattner68a12142005-01-07 22:12:08 +0000767 case ISD::BR:
768 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
769 if (Tmp1 != Node->getOperand(0))
770 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
771 break;
772
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000773 case ISD::BRCOND:
774 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000775
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000776 switch (getTypeAction(Node->getOperand(1).getValueType())) {
777 case Expand: assert(0 && "It's impossible to expand bools");
778 case Legal:
779 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
780 break;
781 case Promote:
782 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
783 break;
784 }
Nate Begeman371e4952005-08-16 19:49:35 +0000785
786 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
787 default: assert(0 && "This action is not supported yet!");
788 case TargetLowering::Expand:
789 // Expand brcond's setcc into its constituent parts and create a BR_CC
790 // Node.
791 if (Tmp2.getOpcode() == ISD::SETCC) {
792 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
793 Tmp2.getOperand(0), Tmp2.getOperand(1),
794 Node->getOperand(2));
795 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000796 // Make sure the condition is either zero or one. It may have been
797 // promoted from something else.
798 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
799
Nate Begeman371e4952005-08-16 19:49:35 +0000800 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
801 DAG.getCondCode(ISD::SETNE), Tmp2,
802 DAG.getConstant(0, Tmp2.getValueType()),
803 Node->getOperand(2));
804 }
805 break;
806 case TargetLowering::Legal:
807 // Basic block destination (Op#2) is always legal.
808 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
809 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
810 Node->getOperand(2));
811 break;
812 }
813 break;
814 case ISD::BR_CC:
815 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
816
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000817 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000818 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
819 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
820 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
821 Tmp3 != Node->getOperand(3)) {
822 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
823 Tmp2, Tmp3, Node->getOperand(4));
824 }
825 break;
826 } else {
827 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
828 Node->getOperand(2), // LHS
829 Node->getOperand(3), // RHS
830 Node->getOperand(1)));
831 // If we get a SETCC back from legalizing the SETCC node we just
832 // created, then use its LHS, RHS, and CC directly in creating a new
833 // node. Otherwise, select between the true and false value based on
834 // comparing the result of the legalized with zero.
835 if (Tmp2.getOpcode() == ISD::SETCC) {
836 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
837 Tmp2.getOperand(0), Tmp2.getOperand(1),
838 Node->getOperand(4));
839 } else {
840 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
841 DAG.getCondCode(ISD::SETNE),
842 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
843 Node->getOperand(4));
844 }
845 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000846 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000847 case ISD::BRCONDTWOWAY:
848 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
849 switch (getTypeAction(Node->getOperand(1).getValueType())) {
850 case Expand: assert(0 && "It's impossible to expand bools");
851 case Legal:
852 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
853 break;
854 case Promote:
855 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
856 break;
857 }
858 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
859 // pair.
860 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
861 case TargetLowering::Promote:
862 default: assert(0 && "This action is not supported yet!");
863 case TargetLowering::Legal:
864 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
865 std::vector<SDOperand> Ops;
866 Ops.push_back(Tmp1);
867 Ops.push_back(Tmp2);
868 Ops.push_back(Node->getOperand(2));
869 Ops.push_back(Node->getOperand(3));
870 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
871 }
872 break;
873 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000874 // If BRTWOWAY_CC is legal for this target, then simply expand this node
875 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
876 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000877 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000878 if (Tmp2.getOpcode() == ISD::SETCC) {
879 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
880 Tmp2.getOperand(0), Tmp2.getOperand(1),
881 Node->getOperand(2), Node->getOperand(3));
882 } else {
883 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
884 DAG.getConstant(0, Tmp2.getValueType()),
885 Node->getOperand(2), Node->getOperand(3));
886 }
887 } else {
888 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000889 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000890 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
891 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000892 break;
893 }
894 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000895 case ISD::BRTWOWAY_CC:
896 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000897 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000898 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
899 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
900 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
901 Tmp3 != Node->getOperand(3)) {
902 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
903 Node->getOperand(4), Node->getOperand(5));
904 }
905 break;
906 } else {
907 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
908 Node->getOperand(2), // LHS
909 Node->getOperand(3), // RHS
910 Node->getOperand(1)));
911 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
912 // pair.
913 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Legal:
916 // If we get a SETCC back from legalizing the SETCC node we just
917 // created, then use its LHS, RHS, and CC directly in creating a new
918 // node. Otherwise, select between the true and false value based on
919 // comparing the result of the legalized with zero.
920 if (Tmp2.getOpcode() == ISD::SETCC) {
921 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
922 Tmp2.getOperand(0), Tmp2.getOperand(1),
923 Node->getOperand(4), Node->getOperand(5));
924 } else {
925 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
926 DAG.getConstant(0, Tmp2.getValueType()),
927 Node->getOperand(4), Node->getOperand(5));
928 }
929 break;
930 case TargetLowering::Expand:
931 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
932 Node->getOperand(4));
933 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
934 break;
935 }
936 }
937 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000938 case ISD::LOAD:
939 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
940 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000941
Chris Lattnerdc750592005-01-07 07:47:09 +0000942 if (Tmp1 != Node->getOperand(0) ||
943 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000944 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
945 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000946 else
947 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000948
Chris Lattnerea4ca942005-01-07 22:28:47 +0000949 // Since loads produce two values, make sure to remember that we legalized
950 // both of them.
951 AddLegalizedOperand(SDOperand(Node, 0), Result);
952 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
953 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000954
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000955 case ISD::EXTLOAD:
956 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000957 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000958 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
959 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000960
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000961 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000962 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000963 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000964 case TargetLowering::Promote:
965 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000966 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
967 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000968 // Since loads produce two values, make sure to remember that we legalized
969 // both of them.
970 AddLegalizedOperand(SDOperand(Node, 0), Result);
971 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
972 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000973
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000974 case TargetLowering::Legal:
975 if (Tmp1 != Node->getOperand(0) ||
976 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000977 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
978 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000979 else
980 Result = SDOperand(Node, 0);
981
982 // Since loads produce two values, make sure to remember that we legalized
983 // both of them.
984 AddLegalizedOperand(SDOperand(Node, 0), Result);
985 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
986 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000987 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000988 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
989 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
990 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000991 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000992 if (Op.ResNo)
993 return Load.getValue(1);
994 return Result;
995 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000996 assert(Node->getOpcode() != ISD::EXTLOAD &&
997 "EXTLOAD should always be supported!");
998 // Turn the unsupported load into an EXTLOAD followed by an explicit
999 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001000 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1001 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001002 SDOperand ValRes;
1003 if (Node->getOpcode() == ISD::SEXTLOAD)
1004 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001005 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001006 else
1007 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001008 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1009 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1010 if (Op.ResNo)
1011 return Result.getValue(1);
1012 return ValRes;
1013 }
1014 assert(0 && "Unreachable");
1015 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001016 case ISD::EXTRACT_ELEMENT: {
1017 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1018 switch (getTypeAction(OpTy)) {
1019 default:
1020 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1021 break;
1022 case Legal:
1023 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1024 // 1 -> Hi
1025 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1026 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1027 TLI.getShiftAmountTy()));
1028 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1029 } else {
1030 // 0 -> Lo
1031 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1032 Node->getOperand(0));
1033 }
1034 Result = LegalizeOp(Result);
1035 break;
1036 case Expand:
1037 // Get both the low and high parts.
1038 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1039 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1040 Result = Tmp2; // 1 -> Hi
1041 else
1042 Result = Tmp1; // 0 -> Lo
1043 break;
1044 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001045 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001046 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001047
1048 case ISD::CopyToReg:
1049 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001050
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001051 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001052 "Register type must be legal!");
1053 // Legalize the incoming value (must be legal).
1054 Tmp2 = LegalizeOp(Node->getOperand(2));
1055 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1056 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1057 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001058 break;
1059
1060 case ISD::RET:
1061 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1062 switch (Node->getNumOperands()) {
1063 case 2: // ret val
1064 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1065 case Legal:
1066 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001067 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001068 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1069 break;
1070 case Expand: {
1071 SDOperand Lo, Hi;
1072 ExpandOp(Node->getOperand(1), Lo, Hi);
1073 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001074 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001075 }
1076 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001077 Tmp2 = PromoteOp(Node->getOperand(1));
1078 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1079 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001080 }
1081 break;
1082 case 1: // ret void
1083 if (Tmp1 != Node->getOperand(0))
1084 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1085 break;
1086 default: { // ret <values>
1087 std::vector<SDOperand> NewValues;
1088 NewValues.push_back(Tmp1);
1089 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1090 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1091 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001092 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001093 break;
1094 case Expand: {
1095 SDOperand Lo, Hi;
1096 ExpandOp(Node->getOperand(i), Lo, Hi);
1097 NewValues.push_back(Lo);
1098 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001099 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001100 }
1101 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001102 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001103 }
1104 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1105 break;
1106 }
1107 }
1108 break;
1109 case ISD::STORE:
1110 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1111 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1112
Chris Lattnere69daaf2005-01-08 06:25:56 +00001113 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001114 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001115 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001116 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001117 DAG.getConstant(FloatToBits(CFP->getValue()),
1118 MVT::i32),
1119 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001120 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001121 } else {
1122 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001123 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001124 DAG.getConstant(DoubleToBits(CFP->getValue()),
1125 MVT::i64),
1126 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001127 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001128 }
Chris Lattnera4743132005-02-22 07:23:39 +00001129 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001130 }
1131
Chris Lattnerdc750592005-01-07 07:47:09 +00001132 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1133 case Legal: {
1134 SDOperand Val = LegalizeOp(Node->getOperand(1));
1135 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1136 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001137 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1138 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001139 break;
1140 }
1141 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001142 // Truncate the value and store the result.
1143 Tmp3 = PromoteOp(Node->getOperand(1));
1144 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001145 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001146 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001147 break;
1148
Chris Lattnerdc750592005-01-07 07:47:09 +00001149 case Expand:
1150 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001151 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001152 ExpandOp(Node->getOperand(1), Lo, Hi);
1153
1154 if (!TLI.isLittleEndian())
1155 std::swap(Lo, Hi);
1156
Chris Lattner55e9cde2005-05-11 04:51:16 +00001157 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1158 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001159 // If this is a vector type, then we have to calculate the increment as
1160 // the product of the element size in bytes, and the number of elements
1161 // in the high half of the vector.
1162 if (MVT::Vector == Hi.getValueType()) {
1163 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1164 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1165 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1166 } else {
1167 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1168 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001169 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1170 getIntPtrConstant(IncrementSize));
1171 assert(isTypeLegal(Tmp2.getValueType()) &&
1172 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001173 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001174 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1175 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001176 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1177 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001178 }
1179 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001180 case ISD::PCMARKER:
1181 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001182 if (Tmp1 != Node->getOperand(0))
1183 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001184 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001185 case ISD::READCYCLECOUNTER:
1186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
1187 if (Tmp1 != Node->getOperand(0))
1188 Result = DAG.getNode(ISD::READCYCLECOUNTER, MVT::i64, Tmp1);
1189 break;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001190
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001191 case ISD::TRUNCSTORE:
1192 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1193 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1194
1195 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1196 case Legal:
1197 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001198
1199 // The only promote case we handle is TRUNCSTORE:i1 X into
1200 // -> TRUNCSTORE:i8 (and X, 1)
1201 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1202 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1203 TargetLowering::Promote) {
1204 // Promote the bool to a mask then store.
1205 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1206 DAG.getConstant(1, Tmp2.getValueType()));
1207 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1208 Node->getOperand(3), DAG.getValueType(MVT::i8));
1209
1210 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1211 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001212 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001213 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001214 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001215 break;
1216 case Promote:
1217 case Expand:
1218 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1219 }
1220 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001221 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001222 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1223 case Expand: assert(0 && "It's impossible to expand bools");
1224 case Legal:
1225 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1226 break;
1227 case Promote:
1228 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1229 break;
1230 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001231 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001232 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001233
Nate Begeman987121a2005-08-23 04:29:48 +00001234 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001235 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001236 case TargetLowering::Expand:
1237 if (Tmp1.getOpcode() == ISD::SETCC) {
1238 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1239 Tmp2, Tmp3,
1240 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1241 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001242 // Make sure the condition is either zero or one. It may have been
1243 // promoted from something else.
1244 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001245 Result = DAG.getSelectCC(Tmp1,
1246 DAG.getConstant(0, Tmp1.getValueType()),
1247 Tmp2, Tmp3, ISD::SETNE);
1248 }
1249 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001250 case TargetLowering::Legal:
1251 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1252 Tmp3 != Node->getOperand(2))
1253 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1254 Tmp1, Tmp2, Tmp3);
1255 break;
1256 case TargetLowering::Promote: {
1257 MVT::ValueType NVT =
1258 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1259 unsigned ExtOp, TruncOp;
1260 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001261 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001262 TruncOp = ISD::TRUNCATE;
1263 } else {
1264 ExtOp = ISD::FP_EXTEND;
1265 TruncOp = ISD::FP_ROUND;
1266 }
1267 // Promote each of the values to the new type.
1268 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1269 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1270 // Perform the larger operation, then round down.
1271 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1272 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1273 break;
1274 }
1275 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001276 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001277 case ISD::SELECT_CC:
1278 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1279 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1280
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001281 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001282 // Everything is legal, see if we should expand this op or something.
1283 switch (TLI.getOperationAction(ISD::SELECT_CC,
1284 Node->getOperand(0).getValueType())) {
1285 default: assert(0 && "This action is not supported yet!");
1286 case TargetLowering::Custom: {
1287 SDOperand Tmp =
1288 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1289 Node->getOperand(0),
1290 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001291 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001292 if (Tmp.Val) {
1293 Result = LegalizeOp(Tmp);
1294 break;
1295 }
1296 } // FALLTHROUGH if the target can't lower this operation after all.
1297 case TargetLowering::Legal:
1298 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1299 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1300 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1301 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1302 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1303 Tmp3, Tmp4, Node->getOperand(4));
1304 }
1305 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001306 }
1307 break;
1308 } else {
1309 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1310 Node->getOperand(0), // LHS
1311 Node->getOperand(1), // RHS
1312 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001313 // If we get a SETCC back from legalizing the SETCC node we just
1314 // created, then use its LHS, RHS, and CC directly in creating a new
1315 // node. Otherwise, select between the true and false value based on
1316 // comparing the result of the legalized with zero.
1317 if (Tmp1.getOpcode() == ISD::SETCC) {
1318 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1319 Tmp1.getOperand(0), Tmp1.getOperand(1),
1320 Tmp3, Tmp4, Tmp1.getOperand(2));
1321 } else {
1322 Result = DAG.getSelectCC(Tmp1,
1323 DAG.getConstant(0, Tmp1.getValueType()),
1324 Tmp3, Tmp4, ISD::SETNE);
1325 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001326 }
1327 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001328 case ISD::SETCC:
1329 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1330 case Legal:
1331 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1332 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001333 break;
1334 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001335 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1336 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1337
1338 // If this is an FP compare, the operands have already been extended.
1339 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1340 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001341 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001342
1343 // Otherwise, we have to insert explicit sign or zero extends. Note
1344 // that we could insert sign extends for ALL conditions, but zero extend
1345 // is cheaper on many machines (an AND instead of two shifts), so prefer
1346 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001347 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001348 default: assert(0 && "Unknown integer comparison!");
1349 case ISD::SETEQ:
1350 case ISD::SETNE:
1351 case ISD::SETUGE:
1352 case ISD::SETUGT:
1353 case ISD::SETULE:
1354 case ISD::SETULT:
1355 // ALL of these operations will work if we either sign or zero extend
1356 // the operands (including the unsigned comparisons!). Zero extend is
1357 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001358 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1359 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001360 break;
1361 case ISD::SETGE:
1362 case ISD::SETGT:
1363 case ISD::SETLT:
1364 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001365 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1366 DAG.getValueType(VT));
1367 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1368 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001369 break;
1370 }
Chris Lattner4d978642005-01-15 22:16:26 +00001371 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001372 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001373 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001374 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1375 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1376 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001377 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001378 case ISD::SETEQ:
1379 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001380 if (RHSLo == RHSHi)
1381 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1382 if (RHSCST->isAllOnesValue()) {
1383 // Comparison to -1.
1384 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001385 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001386 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001387 }
1388
Chris Lattnerdc750592005-01-07 07:47:09 +00001389 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1390 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1391 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001392 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001393 break;
1394 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001395 // If this is a comparison of the sign bit, just look at the top part.
1396 // X > -1, x < 0
1397 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001398 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001399 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001400 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001401 (CST->isAllOnesValue()))) { // X > -1
1402 Tmp1 = LHSHi;
1403 Tmp2 = RHSHi;
1404 break;
1405 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001406
Chris Lattnerdc750592005-01-07 07:47:09 +00001407 // FIXME: This generated code sucks.
1408 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001409 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001410 default: assert(0 && "Unknown integer setcc!");
1411 case ISD::SETLT:
1412 case ISD::SETULT: LowCC = ISD::SETULT; break;
1413 case ISD::SETGT:
1414 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1415 case ISD::SETLE:
1416 case ISD::SETULE: LowCC = ISD::SETULE; break;
1417 case ISD::SETGE:
1418 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1419 }
Misha Brukman835702a2005-04-21 22:36:52 +00001420
Chris Lattnerdc750592005-01-07 07:47:09 +00001421 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1422 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1423 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1424
1425 // NOTE: on targets without efficient SELECT of bools, we can always use
1426 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001427 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1428 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1429 Node->getOperand(2));
1430 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001431 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1432 Result, Tmp1, Tmp2));
1433 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001434 }
1435 }
Nate Begeman987121a2005-08-23 04:29:48 +00001436
1437 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1438 default:
1439 assert(0 && "Cannot handle this action for SETCC yet!");
1440 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001441 case TargetLowering::Promote: {
1442 // First step, figure out the appropriate operation to use.
1443 // Allow SETCC to not be supported for all legal data types
1444 // Mostly this targets FP
1445 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1446 MVT::ValueType OldVT = NewInTy;
1447
1448 // Scan for the appropriate larger type to use.
1449 while (1) {
1450 NewInTy = (MVT::ValueType)(NewInTy+1);
1451
1452 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1453 "Fell off of the edge of the integer world");
1454 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1455 "Fell off of the edge of the floating point world");
1456
1457 // If the target supports SETCC of this type, use it.
1458 if (TLI.getOperationAction(ISD::SETCC, NewInTy) == TargetLowering::Legal)
1459 break;
1460 }
1461 if (MVT::isInteger(NewInTy))
1462 assert(0 && "Cannot promote Legal Integer SETCC yet");
1463 else {
1464 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1465 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1466 }
1467
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001468 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1469 Node->getOperand(2));
1470 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001471 }
Nate Begeman987121a2005-08-23 04:29:48 +00001472 case TargetLowering::Legal:
1473 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1474 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1475 Node->getOperand(2));
1476 break;
1477 case TargetLowering::Expand:
1478 // Expand a setcc node into a select_cc of the same condition, lhs, and
1479 // rhs that selects between const 1 (true) and const 0 (false).
1480 MVT::ValueType VT = Node->getValueType(0);
1481 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1482 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1483 Node->getOperand(2));
1484 Result = LegalizeOp(Result);
1485 break;
1486 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001487 break;
1488
Chris Lattner85d70c62005-01-11 05:57:22 +00001489 case ISD::MEMSET:
1490 case ISD::MEMCPY:
1491 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001492 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001493 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1494
1495 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1496 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1497 case Expand: assert(0 && "Cannot expand a byte!");
1498 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001499 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001500 break;
1501 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001502 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001503 break;
1504 }
1505 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001506 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001507 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001508
1509 SDOperand Tmp4;
1510 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001511 case Expand: {
1512 // Length is too big, just take the lo-part of the length.
1513 SDOperand HiPart;
1514 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1515 break;
1516 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001517 case Legal:
1518 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001519 break;
1520 case Promote:
1521 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001522 break;
1523 }
1524
1525 SDOperand Tmp5;
1526 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1527 case Expand: assert(0 && "Cannot expand this yet!");
1528 case Legal:
1529 Tmp5 = LegalizeOp(Node->getOperand(4));
1530 break;
1531 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001532 Tmp5 = PromoteOp(Node->getOperand(4));
1533 break;
1534 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001535
1536 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1537 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001538 case TargetLowering::Custom: {
1539 SDOperand Tmp =
1540 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1541 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1542 if (Tmp.Val) {
1543 Result = LegalizeOp(Tmp);
1544 break;
1545 }
1546 // FALLTHROUGH if the target thinks it is legal.
1547 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001548 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001549 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1550 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1551 Tmp5 != Node->getOperand(4)) {
1552 std::vector<SDOperand> Ops;
1553 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1554 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1555 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1556 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001557 break;
1558 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001559 // Otherwise, the target does not support this operation. Lower the
1560 // operation to an explicit libcall as appropriate.
1561 MVT::ValueType IntPtr = TLI.getPointerTy();
1562 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1563 std::vector<std::pair<SDOperand, const Type*> > Args;
1564
Reid Spencer6dced922005-01-12 14:53:45 +00001565 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001566 if (Node->getOpcode() == ISD::MEMSET) {
1567 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1568 // Extend the ubyte argument to be an int value for the call.
1569 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1570 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1571 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1572
1573 FnName = "memset";
1574 } else if (Node->getOpcode() == ISD::MEMCPY ||
1575 Node->getOpcode() == ISD::MEMMOVE) {
1576 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1577 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1578 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1579 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1580 } else {
1581 assert(0 && "Unknown op!");
1582 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001583
Chris Lattner85d70c62005-01-11 05:57:22 +00001584 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001585 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001586 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001587 Result = CallResult.second;
1588 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001589 break;
1590 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001591 }
1592 break;
1593 }
Chris Lattner5385db52005-05-09 20:23:03 +00001594
1595 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001596 Tmp1 = LegalizeOp(Node->getOperand(0));
1597 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001598
Chris Lattner86535992005-05-14 07:45:46 +00001599 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1600 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1601 std::vector<SDOperand> Ops;
1602 Ops.push_back(Tmp1);
1603 Ops.push_back(Tmp2);
1604 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1605 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001606 Result = SDOperand(Node, 0);
1607 // Since these produce two values, make sure to remember that we legalized
1608 // both of them.
1609 AddLegalizedOperand(SDOperand(Node, 0), Result);
1610 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1611 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001612 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001613 Tmp1 = LegalizeOp(Node->getOperand(0));
1614 Tmp2 = LegalizeOp(Node->getOperand(1));
1615 Tmp3 = LegalizeOp(Node->getOperand(2));
1616 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1617 Tmp3 != Node->getOperand(2))
1618 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1619 break;
1620
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001621 case ISD::READIO:
1622 Tmp1 = LegalizeOp(Node->getOperand(0));
1623 Tmp2 = LegalizeOp(Node->getOperand(1));
1624
1625 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1626 case TargetLowering::Custom:
1627 default: assert(0 && "This action not implemented for this operation!");
1628 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001629 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1630 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1631 std::vector<SDOperand> Ops;
1632 Ops.push_back(Tmp1);
1633 Ops.push_back(Tmp2);
1634 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1635 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001636 Result = SDOperand(Node, 0);
1637 break;
1638 case TargetLowering::Expand:
1639 // Replace this with a load from memory.
1640 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1641 Node->getOperand(1), DAG.getSrcValue(NULL));
1642 Result = LegalizeOp(Result);
1643 break;
1644 }
1645
1646 // Since these produce two values, make sure to remember that we legalized
1647 // both of them.
1648 AddLegalizedOperand(SDOperand(Node, 0), Result);
1649 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1650 return Result.getValue(Op.ResNo);
1651
1652 case ISD::WRITEIO:
1653 Tmp1 = LegalizeOp(Node->getOperand(0));
1654 Tmp2 = LegalizeOp(Node->getOperand(1));
1655 Tmp3 = LegalizeOp(Node->getOperand(2));
1656
1657 switch (TLI.getOperationAction(Node->getOpcode(),
1658 Node->getOperand(1).getValueType())) {
1659 case TargetLowering::Custom:
1660 default: assert(0 && "This action not implemented for this operation!");
1661 case TargetLowering::Legal:
1662 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1663 Tmp3 != Node->getOperand(2))
1664 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1665 break;
1666 case TargetLowering::Expand:
1667 // Replace this with a store to memory.
1668 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1669 Node->getOperand(1), Node->getOperand(2),
1670 DAG.getSrcValue(NULL));
1671 Result = LegalizeOp(Result);
1672 break;
1673 }
1674 break;
1675
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001676 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001677 case ISD::SUB_PARTS:
1678 case ISD::SHL_PARTS:
1679 case ISD::SRA_PARTS:
1680 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001681 std::vector<SDOperand> Ops;
1682 bool Changed = false;
1683 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1684 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1685 Changed |= Ops.back() != Node->getOperand(i);
1686 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001687 if (Changed) {
1688 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1689 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1690 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001691
1692 // Since these produce multiple values, make sure to remember that we
1693 // legalized all of them.
1694 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1695 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1696 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001697 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001698
1699 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001700 case ISD::ADD:
1701 case ISD::SUB:
1702 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001703 case ISD::MULHS:
1704 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001705 case ISD::UDIV:
1706 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001707 case ISD::AND:
1708 case ISD::OR:
1709 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001710 case ISD::SHL:
1711 case ISD::SRL:
1712 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001713 case ISD::FADD:
1714 case ISD::FSUB:
1715 case ISD::FMUL:
1716 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001717 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001718 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1719 case Expand: assert(0 && "Not possible");
1720 case Legal:
1721 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1722 break;
1723 case Promote:
1724 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1725 break;
1726 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001727 if (Tmp1 != Node->getOperand(0) ||
1728 Tmp2 != Node->getOperand(1))
1729 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1730 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001731
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001732 case ISD::BUILD_PAIR: {
1733 MVT::ValueType PairTy = Node->getValueType(0);
1734 // TODO: handle the case where the Lo and Hi operands are not of legal type
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1737 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1738 case TargetLowering::Legal:
1739 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1740 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1741 break;
1742 case TargetLowering::Promote:
1743 case TargetLowering::Custom:
1744 assert(0 && "Cannot promote/custom this yet!");
1745 case TargetLowering::Expand:
1746 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1747 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1748 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1749 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1750 TLI.getShiftAmountTy()));
1751 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1752 break;
1753 }
1754 break;
1755 }
1756
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001757 case ISD::UREM:
1758 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001759 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001760 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1761 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1762 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1763 case TargetLowering::Legal:
1764 if (Tmp1 != Node->getOperand(0) ||
1765 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001766 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001767 Tmp2);
1768 break;
1769 case TargetLowering::Promote:
1770 case TargetLowering::Custom:
1771 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001772 case TargetLowering::Expand:
1773 if (MVT::isInteger(Node->getValueType(0))) {
1774 MVT::ValueType VT = Node->getValueType(0);
1775 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1776 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1777 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1778 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1779 } else {
1780 // Floating point mod -> fmod libcall.
1781 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1782 SDOperand Dummy;
1783 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001784 }
1785 break;
1786 }
1787 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001788
Andrew Lenharth5e177822005-05-03 17:19:30 +00001789 case ISD::CTPOP:
1790 case ISD::CTTZ:
1791 case ISD::CTLZ:
1792 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1793 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1794 case TargetLowering::Legal:
1795 if (Tmp1 != Node->getOperand(0))
1796 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1797 break;
1798 case TargetLowering::Promote: {
1799 MVT::ValueType OVT = Tmp1.getValueType();
1800 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001801
1802 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001803 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1804 // Perform the larger operation, then subtract if needed.
1805 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1806 switch(Node->getOpcode())
1807 {
1808 case ISD::CTPOP:
1809 Result = Tmp1;
1810 break;
1811 case ISD::CTTZ:
1812 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001813 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1814 DAG.getConstant(getSizeInBits(NVT), NVT),
1815 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001816 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001817 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1818 break;
1819 case ISD::CTLZ:
1820 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001821 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1822 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001823 getSizeInBits(OVT), NVT));
1824 break;
1825 }
1826 break;
1827 }
1828 case TargetLowering::Custom:
1829 assert(0 && "Cannot custom handle this yet!");
1830 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001831 switch(Node->getOpcode())
1832 {
1833 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001834 static const uint64_t mask[6] = {
1835 0x5555555555555555ULL, 0x3333333333333333ULL,
1836 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1837 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1838 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001839 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001840 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1841 unsigned len = getSizeInBits(VT);
1842 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001843 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001844 Tmp2 = DAG.getConstant(mask[i], VT);
1845 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001846 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001847 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1848 DAG.getNode(ISD::AND, VT,
1849 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1850 Tmp2));
1851 }
1852 Result = Tmp1;
1853 break;
1854 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001855 case ISD::CTLZ: {
1856 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001857 x = x | (x >> 1);
1858 x = x | (x >> 2);
1859 ...
1860 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001861 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001862 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001863
Chris Lattner56add052005-05-11 18:35:21 +00001864 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1865 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001866 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1867 unsigned len = getSizeInBits(VT);
1868 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1869 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001870 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001871 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1872 }
1873 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001874 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001875 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001876 }
1877 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001878 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001879 // unless the target has ctlz but not ctpop, in which case we use:
1880 // { return 32 - nlz(~x & (x-1)); }
1881 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001882 MVT::ValueType VT = Tmp1.getValueType();
1883 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001884 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001885 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1886 DAG.getNode(ISD::SUB, VT, Tmp1,
1887 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001888 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001889 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1890 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001891 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001892 DAG.getConstant(getSizeInBits(VT), VT),
1893 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1894 } else {
1895 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1896 }
Chris Lattner72473242005-05-11 05:27:09 +00001897 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001898 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001899 default:
1900 assert(0 && "Cannot expand this yet!");
1901 break;
1902 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001903 break;
1904 }
1905 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001906
Chris Lattner13fe99c2005-04-02 05:00:07 +00001907 // Unary operators
1908 case ISD::FABS:
1909 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001910 case ISD::FSQRT:
1911 case ISD::FSIN:
1912 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001913 Tmp1 = LegalizeOp(Node->getOperand(0));
1914 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1915 case TargetLowering::Legal:
1916 if (Tmp1 != Node->getOperand(0))
1917 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1918 break;
1919 case TargetLowering::Promote:
1920 case TargetLowering::Custom:
1921 assert(0 && "Cannot promote/custom handle this yet!");
1922 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001923 switch(Node->getOpcode()) {
1924 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001925 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1926 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001927 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00001928 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001929 break;
1930 }
1931 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001932 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1933 MVT::ValueType VT = Node->getValueType(0);
1934 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001935 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001936 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1937 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1938 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001939 break;
1940 }
1941 case ISD::FSQRT:
1942 case ISD::FSIN:
1943 case ISD::FCOS: {
1944 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001945 const char *FnName = 0;
1946 switch(Node->getOpcode()) {
1947 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1948 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1949 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1950 default: assert(0 && "Unreachable!");
1951 }
Nate Begeman77558da2005-08-04 21:43:28 +00001952 SDOperand Dummy;
1953 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001954 break;
1955 }
1956 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001957 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001958 }
1959 break;
1960 }
1961 break;
1962
1963 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001964 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001965 case ISD::UINT_TO_FP: {
1966 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001967 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1968 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001969 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001970 Node->getOperand(0).getValueType())) {
1971 default: assert(0 && "Unknown operation action!");
1972 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001973 Result = ExpandLegalINT_TO_FP(isSigned,
1974 LegalizeOp(Node->getOperand(0)),
1975 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001976 AddLegalizedOperand(Op, Result);
1977 return Result;
1978 case TargetLowering::Promote:
1979 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1980 Node->getValueType(0),
1981 isSigned);
1982 AddLegalizedOperand(Op, Result);
1983 return Result;
1984 case TargetLowering::Legal:
1985 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00001986 case TargetLowering::Custom: {
1987 Tmp1 = LegalizeOp(Node->getOperand(0));
1988 SDOperand Tmp =
1989 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1990 Tmp = TLI.LowerOperation(Tmp, DAG);
1991 if (Tmp.Val) {
1992 AddLegalizedOperand(Op, Tmp);
1993 NeedsAnotherIteration = true;
1994 return Tmp;
1995 } else {
1996 assert(0 && "Target Must Lower this");
1997 }
1998 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001999 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002000
Chris Lattnerdc750592005-01-07 07:47:09 +00002001 Tmp1 = LegalizeOp(Node->getOperand(0));
2002 if (Tmp1 != Node->getOperand(0))
2003 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2004 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002005 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002006 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2007 Node->getValueType(0), Node->getOperand(0));
2008 break;
2009 case Promote:
2010 if (isSigned) {
2011 Result = PromoteOp(Node->getOperand(0));
2012 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2013 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2014 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2015 } else {
2016 Result = PromoteOp(Node->getOperand(0));
2017 Result = DAG.getZeroExtendInReg(Result,
2018 Node->getOperand(0).getValueType());
2019 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002020 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002021 break;
2022 }
2023 break;
2024 }
2025 case ISD::TRUNCATE:
2026 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2027 case Legal:
2028 Tmp1 = LegalizeOp(Node->getOperand(0));
2029 if (Tmp1 != Node->getOperand(0))
2030 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2031 break;
2032 case Expand:
2033 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2034
2035 // Since the result is legal, we should just be able to truncate the low
2036 // part of the source.
2037 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2038 break;
2039 case Promote:
2040 Result = PromoteOp(Node->getOperand(0));
2041 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2042 break;
2043 }
2044 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002045
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002046 case ISD::FP_TO_SINT:
2047 case ISD::FP_TO_UINT:
2048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2049 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002050 Tmp1 = LegalizeOp(Node->getOperand(0));
2051
Chris Lattner44fe26f2005-07-29 00:11:56 +00002052 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2053 default: assert(0 && "Unknown operation action!");
2054 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002055 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2056 SDOperand True, False;
2057 MVT::ValueType VT = Node->getOperand(0).getValueType();
2058 MVT::ValueType NVT = Node->getValueType(0);
2059 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2060 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2061 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2062 Node->getOperand(0), Tmp2, ISD::SETLT);
2063 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2064 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002065 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002066 Tmp2));
2067 False = DAG.getNode(ISD::XOR, NVT, False,
2068 DAG.getConstant(1ULL << ShiftAmt, NVT));
2069 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002070 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002071 } else {
2072 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2073 }
2074 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002075 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002076 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002077 Node->getOpcode() == ISD::FP_TO_SINT);
2078 AddLegalizedOperand(Op, Result);
2079 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002080 case TargetLowering::Custom: {
2081 SDOperand Tmp =
2082 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2083 Tmp = TLI.LowerOperation(Tmp, DAG);
2084 if (Tmp.Val) {
2085 AddLegalizedOperand(Op, Tmp);
2086 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002087 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002088 } else {
2089 // The target thinks this is legal afterall.
2090 break;
2091 }
2092 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002093 case TargetLowering::Legal:
2094 break;
2095 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002096
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002097 if (Tmp1 != Node->getOperand(0))
2098 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2099 break;
2100 case Expand:
2101 assert(0 && "Shouldn't need to expand other operators here!");
2102 case Promote:
2103 Result = PromoteOp(Node->getOperand(0));
2104 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2105 break;
2106 }
2107 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002108
Chris Lattner7753f172005-09-02 00:18:10 +00002109 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002110 case ISD::ZERO_EXTEND:
2111 case ISD::SIGN_EXTEND:
2112 case ISD::FP_EXTEND:
2113 case ISD::FP_ROUND:
2114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2115 case Legal:
2116 Tmp1 = LegalizeOp(Node->getOperand(0));
2117 if (Tmp1 != Node->getOperand(0))
2118 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2119 break;
2120 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002121 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002122
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002123 case Promote:
2124 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002125 case ISD::ANY_EXTEND:
2126 Result = PromoteOp(Node->getOperand(0));
2127 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2128 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002129 case ISD::ZERO_EXTEND:
2130 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002131 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002132 Result = DAG.getZeroExtendInReg(Result,
2133 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002134 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002135 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002136 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002137 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002138 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002139 Result,
2140 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002141 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002142 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002143 Result = PromoteOp(Node->getOperand(0));
2144 if (Result.getValueType() != Op.getValueType())
2145 // Dynamically dead while we have only 2 FP types.
2146 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2147 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002148 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002149 Result = PromoteOp(Node->getOperand(0));
2150 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2151 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002152 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002153 }
2154 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002155 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002156 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002157 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002158 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002159
2160 // If this operation is not supported, convert it to a shl/shr or load/store
2161 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002162 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2163 default: assert(0 && "This action not supported for this op yet!");
2164 case TargetLowering::Legal:
2165 if (Tmp1 != Node->getOperand(0))
2166 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002167 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002168 break;
2169 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002170 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002171 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002172 // NOTE: we could fall back on load/store here too for targets without
2173 // SAR. However, it is doubtful that any exist.
2174 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2175 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002176 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002177 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2178 Node->getOperand(0), ShiftCst);
2179 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2180 Result, ShiftCst);
2181 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2182 // The only way we can lower this is to turn it into a STORETRUNC,
2183 // EXTLOAD pair, targetting a temporary location (a stack slot).
2184
2185 // NOTE: there is a choice here between constantly creating new stack
2186 // slots and always reusing the same one. We currently always create
2187 // new ones, as reuse may inhibit scheduling.
2188 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2189 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2190 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2191 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002192 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002193 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2194 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2195 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002196 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002197 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002198 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2199 Result, StackSlot, DAG.getSrcValue(NULL),
2200 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002201 } else {
2202 assert(0 && "Unknown op");
2203 }
2204 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002205 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002206 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002207 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002208 }
Chris Lattner99222f72005-01-15 07:15:18 +00002209 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002210
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002211 // Note that LegalizeOp may be reentered even from single-use nodes, which
2212 // means that we always must cache transformed nodes.
2213 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002214 return Result;
2215}
2216
Chris Lattner4d978642005-01-15 22:16:26 +00002217/// PromoteOp - Given an operation that produces a value in an invalid type,
2218/// promote it to compute the value into a larger type. The produced value will
2219/// have the correct bits for the low portion of the register, but no guarantee
2220/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002221SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2222 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002223 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002224 assert(getTypeAction(VT) == Promote &&
2225 "Caller should expand or legalize operands that are not promotable!");
2226 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2227 "Cannot promote to smaller type!");
2228
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002229 SDOperand Tmp1, Tmp2, Tmp3;
2230
2231 SDOperand Result;
2232 SDNode *Node = Op.Val;
2233
Chris Lattner1a570f12005-09-02 20:32:45 +00002234 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2235 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002236
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002237 // Promotion needs an optimization step to clean up after it, and is not
2238 // careful to avoid operations the target does not support. Make sure that
2239 // all generated operations are legalized in the next iteration.
2240 NeedsAnotherIteration = true;
2241
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002242 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002243 case ISD::CopyFromReg:
2244 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002245 default:
2246 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2247 assert(0 && "Do not know how to promote this operator!");
2248 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002249 case ISD::UNDEF:
2250 Result = DAG.getNode(ISD::UNDEF, NVT);
2251 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002252 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002253 if (VT != MVT::i1)
2254 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2255 else
2256 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002257 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2258 break;
2259 case ISD::ConstantFP:
2260 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2261 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2262 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002263
Chris Lattner2cb338d2005-01-18 02:59:52 +00002264 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002265 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002266 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2267 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002268 Result = LegalizeOp(Result);
2269 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002270
2271 case ISD::TRUNCATE:
2272 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2273 case Legal:
2274 Result = LegalizeOp(Node->getOperand(0));
2275 assert(Result.getValueType() >= NVT &&
2276 "This truncation doesn't make sense!");
2277 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2278 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2279 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002280 case Promote:
2281 // The truncation is not required, because we don't guarantee anything
2282 // about high bits anyway.
2283 Result = PromoteOp(Node->getOperand(0));
2284 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002285 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002286 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2287 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002288 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002289 }
2290 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002291 case ISD::SIGN_EXTEND:
2292 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002293 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002294 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2295 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2296 case Legal:
2297 // Input is legal? Just do extend all the way to the larger type.
2298 Result = LegalizeOp(Node->getOperand(0));
2299 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2300 break;
2301 case Promote:
2302 // Promote the reg if it's smaller.
2303 Result = PromoteOp(Node->getOperand(0));
2304 // The high bits are not guaranteed to be anything. Insert an extend.
2305 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002306 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002307 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002308 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002309 Result = DAG.getZeroExtendInReg(Result,
2310 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002311 break;
2312 }
2313 break;
2314
2315 case ISD::FP_EXTEND:
2316 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2317 case ISD::FP_ROUND:
2318 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2319 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2320 case Promote: assert(0 && "Unreachable with 2 FP types!");
2321 case Legal:
2322 // Input is legal? Do an FP_ROUND_INREG.
2323 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002324 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2325 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002326 break;
2327 }
2328 break;
2329
2330 case ISD::SINT_TO_FP:
2331 case ISD::UINT_TO_FP:
2332 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2333 case Legal:
2334 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002335 // No extra round required here.
2336 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002337 break;
2338
2339 case Promote:
2340 Result = PromoteOp(Node->getOperand(0));
2341 if (Node->getOpcode() == ISD::SINT_TO_FP)
2342 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002343 Result,
2344 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002345 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002346 Result = DAG.getZeroExtendInReg(Result,
2347 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002348 // No extra round required here.
2349 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002350 break;
2351 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002352 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2353 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002354 // Round if we cannot tolerate excess precision.
2355 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002356 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2357 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002358 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002359 }
Chris Lattner4d978642005-01-15 22:16:26 +00002360 break;
2361
2362 case ISD::FP_TO_SINT:
2363 case ISD::FP_TO_UINT:
2364 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2365 case Legal:
2366 Tmp1 = LegalizeOp(Node->getOperand(0));
2367 break;
2368 case Promote:
2369 // The input result is prerounded, so we don't have to do anything
2370 // special.
2371 Tmp1 = PromoteOp(Node->getOperand(0));
2372 break;
2373 case Expand:
2374 assert(0 && "not implemented");
2375 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002376 // If we're promoting a UINT to a larger size, check to see if the new node
2377 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2378 // we can use that instead. This allows us to generate better code for
2379 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2380 // legal, such as PowerPC.
2381 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002382 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002383 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2384 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002385 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2386 } else {
2387 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2388 }
Chris Lattner4d978642005-01-15 22:16:26 +00002389 break;
2390
Chris Lattner13fe99c2005-04-02 05:00:07 +00002391 case ISD::FABS:
2392 case ISD::FNEG:
2393 Tmp1 = PromoteOp(Node->getOperand(0));
2394 assert(Tmp1.getValueType() == NVT);
2395 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2396 // NOTE: we do not have to do any extra rounding here for
2397 // NoExcessFPPrecision, because we know the input will have the appropriate
2398 // precision, and these operations don't modify precision at all.
2399 break;
2400
Chris Lattner9d6fa982005-04-28 21:44:33 +00002401 case ISD::FSQRT:
2402 case ISD::FSIN:
2403 case ISD::FCOS:
2404 Tmp1 = PromoteOp(Node->getOperand(0));
2405 assert(Tmp1.getValueType() == NVT);
2406 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2407 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002408 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2409 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002410 break;
2411
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002412 case ISD::AND:
2413 case ISD::OR:
2414 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002415 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002416 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002417 case ISD::MUL:
2418 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002419 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002420 // that too is okay if they are integer operations.
2421 Tmp1 = PromoteOp(Node->getOperand(0));
2422 Tmp2 = PromoteOp(Node->getOperand(1));
2423 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2424 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002425 break;
2426 case ISD::FADD:
2427 case ISD::FSUB:
2428 case ISD::FMUL:
2429 // The input may have strange things in the top bits of the registers, but
2430 // these operations don't care.
2431 Tmp1 = PromoteOp(Node->getOperand(0));
2432 Tmp2 = PromoteOp(Node->getOperand(1));
2433 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2434 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2435
2436 // Floating point operations will give excess precision that we may not be
2437 // able to tolerate. If we DO allow excess precision, just leave it,
2438 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002439 // FIXME: Why would we need to round FP ops more than integer ones?
2440 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002441 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002442 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2443 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002444 break;
2445
Chris Lattner4d978642005-01-15 22:16:26 +00002446 case ISD::SDIV:
2447 case ISD::SREM:
2448 // These operators require that their input be sign extended.
2449 Tmp1 = PromoteOp(Node->getOperand(0));
2450 Tmp2 = PromoteOp(Node->getOperand(1));
2451 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002452 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2453 DAG.getValueType(VT));
2454 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2455 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002456 }
2457 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2458
2459 // Perform FP_ROUND: this is probably overly pessimistic.
2460 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002461 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2462 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002463 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002464 case ISD::FDIV:
2465 case ISD::FREM:
2466 // These operators require that their input be fp extended.
2467 Tmp1 = PromoteOp(Node->getOperand(0));
2468 Tmp2 = PromoteOp(Node->getOperand(1));
2469 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2470
2471 // Perform FP_ROUND: this is probably overly pessimistic.
2472 if (NoExcessFPPrecision)
2473 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2474 DAG.getValueType(VT));
2475 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002476
2477 case ISD::UDIV:
2478 case ISD::UREM:
2479 // These operators require that their input be zero extended.
2480 Tmp1 = PromoteOp(Node->getOperand(0));
2481 Tmp2 = PromoteOp(Node->getOperand(1));
2482 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002483 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2484 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002485 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2486 break;
2487
2488 case ISD::SHL:
2489 Tmp1 = PromoteOp(Node->getOperand(0));
2490 Tmp2 = LegalizeOp(Node->getOperand(1));
2491 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2492 break;
2493 case ISD::SRA:
2494 // The input value must be properly sign extended.
2495 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002496 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2497 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002498 Tmp2 = LegalizeOp(Node->getOperand(1));
2499 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2500 break;
2501 case ISD::SRL:
2502 // The input value must be properly zero extended.
2503 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002504 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002505 Tmp2 = LegalizeOp(Node->getOperand(1));
2506 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2507 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002508 case ISD::LOAD:
2509 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2510 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002511 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2512 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002513 // Remember that we legalized the chain.
2514 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2515 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002516 case ISD::SEXTLOAD:
2517 case ISD::ZEXTLOAD:
2518 case ISD::EXTLOAD:
2519 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2520 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002521 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2522 Node->getOperand(2),
2523 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002524 // Remember that we legalized the chain.
2525 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2526 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002527 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002528 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2529 case Expand: assert(0 && "It's impossible to expand bools");
2530 case Legal:
2531 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2532 break;
2533 case Promote:
2534 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2535 break;
2536 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002537 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2538 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2539 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2540 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002541 case ISD::SELECT_CC:
2542 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2543 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2544 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2545 Node->getOperand(1), Tmp2, Tmp3,
2546 Node->getOperand(4));
2547 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002548 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002549 case ISD::CALL: {
2550 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2551 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2552
Chris Lattner3d95c142005-01-19 20:24:35 +00002553 std::vector<SDOperand> Ops;
2554 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2555 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2556
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002557 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2558 "Can only promote single result calls");
2559 std::vector<MVT::ValueType> RetTyVTs;
2560 RetTyVTs.reserve(2);
2561 RetTyVTs.push_back(NVT);
2562 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002563 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2564 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002565 Result = SDOperand(NC, 0);
2566
2567 // Insert the new chain mapping.
2568 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2569 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002570 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002571 case ISD::CTPOP:
2572 case ISD::CTTZ:
2573 case ISD::CTLZ:
2574 Tmp1 = Node->getOperand(0);
2575 //Zero extend the argument
2576 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2577 // Perform the larger operation, then subtract if needed.
2578 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2579 switch(Node->getOpcode())
2580 {
2581 case ISD::CTPOP:
2582 Result = Tmp1;
2583 break;
2584 case ISD::CTTZ:
2585 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002586 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002587 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002588 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002589 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2590 break;
2591 case ISD::CTLZ:
2592 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002593 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2594 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002595 getSizeInBits(VT), NVT));
2596 break;
2597 }
2598 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002599 }
2600
2601 assert(Result.Val && "Didn't set a result!");
2602 AddPromotedOperand(Op, Result);
2603 return Result;
2604}
Chris Lattnerdc750592005-01-07 07:47:09 +00002605
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002606/// ExpandAddSub - Find a clever way to expand this add operation into
2607/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002608void SelectionDAGLegalize::
2609ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2610 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002611 // Expand the subcomponents.
2612 SDOperand LHSL, LHSH, RHSL, RHSH;
2613 ExpandOp(LHS, LHSL, LHSH);
2614 ExpandOp(RHS, RHSL, RHSH);
2615
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002616 std::vector<SDOperand> Ops;
2617 Ops.push_back(LHSL);
2618 Ops.push_back(LHSH);
2619 Ops.push_back(RHSL);
2620 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002621 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2622 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002623 Hi = Lo.getValue(1);
2624}
2625
Chris Lattner4157c412005-04-02 04:00:59 +00002626void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2627 SDOperand Op, SDOperand Amt,
2628 SDOperand &Lo, SDOperand &Hi) {
2629 // Expand the subcomponents.
2630 SDOperand LHSL, LHSH;
2631 ExpandOp(Op, LHSL, LHSH);
2632
2633 std::vector<SDOperand> Ops;
2634 Ops.push_back(LHSL);
2635 Ops.push_back(LHSH);
2636 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002637 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002638 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002639 Hi = Lo.getValue(1);
2640}
2641
2642
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002643/// ExpandShift - Try to find a clever way to expand this shift operation out to
2644/// smaller elements. If we can't find a way that is more efficient than a
2645/// libcall on this target, return false. Otherwise, return true with the
2646/// low-parts expanded into Lo and Hi.
2647bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2648 SDOperand &Lo, SDOperand &Hi) {
2649 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2650 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002651
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002652 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002653 SDOperand ShAmt = LegalizeOp(Amt);
2654 MVT::ValueType ShTy = ShAmt.getValueType();
2655 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2656 unsigned NVTBits = MVT::getSizeInBits(NVT);
2657
2658 // Handle the case when Amt is an immediate. Other cases are currently broken
2659 // and are disabled.
2660 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2661 unsigned Cst = CN->getValue();
2662 // Expand the incoming operand to be shifted, so that we have its parts
2663 SDOperand InL, InH;
2664 ExpandOp(Op, InL, InH);
2665 switch(Opc) {
2666 case ISD::SHL:
2667 if (Cst > VTBits) {
2668 Lo = DAG.getConstant(0, NVT);
2669 Hi = DAG.getConstant(0, NVT);
2670 } else if (Cst > NVTBits) {
2671 Lo = DAG.getConstant(0, NVT);
2672 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002673 } else if (Cst == NVTBits) {
2674 Lo = DAG.getConstant(0, NVT);
2675 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002676 } else {
2677 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2678 Hi = DAG.getNode(ISD::OR, NVT,
2679 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2680 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2681 }
2682 return true;
2683 case ISD::SRL:
2684 if (Cst > VTBits) {
2685 Lo = DAG.getConstant(0, NVT);
2686 Hi = DAG.getConstant(0, NVT);
2687 } else if (Cst > NVTBits) {
2688 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2689 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002690 } else if (Cst == NVTBits) {
2691 Lo = InH;
2692 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002693 } else {
2694 Lo = DAG.getNode(ISD::OR, NVT,
2695 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2696 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2697 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2698 }
2699 return true;
2700 case ISD::SRA:
2701 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002702 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002703 DAG.getConstant(NVTBits-1, ShTy));
2704 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002705 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002706 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002707 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002708 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002709 } else if (Cst == NVTBits) {
2710 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002711 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002712 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002713 } else {
2714 Lo = DAG.getNode(ISD::OR, NVT,
2715 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2716 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2717 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2718 }
2719 return true;
2720 }
2721 }
2722 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2723 // so disable it for now. Currently targets are handling this via SHL_PARTS
2724 // and friends.
2725 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002726
2727 // If we have an efficient select operation (or if the selects will all fold
2728 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002729 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002730 return false;
2731
2732 SDOperand InL, InH;
2733 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002734 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2735 DAG.getConstant(NVTBits, ShTy), ShAmt);
2736
Chris Lattner4d25c042005-01-20 20:29:23 +00002737 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002738 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2739 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002740
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002741 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2742 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2743 DAG.getConstant(NVTBits-1, ShTy));
2744 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2745 DAG.getConstant(NVTBits-1, ShTy));
2746 }
2747
2748 if (Opc == ISD::SHL) {
2749 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2750 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2751 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002752 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002753
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002754 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2755 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2756 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002757 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002758 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2759 DAG.getConstant(32, ShTy),
2760 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002761 DAG.getConstant(0, NVT),
2762 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002763 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002764 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002765 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002766 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002767
2768 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002769 if (Opc == ISD::SRA)
2770 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2771 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002772 else
2773 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002774 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002775 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002776 }
2777 return true;
2778}
Chris Lattneraac464e2005-01-21 06:05:23 +00002779
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002780/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2781/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002782/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002783static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002784 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002785
Chris Lattner2dce7032005-05-12 23:24:06 +00002786 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002787 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002788 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002789 Found = Node;
2790 return;
2791 }
2792
2793 // Otherwise, scan the operands of Node to see if any of them is a call.
2794 assert(Node->getNumOperands() != 0 &&
2795 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002796 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002797 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002798
2799 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002800 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002801 Found);
2802}
2803
2804
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002805/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2806/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002807/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002808static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2809 std::set<SDNode*> &Visited) {
2810 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2811 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002812
Chris Lattner2dce7032005-05-12 23:24:06 +00002813 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002814 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002815 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002816 Found = Node;
2817 return;
2818 }
2819
2820 // Otherwise, scan the operands of Node to see if any of them is a call.
2821 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2822 if (UI == E) return;
2823 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002824 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002825
2826 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002827 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002828}
2829
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002830/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002831/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002832static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002833 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002834 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002835 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002836 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002837
Chris Lattner4add7e32005-01-23 04:42:50 +00002838 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002839 if (TheChain.getValueType() != MVT::Other)
2840 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002841 if (TheChain.getValueType() != MVT::Other)
2842 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002843
2844 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002845 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002846
Chris Lattner4add7e32005-01-23 04:42:50 +00002847 // Make sure to only follow users of our token chain.
2848 SDNode *User = *UI;
2849 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2850 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002851 if (SDNode *Result = FindCallSeqEnd(User))
2852 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002853 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002854 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002855}
2856
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002857/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002858/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002859static SDNode *FindCallSeqStart(SDNode *Node) {
2860 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002861 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002862
2863 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2864 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002865 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002866}
2867
2868
Chris Lattner4add7e32005-01-23 04:42:50 +00002869/// FindInputOutputChains - If we are replacing an operation with a call we need
2870/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002871/// properly serialize the calls in the block. The returned operand is the
2872/// input chain value for the new call (e.g. the entry node or the previous
2873/// call), and OutChain is set to be the chain node to update to point to the
2874/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002875static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2876 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002877 SDNode *LatestCallSeqStart = Entry.Val;
2878 SDNode *LatestCallSeqEnd = 0;
2879 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2880 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002881
Chris Lattner2dce7032005-05-12 23:24:06 +00002882 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002883 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002884 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2885 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002886 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002887 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002888 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2889 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002890 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002891 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002892 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002893
Chris Lattner06bbeb62005-05-11 19:02:11 +00002894 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002895 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002896 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002897 std::set<SDNode*> Visited;
2898 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002899
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002900 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002901 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002902 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002903
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002904 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002905}
2906
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002907/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002908void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2909 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002910 // Nothing to splice it into?
2911 if (OutChain == 0) return;
2912
2913 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2914 //OutChain->dump();
2915
2916 // Form a token factor node merging the old inval and the new inval.
2917 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2918 OutChain->getOperand(0));
2919 // Change the node to refer to the new token.
2920 OutChain->setAdjCallChain(InToken);
2921}
Chris Lattner4add7e32005-01-23 04:42:50 +00002922
2923
Chris Lattneraac464e2005-01-21 06:05:23 +00002924// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2925// does not fit into a register, return the lo part and set the hi part to the
2926// by-reg argument. If it does fit into a single register, return the result
2927// and leave the Hi part unset.
2928SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2929 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002930 SDNode *OutChain;
2931 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2932 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002933 if (InChain.Val == 0)
2934 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002935
Chris Lattneraac464e2005-01-21 06:05:23 +00002936 TargetLowering::ArgListTy Args;
2937 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2938 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2939 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2940 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2941 }
2942 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002943
Chris Lattner06bbeb62005-05-11 19:02:11 +00002944 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002945 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002946 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002947 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2948 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002949
Chris Lattner63022662005-09-02 20:26:58 +00002950 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002951 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002952 default: assert(0 && "Unknown thing");
2953 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00002954 Result = CallInfo.first;
2955 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002956 case Promote:
2957 assert(0 && "Cannot promote this yet!");
2958 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00002959 ExpandOp(CallInfo.first, Result, Hi);
2960 CallInfo.second = LegalizeOp(CallInfo.second);
2961 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002962 }
Chris Lattner63022662005-09-02 20:26:58 +00002963
2964 SpliceCallInto(CallInfo.second, OutChain);
2965 NeedsAnotherIteration = true;
2966 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00002967}
2968
Chris Lattner4add7e32005-01-23 04:42:50 +00002969
Chris Lattneraac464e2005-01-21 06:05:23 +00002970/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2971/// destination type is legal.
2972SDOperand SelectionDAGLegalize::
2973ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002974 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00002975 assert(getTypeAction(Source.getValueType()) == Expand &&
2976 "This is not an expansion!");
2977 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2978
Chris Lattner06bbeb62005-05-11 19:02:11 +00002979 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002980 assert(Source.getValueType() == MVT::i64 &&
2981 "This only works for 64-bit -> FP");
2982 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2983 // incoming integer is set. To handle this, we dynamically test to see if
2984 // it is set, and, if so, add a fudge factor.
2985 SDOperand Lo, Hi;
2986 ExpandOp(Source, Lo, Hi);
2987
Chris Lattner2a4f7312005-05-13 04:45:13 +00002988 // If this is unsigned, and not supported, first perform the conversion to
2989 // signed, then adjust the result if the sign bit is set.
2990 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2991 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2992
Chris Lattnerd47675e2005-08-09 20:20:18 +00002993 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2994 DAG.getConstant(0, Hi.getValueType()),
2995 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002996 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2997 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2998 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002999 uint64_t FF = 0x5f800000ULL;
3000 if (TLI.isLittleEndian()) FF <<= 32;
3001 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003002
Chris Lattnerc30405e2005-08-26 17:15:30 +00003003 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003004 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3005 SDOperand FudgeInReg;
3006 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003007 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3008 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003009 else {
3010 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003011 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3012 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003013 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003014 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003015 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003016
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003017 // Check to see if the target has a custom way to lower this. If so, use it.
3018 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3019 default: assert(0 && "This action not implemented for this operation!");
3020 case TargetLowering::Legal:
3021 case TargetLowering::Expand:
3022 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003023 case TargetLowering::Custom: {
3024 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3025 Source), DAG);
3026 if (NV.Val)
3027 return LegalizeOp(NV);
3028 break; // The target decided this was legal after all
3029 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003030 }
3031
Chris Lattner153587e2005-05-12 07:00:44 +00003032 // Expand the source, then glue it back together for the call. We must expand
3033 // the source in case it is shared (this pass of legalize must traverse it).
3034 SDOperand SrcLo, SrcHi;
3035 ExpandOp(Source, SrcLo, SrcHi);
3036 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3037
Chris Lattner06bbeb62005-05-11 19:02:11 +00003038 SDNode *OutChain = 0;
3039 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3040 DAG.getEntryNode());
3041 const char *FnName = 0;
3042 if (DestTy == MVT::f32)
3043 FnName = "__floatdisf";
3044 else {
3045 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3046 FnName = "__floatdidf";
3047 }
3048
Chris Lattneraac464e2005-01-21 06:05:23 +00003049 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3050
3051 TargetLowering::ArgListTy Args;
3052 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003053
Chris Lattneraac464e2005-01-21 06:05:23 +00003054 Args.push_back(std::make_pair(Source, ArgTy));
3055
3056 // We don't care about token chains for libcalls. We just use the entry
3057 // node as our input and ignore the output chain. This allows us to place
3058 // calls wherever we need them to satisfy data dependences.
3059 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003060
3061 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003062 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3063 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003064
Chris Lattnera5bf1032005-05-12 04:49:08 +00003065 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003066 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003067}
Misha Brukman835702a2005-04-21 22:36:52 +00003068
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003069
3070
Chris Lattnerdc750592005-01-07 07:47:09 +00003071/// ExpandOp - Expand the specified SDOperand into its two component pieces
3072/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3073/// LegalizeNodes map is filled in for any results that are not expanded, the
3074/// ExpandedNodes map is filled in for any results that are expanded, and the
3075/// Lo/Hi values are returned.
3076void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3077 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003078 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003079 SDNode *Node = Op.Val;
3080 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003081 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3082 "Cannot expand FP values!");
3083 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003084 "Cannot expand to FP value or to larger int value!");
3085
Chris Lattner1a570f12005-09-02 20:32:45 +00003086 // See if we already expanded it.
3087 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3088 = ExpandedNodes.find(Op);
3089 if (I != ExpandedNodes.end()) {
3090 Lo = I->second.first;
3091 Hi = I->second.second;
3092 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003093 }
3094
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003095 // Expanding to multiple registers needs to perform an optimization step, and
3096 // is not careful to avoid operations the target does not support. Make sure
3097 // that all generated operations are legalized in the next iteration.
3098 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003099
Chris Lattnerdc750592005-01-07 07:47:09 +00003100 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003101 case ISD::CopyFromReg:
3102 assert(0 && "CopyFromReg must be legal!");
3103 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003104 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3105 assert(0 && "Do not know how to expand this operator!");
3106 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003107 case ISD::UNDEF:
3108 Lo = DAG.getNode(ISD::UNDEF, NVT);
3109 Hi = DAG.getNode(ISD::UNDEF, NVT);
3110 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003111 case ISD::Constant: {
3112 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3113 Lo = DAG.getConstant(Cst, NVT);
3114 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3115 break;
3116 }
3117
Chris Lattner32e08b72005-03-28 22:03:13 +00003118 case ISD::BUILD_PAIR:
3119 // Legalize both operands. FIXME: in the future we should handle the case
3120 // where the two elements are not legal.
3121 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3122 Lo = LegalizeOp(Node->getOperand(0));
3123 Hi = LegalizeOp(Node->getOperand(1));
3124 break;
3125
Chris Lattner55e9cde2005-05-11 04:51:16 +00003126 case ISD::CTPOP:
3127 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003128 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3129 DAG.getNode(ISD::CTPOP, NVT, Lo),
3130 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003131 Hi = DAG.getConstant(0, NVT);
3132 break;
3133
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003134 case ISD::CTLZ: {
3135 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003136 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003137 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3138 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003139 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3140 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003141 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3142 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3143
3144 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3145 Hi = DAG.getConstant(0, NVT);
3146 break;
3147 }
3148
3149 case ISD::CTTZ: {
3150 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003151 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003152 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3153 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003154 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3155 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003156 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3157 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3158
3159 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3160 Hi = DAG.getConstant(0, NVT);
3161 break;
3162 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003163
Chris Lattnerdc750592005-01-07 07:47:09 +00003164 case ISD::LOAD: {
3165 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3166 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003167 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003168
3169 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003170 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003171 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3172 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003173 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003174 //are from the same instruction?
3175 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003176
3177 // Build a factor node to remember that this load is independent of the
3178 // other one.
3179 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3180 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003181
Chris Lattnerdc750592005-01-07 07:47:09 +00003182 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003183 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003184 if (!TLI.isLittleEndian())
3185 std::swap(Lo, Hi);
3186 break;
3187 }
Nate Begemand37c1312005-11-22 18:16:00 +00003188 case ISD::VLOAD: {
3189 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3190 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3191 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3192 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3193
3194 // If we only have two elements, turn into a pair of scalar loads.
3195 // FIXME: handle case where a vector of two elements is fine, such as
3196 // 2 x double on SSE2.
3197 if (NumElements == 2) {
3198 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3199 // Increment the pointer to the other half.
3200 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3201 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3202 getIntPtrConstant(IncrementSize));
3203 //Is this safe? declaring that the two parts of the split load
3204 //are from the same instruction?
3205 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3206 } else {
3207 NumElements /= 2; // Split the vector in half
3208 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3209 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3210 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3211 getIntPtrConstant(IncrementSize));
3212 //Is this safe? declaring that the two parts of the split load
3213 //are from the same instruction?
3214 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3215 }
3216
3217 // Build a factor node to remember that this load is independent of the
3218 // other one.
3219 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3220 Hi.getValue(1));
3221
3222 // Remember that we legalized the chain.
3223 AddLegalizedOperand(Op.getValue(1), TF);
3224 if (!TLI.isLittleEndian())
3225 std::swap(Lo, Hi);
3226 break;
3227 }
3228 case ISD::VADD:
3229 case ISD::VSUB:
3230 case ISD::VMUL: {
3231 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3232 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3233 SDOperand LL, LH, RL, RH;
3234
3235 ExpandOp(Node->getOperand(0), LL, LH);
3236 ExpandOp(Node->getOperand(1), RL, RH);
3237
3238 // If we only have two elements, turn into a pair of scalar loads.
3239 // FIXME: handle case where a vector of two elements is fine, such as
3240 // 2 x double on SSE2.
3241 if (NumElements == 2) {
3242 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3243 Lo = DAG.getNode(Opc, EVT, LL, RL);
3244 Hi = DAG.getNode(Opc, EVT, LH, RH);
3245 } else {
3246 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3247 LL.getOperand(3));
3248 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3249 LH.getOperand(3));
3250 }
3251 break;
3252 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003253 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003254 case ISD::CALL: {
3255 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3256 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3257
Chris Lattner3d95c142005-01-19 20:24:35 +00003258 bool Changed = false;
3259 std::vector<SDOperand> Ops;
3260 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3261 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3262 Changed |= Ops.back() != Node->getOperand(i);
3263 }
3264
Chris Lattnerdc750592005-01-07 07:47:09 +00003265 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3266 "Can only expand a call once so far, not i64 -> i16!");
3267
3268 std::vector<MVT::ValueType> RetTyVTs;
3269 RetTyVTs.reserve(3);
3270 RetTyVTs.push_back(NVT);
3271 RetTyVTs.push_back(NVT);
3272 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003273 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3274 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003275 Lo = SDOperand(NC, 0);
3276 Hi = SDOperand(NC, 1);
3277
3278 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003279 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003280 break;
3281 }
3282 case ISD::AND:
3283 case ISD::OR:
3284 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3285 SDOperand LL, LH, RL, RH;
3286 ExpandOp(Node->getOperand(0), LL, LH);
3287 ExpandOp(Node->getOperand(1), RL, RH);
3288 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3289 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3290 break;
3291 }
3292 case ISD::SELECT: {
3293 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003294
3295 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3296 case Expand: assert(0 && "It's impossible to expand bools");
3297 case Legal:
3298 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3299 break;
3300 case Promote:
3301 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3302 break;
3303 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003304 ExpandOp(Node->getOperand(1), LL, LH);
3305 ExpandOp(Node->getOperand(2), RL, RH);
3306 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3307 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3308 break;
3309 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003310 case ISD::SELECT_CC: {
3311 SDOperand TL, TH, FL, FH;
3312 ExpandOp(Node->getOperand(2), TL, TH);
3313 ExpandOp(Node->getOperand(3), FL, FH);
3314 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3315 Node->getOperand(1), TL, FL, Node->getOperand(4));
3316 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3317 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003318 Lo = LegalizeOp(Lo);
3319 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003320 break;
3321 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003322 case ISD::SEXTLOAD: {
3323 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3324 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3325 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3326
3327 if (EVT == NVT)
3328 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3329 else
3330 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3331 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003332
3333 // Remember that we legalized the chain.
3334 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3335
Nate Begemanc3a89c52005-10-13 17:15:37 +00003336 // The high part is obtained by SRA'ing all but one of the bits of the lo
3337 // part.
3338 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3339 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3340 TLI.getShiftAmountTy()));
3341 Lo = LegalizeOp(Lo);
3342 Hi = LegalizeOp(Hi);
3343 break;
3344 }
3345 case ISD::ZEXTLOAD: {
3346 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3347 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3348 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3349
3350 if (EVT == NVT)
3351 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3352 else
3353 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3354 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003355
3356 // Remember that we legalized the chain.
3357 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3358
Nate Begemanc3a89c52005-10-13 17:15:37 +00003359 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003360 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003361 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003362 break;
3363 }
3364 case ISD::EXTLOAD: {
3365 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3366 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3367 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3368
3369 if (EVT == NVT)
3370 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3371 else
3372 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3373 EVT);
3374
3375 // Remember that we legalized the chain.
3376 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3377
3378 // The high part is undefined.
3379 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3380 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003381 break;
3382 }
Chris Lattner7753f172005-09-02 00:18:10 +00003383 case ISD::ANY_EXTEND: {
3384 SDOperand In;
3385 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3386 case Expand: assert(0 && "expand-expand not implemented yet!");
3387 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3388 case Promote:
3389 In = PromoteOp(Node->getOperand(0));
3390 break;
3391 }
3392
3393 // The low part is any extension of the input (which degenerates to a copy).
3394 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3395 // The high part is undefined.
3396 Hi = DAG.getNode(ISD::UNDEF, NVT);
3397 break;
3398 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003399 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003400 SDOperand In;
3401 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3402 case Expand: assert(0 && "expand-expand not implemented yet!");
3403 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3404 case Promote:
3405 In = PromoteOp(Node->getOperand(0));
3406 // Emit the appropriate sign_extend_inreg to get the value we want.
3407 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003408 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003409 break;
3410 }
3411
Chris Lattnerdc750592005-01-07 07:47:09 +00003412 // The low part is just a sign extension of the input (which degenerates to
3413 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003414 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003415
Chris Lattnerdc750592005-01-07 07:47:09 +00003416 // The high part is obtained by SRA'ing all but one of the bits of the lo
3417 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003418 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003419 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3420 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003421 break;
3422 }
Chris Lattner47844892005-04-03 23:41:52 +00003423 case ISD::ZERO_EXTEND: {
3424 SDOperand In;
3425 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3426 case Expand: assert(0 && "expand-expand not implemented yet!");
3427 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3428 case Promote:
3429 In = PromoteOp(Node->getOperand(0));
3430 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003431 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003432 break;
3433 }
3434
Chris Lattnerdc750592005-01-07 07:47:09 +00003435 // The low part is just a zero extension of the input (which degenerates to
3436 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003437 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003438
Chris Lattnerdc750592005-01-07 07:47:09 +00003439 // The high part is just a zero.
3440 Hi = DAG.getConstant(0, NVT);
3441 break;
Chris Lattner47844892005-04-03 23:41:52 +00003442 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003443
Chris Lattner44c28c22005-11-20 22:56:56 +00003444 case ISD::READCYCLECOUNTER: {
3445 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3446 TargetLowering::Custom &&
3447 "Must custom expand ReadCycleCounter");
3448 SDOperand T = TLI.LowerOperation(Op, DAG);
3449 assert(T.Val && "Node must be custom expanded!");
3450 Lo = LegalizeOp(T.getValue(0));
3451 Hi = LegalizeOp(T.getValue(1));
3452 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3453 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003454 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003455 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003456
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003457 // These operators cannot be expanded directly, emit them as calls to
3458 // library functions.
3459 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003460 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003461 SDOperand Op;
3462 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3463 case Expand: assert(0 && "cannot expand FP!");
3464 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3465 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3466 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003467
Chris Lattner941d84a2005-07-30 01:40:57 +00003468 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3469
Chris Lattnerfe68d752005-07-29 00:33:32 +00003470 // Now that the custom expander is done, expand the result, which is still
3471 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003472 if (Op.Val) {
3473 ExpandOp(Op, Lo, Hi);
3474 break;
3475 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003476 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003477
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003478 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003479 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003480 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003481 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003482 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003483
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003484 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003485 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3486 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3487 LegalizeOp(Node->getOperand(0)));
3488 // Now that the custom expander is done, expand the result, which is still
3489 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003490 Op = TLI.LowerOperation(Op, DAG);
3491 if (Op.Val) {
3492 ExpandOp(Op, Lo, Hi);
3493 break;
3494 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003495 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003496
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003497 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003498 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003499 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003500 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003501 break;
3502
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003503 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003504 // If the target wants custom lowering, do so.
3505 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3506 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3507 LegalizeOp(Node->getOperand(1)));
3508 Op = TLI.LowerOperation(Op, DAG);
3509 if (Op.Val) {
3510 // Now that the custom expander is done, expand the result, which is
3511 // still VT.
3512 ExpandOp(Op, Lo, Hi);
3513 break;
3514 }
3515 }
3516
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003517 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003518 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003519 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003520
3521 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003522 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003523 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3524 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003525 break;
3526 }
3527
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003528 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003529 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003530 break;
3531
3532 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003533 // If the target wants custom lowering, do so.
3534 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3535 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3536 LegalizeOp(Node->getOperand(1)));
3537 Op = TLI.LowerOperation(Op, DAG);
3538 if (Op.Val) {
3539 // Now that the custom expander is done, expand the result, which is
3540 // still VT.
3541 ExpandOp(Op, Lo, Hi);
3542 break;
3543 }
3544 }
3545
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003546 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003547 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003548 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003549
3550 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003551 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003552 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3553 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003554 break;
3555 }
3556
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003557 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003558 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003559 break;
3560 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003561 // If the target wants custom lowering, do so.
3562 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3563 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3564 LegalizeOp(Node->getOperand(1)));
3565 Op = TLI.LowerOperation(Op, DAG);
3566 if (Op.Val) {
3567 // Now that the custom expander is done, expand the result, which is
3568 // still VT.
3569 ExpandOp(Op, Lo, Hi);
3570 break;
3571 }
3572 }
3573
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003574 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003575 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003576 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003577
3578 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003579 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003580 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3581 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003582 break;
3583 }
3584
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003585 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003586 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003587 break;
3588
Misha Brukman835702a2005-04-21 22:36:52 +00003589 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003590 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3591 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003592 break;
3593 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003594 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3595 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003596 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003597 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003598 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003599 SDOperand LL, LH, RL, RH;
3600 ExpandOp(Node->getOperand(0), LL, LH);
3601 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003602 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3603 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3604 // extended the sign bit of the low half through the upper half, and if so
3605 // emit a MULHS instead of the alternate sequence that is valid for any
3606 // i64 x i64 multiply.
3607 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3608 // is RH an extension of the sign bit of RL?
3609 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3610 RH.getOperand(1).getOpcode() == ISD::Constant &&
3611 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3612 // is LH an extension of the sign bit of LL?
3613 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3614 LH.getOperand(1).getOpcode() == ISD::Constant &&
3615 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3616 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3617 } else {
3618 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3619 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3620 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3621 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3622 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3623 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003624 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3625 } else {
3626 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3627 }
3628 break;
3629 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003630 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3631 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3632 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3633 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003634 }
3635
3636 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003637 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3638 std::make_pair(Lo, Hi))).second;
3639 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003640}
3641
3642
3643// SelectionDAG::Legalize - This is the entry point for the file.
3644//
Chris Lattner4add7e32005-01-23 04:42:50 +00003645void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003646 /// run - This is the main entry point to this class.
3647 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003648 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003649}
3650