blob: 55dcbd9ffdddfbe9dc40a4adf5ddf4d1ed529cc2 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-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)".
55 unsigned ValueTypeActions;
56
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 Lattner03c85462005-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 Lattner3e928bb2005-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 Lattner8afc48e2005-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 Lattner03c85462005-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 Lattner8afc48e2005-01-07 22:28:47 +000087
Chris Lattner3e928bb2005-01-07 07:47:09 +000088public:
89
Chris Lattner9c32d3b2005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-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 Lattner03c85462005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner77e77a62005-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 Lattnercad063f2005-07-16 00:19:57 +0000128
Jim Laskey6269ed12005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000136
Chris Lattnere34b3962005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-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 Lattner47599822005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000143
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
152
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000153SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
154 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
155 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156 assert(MVT::LAST_VALUETYPE <= 16 &&
157 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
Jim Laskey6269ed12005-08-17 00:39:29 +0000160/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
161/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000162/// we expand it. At this point, we know that the result and operand types are
163/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000164SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
165 SDOperand Op0,
166 MVT::ValueType DestVT) {
167 if (Op0.getValueType() == MVT::i32) {
168 // simple 32-bit [signed|unsigned] integer to float/double expansion
169
170 // get the stack frame index of a 8 byte buffer
171 MachineFunction &MF = DAG.getMachineFunction();
172 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
173 // get address of 8 byte buffer
174 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
175 // word offset constant for Hi/Lo address computation
176 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
177 // set up Hi and Lo (into buffer) address based on endian
178 SDOperand Hi, Lo;
179 if (TLI.isLittleEndian()) {
180 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
181 Lo = StackSlot;
182 } else {
183 Hi = StackSlot;
184 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
185 }
186 // if signed map to unsigned space
187 SDOperand Op0Mapped;
188 if (isSigned) {
189 // constant used to invert sign bit (signed to unsigned mapping)
190 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
191 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
192 } else {
193 Op0Mapped = Op0;
194 }
195 // store the lo of the constructed double - based on integer input
196 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
197 Op0Mapped, Lo, DAG.getSrcValue(NULL));
198 // initial hi portion of constructed double
199 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
200 // store the hi of the constructed double - biased exponent
201 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
202 InitialHi, Hi, DAG.getSrcValue(NULL));
203 // load the constructed double
204 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
205 DAG.getSrcValue(NULL));
206 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000207 SDOperand Bias = DAG.getConstantFP(isSigned ?
208 BitsToDouble(0x4330000080000000ULL)
209 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000210 MVT::f64);
211 // subtract the bias
212 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
213 // final result
214 SDOperand Result;
215 // handle final rounding
216 if (DestVT == MVT::f64) {
217 // do nothing
218 Result = Sub;
219 } else {
220 // if f32 then cast to f32
221 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
222 }
223 NeedsAnotherIteration = true;
224 return Result;
225 }
226 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000227 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000229 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
230 DAG.getConstant(0, Op0.getValueType()),
231 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000232 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
233 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
234 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000235
Jim Laskey6269ed12005-08-17 00:39:29 +0000236 // If the sign bit of the integer is set, the large number will be treated
237 // as a negative number. To counteract this, the dynamic code adds an
238 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000239 uint64_t FF;
240 switch (Op0.getValueType()) {
241 default: assert(0 && "Unsupported integer type!");
242 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
243 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
244 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
245 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
246 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000247 if (TLI.isLittleEndian()) FF <<= 32;
248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000249
Chris Lattner5839bf22005-08-26 17:15:30 +0000250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000251 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
252 SDOperand FudgeInReg;
253 if (DestVT == MVT::f32)
254 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
255 DAG.getSrcValue(NULL));
256 else {
257 assert(DestVT == MVT::f64 && "Unexpected conversion");
258 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
259 DAG.getEntryNode(), CPIdx,
260 DAG.getSrcValue(NULL), MVT::f32));
261 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000262
Chris Lattnercad063f2005-07-16 00:19:57 +0000263 NeedsAnotherIteration = true;
264 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
265}
266
Chris Lattner149c58c2005-08-16 18:17:10 +0000267/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000268/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000269/// we promote it. At this point, we know that the result and operand types are
270/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
271/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000272SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
273 MVT::ValueType DestVT,
274 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000275 // First step, figure out the appropriate *INT_TO_FP operation to use.
276 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000277
Chris Lattnercad063f2005-07-16 00:19:57 +0000278 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000279
Chris Lattnercad063f2005-07-16 00:19:57 +0000280 // Scan for the appropriate larger type to use.
281 while (1) {
282 NewInTy = (MVT::ValueType)(NewInTy+1);
283 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000284
Chris Lattnercad063f2005-07-16 00:19:57 +0000285 // If the target supports SINT_TO_FP of this type, use it.
286 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
287 default: break;
288 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000289 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000290 break; // Can't use this datatype.
291 // FALL THROUGH.
292 case TargetLowering::Custom:
293 OpToUse = ISD::SINT_TO_FP;
294 break;
295 }
296 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000297 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000298
Chris Lattnercad063f2005-07-16 00:19:57 +0000299 // If the target supports UINT_TO_FP of this type, use it.
300 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
301 default: break;
302 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000303 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000304 break; // Can't use this datatype.
305 // FALL THROUGH.
306 case TargetLowering::Custom:
307 OpToUse = ISD::UINT_TO_FP;
308 break;
309 }
310 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000311
Chris Lattnercad063f2005-07-16 00:19:57 +0000312 // Otherwise, try a larger type.
313 }
314
315 // Make sure to legalize any nodes we create here in the next pass.
316 NeedsAnotherIteration = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000317
Chris Lattnercad063f2005-07-16 00:19:57 +0000318 // Okay, we found the operation and type to use. Zero extend our input to the
319 // desired type then run the operation on it.
320 return DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000321 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
322 NewInTy, LegalOp));
Chris Lattnercad063f2005-07-16 00:19:57 +0000323}
324
Chris Lattner1618beb2005-07-29 00:11:56 +0000325/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
326/// FP_TO_*INT operation of the specified operand when the target requests that
327/// we promote it. At this point, we know that the result and operand types are
328/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
329/// operation that returns a larger result.
330SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
331 MVT::ValueType DestVT,
332 bool isSigned) {
333 // First step, figure out the appropriate FP_TO*INT operation to use.
334 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000335
Chris Lattner1618beb2005-07-29 00:11:56 +0000336 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000337
Chris Lattner1618beb2005-07-29 00:11:56 +0000338 // Scan for the appropriate larger type to use.
339 while (1) {
340 NewOutTy = (MVT::ValueType)(NewOutTy+1);
341 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000342
Chris Lattner1618beb2005-07-29 00:11:56 +0000343 // If the target supports FP_TO_SINT returning this type, use it.
344 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
345 default: break;
346 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000347 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000348 break; // Can't use this datatype.
349 // FALL THROUGH.
350 case TargetLowering::Custom:
351 OpToUse = ISD::FP_TO_SINT;
352 break;
353 }
354 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000355
Chris Lattner1618beb2005-07-29 00:11:56 +0000356 // If the target supports FP_TO_UINT of this type, use it.
357 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
358 default: break;
359 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000360 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000361 break; // Can't use this datatype.
362 // FALL THROUGH.
363 case TargetLowering::Custom:
364 OpToUse = ISD::FP_TO_UINT;
365 break;
366 }
367 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000368
Chris Lattner1618beb2005-07-29 00:11:56 +0000369 // Otherwise, try a larger type.
370 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000371
Chris Lattner1618beb2005-07-29 00:11:56 +0000372 // Make sure to legalize any nodes we create here in the next pass.
373 NeedsAnotherIteration = true;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000374
Chris Lattner1618beb2005-07-29 00:11:56 +0000375 // Okay, we found the operation and type to use. Truncate the result of the
376 // extended FP_TO_*INT operation to the desired size.
377 return DAG.getNode(ISD::TRUNCATE, DestVT,
378 DAG.getNode(OpToUse, NewOutTy, LegalOp));
379}
380
381
Chris Lattner3e928bb2005-01-07 07:47:09 +0000382void SelectionDAGLegalize::LegalizeDAG() {
383 SDOperand OldRoot = DAG.getRoot();
384 SDOperand NewRoot = LegalizeOp(OldRoot);
385 DAG.setRoot(NewRoot);
386
387 ExpandedNodes.clear();
388 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000389 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000390
391 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000392 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000393}
394
395SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000396 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000397 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000398 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000399
Chris Lattner3e928bb2005-01-07 07:47:09 +0000400 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000401 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000402 if (Node->getNumValues() > 1) {
403 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
404 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000405 case Legal: break; // Nothing to do.
406 case Expand: {
407 SDOperand T1, T2;
408 ExpandOp(Op.getValue(i), T1, T2);
409 assert(LegalizedNodes.count(Op) &&
410 "Expansion didn't add legal operands!");
411 return LegalizedNodes[Op];
412 }
413 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000414 PromoteOp(Op.getValue(i));
415 assert(LegalizedNodes.count(Op) &&
416 "Expansion didn't add legal operands!");
417 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000418 }
419 }
420
Chris Lattner45982da2005-05-12 16:53:42 +0000421 // Note that LegalizeOp may be reentered even from single-use nodes, which
422 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000423 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
424 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000425
Nate Begeman9373a812005-08-10 20:51:12 +0000426 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000427
428 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429
430 switch (Node->getOpcode()) {
431 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000432 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
433 // If this is a target node, legalize it by legalizing the operands then
434 // passing it through.
435 std::vector<SDOperand> Ops;
436 bool Changed = false;
437 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
438 Ops.push_back(LegalizeOp(Node->getOperand(i)));
439 Changed = Changed || Node->getOperand(i) != Ops.back();
440 }
441 if (Changed)
442 if (Node->getNumValues() == 1)
443 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
444 else {
445 std::vector<MVT::ValueType> VTs(Node->value_begin(),
446 Node->value_end());
447 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
448 }
449
450 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
451 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
452 return Result.getValue(Op.ResNo);
453 }
454 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000455 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
456 assert(0 && "Do not know how to legalize this operator!");
457 abort();
458 case ISD::EntryToken:
459 case ISD::FrameIndex:
460 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000461 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000462 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000463 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000464 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000465 case ISD::AssertSext:
466 case ISD::AssertZext:
467 Tmp1 = LegalizeOp(Node->getOperand(0));
468 if (Tmp1 != Node->getOperand(0))
469 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
470 Node->getOperand(1));
471 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000472 case ISD::CopyFromReg:
473 Tmp1 = LegalizeOp(Node->getOperand(0));
474 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000475 Result = DAG.getCopyFromReg(Tmp1,
476 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
477 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000478 else
479 Result = Op.getValue(0);
480
481 // Since CopyFromReg produces two values, make sure to remember that we
482 // legalized both of them.
483 AddLegalizedOperand(Op.getValue(0), Result);
484 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
485 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000486 case ISD::ImplicitDef:
487 Tmp1 = LegalizeOp(Node->getOperand(0));
488 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000489 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
490 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000491 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000492 case ISD::UNDEF: {
493 MVT::ValueType VT = Op.getValueType();
494 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000495 default: assert(0 && "This action is not supported yet!");
496 case TargetLowering::Expand:
497 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000498 if (MVT::isInteger(VT))
499 Result = DAG.getConstant(0, VT);
500 else if (MVT::isFloatingPoint(VT))
501 Result = DAG.getConstantFP(0, VT);
502 else
503 assert(0 && "Unknown value type!");
504 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000505 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000506 break;
507 }
508 break;
509 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000510 case ISD::Constant:
511 // We know we don't need to expand constants here, constants only have one
512 // value and we check that it is fine above.
513
514 // FIXME: Maybe we should handle things like targets that don't support full
515 // 32-bit immediates?
516 break;
517 case ISD::ConstantFP: {
518 // Spill FP immediates to the constant pool if the target cannot directly
519 // codegen them. Targets often have some immediate values that can be
520 // efficiently generated into an FP register without a load. We explicitly
521 // leave these constants as ConstantFP nodes for the target to deal with.
522
523 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
524
525 // Check to see if this FP immediate is already legal.
526 bool isLegal = false;
527 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
528 E = TLI.legal_fpimm_end(); I != E; ++I)
529 if (CFP->isExactlyValue(*I)) {
530 isLegal = true;
531 break;
532 }
533
534 if (!isLegal) {
535 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000536 bool Extend = false;
537
538 // If a FP immediate is precise when represented as a float, we put it
539 // into the constant pool as a float, even if it's is statically typed
540 // as a double.
541 MVT::ValueType VT = CFP->getValueType(0);
542 bool isDouble = VT == MVT::f64;
543 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
544 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000545 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
546 // Only do this if the target has a native EXTLOAD instruction from
547 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000548 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000549 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
550 VT = MVT::f32;
551 Extend = true;
552 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000553
Chris Lattner5839bf22005-08-26 17:15:30 +0000554 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000555 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000556 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
557 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000558 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000559 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
560 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000561 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000562 }
563 break;
564 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000565 case ISD::TokenFactor: {
566 std::vector<SDOperand> Ops;
567 bool Changed = false;
568 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000569 SDOperand Op = Node->getOperand(i);
570 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000571 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000572 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
573 Changed = true;
574 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
575 Ops.push_back(LegalizeOp(Op.getOperand(j)));
576 } else {
577 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
578 Changed |= Ops[i] != Op;
579 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000580 }
581 if (Changed)
582 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
583 break;
584 }
585
Chris Lattner16cd04d2005-05-12 23:24:06 +0000586 case ISD::CALLSEQ_START:
587 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000588 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000589 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000590 Tmp2 = Node->getOperand(0);
591 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000592 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000593
594 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
595 // this will cause the maps used to memoize results to get confused.
596 // Create and add a dummy use, just to increase its use count. This will
597 // be removed at the end of legalize when dead nodes are removed.
598 if (Tmp2.Val->hasOneUse())
599 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
600 DAG.getConstant(0, MVT::i32));
601 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000602 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000603 // nodes are treated specially and are mutated in place. This makes the dag
604 // legalization process more efficient and also makes libcall insertion
605 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000606 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000607 case ISD::DYNAMIC_STACKALLOC:
608 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
609 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
610 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
611 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000612 Tmp3 != Node->getOperand(2)) {
613 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
614 std::vector<SDOperand> Ops;
615 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
616 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
617 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000618 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000619
620 // Since this op produces two values, make sure to remember that we
621 // legalized both of them.
622 AddLegalizedOperand(SDOperand(Node, 0), Result);
623 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
624 return Result.getValue(Op.ResNo);
625
Chris Lattnerd71c0412005-05-13 18:43:43 +0000626 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000627 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000628 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
629 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000630
631 bool Changed = false;
632 std::vector<SDOperand> Ops;
633 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
634 Ops.push_back(LegalizeOp(Node->getOperand(i)));
635 Changed |= Ops.back() != Node->getOperand(i);
636 }
637
638 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000639 std::vector<MVT::ValueType> RetTyVTs;
640 RetTyVTs.reserve(Node->getNumValues());
641 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000642 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000643 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
644 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000645 } else {
646 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000647 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000648 // Since calls produce multiple values, make sure to remember that we
649 // legalized all of them.
650 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
651 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
652 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000653 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000654 case ISD::BR:
655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
656 if (Tmp1 != Node->getOperand(0))
657 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
658 break;
659
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000660 case ISD::BRCOND:
661 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000662
Chris Lattner47e92232005-01-18 19:27:06 +0000663 switch (getTypeAction(Node->getOperand(1).getValueType())) {
664 case Expand: assert(0 && "It's impossible to expand bools");
665 case Legal:
666 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
667 break;
668 case Promote:
669 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
670 break;
671 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000672
673 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
674 default: assert(0 && "This action is not supported yet!");
675 case TargetLowering::Expand:
676 // Expand brcond's setcc into its constituent parts and create a BR_CC
677 // Node.
678 if (Tmp2.getOpcode() == ISD::SETCC) {
679 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
680 Tmp2.getOperand(0), Tmp2.getOperand(1),
681 Node->getOperand(2));
682 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000683 // Make sure the condition is either zero or one. It may have been
684 // promoted from something else.
685 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
686
Nate Begeman7cbd5252005-08-16 19:49:35 +0000687 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
688 DAG.getCondCode(ISD::SETNE), Tmp2,
689 DAG.getConstant(0, Tmp2.getValueType()),
690 Node->getOperand(2));
691 }
692 break;
693 case TargetLowering::Legal:
694 // Basic block destination (Op#2) is always legal.
695 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
696 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
697 Node->getOperand(2));
698 break;
699 }
700 break;
701 case ISD::BR_CC:
702 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
703
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000704 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000705 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
706 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
707 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
708 Tmp3 != Node->getOperand(3)) {
709 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
710 Tmp2, Tmp3, Node->getOperand(4));
711 }
712 break;
713 } else {
714 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
715 Node->getOperand(2), // LHS
716 Node->getOperand(3), // RHS
717 Node->getOperand(1)));
718 // If we get a SETCC back from legalizing the SETCC node we just
719 // created, then use its LHS, RHS, and CC directly in creating a new
720 // node. Otherwise, select between the true and false value based on
721 // comparing the result of the legalized with zero.
722 if (Tmp2.getOpcode() == ISD::SETCC) {
723 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
724 Tmp2.getOperand(0), Tmp2.getOperand(1),
725 Node->getOperand(4));
726 } else {
727 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
728 DAG.getCondCode(ISD::SETNE),
729 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
730 Node->getOperand(4));
731 }
732 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000733 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000734 case ISD::BRCONDTWOWAY:
735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
736 switch (getTypeAction(Node->getOperand(1).getValueType())) {
737 case Expand: assert(0 && "It's impossible to expand bools");
738 case Legal:
739 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
740 break;
741 case Promote:
742 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
743 break;
744 }
745 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
746 // pair.
747 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
748 case TargetLowering::Promote:
749 default: assert(0 && "This action is not supported yet!");
750 case TargetLowering::Legal:
751 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
752 std::vector<SDOperand> Ops;
753 Ops.push_back(Tmp1);
754 Ops.push_back(Tmp2);
755 Ops.push_back(Node->getOperand(2));
756 Ops.push_back(Node->getOperand(3));
757 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
758 }
759 break;
760 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000761 // If BRTWOWAY_CC is legal for this target, then simply expand this node
762 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
763 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000764 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000765 if (Tmp2.getOpcode() == ISD::SETCC) {
766 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
767 Tmp2.getOperand(0), Tmp2.getOperand(1),
768 Node->getOperand(2), Node->getOperand(3));
769 } else {
770 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
771 DAG.getConstant(0, Tmp2.getValueType()),
772 Node->getOperand(2), Node->getOperand(3));
773 }
774 } else {
775 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000776 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000777 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
778 }
Chris Lattner411e8882005-04-09 03:30:19 +0000779 break;
780 }
781 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000782 case ISD::BRTWOWAY_CC:
783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000784 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000785 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
786 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
787 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
788 Tmp3 != Node->getOperand(3)) {
789 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
790 Node->getOperand(4), Node->getOperand(5));
791 }
792 break;
793 } else {
794 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
795 Node->getOperand(2), // LHS
796 Node->getOperand(3), // RHS
797 Node->getOperand(1)));
798 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
799 // pair.
800 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
801 default: assert(0 && "This action is not supported yet!");
802 case TargetLowering::Legal:
803 // If we get a SETCC back from legalizing the SETCC node we just
804 // created, then use its LHS, RHS, and CC directly in creating a new
805 // node. Otherwise, select between the true and false value based on
806 // comparing the result of the legalized with zero.
807 if (Tmp2.getOpcode() == ISD::SETCC) {
808 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
809 Tmp2.getOperand(0), Tmp2.getOperand(1),
810 Node->getOperand(4), Node->getOperand(5));
811 } else {
812 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
813 DAG.getConstant(0, Tmp2.getValueType()),
814 Node->getOperand(4), Node->getOperand(5));
815 }
816 break;
817 case TargetLowering::Expand:
818 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
819 Node->getOperand(4));
820 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
821 break;
822 }
823 }
824 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000825 case ISD::LOAD:
826 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
827 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000828
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 if (Tmp1 != Node->getOperand(0) ||
830 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000831 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
832 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000833 else
834 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000835
Chris Lattner8afc48e2005-01-07 22:28:47 +0000836 // Since loads produce two values, make sure to remember that we legalized
837 // both of them.
838 AddLegalizedOperand(SDOperand(Node, 0), Result);
839 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
840 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000841
Chris Lattner0f69b292005-01-15 06:18:18 +0000842 case ISD::EXTLOAD:
843 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000844 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
846 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000847
Chris Lattner5f056bf2005-07-10 01:55:33 +0000848 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000849 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000850 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000851 case TargetLowering::Promote:
852 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000853 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
854 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000855 // Since loads produce two values, make sure to remember that we legalized
856 // both of them.
857 AddLegalizedOperand(SDOperand(Node, 0), Result);
858 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
859 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000860
Chris Lattner01ff7212005-04-10 22:54:25 +0000861 case TargetLowering::Legal:
862 if (Tmp1 != Node->getOperand(0) ||
863 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000864 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
865 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000866 else
867 Result = SDOperand(Node, 0);
868
869 // Since loads produce two values, make sure to remember that we legalized
870 // both of them.
871 AddLegalizedOperand(SDOperand(Node, 0), Result);
872 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
873 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000874 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000875 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
876 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
877 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000878 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000879 if (Op.ResNo)
880 return Load.getValue(1);
881 return Result;
882 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000883 assert(Node->getOpcode() != ISD::EXTLOAD &&
884 "EXTLOAD should always be supported!");
885 // Turn the unsupported load into an EXTLOAD followed by an explicit
886 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000887 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
888 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000889 SDOperand ValRes;
890 if (Node->getOpcode() == ISD::SEXTLOAD)
891 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000892 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000893 else
894 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000895 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
896 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
897 if (Op.ResNo)
898 return Result.getValue(1);
899 return ValRes;
900 }
901 assert(0 && "Unreachable");
902 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000903 case ISD::EXTRACT_ELEMENT:
904 // Get both the low and high parts.
905 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
906 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
907 Result = Tmp2; // 1 -> Hi
908 else
909 Result = Tmp1; // 0 -> Lo
910 break;
911
912 case ISD::CopyToReg:
913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000914
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000915 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000916 "Register type must be legal!");
917 // Legalize the incoming value (must be legal).
918 Tmp2 = LegalizeOp(Node->getOperand(2));
919 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
920 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
921 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000922 break;
923
924 case ISD::RET:
925 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
926 switch (Node->getNumOperands()) {
927 case 2: // ret val
928 switch (getTypeAction(Node->getOperand(1).getValueType())) {
929 case Legal:
930 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000931 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000932 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
933 break;
934 case Expand: {
935 SDOperand Lo, Hi;
936 ExpandOp(Node->getOperand(1), Lo, Hi);
937 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000938 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000939 }
940 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000941 Tmp2 = PromoteOp(Node->getOperand(1));
942 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
943 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000944 }
945 break;
946 case 1: // ret void
947 if (Tmp1 != Node->getOperand(0))
948 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
949 break;
950 default: { // ret <values>
951 std::vector<SDOperand> NewValues;
952 NewValues.push_back(Tmp1);
953 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
954 switch (getTypeAction(Node->getOperand(i).getValueType())) {
955 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000956 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000957 break;
958 case Expand: {
959 SDOperand Lo, Hi;
960 ExpandOp(Node->getOperand(i), Lo, Hi);
961 NewValues.push_back(Lo);
962 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000963 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000964 }
965 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000966 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000967 }
968 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
969 break;
970 }
971 }
972 break;
973 case ISD::STORE:
974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
975 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
976
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000977 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000978 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000979 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000980 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +0000981 DAG.getConstant(FloatToBits(CFP->getValue()),
982 MVT::i32),
983 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000984 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000985 } else {
986 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000987 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +0000988 DAG.getConstant(DoubleToBits(CFP->getValue()),
989 MVT::i64),
990 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000991 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000992 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000993 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000994 }
995
Chris Lattner3e928bb2005-01-07 07:47:09 +0000996 switch (getTypeAction(Node->getOperand(1).getValueType())) {
997 case Legal: {
998 SDOperand Val = LegalizeOp(Node->getOperand(1));
999 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1000 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001001 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1002 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001003 break;
1004 }
1005 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001006 // Truncate the value and store the result.
1007 Tmp3 = PromoteOp(Node->getOperand(1));
1008 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001009 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001010 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001011 break;
1012
Chris Lattner3e928bb2005-01-07 07:47:09 +00001013 case Expand:
1014 SDOperand Lo, Hi;
1015 ExpandOp(Node->getOperand(1), Lo, Hi);
1016
1017 if (!TLI.isLittleEndian())
1018 std::swap(Lo, Hi);
1019
Chris Lattneredb1add2005-05-11 04:51:16 +00001020 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1021 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001022 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001023 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1024 getIntPtrConstant(IncrementSize));
1025 assert(isTypeLegal(Tmp2.getValueType()) &&
1026 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001027 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001028 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1029 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001030 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1031 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001032 }
1033 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001034 case ISD::PCMARKER:
1035 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001036 if (Tmp1 != Node->getOperand(0))
1037 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001038 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001039 case ISD::TRUNCSTORE:
1040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1041 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1042
1043 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1044 case Legal:
1045 Tmp2 = LegalizeOp(Node->getOperand(1));
1046 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1047 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +00001048 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001049 Node->getOperand(3), Node->getOperand(4));
Chris Lattner0f69b292005-01-15 06:18:18 +00001050 break;
1051 case Promote:
1052 case Expand:
1053 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1054 }
1055 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001056 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001057 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1058 case Expand: assert(0 && "It's impossible to expand bools");
1059 case Legal:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1061 break;
1062 case Promote:
1063 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1064 break;
1065 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001066 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001067 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001068
Nate Begemanb942a3d2005-08-23 04:29:48 +00001069 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001070 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001071 case TargetLowering::Expand:
1072 if (Tmp1.getOpcode() == ISD::SETCC) {
1073 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1074 Tmp2, Tmp3,
1075 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1076 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001077 // Make sure the condition is either zero or one. It may have been
1078 // promoted from something else.
1079 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001080 Result = DAG.getSelectCC(Tmp1,
1081 DAG.getConstant(0, Tmp1.getValueType()),
1082 Tmp2, Tmp3, ISD::SETNE);
1083 }
1084 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001085 case TargetLowering::Legal:
1086 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1087 Tmp3 != Node->getOperand(2))
1088 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1089 Tmp1, Tmp2, Tmp3);
1090 break;
1091 case TargetLowering::Promote: {
1092 MVT::ValueType NVT =
1093 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1094 unsigned ExtOp, TruncOp;
1095 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001096 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001097 TruncOp = ISD::TRUNCATE;
1098 } else {
1099 ExtOp = ISD::FP_EXTEND;
1100 TruncOp = ISD::FP_ROUND;
1101 }
1102 // Promote each of the values to the new type.
1103 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1104 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1105 // Perform the larger operation, then round down.
1106 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1107 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1108 break;
1109 }
1110 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001111 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001112 case ISD::SELECT_CC:
1113 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1114 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1115
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001116 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001117 // Everything is legal, see if we should expand this op or something.
1118 switch (TLI.getOperationAction(ISD::SELECT_CC,
1119 Node->getOperand(0).getValueType())) {
1120 default: assert(0 && "This action is not supported yet!");
1121 case TargetLowering::Custom: {
1122 SDOperand Tmp =
1123 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1124 Node->getOperand(0),
1125 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001126 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001127 if (Tmp.Val) {
1128 Result = LegalizeOp(Tmp);
1129 break;
1130 }
1131 } // FALLTHROUGH if the target can't lower this operation after all.
1132 case TargetLowering::Legal:
1133 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1134 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1135 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1136 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1137 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1138 Tmp3, Tmp4, Node->getOperand(4));
1139 }
1140 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001141 }
1142 break;
1143 } else {
1144 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1145 Node->getOperand(0), // LHS
1146 Node->getOperand(1), // RHS
1147 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001148 // If we get a SETCC back from legalizing the SETCC node we just
1149 // created, then use its LHS, RHS, and CC directly in creating a new
1150 // node. Otherwise, select between the true and false value based on
1151 // comparing the result of the legalized with zero.
1152 if (Tmp1.getOpcode() == ISD::SETCC) {
1153 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1154 Tmp1.getOperand(0), Tmp1.getOperand(1),
1155 Tmp3, Tmp4, Tmp1.getOperand(2));
1156 } else {
1157 Result = DAG.getSelectCC(Tmp1,
1158 DAG.getConstant(0, Tmp1.getValueType()),
1159 Tmp3, Tmp4, ISD::SETNE);
1160 }
Nate Begeman9373a812005-08-10 20:51:12 +00001161 }
1162 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001163 case ISD::SETCC:
1164 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1165 case Legal:
1166 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1167 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001168 break;
1169 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001170 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1171 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1172
1173 // If this is an FP compare, the operands have already been extended.
1174 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1175 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001176 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001177
1178 // Otherwise, we have to insert explicit sign or zero extends. Note
1179 // that we could insert sign extends for ALL conditions, but zero extend
1180 // is cheaper on many machines (an AND instead of two shifts), so prefer
1181 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001182 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001183 default: assert(0 && "Unknown integer comparison!");
1184 case ISD::SETEQ:
1185 case ISD::SETNE:
1186 case ISD::SETUGE:
1187 case ISD::SETUGT:
1188 case ISD::SETULE:
1189 case ISD::SETULT:
1190 // ALL of these operations will work if we either sign or zero extend
1191 // the operands (including the unsigned comparisons!). Zero extend is
1192 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001193 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1194 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001195 break;
1196 case ISD::SETGE:
1197 case ISD::SETGT:
1198 case ISD::SETLT:
1199 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001200 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1201 DAG.getValueType(VT));
1202 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1203 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001204 break;
1205 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001206 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001207 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001208 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1210 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1211 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001212 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001213 case ISD::SETEQ:
1214 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001215 if (RHSLo == RHSHi)
1216 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1217 if (RHSCST->isAllOnesValue()) {
1218 // Comparison to -1.
1219 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001220 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001221 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001222 }
1223
Chris Lattner3e928bb2005-01-07 07:47:09 +00001224 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1225 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1226 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001227 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001228 break;
1229 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001230 // If this is a comparison of the sign bit, just look at the top part.
1231 // X > -1, x < 0
1232 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001233 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001234 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001235 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001236 (CST->isAllOnesValue()))) { // X > -1
1237 Tmp1 = LHSHi;
1238 Tmp2 = RHSHi;
1239 break;
1240 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001241
Chris Lattner3e928bb2005-01-07 07:47:09 +00001242 // FIXME: This generated code sucks.
1243 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001244 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001245 default: assert(0 && "Unknown integer setcc!");
1246 case ISD::SETLT:
1247 case ISD::SETULT: LowCC = ISD::SETULT; break;
1248 case ISD::SETGT:
1249 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1250 case ISD::SETLE:
1251 case ISD::SETULE: LowCC = ISD::SETULE; break;
1252 case ISD::SETGE:
1253 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1254 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001255
Chris Lattner3e928bb2005-01-07 07:47:09 +00001256 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1257 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1258 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1259
1260 // NOTE: on targets without efficient SELECT of bools, we can always use
1261 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001262 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1263 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1264 Node->getOperand(2));
1265 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001266 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1267 Result, Tmp1, Tmp2));
1268 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001269 }
1270 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001271
1272 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1273 default:
1274 assert(0 && "Cannot handle this action for SETCC yet!");
1275 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001276 case TargetLowering::Promote:
1277 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1278 Node->getOperand(2));
1279 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001280 case TargetLowering::Legal:
1281 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1282 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1283 Node->getOperand(2));
1284 break;
1285 case TargetLowering::Expand:
1286 // Expand a setcc node into a select_cc of the same condition, lhs, and
1287 // rhs that selects between const 1 (true) and const 0 (false).
1288 MVT::ValueType VT = Node->getValueType(0);
1289 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1290 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1291 Node->getOperand(2));
1292 Result = LegalizeOp(Result);
1293 break;
1294 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001295 break;
1296
Chris Lattnere1bd8222005-01-11 05:57:22 +00001297 case ISD::MEMSET:
1298 case ISD::MEMCPY:
1299 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001300 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001301 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1302
1303 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1304 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1305 case Expand: assert(0 && "Cannot expand a byte!");
1306 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001307 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001308 break;
1309 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001310 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001311 break;
1312 }
1313 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001314 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001315 }
Chris Lattner272455b2005-02-02 03:44:41 +00001316
1317 SDOperand Tmp4;
1318 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001319 case Expand: {
1320 // Length is too big, just take the lo-part of the length.
1321 SDOperand HiPart;
1322 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1323 break;
1324 }
Chris Lattnere5605212005-01-28 22:29:18 +00001325 case Legal:
1326 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001327 break;
1328 case Promote:
1329 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001330 break;
1331 }
1332
1333 SDOperand Tmp5;
1334 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1335 case Expand: assert(0 && "Cannot expand this yet!");
1336 case Legal:
1337 Tmp5 = LegalizeOp(Node->getOperand(4));
1338 break;
1339 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001340 Tmp5 = PromoteOp(Node->getOperand(4));
1341 break;
1342 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001343
1344 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1345 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001346 case TargetLowering::Custom: {
1347 SDOperand Tmp =
1348 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1349 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1350 if (Tmp.Val) {
1351 Result = LegalizeOp(Tmp);
1352 break;
1353 }
1354 // FALLTHROUGH if the target thinks it is legal.
1355 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001356 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001357 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1358 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1359 Tmp5 != Node->getOperand(4)) {
1360 std::vector<SDOperand> Ops;
1361 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1362 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1363 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1364 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001365 break;
1366 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001367 // Otherwise, the target does not support this operation. Lower the
1368 // operation to an explicit libcall as appropriate.
1369 MVT::ValueType IntPtr = TLI.getPointerTy();
1370 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1371 std::vector<std::pair<SDOperand, const Type*> > Args;
1372
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001373 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001374 if (Node->getOpcode() == ISD::MEMSET) {
1375 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1376 // Extend the ubyte argument to be an int value for the call.
1377 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1378 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1379 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1380
1381 FnName = "memset";
1382 } else if (Node->getOpcode() == ISD::MEMCPY ||
1383 Node->getOpcode() == ISD::MEMMOVE) {
1384 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1385 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1386 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1387 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1388 } else {
1389 assert(0 && "Unknown op!");
1390 }
Chris Lattner45982da2005-05-12 16:53:42 +00001391
Chris Lattnere1bd8222005-01-11 05:57:22 +00001392 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001393 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001394 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001395 Result = CallResult.second;
1396 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001397 break;
1398 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001399 }
1400 break;
1401 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001402
1403 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001404 Tmp1 = LegalizeOp(Node->getOperand(0));
1405 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001406
Chris Lattner3e011362005-05-14 07:45:46 +00001407 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1408 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1409 std::vector<SDOperand> Ops;
1410 Ops.push_back(Tmp1);
1411 Ops.push_back(Tmp2);
1412 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1413 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001414 Result = SDOperand(Node, 0);
1415 // Since these produce two values, make sure to remember that we legalized
1416 // both of them.
1417 AddLegalizedOperand(SDOperand(Node, 0), Result);
1418 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1419 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001420 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001421 Tmp1 = LegalizeOp(Node->getOperand(0));
1422 Tmp2 = LegalizeOp(Node->getOperand(1));
1423 Tmp3 = LegalizeOp(Node->getOperand(2));
1424 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1425 Tmp3 != Node->getOperand(2))
1426 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1427 break;
1428
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001429 case ISD::READIO:
1430 Tmp1 = LegalizeOp(Node->getOperand(0));
1431 Tmp2 = LegalizeOp(Node->getOperand(1));
1432
1433 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1434 case TargetLowering::Custom:
1435 default: assert(0 && "This action not implemented for this operation!");
1436 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001437 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1438 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1439 std::vector<SDOperand> Ops;
1440 Ops.push_back(Tmp1);
1441 Ops.push_back(Tmp2);
1442 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1443 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001444 Result = SDOperand(Node, 0);
1445 break;
1446 case TargetLowering::Expand:
1447 // Replace this with a load from memory.
1448 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1449 Node->getOperand(1), DAG.getSrcValue(NULL));
1450 Result = LegalizeOp(Result);
1451 break;
1452 }
1453
1454 // Since these produce two values, make sure to remember that we legalized
1455 // both of them.
1456 AddLegalizedOperand(SDOperand(Node, 0), Result);
1457 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1458 return Result.getValue(Op.ResNo);
1459
1460 case ISD::WRITEIO:
1461 Tmp1 = LegalizeOp(Node->getOperand(0));
1462 Tmp2 = LegalizeOp(Node->getOperand(1));
1463 Tmp3 = LegalizeOp(Node->getOperand(2));
1464
1465 switch (TLI.getOperationAction(Node->getOpcode(),
1466 Node->getOperand(1).getValueType())) {
1467 case TargetLowering::Custom:
1468 default: assert(0 && "This action not implemented for this operation!");
1469 case TargetLowering::Legal:
1470 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1471 Tmp3 != Node->getOperand(2))
1472 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1473 break;
1474 case TargetLowering::Expand:
1475 // Replace this with a store to memory.
1476 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1477 Node->getOperand(1), Node->getOperand(2),
1478 DAG.getSrcValue(NULL));
1479 Result = LegalizeOp(Result);
1480 break;
1481 }
1482 break;
1483
Chris Lattner84f67882005-01-20 18:52:28 +00001484 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001485 case ISD::SUB_PARTS:
1486 case ISD::SHL_PARTS:
1487 case ISD::SRA_PARTS:
1488 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001489 std::vector<SDOperand> Ops;
1490 bool Changed = false;
1491 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1492 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1493 Changed |= Ops.back() != Node->getOperand(i);
1494 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001495 if (Changed) {
1496 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1497 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1498 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001499
1500 // Since these produce multiple values, make sure to remember that we
1501 // legalized all of them.
1502 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1503 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1504 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001505 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001506
1507 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001508 case ISD::ADD:
1509 case ISD::SUB:
1510 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001511 case ISD::MULHS:
1512 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001513 case ISD::UDIV:
1514 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001515 case ISD::AND:
1516 case ISD::OR:
1517 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001518 case ISD::SHL:
1519 case ISD::SRL:
1520 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001521 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001522 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1523 case Expand: assert(0 && "Not possible");
1524 case Legal:
1525 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1526 break;
1527 case Promote:
1528 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1529 break;
1530 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001531 if (Tmp1 != Node->getOperand(0) ||
1532 Tmp2 != Node->getOperand(1))
1533 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1534 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001535
Nate Begemanc105e192005-04-06 00:23:54 +00001536 case ISD::UREM:
1537 case ISD::SREM:
1538 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1539 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1540 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1541 case TargetLowering::Legal:
1542 if (Tmp1 != Node->getOperand(0) ||
1543 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001544 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001545 Tmp2);
1546 break;
1547 case TargetLowering::Promote:
1548 case TargetLowering::Custom:
1549 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001550 case TargetLowering::Expand:
1551 if (MVT::isInteger(Node->getValueType(0))) {
1552 MVT::ValueType VT = Node->getValueType(0);
1553 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1554 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1555 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1556 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1557 } else {
1558 // Floating point mod -> fmod libcall.
1559 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1560 SDOperand Dummy;
1561 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001562 }
1563 break;
1564 }
1565 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001566
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001567 case ISD::CTPOP:
1568 case ISD::CTTZ:
1569 case ISD::CTLZ:
1570 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1571 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1572 case TargetLowering::Legal:
1573 if (Tmp1 != Node->getOperand(0))
1574 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1575 break;
1576 case TargetLowering::Promote: {
1577 MVT::ValueType OVT = Tmp1.getValueType();
1578 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001579
1580 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001581 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1582 // Perform the larger operation, then subtract if needed.
1583 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1584 switch(Node->getOpcode())
1585 {
1586 case ISD::CTPOP:
1587 Result = Tmp1;
1588 break;
1589 case ISD::CTTZ:
1590 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001591 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1592 DAG.getConstant(getSizeInBits(NVT), NVT),
1593 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001594 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001595 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1596 break;
1597 case ISD::CTLZ:
1598 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001599 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1600 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001601 getSizeInBits(OVT), NVT));
1602 break;
1603 }
1604 break;
1605 }
1606 case TargetLowering::Custom:
1607 assert(0 && "Cannot custom handle this yet!");
1608 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001609 switch(Node->getOpcode())
1610 {
1611 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001612 static const uint64_t mask[6] = {
1613 0x5555555555555555ULL, 0x3333333333333333ULL,
1614 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1615 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1616 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001617 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001618 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1619 unsigned len = getSizeInBits(VT);
1620 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001621 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001622 Tmp2 = DAG.getConstant(mask[i], VT);
1623 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001624 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001625 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1626 DAG.getNode(ISD::AND, VT,
1627 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1628 Tmp2));
1629 }
1630 Result = Tmp1;
1631 break;
1632 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001633 case ISD::CTLZ: {
1634 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001635 x = x | (x >> 1);
1636 x = x | (x >> 2);
1637 ...
1638 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001639 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001640 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001641
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001642 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1643 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001644 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1645 unsigned len = getSizeInBits(VT);
1646 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1647 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001648 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001649 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1650 }
1651 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001652 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001653 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001654 }
1655 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001656 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001657 // unless the target has ctlz but not ctpop, in which case we use:
1658 // { return 32 - nlz(~x & (x-1)); }
1659 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001660 MVT::ValueType VT = Tmp1.getValueType();
1661 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001662 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001663 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1664 DAG.getNode(ISD::SUB, VT, Tmp1,
1665 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001666 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001667 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1668 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001669 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001670 DAG.getConstant(getSizeInBits(VT), VT),
1671 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1672 } else {
1673 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1674 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001675 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001676 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001677 default:
1678 assert(0 && "Cannot expand this yet!");
1679 break;
1680 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001681 break;
1682 }
1683 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001684
Chris Lattner2c8086f2005-04-02 05:00:07 +00001685 // Unary operators
1686 case ISD::FABS:
1687 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001688 case ISD::FSQRT:
1689 case ISD::FSIN:
1690 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001691 Tmp1 = LegalizeOp(Node->getOperand(0));
1692 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1693 case TargetLowering::Legal:
1694 if (Tmp1 != Node->getOperand(0))
1695 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1696 break;
1697 case TargetLowering::Promote:
1698 case TargetLowering::Custom:
1699 assert(0 && "Cannot promote/custom handle this yet!");
1700 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001701 switch(Node->getOpcode()) {
1702 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001703 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1704 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1705 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1706 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001707 break;
1708 }
1709 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001710 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1711 MVT::ValueType VT = Node->getValueType(0);
1712 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001713 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001714 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1715 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1716 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001717 break;
1718 }
1719 case ISD::FSQRT:
1720 case ISD::FSIN:
1721 case ISD::FCOS: {
1722 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001723 const char *FnName = 0;
1724 switch(Node->getOpcode()) {
1725 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1726 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1727 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1728 default: assert(0 && "Unreachable!");
1729 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001730 SDOperand Dummy;
1731 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001732 break;
1733 }
1734 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001735 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001736 }
1737 break;
1738 }
1739 break;
1740
1741 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001742 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001743 case ISD::UINT_TO_FP: {
1744 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001745 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1746 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001747 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001748 Node->getOperand(0).getValueType())) {
1749 default: assert(0 && "Unknown operation action!");
1750 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001751 Result = ExpandLegalINT_TO_FP(isSigned,
1752 LegalizeOp(Node->getOperand(0)),
1753 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001754 AddLegalizedOperand(Op, Result);
1755 return Result;
1756 case TargetLowering::Promote:
1757 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1758 Node->getValueType(0),
1759 isSigned);
1760 AddLegalizedOperand(Op, Result);
1761 return Result;
1762 case TargetLowering::Legal:
1763 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001764 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001765
Chris Lattner3e928bb2005-01-07 07:47:09 +00001766 Tmp1 = LegalizeOp(Node->getOperand(0));
1767 if (Tmp1 != Node->getOperand(0))
1768 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1769 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001770 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001771 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1772 Node->getValueType(0), Node->getOperand(0));
1773 break;
1774 case Promote:
1775 if (isSigned) {
1776 Result = PromoteOp(Node->getOperand(0));
1777 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1778 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1779 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1780 } else {
1781 Result = PromoteOp(Node->getOperand(0));
1782 Result = DAG.getZeroExtendInReg(Result,
1783 Node->getOperand(0).getValueType());
1784 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001785 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001786 break;
1787 }
1788 break;
1789 }
1790 case ISD::TRUNCATE:
1791 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1792 case Legal:
1793 Tmp1 = LegalizeOp(Node->getOperand(0));
1794 if (Tmp1 != Node->getOperand(0))
1795 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1796 break;
1797 case Expand:
1798 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1799
1800 // Since the result is legal, we should just be able to truncate the low
1801 // part of the source.
1802 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1803 break;
1804 case Promote:
1805 Result = PromoteOp(Node->getOperand(0));
1806 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1807 break;
1808 }
1809 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001810
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001811 case ISD::FP_TO_SINT:
1812 case ISD::FP_TO_UINT:
1813 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1814 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001815 Tmp1 = LegalizeOp(Node->getOperand(0));
1816
Chris Lattner1618beb2005-07-29 00:11:56 +00001817 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1818 default: assert(0 && "Unknown operation action!");
1819 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001820 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1821 SDOperand True, False;
1822 MVT::ValueType VT = Node->getOperand(0).getValueType();
1823 MVT::ValueType NVT = Node->getValueType(0);
1824 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1825 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1826 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1827 Node->getOperand(0), Tmp2, ISD::SETLT);
1828 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1829 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1830 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1831 Tmp2));
1832 False = DAG.getNode(ISD::XOR, NVT, False,
1833 DAG.getConstant(1ULL << ShiftAmt, NVT));
1834 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001835 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001836 } else {
1837 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1838 }
1839 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001840 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001841 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001842 Node->getOpcode() == ISD::FP_TO_SINT);
1843 AddLegalizedOperand(Op, Result);
1844 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001845 case TargetLowering::Custom: {
1846 SDOperand Tmp =
1847 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1848 Tmp = TLI.LowerOperation(Tmp, DAG);
1849 if (Tmp.Val) {
1850 AddLegalizedOperand(Op, Tmp);
1851 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001852 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001853 } else {
1854 // The target thinks this is legal afterall.
1855 break;
1856 }
1857 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001858 case TargetLowering::Legal:
1859 break;
1860 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001861
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001862 if (Tmp1 != Node->getOperand(0))
1863 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1864 break;
1865 case Expand:
1866 assert(0 && "Shouldn't need to expand other operators here!");
1867 case Promote:
1868 Result = PromoteOp(Node->getOperand(0));
1869 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1870 break;
1871 }
1872 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001873
Chris Lattner13c78e22005-09-02 00:18:10 +00001874 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001875 case ISD::ZERO_EXTEND:
1876 case ISD::SIGN_EXTEND:
1877 case ISD::FP_EXTEND:
1878 case ISD::FP_ROUND:
1879 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1880 case Legal:
1881 Tmp1 = LegalizeOp(Node->getOperand(0));
1882 if (Tmp1 != Node->getOperand(0))
1883 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1884 break;
1885 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001886 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001887
Chris Lattner03c85462005-01-15 05:21:40 +00001888 case Promote:
1889 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001890 case ISD::ANY_EXTEND:
1891 Result = PromoteOp(Node->getOperand(0));
1892 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
1893 break;
Chris Lattner1713e732005-01-16 00:38:00 +00001894 case ISD::ZERO_EXTEND:
1895 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001896 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001897 Result = DAG.getZeroExtendInReg(Result,
1898 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001899 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001900 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001901 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001902 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001903 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001904 Result,
1905 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001906 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001907 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001908 Result = PromoteOp(Node->getOperand(0));
1909 if (Result.getValueType() != Op.getValueType())
1910 // Dynamically dead while we have only 2 FP types.
1911 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1912 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001913 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001914 Result = PromoteOp(Node->getOperand(0));
1915 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1916 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001917 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001918 }
1919 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001920 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001921 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001922 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001923 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001924
1925 // If this operation is not supported, convert it to a shl/shr or load/store
1926 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001927 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1928 default: assert(0 && "This action not supported for this op yet!");
1929 case TargetLowering::Legal:
1930 if (Tmp1 != Node->getOperand(0))
1931 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001932 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001933 break;
1934 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001935 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001936 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001937 // NOTE: we could fall back on load/store here too for targets without
1938 // SAR. However, it is doubtful that any exist.
1939 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1940 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001941 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001942 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1943 Node->getOperand(0), ShiftCst);
1944 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1945 Result, ShiftCst);
1946 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1947 // The only way we can lower this is to turn it into a STORETRUNC,
1948 // EXTLOAD pair, targetting a temporary location (a stack slot).
1949
1950 // NOTE: there is a choice here between constantly creating new stack
1951 // slots and always reusing the same one. We currently always create
1952 // new ones, as reuse may inhibit scheduling.
1953 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1954 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1955 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1956 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001957 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001958 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1959 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1960 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001961 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001962 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00001963 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1964 Result, StackSlot, DAG.getSrcValue(NULL),
1965 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001966 } else {
1967 assert(0 && "Unknown op");
1968 }
1969 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001970 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001971 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001972 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001973 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001974 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001975
Chris Lattner45982da2005-05-12 16:53:42 +00001976 // Note that LegalizeOp may be reentered even from single-use nodes, which
1977 // means that we always must cache transformed nodes.
1978 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001979 return Result;
1980}
1981
Chris Lattner8b6fa222005-01-15 22:16:26 +00001982/// PromoteOp - Given an operation that produces a value in an invalid type,
1983/// promote it to compute the value into a larger type. The produced value will
1984/// have the correct bits for the low portion of the register, but no guarantee
1985/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001986SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1987 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001988 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001989 assert(getTypeAction(VT) == Promote &&
1990 "Caller should expand or legalize operands that are not promotable!");
1991 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1992 "Cannot promote to smaller type!");
1993
Chris Lattner03c85462005-01-15 05:21:40 +00001994 SDOperand Tmp1, Tmp2, Tmp3;
1995
1996 SDOperand Result;
1997 SDNode *Node = Op.Val;
1998
Chris Lattner6fdcb252005-09-02 20:32:45 +00001999 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2000 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002001
Chris Lattner0f69b292005-01-15 06:18:18 +00002002 // Promotion needs an optimization step to clean up after it, and is not
2003 // careful to avoid operations the target does not support. Make sure that
2004 // all generated operations are legalized in the next iteration.
2005 NeedsAnotherIteration = true;
2006
Chris Lattner03c85462005-01-15 05:21:40 +00002007 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002008 case ISD::CopyFromReg:
2009 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002010 default:
2011 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2012 assert(0 && "Do not know how to promote this operator!");
2013 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002014 case ISD::UNDEF:
2015 Result = DAG.getNode(ISD::UNDEF, NVT);
2016 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002017 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002018 if (VT != MVT::i1)
2019 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2020 else
2021 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002022 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2023 break;
2024 case ISD::ConstantFP:
2025 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2026 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2027 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002028
Chris Lattner82fbfb62005-01-18 02:59:52 +00002029 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002030 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002031 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2032 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002033 Result = LegalizeOp(Result);
2034 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002035
2036 case ISD::TRUNCATE:
2037 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2038 case Legal:
2039 Result = LegalizeOp(Node->getOperand(0));
2040 assert(Result.getValueType() >= NVT &&
2041 "This truncation doesn't make sense!");
2042 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2043 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2044 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002045 case Promote:
2046 // The truncation is not required, because we don't guarantee anything
2047 // about high bits anyway.
2048 Result = PromoteOp(Node->getOperand(0));
2049 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002050 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002051 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2052 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002053 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002054 }
2055 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002056 case ISD::SIGN_EXTEND:
2057 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002058 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2060 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2061 case Legal:
2062 // Input is legal? Just do extend all the way to the larger type.
2063 Result = LegalizeOp(Node->getOperand(0));
2064 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2065 break;
2066 case Promote:
2067 // Promote the reg if it's smaller.
2068 Result = PromoteOp(Node->getOperand(0));
2069 // The high bits are not guaranteed to be anything. Insert an extend.
2070 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002071 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002072 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002073 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002074 Result = DAG.getZeroExtendInReg(Result,
2075 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002076 break;
2077 }
2078 break;
2079
2080 case ISD::FP_EXTEND:
2081 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2082 case ISD::FP_ROUND:
2083 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2084 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2085 case Promote: assert(0 && "Unreachable with 2 FP types!");
2086 case Legal:
2087 // Input is legal? Do an FP_ROUND_INREG.
2088 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002089 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2090 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002091 break;
2092 }
2093 break;
2094
2095 case ISD::SINT_TO_FP:
2096 case ISD::UINT_TO_FP:
2097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2098 case Legal:
2099 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002100 // No extra round required here.
2101 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002102 break;
2103
2104 case Promote:
2105 Result = PromoteOp(Node->getOperand(0));
2106 if (Node->getOpcode() == ISD::SINT_TO_FP)
2107 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002108 Result,
2109 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002110 else
Chris Lattner23993562005-04-13 02:38:47 +00002111 Result = DAG.getZeroExtendInReg(Result,
2112 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002113 // No extra round required here.
2114 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002115 break;
2116 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002117 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2118 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002119 // Round if we cannot tolerate excess precision.
2120 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002121 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2122 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002123 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002124 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002125 break;
2126
2127 case ISD::FP_TO_SINT:
2128 case ISD::FP_TO_UINT:
2129 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2130 case Legal:
2131 Tmp1 = LegalizeOp(Node->getOperand(0));
2132 break;
2133 case Promote:
2134 // The input result is prerounded, so we don't have to do anything
2135 // special.
2136 Tmp1 = PromoteOp(Node->getOperand(0));
2137 break;
2138 case Expand:
2139 assert(0 && "not implemented");
2140 }
Nate Begemand2558e32005-08-14 01:20:53 +00002141 // If we're promoting a UINT to a larger size, check to see if the new node
2142 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2143 // we can use that instead. This allows us to generate better code for
2144 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2145 // legal, such as PowerPC.
2146 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002147 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2148 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begemand2558e32005-08-14 01:20:53 +00002149 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2150 } else {
2151 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2152 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002153 break;
2154
Chris Lattner2c8086f2005-04-02 05:00:07 +00002155 case ISD::FABS:
2156 case ISD::FNEG:
2157 Tmp1 = PromoteOp(Node->getOperand(0));
2158 assert(Tmp1.getValueType() == NVT);
2159 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2160 // NOTE: we do not have to do any extra rounding here for
2161 // NoExcessFPPrecision, because we know the input will have the appropriate
2162 // precision, and these operations don't modify precision at all.
2163 break;
2164
Chris Lattnerda6ba872005-04-28 21:44:33 +00002165 case ISD::FSQRT:
2166 case ISD::FSIN:
2167 case ISD::FCOS:
2168 Tmp1 = PromoteOp(Node->getOperand(0));
2169 assert(Tmp1.getValueType() == NVT);
2170 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2171 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002172 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2173 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002174 break;
2175
Chris Lattner03c85462005-01-15 05:21:40 +00002176 case ISD::AND:
2177 case ISD::OR:
2178 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002179 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002180 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002181 case ISD::MUL:
2182 // The input may have strange things in the top bits of the registers, but
2183 // these operations don't care. They may have wierd bits going out, but
2184 // that too is okay if they are integer operations.
2185 Tmp1 = PromoteOp(Node->getOperand(0));
2186 Tmp2 = PromoteOp(Node->getOperand(1));
2187 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2188 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2189
2190 // However, if this is a floating point operation, they will give excess
2191 // precision that we may not be able to tolerate. If we DO allow excess
2192 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002193 // FIXME: Why would we need to round FP ops more than integer ones?
2194 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00002195 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002196 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2197 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002198 break;
2199
Chris Lattner8b6fa222005-01-15 22:16:26 +00002200 case ISD::SDIV:
2201 case ISD::SREM:
2202 // These operators require that their input be sign extended.
2203 Tmp1 = PromoteOp(Node->getOperand(0));
2204 Tmp2 = PromoteOp(Node->getOperand(1));
2205 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002206 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2207 DAG.getValueType(VT));
2208 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2209 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002210 }
2211 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2212
2213 // Perform FP_ROUND: this is probably overly pessimistic.
2214 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002215 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2216 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002217 break;
2218
2219 case ISD::UDIV:
2220 case ISD::UREM:
2221 // These operators require that their input be zero extended.
2222 Tmp1 = PromoteOp(Node->getOperand(0));
2223 Tmp2 = PromoteOp(Node->getOperand(1));
2224 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002225 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2226 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002227 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2228 break;
2229
2230 case ISD::SHL:
2231 Tmp1 = PromoteOp(Node->getOperand(0));
2232 Tmp2 = LegalizeOp(Node->getOperand(1));
2233 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2234 break;
2235 case ISD::SRA:
2236 // The input value must be properly sign extended.
2237 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002238 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2239 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002240 Tmp2 = LegalizeOp(Node->getOperand(1));
2241 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2242 break;
2243 case ISD::SRL:
2244 // The input value must be properly zero extended.
2245 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002246 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002247 Tmp2 = LegalizeOp(Node->getOperand(1));
2248 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2249 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002250 case ISD::LOAD:
2251 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2252 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002253 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002254 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002255 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2256 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002257 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002258 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2259 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002260
2261 // Remember that we legalized the chain.
2262 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2263 break;
2264 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002265 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2266 case Expand: assert(0 && "It's impossible to expand bools");
2267 case Legal:
2268 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2269 break;
2270 case Promote:
2271 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2272 break;
2273 }
Chris Lattner03c85462005-01-15 05:21:40 +00002274 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2275 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2276 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2277 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002278 case ISD::SELECT_CC:
2279 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2280 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2281 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2282 Node->getOperand(1), Tmp2, Tmp3,
2283 Node->getOperand(4));
2284 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002285 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002286 case ISD::CALL: {
2287 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2288 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2289
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002290 std::vector<SDOperand> Ops;
2291 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2292 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2293
Chris Lattner8ac532c2005-01-16 19:46:48 +00002294 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2295 "Can only promote single result calls");
2296 std::vector<MVT::ValueType> RetTyVTs;
2297 RetTyVTs.reserve(2);
2298 RetTyVTs.push_back(NVT);
2299 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002300 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2301 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002302 Result = SDOperand(NC, 0);
2303
2304 // Insert the new chain mapping.
2305 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2306 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002307 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002308 case ISD::CTPOP:
2309 case ISD::CTTZ:
2310 case ISD::CTLZ:
2311 Tmp1 = Node->getOperand(0);
2312 //Zero extend the argument
2313 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2314 // Perform the larger operation, then subtract if needed.
2315 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2316 switch(Node->getOpcode())
2317 {
2318 case ISD::CTPOP:
2319 Result = Tmp1;
2320 break;
2321 case ISD::CTTZ:
2322 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002323 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002324 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002325 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002326 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2327 break;
2328 case ISD::CTLZ:
2329 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002330 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2331 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002332 getSizeInBits(VT), NVT));
2333 break;
2334 }
2335 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002336 }
2337
2338 assert(Result.Val && "Didn't set a result!");
2339 AddPromotedOperand(Op, Result);
2340 return Result;
2341}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002342
Chris Lattner84f67882005-01-20 18:52:28 +00002343/// ExpandAddSub - Find a clever way to expand this add operation into
2344/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002345void SelectionDAGLegalize::
2346ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2347 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002348 // Expand the subcomponents.
2349 SDOperand LHSL, LHSH, RHSL, RHSH;
2350 ExpandOp(LHS, LHSL, LHSH);
2351 ExpandOp(RHS, RHSL, RHSH);
2352
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002353 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002354 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2355 if (LHSL.getValueType() == MVT::i32) {
2356 SDOperand LowEl;
2357 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2358 if (C->getValue() == 0)
2359 LowEl = RHSL;
2360 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2361 if (C->getValue() == 0)
2362 LowEl = LHSL;
2363 if (LowEl.Val) {
2364 // Turn this into an add/sub of the high part only.
2365 SDOperand HiEl =
2366 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2367 LowEl.getValueType(), LHSH, RHSH);
2368 Lo = LowEl;
2369 Hi = HiEl;
2370 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002371 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002372 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002373
Chris Lattner84f67882005-01-20 18:52:28 +00002374 std::vector<SDOperand> Ops;
2375 Ops.push_back(LHSL);
2376 Ops.push_back(LHSH);
2377 Ops.push_back(RHSL);
2378 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002379
2380 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2381 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002382 Hi = Lo.getValue(1);
2383}
2384
Chris Lattner5b359c62005-04-02 04:00:59 +00002385void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2386 SDOperand Op, SDOperand Amt,
2387 SDOperand &Lo, SDOperand &Hi) {
2388 // Expand the subcomponents.
2389 SDOperand LHSL, LHSH;
2390 ExpandOp(Op, LHSL, LHSH);
2391
2392 std::vector<SDOperand> Ops;
2393 Ops.push_back(LHSL);
2394 Ops.push_back(LHSH);
2395 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002396 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002397 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002398 Hi = Lo.getValue(1);
2399}
2400
2401
Chris Lattnere34b3962005-01-19 04:19:40 +00002402/// ExpandShift - Try to find a clever way to expand this shift operation out to
2403/// smaller elements. If we can't find a way that is more efficient than a
2404/// libcall on this target, return false. Otherwise, return true with the
2405/// low-parts expanded into Lo and Hi.
2406bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2407 SDOperand &Lo, SDOperand &Hi) {
2408 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2409 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002410
Chris Lattnere34b3962005-01-19 04:19:40 +00002411 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002412 SDOperand ShAmt = LegalizeOp(Amt);
2413 MVT::ValueType ShTy = ShAmt.getValueType();
2414 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2415 unsigned NVTBits = MVT::getSizeInBits(NVT);
2416
2417 // Handle the case when Amt is an immediate. Other cases are currently broken
2418 // and are disabled.
2419 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2420 unsigned Cst = CN->getValue();
2421 // Expand the incoming operand to be shifted, so that we have its parts
2422 SDOperand InL, InH;
2423 ExpandOp(Op, InL, InH);
2424 switch(Opc) {
2425 case ISD::SHL:
2426 if (Cst > VTBits) {
2427 Lo = DAG.getConstant(0, NVT);
2428 Hi = DAG.getConstant(0, NVT);
2429 } else if (Cst > NVTBits) {
2430 Lo = DAG.getConstant(0, NVT);
2431 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002432 } else if (Cst == NVTBits) {
2433 Lo = DAG.getConstant(0, NVT);
2434 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002435 } else {
2436 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2437 Hi = DAG.getNode(ISD::OR, NVT,
2438 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2439 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2440 }
2441 return true;
2442 case ISD::SRL:
2443 if (Cst > VTBits) {
2444 Lo = DAG.getConstant(0, NVT);
2445 Hi = DAG.getConstant(0, NVT);
2446 } else if (Cst > NVTBits) {
2447 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2448 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002449 } else if (Cst == NVTBits) {
2450 Lo = InH;
2451 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002452 } else {
2453 Lo = DAG.getNode(ISD::OR, NVT,
2454 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2455 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2456 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2457 }
2458 return true;
2459 case ISD::SRA:
2460 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002461 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002462 DAG.getConstant(NVTBits-1, ShTy));
2463 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002464 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002465 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002466 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002467 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002468 } else if (Cst == NVTBits) {
2469 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002470 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002471 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002472 } else {
2473 Lo = DAG.getNode(ISD::OR, NVT,
2474 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2475 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2476 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2477 }
2478 return true;
2479 }
2480 }
2481 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2482 // so disable it for now. Currently targets are handling this via SHL_PARTS
2483 // and friends.
2484 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002485
2486 // If we have an efficient select operation (or if the selects will all fold
2487 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002488 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002489 return false;
2490
2491 SDOperand InL, InH;
2492 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002493 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2494 DAG.getConstant(NVTBits, ShTy), ShAmt);
2495
Chris Lattnere5544f82005-01-20 20:29:23 +00002496 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002497 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2498 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002499
Chris Lattnere34b3962005-01-19 04:19:40 +00002500 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2501 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2502 DAG.getConstant(NVTBits-1, ShTy));
2503 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2504 DAG.getConstant(NVTBits-1, ShTy));
2505 }
2506
2507 if (Opc == ISD::SHL) {
2508 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2509 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2510 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002511 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002512
Chris Lattnere34b3962005-01-19 04:19:40 +00002513 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2514 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2515 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002516 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002517 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2518 DAG.getConstant(32, ShTy),
2519 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002520 DAG.getConstant(0, NVT),
2521 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002522 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002523 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002524 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002525 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002526
2527 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002528 if (Opc == ISD::SRA)
2529 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2530 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002531 else
2532 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002533 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002534 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002535 }
2536 return true;
2537}
Chris Lattner77e77a62005-01-21 06:05:23 +00002538
Chris Lattner9530ddc2005-05-13 05:09:11 +00002539/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2540/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002541/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002542static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002543 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002544
Chris Lattner16cd04d2005-05-12 23:24:06 +00002545 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002546 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002547 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002548 Found = Node;
2549 return;
2550 }
2551
2552 // Otherwise, scan the operands of Node to see if any of them is a call.
2553 assert(Node->getNumOperands() != 0 &&
2554 "All leaves should have depth equal to the entry node!");
2555 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002556 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002557
2558 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002559 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002560 Found);
2561}
2562
2563
Chris Lattner9530ddc2005-05-13 05:09:11 +00002564/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2565/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002566/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002567static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2568 std::set<SDNode*> &Visited) {
2569 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2570 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002571
Chris Lattner16cd04d2005-05-12 23:24:06 +00002572 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002573 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002574 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002575 Found = Node;
2576 return;
2577 }
2578
2579 // Otherwise, scan the operands of Node to see if any of them is a call.
2580 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2581 if (UI == E) return;
2582 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002583 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002584
2585 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002586 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002587}
2588
Chris Lattner9530ddc2005-05-13 05:09:11 +00002589/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002590/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002591static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002592 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002593 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002594 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002595 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002596
2597 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002598 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002599
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002600 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002601 if (TheChain.getValueType() != MVT::Other)
2602 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002603 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002604
2605 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002606 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002607
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002608 // Make sure to only follow users of our token chain.
2609 SDNode *User = *UI;
2610 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2611 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002612 if (SDNode *Result = FindCallSeqEnd(User))
2613 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002614 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002615 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002616}
2617
Chris Lattner9530ddc2005-05-13 05:09:11 +00002618/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002619/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002620static SDNode *FindCallSeqStart(SDNode *Node) {
2621 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002622 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002623
2624 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2625 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002626 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002627}
2628
2629
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002630/// FindInputOutputChains - If we are replacing an operation with a call we need
2631/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002632/// properly serialize the calls in the block. The returned operand is the
2633/// input chain value for the new call (e.g. the entry node or the previous
2634/// call), and OutChain is set to be the chain node to update to point to the
2635/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002636static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2637 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002638 SDNode *LatestCallSeqStart = Entry.Val;
2639 SDNode *LatestCallSeqEnd = 0;
2640 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2641 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002642
Chris Lattner16cd04d2005-05-12 23:24:06 +00002643 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002644 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002645 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2646 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002647 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2648 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002649 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002650 LatestCallSeqEnd = Entry.Val;
2651 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002652
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002653 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002654 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002655 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002656 std::set<SDNode*> Visited;
2657 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002658
Chris Lattner9530ddc2005-05-13 05:09:11 +00002659 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002660 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002661 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002662
Chris Lattner9530ddc2005-05-13 05:09:11 +00002663 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002664}
2665
Jeff Cohen00b168892005-07-27 06:12:32 +00002666/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002667void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2668 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002669 // Nothing to splice it into?
2670 if (OutChain == 0) return;
2671
2672 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2673 //OutChain->dump();
2674
2675 // Form a token factor node merging the old inval and the new inval.
2676 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2677 OutChain->getOperand(0));
2678 // Change the node to refer to the new token.
2679 OutChain->setAdjCallChain(InToken);
2680}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002681
2682
Chris Lattner77e77a62005-01-21 06:05:23 +00002683// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2684// does not fit into a register, return the lo part and set the hi part to the
2685// by-reg argument. If it does fit into a single register, return the result
2686// and leave the Hi part unset.
2687SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2688 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002689 SDNode *OutChain;
2690 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2691 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002692 if (InChain.Val == 0)
2693 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002694
Chris Lattner77e77a62005-01-21 06:05:23 +00002695 TargetLowering::ArgListTy Args;
2696 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2697 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2698 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2699 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2700 }
2701 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002702
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002703 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002704 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002705 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002706 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2707 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002708
Chris Lattner99c25b82005-09-02 20:26:58 +00002709 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002710 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002711 default: assert(0 && "Unknown thing");
2712 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002713 Result = CallInfo.first;
2714 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002715 case Promote:
2716 assert(0 && "Cannot promote this yet!");
2717 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002718 ExpandOp(CallInfo.first, Result, Hi);
2719 CallInfo.second = LegalizeOp(CallInfo.second);
2720 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002721 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002722
2723 SpliceCallInto(CallInfo.second, OutChain);
2724 NeedsAnotherIteration = true;
2725 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002726}
2727
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002728
Chris Lattner77e77a62005-01-21 06:05:23 +00002729/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2730/// destination type is legal.
2731SDOperand SelectionDAGLegalize::
2732ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002733 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002734 assert(getTypeAction(Source.getValueType()) == Expand &&
2735 "This is not an expansion!");
2736 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2737
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002738 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002739 assert(Source.getValueType() == MVT::i64 &&
2740 "This only works for 64-bit -> FP");
2741 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2742 // incoming integer is set. To handle this, we dynamically test to see if
2743 // it is set, and, if so, add a fudge factor.
2744 SDOperand Lo, Hi;
2745 ExpandOp(Source, Lo, Hi);
2746
Chris Lattner66de05b2005-05-13 04:45:13 +00002747 // If this is unsigned, and not supported, first perform the conversion to
2748 // signed, then adjust the result if the sign bit is set.
2749 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2750 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2751
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002752 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2753 DAG.getConstant(0, Hi.getValueType()),
2754 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002755 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2756 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2757 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002758 uint64_t FF = 0x5f800000ULL;
2759 if (TLI.isLittleEndian()) FF <<= 32;
2760 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002761
Chris Lattner5839bf22005-08-26 17:15:30 +00002762 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002763 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2764 SDOperand FudgeInReg;
2765 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002766 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2767 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002768 else {
2769 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002770 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2771 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002772 }
2773 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002774 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002775
Chris Lattnera88a2602005-05-14 05:33:54 +00002776 // Check to see if the target has a custom way to lower this. If so, use it.
2777 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2778 default: assert(0 && "This action not implemented for this operation!");
2779 case TargetLowering::Legal:
2780 case TargetLowering::Expand:
2781 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002782 case TargetLowering::Custom: {
2783 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2784 Source), DAG);
2785 if (NV.Val)
2786 return LegalizeOp(NV);
2787 break; // The target decided this was legal after all
2788 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002789 }
2790
Chris Lattner13689e22005-05-12 07:00:44 +00002791 // Expand the source, then glue it back together for the call. We must expand
2792 // the source in case it is shared (this pass of legalize must traverse it).
2793 SDOperand SrcLo, SrcHi;
2794 ExpandOp(Source, SrcLo, SrcHi);
2795 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2796
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002797 SDNode *OutChain = 0;
2798 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2799 DAG.getEntryNode());
2800 const char *FnName = 0;
2801 if (DestTy == MVT::f32)
2802 FnName = "__floatdisf";
2803 else {
2804 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2805 FnName = "__floatdidf";
2806 }
2807
Chris Lattner77e77a62005-01-21 06:05:23 +00002808 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2809
2810 TargetLowering::ArgListTy Args;
2811 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002812
Chris Lattner77e77a62005-01-21 06:05:23 +00002813 Args.push_back(std::make_pair(Source, ArgTy));
2814
2815 // We don't care about token chains for libcalls. We just use the entry
2816 // node as our input and ignore the output chain. This allows us to place
2817 // calls wherever we need them to satisfy data dependences.
2818 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002819
2820 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002821 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2822 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002823
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002824 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002825 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002826}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002827
Chris Lattnere34b3962005-01-19 04:19:40 +00002828
2829
Chris Lattner3e928bb2005-01-07 07:47:09 +00002830/// ExpandOp - Expand the specified SDOperand into its two component pieces
2831/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2832/// LegalizeNodes map is filled in for any results that are not expanded, the
2833/// ExpandedNodes map is filled in for any results that are expanded, and the
2834/// Lo/Hi values are returned.
2835void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2836 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002837 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002838 SDNode *Node = Op.Val;
2839 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2840 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2841 assert(MVT::isInteger(NVT) && NVT < VT &&
2842 "Cannot expand to FP value or to larger int value!");
2843
Chris Lattner6fdcb252005-09-02 20:32:45 +00002844 // See if we already expanded it.
2845 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2846 = ExpandedNodes.find(Op);
2847 if (I != ExpandedNodes.end()) {
2848 Lo = I->second.first;
2849 Hi = I->second.second;
2850 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002851 }
2852
Chris Lattner4e6c7462005-01-08 19:27:05 +00002853 // Expanding to multiple registers needs to perform an optimization step, and
2854 // is not careful to avoid operations the target does not support. Make sure
2855 // that all generated operations are legalized in the next iteration.
2856 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002857
Chris Lattner3e928bb2005-01-07 07:47:09 +00002858 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002859 case ISD::CopyFromReg:
2860 assert(0 && "CopyFromReg must be legal!");
2861 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002862 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2863 assert(0 && "Do not know how to expand this operator!");
2864 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002865 case ISD::UNDEF:
2866 Lo = DAG.getNode(ISD::UNDEF, NVT);
2867 Hi = DAG.getNode(ISD::UNDEF, NVT);
2868 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002869 case ISD::Constant: {
2870 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2871 Lo = DAG.getConstant(Cst, NVT);
2872 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2873 break;
2874 }
2875
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002876 case ISD::BUILD_PAIR:
2877 // Legalize both operands. FIXME: in the future we should handle the case
2878 // where the two elements are not legal.
2879 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2880 Lo = LegalizeOp(Node->getOperand(0));
2881 Hi = LegalizeOp(Node->getOperand(1));
2882 break;
2883
Chris Lattneredb1add2005-05-11 04:51:16 +00002884 case ISD::CTPOP:
2885 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002886 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2887 DAG.getNode(ISD::CTPOP, NVT, Lo),
2888 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002889 Hi = DAG.getConstant(0, NVT);
2890 break;
2891
Chris Lattner39a8f332005-05-12 19:05:01 +00002892 case ISD::CTLZ: {
2893 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002894 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002895 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2896 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002897 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2898 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002899 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2900 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2901
2902 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2903 Hi = DAG.getConstant(0, NVT);
2904 break;
2905 }
2906
2907 case ISD::CTTZ: {
2908 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002909 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002910 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2911 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002912 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2913 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002914 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2915 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2916
2917 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2918 Hi = DAG.getConstant(0, NVT);
2919 break;
2920 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002921
Chris Lattner3e928bb2005-01-07 07:47:09 +00002922 case ISD::LOAD: {
2923 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2924 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002925 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002926
2927 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002928 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002929 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2930 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00002931 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002932 //are from the same instruction?
2933 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002934
2935 // Build a factor node to remember that this load is independent of the
2936 // other one.
2937 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2938 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002939
Chris Lattner3e928bb2005-01-07 07:47:09 +00002940 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002941 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002942 if (!TLI.isLittleEndian())
2943 std::swap(Lo, Hi);
2944 break;
2945 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002946 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002947 case ISD::CALL: {
2948 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2949 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2950
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002951 bool Changed = false;
2952 std::vector<SDOperand> Ops;
2953 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2954 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2955 Changed |= Ops.back() != Node->getOperand(i);
2956 }
2957
Chris Lattner3e928bb2005-01-07 07:47:09 +00002958 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2959 "Can only expand a call once so far, not i64 -> i16!");
2960
2961 std::vector<MVT::ValueType> RetTyVTs;
2962 RetTyVTs.reserve(3);
2963 RetTyVTs.push_back(NVT);
2964 RetTyVTs.push_back(NVT);
2965 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002966 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2967 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002968 Lo = SDOperand(NC, 0);
2969 Hi = SDOperand(NC, 1);
2970
2971 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002972 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002973 break;
2974 }
2975 case ISD::AND:
2976 case ISD::OR:
2977 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2978 SDOperand LL, LH, RL, RH;
2979 ExpandOp(Node->getOperand(0), LL, LH);
2980 ExpandOp(Node->getOperand(1), RL, RH);
2981 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2982 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2983 break;
2984 }
2985 case ISD::SELECT: {
2986 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002987
2988 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2989 case Expand: assert(0 && "It's impossible to expand bools");
2990 case Legal:
2991 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2992 break;
2993 case Promote:
2994 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2995 break;
2996 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002997 ExpandOp(Node->getOperand(1), LL, LH);
2998 ExpandOp(Node->getOperand(2), RL, RH);
2999 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3000 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3001 break;
3002 }
Nate Begeman9373a812005-08-10 20:51:12 +00003003 case ISD::SELECT_CC: {
3004 SDOperand TL, TH, FL, FH;
3005 ExpandOp(Node->getOperand(2), TL, TH);
3006 ExpandOp(Node->getOperand(3), FL, FH);
3007 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3008 Node->getOperand(1), TL, FL, Node->getOperand(4));
3009 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3010 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003011 Lo = LegalizeOp(Lo);
3012 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003013 break;
3014 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003015 case ISD::ANY_EXTEND: {
3016 SDOperand In;
3017 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3018 case Expand: assert(0 && "expand-expand not implemented yet!");
3019 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3020 case Promote:
3021 In = PromoteOp(Node->getOperand(0));
3022 break;
3023 }
3024
3025 // The low part is any extension of the input (which degenerates to a copy).
3026 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3027 // The high part is undefined.
3028 Hi = DAG.getNode(ISD::UNDEF, NVT);
3029 break;
3030 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003031 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003032 SDOperand In;
3033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3034 case Expand: assert(0 && "expand-expand not implemented yet!");
3035 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3036 case Promote:
3037 In = PromoteOp(Node->getOperand(0));
3038 // Emit the appropriate sign_extend_inreg to get the value we want.
3039 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003040 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003041 break;
3042 }
3043
Chris Lattner3e928bb2005-01-07 07:47:09 +00003044 // The low part is just a sign extension of the input (which degenerates to
3045 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003046 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003047
Chris Lattner3e928bb2005-01-07 07:47:09 +00003048 // The high part is obtained by SRA'ing all but one of the bits of the lo
3049 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003050 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003051 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3052 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003053 break;
3054 }
Chris Lattner06098e02005-04-03 23:41:52 +00003055 case ISD::ZERO_EXTEND: {
3056 SDOperand In;
3057 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3058 case Expand: assert(0 && "expand-expand not implemented yet!");
3059 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3060 case Promote:
3061 In = PromoteOp(Node->getOperand(0));
3062 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003063 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003064 break;
3065 }
3066
Chris Lattner3e928bb2005-01-07 07:47:09 +00003067 // The low part is just a zero extension of the input (which degenerates to
3068 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003069 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003070
Chris Lattner3e928bb2005-01-07 07:47:09 +00003071 // The high part is just a zero.
3072 Hi = DAG.getConstant(0, NVT);
3073 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003074 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003075 // These operators cannot be expanded directly, emit them as calls to
3076 // library functions.
3077 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003078 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003079 SDOperand Op;
3080 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3081 case Expand: assert(0 && "cannot expand FP!");
3082 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3083 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3084 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003085
Chris Lattnerf20d1832005-07-30 01:40:57 +00003086 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3087
Chris Lattner80a3e942005-07-29 00:33:32 +00003088 // Now that the custom expander is done, expand the result, which is still
3089 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003090 if (Op.Val) {
3091 ExpandOp(Op, Lo, Hi);
3092 break;
3093 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003094 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003095
Chris Lattner4e6c7462005-01-08 19:27:05 +00003096 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003097 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003098 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003099 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003100 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003101
Chris Lattner4e6c7462005-01-08 19:27:05 +00003102 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003103 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3104 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3105 LegalizeOp(Node->getOperand(0)));
3106 // Now that the custom expander is done, expand the result, which is still
3107 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003108 Op = TLI.LowerOperation(Op, DAG);
3109 if (Op.Val) {
3110 ExpandOp(Op, Lo, Hi);
3111 break;
3112 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003113 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003114
Chris Lattner4e6c7462005-01-08 19:27:05 +00003115 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003116 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003117 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003118 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003119 break;
3120
Chris Lattnere34b3962005-01-19 04:19:40 +00003121 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003122 // If the target wants custom lowering, do so.
3123 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3124 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3125 LegalizeOp(Node->getOperand(1)));
3126 Op = TLI.LowerOperation(Op, DAG);
3127 if (Op.Val) {
3128 // Now that the custom expander is done, expand the result, which is
3129 // still VT.
3130 ExpandOp(Op, Lo, Hi);
3131 break;
3132 }
3133 }
3134
Chris Lattnere34b3962005-01-19 04:19:40 +00003135 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003136 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003137 break;
Chris Lattner47599822005-04-02 03:38:53 +00003138
3139 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003140 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003141 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3142 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003143 break;
3144 }
3145
Chris Lattnere34b3962005-01-19 04:19:40 +00003146 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003147 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003148 break;
3149
3150 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003151 // If the target wants custom lowering, do so.
3152 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3153 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3154 LegalizeOp(Node->getOperand(1)));
3155 Op = TLI.LowerOperation(Op, DAG);
3156 if (Op.Val) {
3157 // Now that the custom expander is done, expand the result, which is
3158 // still VT.
3159 ExpandOp(Op, Lo, Hi);
3160 break;
3161 }
3162 }
3163
Chris Lattnere34b3962005-01-19 04:19:40 +00003164 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003165 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003166 break;
Chris Lattner47599822005-04-02 03:38:53 +00003167
3168 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003169 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003170 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3171 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003172 break;
3173 }
3174
Chris Lattnere34b3962005-01-19 04:19:40 +00003175 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003176 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003177 break;
3178 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003179 // If the target wants custom lowering, do so.
3180 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3181 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3182 LegalizeOp(Node->getOperand(1)));
3183 Op = TLI.LowerOperation(Op, DAG);
3184 if (Op.Val) {
3185 // Now that the custom expander is done, expand the result, which is
3186 // still VT.
3187 ExpandOp(Op, Lo, Hi);
3188 break;
3189 }
3190 }
3191
Chris Lattnere34b3962005-01-19 04:19:40 +00003192 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003193 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003194 break;
Chris Lattner47599822005-04-02 03:38:53 +00003195
3196 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003197 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003198 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3199 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003200 break;
3201 }
3202
Chris Lattnere34b3962005-01-19 04:19:40 +00003203 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003204 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003205 break;
3206
Misha Brukmanedf128a2005-04-21 22:36:52 +00003207 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003208 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3209 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003210 break;
3211 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003212 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3213 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003214 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003215 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003216 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003217 SDOperand LL, LH, RL, RH;
3218 ExpandOp(Node->getOperand(0), LL, LH);
3219 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003220 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3221 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3222 // extended the sign bit of the low half through the upper half, and if so
3223 // emit a MULHS instead of the alternate sequence that is valid for any
3224 // i64 x i64 multiply.
3225 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3226 // is RH an extension of the sign bit of RL?
3227 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3228 RH.getOperand(1).getOpcode() == ISD::Constant &&
3229 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3230 // is LH an extension of the sign bit of LL?
3231 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3232 LH.getOperand(1).getOpcode() == ISD::Constant &&
3233 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3234 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3235 } else {
3236 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3237 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3238 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3239 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3240 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3241 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003242 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3243 } else {
3244 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3245 }
3246 break;
3247 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003248 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3249 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3250 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3251 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003252 }
3253
3254 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003255 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3256 std::make_pair(Lo, Hi))).second;
3257 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003258}
3259
3260
3261// SelectionDAG::Legalize - This is the entry point for the file.
3262//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003263void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003264 /// run - This is the main entry point to this class.
3265 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003266 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003267}
3268