blob: 55f6a2e4e72fc746c211110b9ea74335ae8084ca [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
Chris Lattner01b3d732005-09-28 22:28:18 +0000212 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000213 // 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;
Chris Lattner473a9902005-09-29 06:44:39 +0000264 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnercad063f2005-07-16 00:19:57 +0000265}
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
Chris Lattner32fca002005-10-06 01:20:27 +0000381/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
382/// not been visited yet and if all of its operands have already been visited.
383static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
384 std::map<SDNode*, unsigned> &Visited) {
385 if (++Visited[N] != N->getNumOperands())
386 return; // Haven't visited all operands yet
387
388 Order.push_back(N);
389
390 if (N->hasOneUse()) { // Tail recurse in common case.
391 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
392 return;
393 }
394
395 // Now that we have N in, add anything that uses it if all of their operands
396 // are now done.
397
398 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
399 ComputeTopDownOrdering(*UI, Order, Visited);
400}
401
Chris Lattner1618beb2005-07-29 00:11:56 +0000402
Chris Lattner3e928bb2005-01-07 07:47:09 +0000403void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000404 // The legalize process is inherently a bottom-up recursive process (users
405 // legalize their uses before themselves). Given infinite stack space, we
406 // could just start legalizing on the root and traverse the whole graph. In
407 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000408 // blocks. To avoid this problem, compute an ordering of the nodes where each
409 // node is only legalized after all of its operands are legalized.
410 std::map<SDNode*, unsigned> Visited;
411 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000412
Chris Lattner32fca002005-10-06 01:20:27 +0000413 // Compute ordering from all of the leaves in the graphs, those (like the
414 // entry node) that have no operands.
415 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
416 E = DAG.allnodes_end(); I != E; ++I) {
417 if ((*I)->getNumOperands() == 0) {
418 Visited[*I] = 0 - 1U;
419 ComputeTopDownOrdering(*I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000420 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000421 }
422
Chris Lattner32fca002005-10-06 01:20:27 +0000423 assert(Order.size() == Visited.size() && Order.size() == DAG.allnodes_size()&&
424 "Error: DAG is cyclic!");
425 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000426
Chris Lattner32fca002005-10-06 01:20:27 +0000427 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
428 SDNode *N = Order[i];
429 switch (getTypeAction(N->getValueType(0))) {
430 default: assert(0 && "Bad type action!");
431 case Legal:
432 LegalizeOp(SDOperand(N, 0));
433 break;
434 case Promote:
435 PromoteOp(SDOperand(N, 0));
436 break;
437 case Expand: {
438 SDOperand X, Y;
439 ExpandOp(SDOperand(N, 0), X, Y);
440 break;
441 }
442 }
443 }
444
445 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000446 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000447 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
448 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000449
450 ExpandedNodes.clear();
451 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000452 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000453
454 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000455 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000456}
457
458SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000459 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000460 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000461 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000462
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000464 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000465 if (Node->getNumValues() > 1) {
466 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
467 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000468 case Legal: break; // Nothing to do.
469 case Expand: {
470 SDOperand T1, T2;
471 ExpandOp(Op.getValue(i), T1, T2);
472 assert(LegalizedNodes.count(Op) &&
473 "Expansion didn't add legal operands!");
474 return LegalizedNodes[Op];
475 }
476 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000477 PromoteOp(Op.getValue(i));
478 assert(LegalizedNodes.count(Op) &&
479 "Expansion didn't add legal operands!");
480 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000481 }
482 }
483
Chris Lattner45982da2005-05-12 16:53:42 +0000484 // Note that LegalizeOp may be reentered even from single-use nodes, which
485 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000486 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
487 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000488
Nate Begeman9373a812005-08-10 20:51:12 +0000489 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490
491 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000492
493 switch (Node->getOpcode()) {
494 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000495 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
496 // If this is a target node, legalize it by legalizing the operands then
497 // passing it through.
498 std::vector<SDOperand> Ops;
499 bool Changed = false;
500 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
501 Ops.push_back(LegalizeOp(Node->getOperand(i)));
502 Changed = Changed || Node->getOperand(i) != Ops.back();
503 }
504 if (Changed)
505 if (Node->getNumValues() == 1)
506 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
507 else {
508 std::vector<MVT::ValueType> VTs(Node->value_begin(),
509 Node->value_end());
510 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
511 }
512
513 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
514 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
515 return Result.getValue(Op.ResNo);
516 }
517 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000518 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
519 assert(0 && "Do not know how to legalize this operator!");
520 abort();
521 case ISD::EntryToken:
522 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000523 case ISD::TargetFrameIndex:
524 case ISD::Register:
525 case ISD::TargetConstant:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000526 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000527 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000528 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000529 case ISD::BasicBlock:
530 case ISD::CONDCODE:
531 case ISD::VALUETYPE:
532 case ISD::SRCVALUE:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000533 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000534 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000535 case ISD::AssertSext:
536 case ISD::AssertZext:
537 Tmp1 = LegalizeOp(Node->getOperand(0));
538 if (Tmp1 != Node->getOperand(0))
539 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
540 Node->getOperand(1));
541 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000542 case ISD::CopyFromReg:
543 Tmp1 = LegalizeOp(Node->getOperand(0));
544 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000545 Result = DAG.getCopyFromReg(Tmp1,
546 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
547 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000548 else
549 Result = Op.getValue(0);
550
551 // Since CopyFromReg produces two values, make sure to remember that we
552 // legalized both of them.
553 AddLegalizedOperand(Op.getValue(0), Result);
554 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
555 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000556 case ISD::ImplicitDef:
557 Tmp1 = LegalizeOp(Node->getOperand(0));
558 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000559 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
560 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000561 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000562 case ISD::UNDEF: {
563 MVT::ValueType VT = Op.getValueType();
564 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000565 default: assert(0 && "This action is not supported yet!");
566 case TargetLowering::Expand:
567 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000568 if (MVT::isInteger(VT))
569 Result = DAG.getConstant(0, VT);
570 else if (MVT::isFloatingPoint(VT))
571 Result = DAG.getConstantFP(0, VT);
572 else
573 assert(0 && "Unknown value type!");
574 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000575 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000576 break;
577 }
578 break;
579 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000580 case ISD::Constant:
581 // We know we don't need to expand constants here, constants only have one
582 // value and we check that it is fine above.
583
584 // FIXME: Maybe we should handle things like targets that don't support full
585 // 32-bit immediates?
586 break;
587 case ISD::ConstantFP: {
588 // Spill FP immediates to the constant pool if the target cannot directly
589 // codegen them. Targets often have some immediate values that can be
590 // efficiently generated into an FP register without a load. We explicitly
591 // leave these constants as ConstantFP nodes for the target to deal with.
592
593 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
594
595 // Check to see if this FP immediate is already legal.
596 bool isLegal = false;
597 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
598 E = TLI.legal_fpimm_end(); I != E; ++I)
599 if (CFP->isExactlyValue(*I)) {
600 isLegal = true;
601 break;
602 }
603
604 if (!isLegal) {
605 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000606 bool Extend = false;
607
608 // If a FP immediate is precise when represented as a float, we put it
609 // into the constant pool as a float, even if it's is statically typed
610 // as a double.
611 MVT::ValueType VT = CFP->getValueType(0);
612 bool isDouble = VT == MVT::f64;
613 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
614 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000615 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
616 // Only do this if the target has a native EXTLOAD instruction from
617 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000618 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000619 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
620 VT = MVT::f32;
621 Extend = true;
622 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000623
Chris Lattner5839bf22005-08-26 17:15:30 +0000624 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000625 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000626 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
627 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000628 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000629 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
630 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000631 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000632 }
633 break;
634 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000635 case ISD::TokenFactor: {
636 std::vector<SDOperand> Ops;
637 bool Changed = false;
638 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000639 SDOperand Op = Node->getOperand(i);
640 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000641 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000642 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
643 Changed = true;
644 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
645 Ops.push_back(LegalizeOp(Op.getOperand(j)));
646 } else {
647 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
648 Changed |= Ops[i] != Op;
649 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000650 }
651 if (Changed)
652 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
653 break;
654 }
655
Chris Lattner16cd04d2005-05-12 23:24:06 +0000656 case ISD::CALLSEQ_START:
657 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000659 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000660 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000661 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000662 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000663
Chris Lattner16cd04d2005-05-12 23:24:06 +0000664 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000665 // nodes are treated specially and are mutated in place. This makes the dag
666 // legalization process more efficient and also makes libcall insertion
667 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000668 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000669 case ISD::DYNAMIC_STACKALLOC:
670 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
671 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
672 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
673 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000674 Tmp3 != Node->getOperand(2)) {
675 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
676 std::vector<SDOperand> Ops;
677 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
678 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
679 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000680 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000681
682 // Since this op produces two values, make sure to remember that we
683 // legalized both of them.
684 AddLegalizedOperand(SDOperand(Node, 0), Result);
685 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
686 return Result.getValue(Op.ResNo);
687
Chris Lattnerd71c0412005-05-13 18:43:43 +0000688 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000689 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000690 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
691 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000692
693 bool Changed = false;
694 std::vector<SDOperand> Ops;
695 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
696 Ops.push_back(LegalizeOp(Node->getOperand(i)));
697 Changed |= Ops.back() != Node->getOperand(i);
698 }
699
700 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000701 std::vector<MVT::ValueType> RetTyVTs;
702 RetTyVTs.reserve(Node->getNumValues());
703 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000704 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000705 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
706 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000707 } else {
708 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000709 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000710 // Since calls produce multiple values, make sure to remember that we
711 // legalized all of them.
712 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
713 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
714 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000715 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000716 case ISD::BR:
717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
718 if (Tmp1 != Node->getOperand(0))
719 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
720 break;
721
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000722 case ISD::BRCOND:
723 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000724
Chris Lattner47e92232005-01-18 19:27:06 +0000725 switch (getTypeAction(Node->getOperand(1).getValueType())) {
726 case Expand: assert(0 && "It's impossible to expand bools");
727 case Legal:
728 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
729 break;
730 case Promote:
731 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
732 break;
733 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000734
735 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
736 default: assert(0 && "This action is not supported yet!");
737 case TargetLowering::Expand:
738 // Expand brcond's setcc into its constituent parts and create a BR_CC
739 // Node.
740 if (Tmp2.getOpcode() == ISD::SETCC) {
741 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
742 Tmp2.getOperand(0), Tmp2.getOperand(1),
743 Node->getOperand(2));
744 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000745 // Make sure the condition is either zero or one. It may have been
746 // promoted from something else.
747 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
748
Nate Begeman7cbd5252005-08-16 19:49:35 +0000749 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
750 DAG.getCondCode(ISD::SETNE), Tmp2,
751 DAG.getConstant(0, Tmp2.getValueType()),
752 Node->getOperand(2));
753 }
754 break;
755 case TargetLowering::Legal:
756 // Basic block destination (Op#2) is always legal.
757 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
758 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
759 Node->getOperand(2));
760 break;
761 }
762 break;
763 case ISD::BR_CC:
764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
765
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000766 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000767 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
768 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
769 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
770 Tmp3 != Node->getOperand(3)) {
771 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
772 Tmp2, Tmp3, Node->getOperand(4));
773 }
774 break;
775 } else {
776 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
777 Node->getOperand(2), // LHS
778 Node->getOperand(3), // RHS
779 Node->getOperand(1)));
780 // If we get a SETCC back from legalizing the SETCC node we just
781 // created, then use its LHS, RHS, and CC directly in creating a new
782 // node. Otherwise, select between the true and false value based on
783 // comparing the result of the legalized with zero.
784 if (Tmp2.getOpcode() == ISD::SETCC) {
785 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
786 Tmp2.getOperand(0), Tmp2.getOperand(1),
787 Node->getOperand(4));
788 } else {
789 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
790 DAG.getCondCode(ISD::SETNE),
791 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
792 Node->getOperand(4));
793 }
794 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000795 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000796 case ISD::BRCONDTWOWAY:
797 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
798 switch (getTypeAction(Node->getOperand(1).getValueType())) {
799 case Expand: assert(0 && "It's impossible to expand bools");
800 case Legal:
801 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
802 break;
803 case Promote:
804 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
805 break;
806 }
807 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
808 // pair.
809 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
810 case TargetLowering::Promote:
811 default: assert(0 && "This action is not supported yet!");
812 case TargetLowering::Legal:
813 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
814 std::vector<SDOperand> Ops;
815 Ops.push_back(Tmp1);
816 Ops.push_back(Tmp2);
817 Ops.push_back(Node->getOperand(2));
818 Ops.push_back(Node->getOperand(3));
819 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
820 }
821 break;
822 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000823 // If BRTWOWAY_CC is legal for this target, then simply expand this node
824 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
825 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000826 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000827 if (Tmp2.getOpcode() == ISD::SETCC) {
828 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
829 Tmp2.getOperand(0), Tmp2.getOperand(1),
830 Node->getOperand(2), Node->getOperand(3));
831 } else {
832 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
833 DAG.getConstant(0, Tmp2.getValueType()),
834 Node->getOperand(2), Node->getOperand(3));
835 }
836 } else {
837 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000838 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000839 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
840 }
Chris Lattner411e8882005-04-09 03:30:19 +0000841 break;
842 }
843 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000844 case ISD::BRTWOWAY_CC:
845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000846 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000847 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
848 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
849 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
850 Tmp3 != Node->getOperand(3)) {
851 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
852 Node->getOperand(4), Node->getOperand(5));
853 }
854 break;
855 } else {
856 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
857 Node->getOperand(2), // LHS
858 Node->getOperand(3), // RHS
859 Node->getOperand(1)));
860 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
861 // pair.
862 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
863 default: assert(0 && "This action is not supported yet!");
864 case TargetLowering::Legal:
865 // If we get a SETCC back from legalizing the SETCC node we just
866 // created, then use its LHS, RHS, and CC directly in creating a new
867 // node. Otherwise, select between the true and false value based on
868 // comparing the result of the legalized with zero.
869 if (Tmp2.getOpcode() == ISD::SETCC) {
870 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
871 Tmp2.getOperand(0), Tmp2.getOperand(1),
872 Node->getOperand(4), Node->getOperand(5));
873 } else {
874 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
875 DAG.getConstant(0, Tmp2.getValueType()),
876 Node->getOperand(4), Node->getOperand(5));
877 }
878 break;
879 case TargetLowering::Expand:
880 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
881 Node->getOperand(4));
882 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
883 break;
884 }
885 }
886 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000887 case ISD::LOAD:
888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
889 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000890
Chris Lattner3e928bb2005-01-07 07:47:09 +0000891 if (Tmp1 != Node->getOperand(0) ||
892 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000893 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
894 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000895 else
896 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000897
Chris Lattner8afc48e2005-01-07 22:28:47 +0000898 // Since loads produce two values, make sure to remember that we legalized
899 // both of them.
900 AddLegalizedOperand(SDOperand(Node, 0), Result);
901 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
902 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000903
Chris Lattner0f69b292005-01-15 06:18:18 +0000904 case ISD::EXTLOAD:
905 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000906 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000907 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
908 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000909
Chris Lattner5f056bf2005-07-10 01:55:33 +0000910 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000911 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000912 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000913 case TargetLowering::Promote:
914 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000915 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
916 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000917 // Since loads produce two values, make sure to remember that we legalized
918 // both of them.
919 AddLegalizedOperand(SDOperand(Node, 0), Result);
920 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
921 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000922
Chris Lattner01ff7212005-04-10 22:54:25 +0000923 case TargetLowering::Legal:
924 if (Tmp1 != Node->getOperand(0) ||
925 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000926 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
927 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000928 else
929 Result = SDOperand(Node, 0);
930
931 // Since loads produce two values, make sure to remember that we legalized
932 // both of them.
933 AddLegalizedOperand(SDOperand(Node, 0), Result);
934 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
935 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000936 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000937 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
938 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
939 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000940 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000941 if (Op.ResNo)
942 return Load.getValue(1);
943 return Result;
944 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000945 assert(Node->getOpcode() != ISD::EXTLOAD &&
946 "EXTLOAD should always be supported!");
947 // Turn the unsupported load into an EXTLOAD followed by an explicit
948 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000949 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
950 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000951 SDOperand ValRes;
952 if (Node->getOpcode() == ISD::SEXTLOAD)
953 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000954 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000955 else
956 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000957 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
958 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
959 if (Op.ResNo)
960 return Result.getValue(1);
961 return ValRes;
962 }
963 assert(0 && "Unreachable");
964 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000965 case ISD::EXTRACT_ELEMENT:
966 // Get both the low and high parts.
967 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
968 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
969 Result = Tmp2; // 1 -> Hi
970 else
971 Result = Tmp1; // 0 -> Lo
972 break;
973
974 case ISD::CopyToReg:
975 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000976
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000977 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000978 "Register type must be legal!");
979 // Legalize the incoming value (must be legal).
980 Tmp2 = LegalizeOp(Node->getOperand(2));
981 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
982 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
983 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000984 break;
985
986 case ISD::RET:
987 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
988 switch (Node->getNumOperands()) {
989 case 2: // ret val
990 switch (getTypeAction(Node->getOperand(1).getValueType())) {
991 case Legal:
992 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000993 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000994 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
995 break;
996 case Expand: {
997 SDOperand Lo, Hi;
998 ExpandOp(Node->getOperand(1), Lo, Hi);
999 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001000 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001001 }
1002 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001003 Tmp2 = PromoteOp(Node->getOperand(1));
1004 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1005 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001006 }
1007 break;
1008 case 1: // ret void
1009 if (Tmp1 != Node->getOperand(0))
1010 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1011 break;
1012 default: { // ret <values>
1013 std::vector<SDOperand> NewValues;
1014 NewValues.push_back(Tmp1);
1015 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1016 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1017 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001018 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001019 break;
1020 case Expand: {
1021 SDOperand Lo, Hi;
1022 ExpandOp(Node->getOperand(i), Lo, Hi);
1023 NewValues.push_back(Lo);
1024 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001025 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001026 }
1027 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001028 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001029 }
1030 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1031 break;
1032 }
1033 }
1034 break;
1035 case ISD::STORE:
1036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1037 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1038
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001039 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001040 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001041 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001042 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001043 DAG.getConstant(FloatToBits(CFP->getValue()),
1044 MVT::i32),
1045 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001046 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001047 } else {
1048 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001049 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001050 DAG.getConstant(DoubleToBits(CFP->getValue()),
1051 MVT::i64),
1052 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001053 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001054 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001055 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001056 }
1057
Chris Lattner3e928bb2005-01-07 07:47:09 +00001058 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1059 case Legal: {
1060 SDOperand Val = LegalizeOp(Node->getOperand(1));
1061 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1062 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001063 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1064 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001065 break;
1066 }
1067 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001068 // Truncate the value and store the result.
1069 Tmp3 = PromoteOp(Node->getOperand(1));
1070 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001071 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001072 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001073 break;
1074
Chris Lattner3e928bb2005-01-07 07:47:09 +00001075 case Expand:
1076 SDOperand Lo, Hi;
1077 ExpandOp(Node->getOperand(1), Lo, Hi);
1078
1079 if (!TLI.isLittleEndian())
1080 std::swap(Lo, Hi);
1081
Chris Lattneredb1add2005-05-11 04:51:16 +00001082 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1083 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001084 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001085 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1086 getIntPtrConstant(IncrementSize));
1087 assert(isTypeLegal(Tmp2.getValueType()) &&
1088 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001089 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001090 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1091 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001092 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1093 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001094 }
1095 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001096 case ISD::PCMARKER:
1097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001098 if (Tmp1 != Node->getOperand(0))
1099 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001100 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001101 case ISD::TRUNCSTORE:
1102 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1103 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1104
1105 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1106 case Legal:
1107 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001108
1109 // The only promote case we handle is TRUNCSTORE:i1 X into
1110 // -> TRUNCSTORE:i8 (and X, 1)
1111 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1112 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1113 TargetLowering::Promote) {
1114 // Promote the bool to a mask then store.
1115 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1116 DAG.getConstant(1, Tmp2.getValueType()));
1117 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1118 Node->getOperand(3), DAG.getValueType(MVT::i8));
1119
1120 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1121 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001122 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001123 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001124 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001125 break;
1126 case Promote:
1127 case Expand:
1128 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1129 }
1130 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001131 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001132 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1133 case Expand: assert(0 && "It's impossible to expand bools");
1134 case Legal:
1135 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1136 break;
1137 case Promote:
1138 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1139 break;
1140 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001141 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001142 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001143
Nate Begemanb942a3d2005-08-23 04:29:48 +00001144 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001145 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001146 case TargetLowering::Expand:
1147 if (Tmp1.getOpcode() == ISD::SETCC) {
1148 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1149 Tmp2, Tmp3,
1150 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1151 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001152 // Make sure the condition is either zero or one. It may have been
1153 // promoted from something else.
1154 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001155 Result = DAG.getSelectCC(Tmp1,
1156 DAG.getConstant(0, Tmp1.getValueType()),
1157 Tmp2, Tmp3, ISD::SETNE);
1158 }
1159 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001160 case TargetLowering::Legal:
1161 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1162 Tmp3 != Node->getOperand(2))
1163 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1164 Tmp1, Tmp2, Tmp3);
1165 break;
1166 case TargetLowering::Promote: {
1167 MVT::ValueType NVT =
1168 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1169 unsigned ExtOp, TruncOp;
1170 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001171 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001172 TruncOp = ISD::TRUNCATE;
1173 } else {
1174 ExtOp = ISD::FP_EXTEND;
1175 TruncOp = ISD::FP_ROUND;
1176 }
1177 // Promote each of the values to the new type.
1178 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1179 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1180 // Perform the larger operation, then round down.
1181 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1182 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1183 break;
1184 }
1185 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001186 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001187 case ISD::SELECT_CC:
1188 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1189 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1190
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001191 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001192 // Everything is legal, see if we should expand this op or something.
1193 switch (TLI.getOperationAction(ISD::SELECT_CC,
1194 Node->getOperand(0).getValueType())) {
1195 default: assert(0 && "This action is not supported yet!");
1196 case TargetLowering::Custom: {
1197 SDOperand Tmp =
1198 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1199 Node->getOperand(0),
1200 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001201 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001202 if (Tmp.Val) {
1203 Result = LegalizeOp(Tmp);
1204 break;
1205 }
1206 } // FALLTHROUGH if the target can't lower this operation after all.
1207 case TargetLowering::Legal:
1208 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1209 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1210 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1211 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1212 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1213 Tmp3, Tmp4, Node->getOperand(4));
1214 }
1215 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001216 }
1217 break;
1218 } else {
1219 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1220 Node->getOperand(0), // LHS
1221 Node->getOperand(1), // RHS
1222 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001223 // If we get a SETCC back from legalizing the SETCC node we just
1224 // created, then use its LHS, RHS, and CC directly in creating a new
1225 // node. Otherwise, select between the true and false value based on
1226 // comparing the result of the legalized with zero.
1227 if (Tmp1.getOpcode() == ISD::SETCC) {
1228 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1229 Tmp1.getOperand(0), Tmp1.getOperand(1),
1230 Tmp3, Tmp4, Tmp1.getOperand(2));
1231 } else {
1232 Result = DAG.getSelectCC(Tmp1,
1233 DAG.getConstant(0, Tmp1.getValueType()),
1234 Tmp3, Tmp4, ISD::SETNE);
1235 }
Nate Begeman9373a812005-08-10 20:51:12 +00001236 }
1237 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001238 case ISD::SETCC:
1239 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1240 case Legal:
1241 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1242 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001243 break;
1244 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001245 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1246 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1247
1248 // If this is an FP compare, the operands have already been extended.
1249 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1250 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001251 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001252
1253 // Otherwise, we have to insert explicit sign or zero extends. Note
1254 // that we could insert sign extends for ALL conditions, but zero extend
1255 // is cheaper on many machines (an AND instead of two shifts), so prefer
1256 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001257 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001258 default: assert(0 && "Unknown integer comparison!");
1259 case ISD::SETEQ:
1260 case ISD::SETNE:
1261 case ISD::SETUGE:
1262 case ISD::SETUGT:
1263 case ISD::SETULE:
1264 case ISD::SETULT:
1265 // ALL of these operations will work if we either sign or zero extend
1266 // the operands (including the unsigned comparisons!). Zero extend is
1267 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001268 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1269 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001270 break;
1271 case ISD::SETGE:
1272 case ISD::SETGT:
1273 case ISD::SETLT:
1274 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001275 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1276 DAG.getValueType(VT));
1277 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1278 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001279 break;
1280 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001281 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001282 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001283 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001284 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1285 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1286 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001287 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001288 case ISD::SETEQ:
1289 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001290 if (RHSLo == RHSHi)
1291 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1292 if (RHSCST->isAllOnesValue()) {
1293 // Comparison to -1.
1294 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001295 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001296 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001297 }
1298
Chris Lattner3e928bb2005-01-07 07:47:09 +00001299 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1300 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1301 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001302 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001303 break;
1304 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001305 // If this is a comparison of the sign bit, just look at the top part.
1306 // X > -1, x < 0
1307 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001308 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001309 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001310 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001311 (CST->isAllOnesValue()))) { // X > -1
1312 Tmp1 = LHSHi;
1313 Tmp2 = RHSHi;
1314 break;
1315 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001316
Chris Lattner3e928bb2005-01-07 07:47:09 +00001317 // FIXME: This generated code sucks.
1318 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001319 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001320 default: assert(0 && "Unknown integer setcc!");
1321 case ISD::SETLT:
1322 case ISD::SETULT: LowCC = ISD::SETULT; break;
1323 case ISD::SETGT:
1324 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1325 case ISD::SETLE:
1326 case ISD::SETULE: LowCC = ISD::SETULE; break;
1327 case ISD::SETGE:
1328 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1329 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001330
Chris Lattner3e928bb2005-01-07 07:47:09 +00001331 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1332 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1333 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1334
1335 // NOTE: on targets without efficient SELECT of bools, we can always use
1336 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001337 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1338 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1339 Node->getOperand(2));
1340 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001341 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1342 Result, Tmp1, Tmp2));
1343 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001344 }
1345 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001346
1347 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1348 default:
1349 assert(0 && "Cannot handle this action for SETCC yet!");
1350 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001351 case TargetLowering::Promote:
1352 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1353 Node->getOperand(2));
1354 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001355 case TargetLowering::Legal:
1356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1357 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1358 Node->getOperand(2));
1359 break;
1360 case TargetLowering::Expand:
1361 // Expand a setcc node into a select_cc of the same condition, lhs, and
1362 // rhs that selects between const 1 (true) and const 0 (false).
1363 MVT::ValueType VT = Node->getValueType(0);
1364 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1365 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1366 Node->getOperand(2));
1367 Result = LegalizeOp(Result);
1368 break;
1369 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001370 break;
1371
Chris Lattnere1bd8222005-01-11 05:57:22 +00001372 case ISD::MEMSET:
1373 case ISD::MEMCPY:
1374 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1377
1378 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1379 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1380 case Expand: assert(0 && "Cannot expand a byte!");
1381 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001382 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001383 break;
1384 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001385 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001386 break;
1387 }
1388 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001389 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001390 }
Chris Lattner272455b2005-02-02 03:44:41 +00001391
1392 SDOperand Tmp4;
1393 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001394 case Expand: {
1395 // Length is too big, just take the lo-part of the length.
1396 SDOperand HiPart;
1397 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1398 break;
1399 }
Chris Lattnere5605212005-01-28 22:29:18 +00001400 case Legal:
1401 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001402 break;
1403 case Promote:
1404 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001405 break;
1406 }
1407
1408 SDOperand Tmp5;
1409 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1410 case Expand: assert(0 && "Cannot expand this yet!");
1411 case Legal:
1412 Tmp5 = LegalizeOp(Node->getOperand(4));
1413 break;
1414 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001415 Tmp5 = PromoteOp(Node->getOperand(4));
1416 break;
1417 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001418
1419 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1420 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001421 case TargetLowering::Custom: {
1422 SDOperand Tmp =
1423 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1424 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1425 if (Tmp.Val) {
1426 Result = LegalizeOp(Tmp);
1427 break;
1428 }
1429 // FALLTHROUGH if the target thinks it is legal.
1430 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001431 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001432 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1433 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1434 Tmp5 != Node->getOperand(4)) {
1435 std::vector<SDOperand> Ops;
1436 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1437 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1438 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1439 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001440 break;
1441 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001442 // Otherwise, the target does not support this operation. Lower the
1443 // operation to an explicit libcall as appropriate.
1444 MVT::ValueType IntPtr = TLI.getPointerTy();
1445 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1446 std::vector<std::pair<SDOperand, const Type*> > Args;
1447
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001448 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001449 if (Node->getOpcode() == ISD::MEMSET) {
1450 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1451 // Extend the ubyte argument to be an int value for the call.
1452 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1453 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1454 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1455
1456 FnName = "memset";
1457 } else if (Node->getOpcode() == ISD::MEMCPY ||
1458 Node->getOpcode() == ISD::MEMMOVE) {
1459 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1460 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1461 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1462 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1463 } else {
1464 assert(0 && "Unknown op!");
1465 }
Chris Lattner45982da2005-05-12 16:53:42 +00001466
Chris Lattnere1bd8222005-01-11 05:57:22 +00001467 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001468 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001469 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001470 Result = CallResult.second;
1471 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001472 break;
1473 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001474 }
1475 break;
1476 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001477
1478 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001479 Tmp1 = LegalizeOp(Node->getOperand(0));
1480 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001481
Chris Lattner3e011362005-05-14 07:45:46 +00001482 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1483 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1484 std::vector<SDOperand> Ops;
1485 Ops.push_back(Tmp1);
1486 Ops.push_back(Tmp2);
1487 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1488 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001489 Result = SDOperand(Node, 0);
1490 // Since these produce two values, make sure to remember that we legalized
1491 // both of them.
1492 AddLegalizedOperand(SDOperand(Node, 0), Result);
1493 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1494 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001495 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001496 Tmp1 = LegalizeOp(Node->getOperand(0));
1497 Tmp2 = LegalizeOp(Node->getOperand(1));
1498 Tmp3 = LegalizeOp(Node->getOperand(2));
1499 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1500 Tmp3 != Node->getOperand(2))
1501 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1502 break;
1503
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001504 case ISD::READIO:
1505 Tmp1 = LegalizeOp(Node->getOperand(0));
1506 Tmp2 = LegalizeOp(Node->getOperand(1));
1507
1508 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1509 case TargetLowering::Custom:
1510 default: assert(0 && "This action not implemented for this operation!");
1511 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001512 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1513 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1514 std::vector<SDOperand> Ops;
1515 Ops.push_back(Tmp1);
1516 Ops.push_back(Tmp2);
1517 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1518 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001519 Result = SDOperand(Node, 0);
1520 break;
1521 case TargetLowering::Expand:
1522 // Replace this with a load from memory.
1523 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1524 Node->getOperand(1), DAG.getSrcValue(NULL));
1525 Result = LegalizeOp(Result);
1526 break;
1527 }
1528
1529 // Since these produce two values, make sure to remember that we legalized
1530 // both of them.
1531 AddLegalizedOperand(SDOperand(Node, 0), Result);
1532 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1533 return Result.getValue(Op.ResNo);
1534
1535 case ISD::WRITEIO:
1536 Tmp1 = LegalizeOp(Node->getOperand(0));
1537 Tmp2 = LegalizeOp(Node->getOperand(1));
1538 Tmp3 = LegalizeOp(Node->getOperand(2));
1539
1540 switch (TLI.getOperationAction(Node->getOpcode(),
1541 Node->getOperand(1).getValueType())) {
1542 case TargetLowering::Custom:
1543 default: assert(0 && "This action not implemented for this operation!");
1544 case TargetLowering::Legal:
1545 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1546 Tmp3 != Node->getOperand(2))
1547 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1548 break;
1549 case TargetLowering::Expand:
1550 // Replace this with a store to memory.
1551 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1552 Node->getOperand(1), Node->getOperand(2),
1553 DAG.getSrcValue(NULL));
1554 Result = LegalizeOp(Result);
1555 break;
1556 }
1557 break;
1558
Chris Lattner84f67882005-01-20 18:52:28 +00001559 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001560 case ISD::SUB_PARTS:
1561 case ISD::SHL_PARTS:
1562 case ISD::SRA_PARTS:
1563 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001564 std::vector<SDOperand> Ops;
1565 bool Changed = false;
1566 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1567 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1568 Changed |= Ops.back() != Node->getOperand(i);
1569 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001570 if (Changed) {
1571 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1572 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1573 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001574
1575 // Since these produce multiple values, make sure to remember that we
1576 // legalized all of them.
1577 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1578 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1579 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001580 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001581
1582 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001583 case ISD::ADD:
1584 case ISD::SUB:
1585 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001586 case ISD::MULHS:
1587 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001588 case ISD::UDIV:
1589 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001590 case ISD::AND:
1591 case ISD::OR:
1592 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001593 case ISD::SHL:
1594 case ISD::SRL:
1595 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001596 case ISD::FADD:
1597 case ISD::FSUB:
1598 case ISD::FMUL:
1599 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001600 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001601 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1602 case Expand: assert(0 && "Not possible");
1603 case Legal:
1604 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1605 break;
1606 case Promote:
1607 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1608 break;
1609 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001610 if (Tmp1 != Node->getOperand(0) ||
1611 Tmp2 != Node->getOperand(1))
1612 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1613 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001614
Nate Begemanc105e192005-04-06 00:23:54 +00001615 case ISD::UREM:
1616 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001617 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001618 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1619 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1620 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1621 case TargetLowering::Legal:
1622 if (Tmp1 != Node->getOperand(0) ||
1623 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001624 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001625 Tmp2);
1626 break;
1627 case TargetLowering::Promote:
1628 case TargetLowering::Custom:
1629 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001630 case TargetLowering::Expand:
1631 if (MVT::isInteger(Node->getValueType(0))) {
1632 MVT::ValueType VT = Node->getValueType(0);
1633 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1634 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1635 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1636 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1637 } else {
1638 // Floating point mod -> fmod libcall.
1639 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1640 SDOperand Dummy;
1641 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001642 }
1643 break;
1644 }
1645 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001646
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001647 case ISD::CTPOP:
1648 case ISD::CTTZ:
1649 case ISD::CTLZ:
1650 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1651 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1652 case TargetLowering::Legal:
1653 if (Tmp1 != Node->getOperand(0))
1654 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1655 break;
1656 case TargetLowering::Promote: {
1657 MVT::ValueType OVT = Tmp1.getValueType();
1658 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001659
1660 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001661 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1662 // Perform the larger operation, then subtract if needed.
1663 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1664 switch(Node->getOpcode())
1665 {
1666 case ISD::CTPOP:
1667 Result = Tmp1;
1668 break;
1669 case ISD::CTTZ:
1670 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001671 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1672 DAG.getConstant(getSizeInBits(NVT), NVT),
1673 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001674 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001675 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1676 break;
1677 case ISD::CTLZ:
1678 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001679 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1680 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001681 getSizeInBits(OVT), NVT));
1682 break;
1683 }
1684 break;
1685 }
1686 case TargetLowering::Custom:
1687 assert(0 && "Cannot custom handle this yet!");
1688 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001689 switch(Node->getOpcode())
1690 {
1691 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001692 static const uint64_t mask[6] = {
1693 0x5555555555555555ULL, 0x3333333333333333ULL,
1694 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1695 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1696 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001697 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001698 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1699 unsigned len = getSizeInBits(VT);
1700 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001701 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001702 Tmp2 = DAG.getConstant(mask[i], VT);
1703 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001704 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001705 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1706 DAG.getNode(ISD::AND, VT,
1707 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1708 Tmp2));
1709 }
1710 Result = Tmp1;
1711 break;
1712 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001713 case ISD::CTLZ: {
1714 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001715 x = x | (x >> 1);
1716 x = x | (x >> 2);
1717 ...
1718 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001719 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001720 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001721
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001722 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1723 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001724 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1725 unsigned len = getSizeInBits(VT);
1726 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1727 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001728 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001729 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1730 }
1731 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001732 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001733 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001734 }
1735 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001736 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001737 // unless the target has ctlz but not ctpop, in which case we use:
1738 // { return 32 - nlz(~x & (x-1)); }
1739 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001740 MVT::ValueType VT = Tmp1.getValueType();
1741 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001742 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001743 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1744 DAG.getNode(ISD::SUB, VT, Tmp1,
1745 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001746 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001747 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1748 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001749 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001750 DAG.getConstant(getSizeInBits(VT), VT),
1751 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1752 } else {
1753 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1754 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001755 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001756 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001757 default:
1758 assert(0 && "Cannot expand this yet!");
1759 break;
1760 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001761 break;
1762 }
1763 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001764
Chris Lattner2c8086f2005-04-02 05:00:07 +00001765 // Unary operators
1766 case ISD::FABS:
1767 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001768 case ISD::FSQRT:
1769 case ISD::FSIN:
1770 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001771 Tmp1 = LegalizeOp(Node->getOperand(0));
1772 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1773 case TargetLowering::Legal:
1774 if (Tmp1 != Node->getOperand(0))
1775 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1776 break;
1777 case TargetLowering::Promote:
1778 case TargetLowering::Custom:
1779 assert(0 && "Cannot promote/custom handle this yet!");
1780 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001781 switch(Node->getOpcode()) {
1782 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001783 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1784 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001785 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00001786 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001787 break;
1788 }
1789 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001790 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1791 MVT::ValueType VT = Node->getValueType(0);
1792 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001793 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001794 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1795 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1796 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001797 break;
1798 }
1799 case ISD::FSQRT:
1800 case ISD::FSIN:
1801 case ISD::FCOS: {
1802 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001803 const char *FnName = 0;
1804 switch(Node->getOpcode()) {
1805 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1806 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1807 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1808 default: assert(0 && "Unreachable!");
1809 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001810 SDOperand Dummy;
1811 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001812 break;
1813 }
1814 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001815 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001816 }
1817 break;
1818 }
1819 break;
1820
1821 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001822 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001823 case ISD::UINT_TO_FP: {
1824 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001825 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1826 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001827 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001828 Node->getOperand(0).getValueType())) {
1829 default: assert(0 && "Unknown operation action!");
1830 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001831 Result = ExpandLegalINT_TO_FP(isSigned,
1832 LegalizeOp(Node->getOperand(0)),
1833 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001834 AddLegalizedOperand(Op, Result);
1835 return Result;
1836 case TargetLowering::Promote:
1837 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1838 Node->getValueType(0),
1839 isSigned);
1840 AddLegalizedOperand(Op, Result);
1841 return Result;
1842 case TargetLowering::Legal:
1843 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001844 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001845
Chris Lattner3e928bb2005-01-07 07:47:09 +00001846 Tmp1 = LegalizeOp(Node->getOperand(0));
1847 if (Tmp1 != Node->getOperand(0))
1848 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1849 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001850 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001851 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1852 Node->getValueType(0), Node->getOperand(0));
1853 break;
1854 case Promote:
1855 if (isSigned) {
1856 Result = PromoteOp(Node->getOperand(0));
1857 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1858 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1859 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1860 } else {
1861 Result = PromoteOp(Node->getOperand(0));
1862 Result = DAG.getZeroExtendInReg(Result,
1863 Node->getOperand(0).getValueType());
1864 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001865 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001866 break;
1867 }
1868 break;
1869 }
1870 case ISD::TRUNCATE:
1871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1872 case Legal:
1873 Tmp1 = LegalizeOp(Node->getOperand(0));
1874 if (Tmp1 != Node->getOperand(0))
1875 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1876 break;
1877 case Expand:
1878 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1879
1880 // Since the result is legal, we should just be able to truncate the low
1881 // part of the source.
1882 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1883 break;
1884 case Promote:
1885 Result = PromoteOp(Node->getOperand(0));
1886 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1887 break;
1888 }
1889 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001890
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001891 case ISD::FP_TO_SINT:
1892 case ISD::FP_TO_UINT:
1893 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1894 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001895 Tmp1 = LegalizeOp(Node->getOperand(0));
1896
Chris Lattner1618beb2005-07-29 00:11:56 +00001897 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1898 default: assert(0 && "Unknown operation action!");
1899 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001900 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1901 SDOperand True, False;
1902 MVT::ValueType VT = Node->getOperand(0).getValueType();
1903 MVT::ValueType NVT = Node->getValueType(0);
1904 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1905 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1906 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1907 Node->getOperand(0), Tmp2, ISD::SETLT);
1908 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1909 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00001910 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00001911 Tmp2));
1912 False = DAG.getNode(ISD::XOR, NVT, False,
1913 DAG.getConstant(1ULL << ShiftAmt, NVT));
1914 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001915 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001916 } else {
1917 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1918 }
1919 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001920 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001921 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001922 Node->getOpcode() == ISD::FP_TO_SINT);
1923 AddLegalizedOperand(Op, Result);
1924 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001925 case TargetLowering::Custom: {
1926 SDOperand Tmp =
1927 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1928 Tmp = TLI.LowerOperation(Tmp, DAG);
1929 if (Tmp.Val) {
1930 AddLegalizedOperand(Op, Tmp);
1931 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001932 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001933 } else {
1934 // The target thinks this is legal afterall.
1935 break;
1936 }
1937 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001938 case TargetLowering::Legal:
1939 break;
1940 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001941
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001942 if (Tmp1 != Node->getOperand(0))
1943 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1944 break;
1945 case Expand:
1946 assert(0 && "Shouldn't need to expand other operators here!");
1947 case Promote:
1948 Result = PromoteOp(Node->getOperand(0));
1949 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1950 break;
1951 }
1952 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001953
Chris Lattner13c78e22005-09-02 00:18:10 +00001954 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001955 case ISD::ZERO_EXTEND:
1956 case ISD::SIGN_EXTEND:
1957 case ISD::FP_EXTEND:
1958 case ISD::FP_ROUND:
1959 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1960 case Legal:
1961 Tmp1 = LegalizeOp(Node->getOperand(0));
1962 if (Tmp1 != Node->getOperand(0))
1963 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1964 break;
1965 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001966 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001967
Chris Lattner03c85462005-01-15 05:21:40 +00001968 case Promote:
1969 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001970 case ISD::ANY_EXTEND:
1971 Result = PromoteOp(Node->getOperand(0));
1972 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
1973 break;
Chris Lattner1713e732005-01-16 00:38:00 +00001974 case ISD::ZERO_EXTEND:
1975 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001976 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001977 Result = DAG.getZeroExtendInReg(Result,
1978 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001979 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001980 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001981 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001982 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001983 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001984 Result,
1985 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001986 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001987 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001988 Result = PromoteOp(Node->getOperand(0));
1989 if (Result.getValueType() != Op.getValueType())
1990 // Dynamically dead while we have only 2 FP types.
1991 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1992 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001993 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001994 Result = PromoteOp(Node->getOperand(0));
1995 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1996 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001997 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001998 }
1999 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002000 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002001 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002002 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002003 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002004
2005 // If this operation is not supported, convert it to a shl/shr or load/store
2006 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002007 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2008 default: assert(0 && "This action not supported for this op yet!");
2009 case TargetLowering::Legal:
2010 if (Tmp1 != Node->getOperand(0))
2011 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002012 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002013 break;
2014 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002015 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002016 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002017 // NOTE: we could fall back on load/store here too for targets without
2018 // SAR. However, it is doubtful that any exist.
2019 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2020 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002021 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002022 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2023 Node->getOperand(0), ShiftCst);
2024 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2025 Result, ShiftCst);
2026 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2027 // The only way we can lower this is to turn it into a STORETRUNC,
2028 // EXTLOAD pair, targetting a temporary location (a stack slot).
2029
2030 // NOTE: there is a choice here between constantly creating new stack
2031 // slots and always reusing the same one. We currently always create
2032 // new ones, as reuse may inhibit scheduling.
2033 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2034 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2035 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2036 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002037 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002038 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2039 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2040 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002041 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002042 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002043 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2044 Result, StackSlot, DAG.getSrcValue(NULL),
2045 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002046 } else {
2047 assert(0 && "Unknown op");
2048 }
2049 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002050 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002051 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002052 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002053 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002054 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002055
Chris Lattner45982da2005-05-12 16:53:42 +00002056 // Note that LegalizeOp may be reentered even from single-use nodes, which
2057 // means that we always must cache transformed nodes.
2058 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002059 return Result;
2060}
2061
Chris Lattner8b6fa222005-01-15 22:16:26 +00002062/// PromoteOp - Given an operation that produces a value in an invalid type,
2063/// promote it to compute the value into a larger type. The produced value will
2064/// have the correct bits for the low portion of the register, but no guarantee
2065/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002066SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2067 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002068 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002069 assert(getTypeAction(VT) == Promote &&
2070 "Caller should expand or legalize operands that are not promotable!");
2071 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2072 "Cannot promote to smaller type!");
2073
Chris Lattner03c85462005-01-15 05:21:40 +00002074 SDOperand Tmp1, Tmp2, Tmp3;
2075
2076 SDOperand Result;
2077 SDNode *Node = Op.Val;
2078
Chris Lattner6fdcb252005-09-02 20:32:45 +00002079 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2080 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002081
Chris Lattner0f69b292005-01-15 06:18:18 +00002082 // Promotion needs an optimization step to clean up after it, and is not
2083 // careful to avoid operations the target does not support. Make sure that
2084 // all generated operations are legalized in the next iteration.
2085 NeedsAnotherIteration = true;
2086
Chris Lattner03c85462005-01-15 05:21:40 +00002087 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002088 case ISD::CopyFromReg:
2089 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002090 default:
2091 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2092 assert(0 && "Do not know how to promote this operator!");
2093 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002094 case ISD::UNDEF:
2095 Result = DAG.getNode(ISD::UNDEF, NVT);
2096 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002097 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002098 if (VT != MVT::i1)
2099 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2100 else
2101 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002102 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2103 break;
2104 case ISD::ConstantFP:
2105 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2106 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2107 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002108
Chris Lattner82fbfb62005-01-18 02:59:52 +00002109 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002110 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002111 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2112 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002113 Result = LegalizeOp(Result);
2114 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002115
2116 case ISD::TRUNCATE:
2117 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2118 case Legal:
2119 Result = LegalizeOp(Node->getOperand(0));
2120 assert(Result.getValueType() >= NVT &&
2121 "This truncation doesn't make sense!");
2122 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2123 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2124 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002125 case Promote:
2126 // The truncation is not required, because we don't guarantee anything
2127 // about high bits anyway.
2128 Result = PromoteOp(Node->getOperand(0));
2129 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002130 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002131 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2132 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002133 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002134 }
2135 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002136 case ISD::SIGN_EXTEND:
2137 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002138 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002139 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2140 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2141 case Legal:
2142 // Input is legal? Just do extend all the way to the larger type.
2143 Result = LegalizeOp(Node->getOperand(0));
2144 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2145 break;
2146 case Promote:
2147 // Promote the reg if it's smaller.
2148 Result = PromoteOp(Node->getOperand(0));
2149 // The high bits are not guaranteed to be anything. Insert an extend.
2150 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002151 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002152 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002153 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002154 Result = DAG.getZeroExtendInReg(Result,
2155 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002156 break;
2157 }
2158 break;
2159
2160 case ISD::FP_EXTEND:
2161 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2162 case ISD::FP_ROUND:
2163 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2164 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2165 case Promote: assert(0 && "Unreachable with 2 FP types!");
2166 case Legal:
2167 // Input is legal? Do an FP_ROUND_INREG.
2168 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002169 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2170 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002171 break;
2172 }
2173 break;
2174
2175 case ISD::SINT_TO_FP:
2176 case ISD::UINT_TO_FP:
2177 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2178 case Legal:
2179 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002180 // No extra round required here.
2181 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002182 break;
2183
2184 case Promote:
2185 Result = PromoteOp(Node->getOperand(0));
2186 if (Node->getOpcode() == ISD::SINT_TO_FP)
2187 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002188 Result,
2189 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002190 else
Chris Lattner23993562005-04-13 02:38:47 +00002191 Result = DAG.getZeroExtendInReg(Result,
2192 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002193 // No extra round required here.
2194 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002195 break;
2196 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002197 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2198 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002199 // Round if we cannot tolerate excess precision.
2200 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002201 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2202 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002203 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002204 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002205 break;
2206
2207 case ISD::FP_TO_SINT:
2208 case ISD::FP_TO_UINT:
2209 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2210 case Legal:
2211 Tmp1 = LegalizeOp(Node->getOperand(0));
2212 break;
2213 case Promote:
2214 // The input result is prerounded, so we don't have to do anything
2215 // special.
2216 Tmp1 = PromoteOp(Node->getOperand(0));
2217 break;
2218 case Expand:
2219 assert(0 && "not implemented");
2220 }
Nate Begemand2558e32005-08-14 01:20:53 +00002221 // If we're promoting a UINT to a larger size, check to see if the new node
2222 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2223 // we can use that instead. This allows us to generate better code for
2224 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2225 // legal, such as PowerPC.
2226 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002227 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2228 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begemand2558e32005-08-14 01:20:53 +00002229 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2230 } else {
2231 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2232 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002233 break;
2234
Chris Lattner2c8086f2005-04-02 05:00:07 +00002235 case ISD::FABS:
2236 case ISD::FNEG:
2237 Tmp1 = PromoteOp(Node->getOperand(0));
2238 assert(Tmp1.getValueType() == NVT);
2239 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2240 // NOTE: we do not have to do any extra rounding here for
2241 // NoExcessFPPrecision, because we know the input will have the appropriate
2242 // precision, and these operations don't modify precision at all.
2243 break;
2244
Chris Lattnerda6ba872005-04-28 21:44:33 +00002245 case ISD::FSQRT:
2246 case ISD::FSIN:
2247 case ISD::FCOS:
2248 Tmp1 = PromoteOp(Node->getOperand(0));
2249 assert(Tmp1.getValueType() == NVT);
2250 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2251 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002252 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2253 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002254 break;
2255
Chris Lattner03c85462005-01-15 05:21:40 +00002256 case ISD::AND:
2257 case ISD::OR:
2258 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002259 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002260 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002261 case ISD::MUL:
2262 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002263 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002264 // that too is okay if they are integer operations.
2265 Tmp1 = PromoteOp(Node->getOperand(0));
2266 Tmp2 = PromoteOp(Node->getOperand(1));
2267 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2268 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002269 break;
2270 case ISD::FADD:
2271 case ISD::FSUB:
2272 case ISD::FMUL:
2273 // The input may have strange things in the top bits of the registers, but
2274 // these operations don't care.
2275 Tmp1 = PromoteOp(Node->getOperand(0));
2276 Tmp2 = PromoteOp(Node->getOperand(1));
2277 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2278 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2279
2280 // Floating point operations will give excess precision that we may not be
2281 // able to tolerate. If we DO allow excess precision, just leave it,
2282 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002283 // FIXME: Why would we need to round FP ops more than integer ones?
2284 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002285 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002286 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2287 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002288 break;
2289
Chris Lattner8b6fa222005-01-15 22:16:26 +00002290 case ISD::SDIV:
2291 case ISD::SREM:
2292 // These operators require that their input be sign extended.
2293 Tmp1 = PromoteOp(Node->getOperand(0));
2294 Tmp2 = PromoteOp(Node->getOperand(1));
2295 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002296 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2297 DAG.getValueType(VT));
2298 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2299 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002300 }
2301 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2302
2303 // Perform FP_ROUND: this is probably overly pessimistic.
2304 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002305 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2306 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002307 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002308 case ISD::FDIV:
2309 case ISD::FREM:
2310 // These operators require that their input be fp extended.
2311 Tmp1 = PromoteOp(Node->getOperand(0));
2312 Tmp2 = PromoteOp(Node->getOperand(1));
2313 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2314
2315 // Perform FP_ROUND: this is probably overly pessimistic.
2316 if (NoExcessFPPrecision)
2317 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2318 DAG.getValueType(VT));
2319 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002320
2321 case ISD::UDIV:
2322 case ISD::UREM:
2323 // These operators require that their input be zero extended.
2324 Tmp1 = PromoteOp(Node->getOperand(0));
2325 Tmp2 = PromoteOp(Node->getOperand(1));
2326 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002327 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2328 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002329 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2330 break;
2331
2332 case ISD::SHL:
2333 Tmp1 = PromoteOp(Node->getOperand(0));
2334 Tmp2 = LegalizeOp(Node->getOperand(1));
2335 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2336 break;
2337 case ISD::SRA:
2338 // The input value must be properly sign extended.
2339 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002340 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2341 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002342 Tmp2 = LegalizeOp(Node->getOperand(1));
2343 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2344 break;
2345 case ISD::SRL:
2346 // The input value must be properly zero extended.
2347 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002348 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002349 Tmp2 = LegalizeOp(Node->getOperand(1));
2350 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2351 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002352 case ISD::LOAD:
2353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2354 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002355 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002356 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002357 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2358 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002359 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002360 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2361 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002362
2363 // Remember that we legalized the chain.
2364 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2365 break;
2366 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002367 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2368 case Expand: assert(0 && "It's impossible to expand bools");
2369 case Legal:
2370 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2371 break;
2372 case Promote:
2373 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2374 break;
2375 }
Chris Lattner03c85462005-01-15 05:21:40 +00002376 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2377 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2378 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2379 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002380 case ISD::SELECT_CC:
2381 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2382 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2383 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2384 Node->getOperand(1), Tmp2, Tmp3,
2385 Node->getOperand(4));
2386 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002387 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002388 case ISD::CALL: {
2389 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2390 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2391
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002392 std::vector<SDOperand> Ops;
2393 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2394 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2395
Chris Lattner8ac532c2005-01-16 19:46:48 +00002396 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2397 "Can only promote single result calls");
2398 std::vector<MVT::ValueType> RetTyVTs;
2399 RetTyVTs.reserve(2);
2400 RetTyVTs.push_back(NVT);
2401 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002402 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2403 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002404 Result = SDOperand(NC, 0);
2405
2406 // Insert the new chain mapping.
2407 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2408 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002409 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002410 case ISD::CTPOP:
2411 case ISD::CTTZ:
2412 case ISD::CTLZ:
2413 Tmp1 = Node->getOperand(0);
2414 //Zero extend the argument
2415 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2416 // Perform the larger operation, then subtract if needed.
2417 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2418 switch(Node->getOpcode())
2419 {
2420 case ISD::CTPOP:
2421 Result = Tmp1;
2422 break;
2423 case ISD::CTTZ:
2424 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002425 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002426 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002427 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002428 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2429 break;
2430 case ISD::CTLZ:
2431 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002432 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2433 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002434 getSizeInBits(VT), NVT));
2435 break;
2436 }
2437 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002438 }
2439
2440 assert(Result.Val && "Didn't set a result!");
2441 AddPromotedOperand(Op, Result);
2442 return Result;
2443}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002444
Chris Lattner84f67882005-01-20 18:52:28 +00002445/// ExpandAddSub - Find a clever way to expand this add operation into
2446/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002447void SelectionDAGLegalize::
2448ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2449 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002450 // Expand the subcomponents.
2451 SDOperand LHSL, LHSH, RHSL, RHSH;
2452 ExpandOp(LHS, LHSL, LHSH);
2453 ExpandOp(RHS, RHSL, RHSH);
2454
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002455 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002456 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2457 if (LHSL.getValueType() == MVT::i32) {
2458 SDOperand LowEl;
2459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2460 if (C->getValue() == 0)
2461 LowEl = RHSL;
2462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2463 if (C->getValue() == 0)
2464 LowEl = LHSL;
2465 if (LowEl.Val) {
2466 // Turn this into an add/sub of the high part only.
2467 SDOperand HiEl =
2468 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2469 LowEl.getValueType(), LHSH, RHSH);
2470 Lo = LowEl;
2471 Hi = HiEl;
2472 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002473 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002474 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002475
Chris Lattner84f67882005-01-20 18:52:28 +00002476 std::vector<SDOperand> Ops;
2477 Ops.push_back(LHSL);
2478 Ops.push_back(LHSH);
2479 Ops.push_back(RHSL);
2480 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002481
2482 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2483 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002484 Hi = Lo.getValue(1);
2485}
2486
Chris Lattner5b359c62005-04-02 04:00:59 +00002487void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2488 SDOperand Op, SDOperand Amt,
2489 SDOperand &Lo, SDOperand &Hi) {
2490 // Expand the subcomponents.
2491 SDOperand LHSL, LHSH;
2492 ExpandOp(Op, LHSL, LHSH);
2493
2494 std::vector<SDOperand> Ops;
2495 Ops.push_back(LHSL);
2496 Ops.push_back(LHSH);
2497 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002498 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002499 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002500 Hi = Lo.getValue(1);
2501}
2502
2503
Chris Lattnere34b3962005-01-19 04:19:40 +00002504/// ExpandShift - Try to find a clever way to expand this shift operation out to
2505/// smaller elements. If we can't find a way that is more efficient than a
2506/// libcall on this target, return false. Otherwise, return true with the
2507/// low-parts expanded into Lo and Hi.
2508bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2509 SDOperand &Lo, SDOperand &Hi) {
2510 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2511 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002512
Chris Lattnere34b3962005-01-19 04:19:40 +00002513 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002514 SDOperand ShAmt = LegalizeOp(Amt);
2515 MVT::ValueType ShTy = ShAmt.getValueType();
2516 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2517 unsigned NVTBits = MVT::getSizeInBits(NVT);
2518
2519 // Handle the case when Amt is an immediate. Other cases are currently broken
2520 // and are disabled.
2521 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2522 unsigned Cst = CN->getValue();
2523 // Expand the incoming operand to be shifted, so that we have its parts
2524 SDOperand InL, InH;
2525 ExpandOp(Op, InL, InH);
2526 switch(Opc) {
2527 case ISD::SHL:
2528 if (Cst > VTBits) {
2529 Lo = DAG.getConstant(0, NVT);
2530 Hi = DAG.getConstant(0, NVT);
2531 } else if (Cst > NVTBits) {
2532 Lo = DAG.getConstant(0, NVT);
2533 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002534 } else if (Cst == NVTBits) {
2535 Lo = DAG.getConstant(0, NVT);
2536 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002537 } else {
2538 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2539 Hi = DAG.getNode(ISD::OR, NVT,
2540 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2541 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2542 }
2543 return true;
2544 case ISD::SRL:
2545 if (Cst > VTBits) {
2546 Lo = DAG.getConstant(0, NVT);
2547 Hi = DAG.getConstant(0, NVT);
2548 } else if (Cst > NVTBits) {
2549 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2550 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002551 } else if (Cst == NVTBits) {
2552 Lo = InH;
2553 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002554 } else {
2555 Lo = DAG.getNode(ISD::OR, NVT,
2556 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2557 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2558 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2559 }
2560 return true;
2561 case ISD::SRA:
2562 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002563 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002564 DAG.getConstant(NVTBits-1, ShTy));
2565 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002566 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002567 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002568 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002569 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002570 } else if (Cst == NVTBits) {
2571 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002572 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002573 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002574 } else {
2575 Lo = DAG.getNode(ISD::OR, NVT,
2576 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2577 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2578 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2579 }
2580 return true;
2581 }
2582 }
2583 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2584 // so disable it for now. Currently targets are handling this via SHL_PARTS
2585 // and friends.
2586 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002587
2588 // If we have an efficient select operation (or if the selects will all fold
2589 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002590 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002591 return false;
2592
2593 SDOperand InL, InH;
2594 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002595 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2596 DAG.getConstant(NVTBits, ShTy), ShAmt);
2597
Chris Lattnere5544f82005-01-20 20:29:23 +00002598 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002599 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2600 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002601
Chris Lattnere34b3962005-01-19 04:19:40 +00002602 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2603 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2604 DAG.getConstant(NVTBits-1, ShTy));
2605 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2606 DAG.getConstant(NVTBits-1, ShTy));
2607 }
2608
2609 if (Opc == ISD::SHL) {
2610 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2611 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2612 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002613 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002614
Chris Lattnere34b3962005-01-19 04:19:40 +00002615 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2616 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2617 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002618 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002619 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2620 DAG.getConstant(32, ShTy),
2621 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002622 DAG.getConstant(0, NVT),
2623 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002624 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002625 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002626 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002627 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002628
2629 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002630 if (Opc == ISD::SRA)
2631 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2632 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002633 else
2634 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002635 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002636 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002637 }
2638 return true;
2639}
Chris Lattner77e77a62005-01-21 06:05:23 +00002640
Chris Lattner9530ddc2005-05-13 05:09:11 +00002641/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2642/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002643/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002644static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002645 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002646
Chris Lattner16cd04d2005-05-12 23:24:06 +00002647 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002648 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002649 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002650 Found = Node;
2651 return;
2652 }
2653
2654 // Otherwise, scan the operands of Node to see if any of them is a call.
2655 assert(Node->getNumOperands() != 0 &&
2656 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002657 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002658 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002659
2660 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002661 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002662 Found);
2663}
2664
2665
Chris Lattner9530ddc2005-05-13 05:09:11 +00002666/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2667/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002668/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002669static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2670 std::set<SDNode*> &Visited) {
2671 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2672 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002673
Chris Lattner16cd04d2005-05-12 23:24:06 +00002674 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002675 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002676 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002677 Found = Node;
2678 return;
2679 }
2680
2681 // Otherwise, scan the operands of Node to see if any of them is a call.
2682 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2683 if (UI == E) return;
2684 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002685 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002686
2687 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002688 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002689}
2690
Chris Lattner9530ddc2005-05-13 05:09:11 +00002691/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002692/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002693static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002694 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002695 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002696 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002697 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002698
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002699 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002700 if (TheChain.getValueType() != MVT::Other)
2701 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00002702 if (TheChain.getValueType() != MVT::Other)
2703 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002704
2705 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002706 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002707
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002708 // Make sure to only follow users of our token chain.
2709 SDNode *User = *UI;
2710 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2711 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002712 if (SDNode *Result = FindCallSeqEnd(User))
2713 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002714 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002715 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002716}
2717
Chris Lattner9530ddc2005-05-13 05:09:11 +00002718/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002719/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002720static SDNode *FindCallSeqStart(SDNode *Node) {
2721 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002722 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002723
2724 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2725 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002726 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002727}
2728
2729
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002730/// FindInputOutputChains - If we are replacing an operation with a call we need
2731/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002732/// properly serialize the calls in the block. The returned operand is the
2733/// input chain value for the new call (e.g. the entry node or the previous
2734/// call), and OutChain is set to be the chain node to update to point to the
2735/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002736static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2737 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002738 SDNode *LatestCallSeqStart = Entry.Val;
2739 SDNode *LatestCallSeqEnd = 0;
2740 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2741 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002742
Chris Lattner16cd04d2005-05-12 23:24:06 +00002743 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002744 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002745 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2746 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00002747 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002748 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00002749 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2750 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002751 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00002752 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00002753 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002754
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002755 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002756 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002757 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002758 std::set<SDNode*> Visited;
2759 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002760
Chris Lattner9530ddc2005-05-13 05:09:11 +00002761 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002762 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002763 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002764
Chris Lattner9530ddc2005-05-13 05:09:11 +00002765 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002766}
2767
Jeff Cohen00b168892005-07-27 06:12:32 +00002768/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002769void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2770 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002771 // Nothing to splice it into?
2772 if (OutChain == 0) return;
2773
2774 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2775 //OutChain->dump();
2776
2777 // Form a token factor node merging the old inval and the new inval.
2778 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2779 OutChain->getOperand(0));
2780 // Change the node to refer to the new token.
2781 OutChain->setAdjCallChain(InToken);
2782}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002783
2784
Chris Lattner77e77a62005-01-21 06:05:23 +00002785// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2786// does not fit into a register, return the lo part and set the hi part to the
2787// by-reg argument. If it does fit into a single register, return the result
2788// and leave the Hi part unset.
2789SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2790 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002791 SDNode *OutChain;
2792 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2793 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002794 if (InChain.Val == 0)
2795 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002796
Chris Lattner77e77a62005-01-21 06:05:23 +00002797 TargetLowering::ArgListTy Args;
2798 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2799 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2800 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2801 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2802 }
2803 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002804
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002805 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002806 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002807 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002808 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2809 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002810
Chris Lattner99c25b82005-09-02 20:26:58 +00002811 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002812 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002813 default: assert(0 && "Unknown thing");
2814 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002815 Result = CallInfo.first;
2816 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002817 case Promote:
2818 assert(0 && "Cannot promote this yet!");
2819 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002820 ExpandOp(CallInfo.first, Result, Hi);
2821 CallInfo.second = LegalizeOp(CallInfo.second);
2822 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002823 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002824
2825 SpliceCallInto(CallInfo.second, OutChain);
2826 NeedsAnotherIteration = true;
2827 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002828}
2829
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002830
Chris Lattner77e77a62005-01-21 06:05:23 +00002831/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2832/// destination type is legal.
2833SDOperand SelectionDAGLegalize::
2834ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002835 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002836 assert(getTypeAction(Source.getValueType()) == Expand &&
2837 "This is not an expansion!");
2838 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2839
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002840 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002841 assert(Source.getValueType() == MVT::i64 &&
2842 "This only works for 64-bit -> FP");
2843 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2844 // incoming integer is set. To handle this, we dynamically test to see if
2845 // it is set, and, if so, add a fudge factor.
2846 SDOperand Lo, Hi;
2847 ExpandOp(Source, Lo, Hi);
2848
Chris Lattner66de05b2005-05-13 04:45:13 +00002849 // If this is unsigned, and not supported, first perform the conversion to
2850 // signed, then adjust the result if the sign bit is set.
2851 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2852 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2853
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002854 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2855 DAG.getConstant(0, Hi.getValueType()),
2856 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002857 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2858 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2859 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002860 uint64_t FF = 0x5f800000ULL;
2861 if (TLI.isLittleEndian()) FF <<= 32;
2862 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002863
Chris Lattner5839bf22005-08-26 17:15:30 +00002864 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002865 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2866 SDOperand FudgeInReg;
2867 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002868 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2869 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002870 else {
2871 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002872 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2873 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002874 }
Chris Lattner473a9902005-09-29 06:44:39 +00002875 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002876 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002877
Chris Lattnera88a2602005-05-14 05:33:54 +00002878 // Check to see if the target has a custom way to lower this. If so, use it.
2879 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2880 default: assert(0 && "This action not implemented for this operation!");
2881 case TargetLowering::Legal:
2882 case TargetLowering::Expand:
2883 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002884 case TargetLowering::Custom: {
2885 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2886 Source), DAG);
2887 if (NV.Val)
2888 return LegalizeOp(NV);
2889 break; // The target decided this was legal after all
2890 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002891 }
2892
Chris Lattner13689e22005-05-12 07:00:44 +00002893 // Expand the source, then glue it back together for the call. We must expand
2894 // the source in case it is shared (this pass of legalize must traverse it).
2895 SDOperand SrcLo, SrcHi;
2896 ExpandOp(Source, SrcLo, SrcHi);
2897 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2898
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002899 SDNode *OutChain = 0;
2900 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2901 DAG.getEntryNode());
2902 const char *FnName = 0;
2903 if (DestTy == MVT::f32)
2904 FnName = "__floatdisf";
2905 else {
2906 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2907 FnName = "__floatdidf";
2908 }
2909
Chris Lattner77e77a62005-01-21 06:05:23 +00002910 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2911
2912 TargetLowering::ArgListTy Args;
2913 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002914
Chris Lattner77e77a62005-01-21 06:05:23 +00002915 Args.push_back(std::make_pair(Source, ArgTy));
2916
2917 // We don't care about token chains for libcalls. We just use the entry
2918 // node as our input and ignore the output chain. This allows us to place
2919 // calls wherever we need them to satisfy data dependences.
2920 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002921
2922 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002923 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2924 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002925
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002926 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002927 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002928}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002929
Chris Lattnere34b3962005-01-19 04:19:40 +00002930
2931
Chris Lattner3e928bb2005-01-07 07:47:09 +00002932/// ExpandOp - Expand the specified SDOperand into its two component pieces
2933/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2934/// LegalizeNodes map is filled in for any results that are not expanded, the
2935/// ExpandedNodes map is filled in for any results that are expanded, and the
2936/// Lo/Hi values are returned.
2937void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2938 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002939 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002940 SDNode *Node = Op.Val;
2941 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2942 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2943 assert(MVT::isInteger(NVT) && NVT < VT &&
2944 "Cannot expand to FP value or to larger int value!");
2945
Chris Lattner6fdcb252005-09-02 20:32:45 +00002946 // See if we already expanded it.
2947 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2948 = ExpandedNodes.find(Op);
2949 if (I != ExpandedNodes.end()) {
2950 Lo = I->second.first;
2951 Hi = I->second.second;
2952 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002953 }
2954
Chris Lattner4e6c7462005-01-08 19:27:05 +00002955 // Expanding to multiple registers needs to perform an optimization step, and
2956 // is not careful to avoid operations the target does not support. Make sure
2957 // that all generated operations are legalized in the next iteration.
2958 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002959
Chris Lattner3e928bb2005-01-07 07:47:09 +00002960 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002961 case ISD::CopyFromReg:
2962 assert(0 && "CopyFromReg must be legal!");
2963 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002964 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2965 assert(0 && "Do not know how to expand this operator!");
2966 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002967 case ISD::UNDEF:
2968 Lo = DAG.getNode(ISD::UNDEF, NVT);
2969 Hi = DAG.getNode(ISD::UNDEF, NVT);
2970 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002971 case ISD::Constant: {
2972 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2973 Lo = DAG.getConstant(Cst, NVT);
2974 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2975 break;
2976 }
2977
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002978 case ISD::BUILD_PAIR:
2979 // Legalize both operands. FIXME: in the future we should handle the case
2980 // where the two elements are not legal.
2981 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2982 Lo = LegalizeOp(Node->getOperand(0));
2983 Hi = LegalizeOp(Node->getOperand(1));
2984 break;
2985
Chris Lattneredb1add2005-05-11 04:51:16 +00002986 case ISD::CTPOP:
2987 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002988 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2989 DAG.getNode(ISD::CTPOP, NVT, Lo),
2990 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002991 Hi = DAG.getConstant(0, NVT);
2992 break;
2993
Chris Lattner39a8f332005-05-12 19:05:01 +00002994 case ISD::CTLZ: {
2995 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002996 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002997 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2998 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002999 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3000 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003001 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3002 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3003
3004 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3005 Hi = DAG.getConstant(0, NVT);
3006 break;
3007 }
3008
3009 case ISD::CTTZ: {
3010 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003011 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003012 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3013 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003014 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3015 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003016 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3017 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3018
3019 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3020 Hi = DAG.getConstant(0, NVT);
3021 break;
3022 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003023
Chris Lattner3e928bb2005-01-07 07:47:09 +00003024 case ISD::LOAD: {
3025 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3026 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003027 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003028
3029 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003030 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003031 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3032 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003033 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003034 //are from the same instruction?
3035 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003036
3037 // Build a factor node to remember that this load is independent of the
3038 // other one.
3039 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3040 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003041
Chris Lattner3e928bb2005-01-07 07:47:09 +00003042 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003043 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003044 if (!TLI.isLittleEndian())
3045 std::swap(Lo, Hi);
3046 break;
3047 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003048 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003049 case ISD::CALL: {
3050 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3051 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3052
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003053 bool Changed = false;
3054 std::vector<SDOperand> Ops;
3055 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3056 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3057 Changed |= Ops.back() != Node->getOperand(i);
3058 }
3059
Chris Lattner3e928bb2005-01-07 07:47:09 +00003060 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3061 "Can only expand a call once so far, not i64 -> i16!");
3062
3063 std::vector<MVT::ValueType> RetTyVTs;
3064 RetTyVTs.reserve(3);
3065 RetTyVTs.push_back(NVT);
3066 RetTyVTs.push_back(NVT);
3067 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003068 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3069 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003070 Lo = SDOperand(NC, 0);
3071 Hi = SDOperand(NC, 1);
3072
3073 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003074 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003075 break;
3076 }
3077 case ISD::AND:
3078 case ISD::OR:
3079 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3080 SDOperand LL, LH, RL, RH;
3081 ExpandOp(Node->getOperand(0), LL, LH);
3082 ExpandOp(Node->getOperand(1), RL, RH);
3083 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3084 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3085 break;
3086 }
3087 case ISD::SELECT: {
3088 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003089
3090 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3091 case Expand: assert(0 && "It's impossible to expand bools");
3092 case Legal:
3093 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3094 break;
3095 case Promote:
3096 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3097 break;
3098 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003099 ExpandOp(Node->getOperand(1), LL, LH);
3100 ExpandOp(Node->getOperand(2), RL, RH);
3101 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3102 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3103 break;
3104 }
Nate Begeman9373a812005-08-10 20:51:12 +00003105 case ISD::SELECT_CC: {
3106 SDOperand TL, TH, FL, FH;
3107 ExpandOp(Node->getOperand(2), TL, TH);
3108 ExpandOp(Node->getOperand(3), FL, FH);
3109 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3110 Node->getOperand(1), TL, FL, Node->getOperand(4));
3111 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3112 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003113 Lo = LegalizeOp(Lo);
3114 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003115 break;
3116 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003117 case ISD::ANY_EXTEND: {
3118 SDOperand In;
3119 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3120 case Expand: assert(0 && "expand-expand not implemented yet!");
3121 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3122 case Promote:
3123 In = PromoteOp(Node->getOperand(0));
3124 break;
3125 }
3126
3127 // The low part is any extension of the input (which degenerates to a copy).
3128 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3129 // The high part is undefined.
3130 Hi = DAG.getNode(ISD::UNDEF, NVT);
3131 break;
3132 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003133 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003134 SDOperand In;
3135 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3136 case Expand: assert(0 && "expand-expand not implemented yet!");
3137 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3138 case Promote:
3139 In = PromoteOp(Node->getOperand(0));
3140 // Emit the appropriate sign_extend_inreg to get the value we want.
3141 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003142 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003143 break;
3144 }
3145
Chris Lattner3e928bb2005-01-07 07:47:09 +00003146 // The low part is just a sign extension of the input (which degenerates to
3147 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003148 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003149
Chris Lattner3e928bb2005-01-07 07:47:09 +00003150 // The high part is obtained by SRA'ing all but one of the bits of the lo
3151 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003152 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003153 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3154 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003155 break;
3156 }
Chris Lattner06098e02005-04-03 23:41:52 +00003157 case ISD::ZERO_EXTEND: {
3158 SDOperand In;
3159 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3160 case Expand: assert(0 && "expand-expand not implemented yet!");
3161 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3162 case Promote:
3163 In = PromoteOp(Node->getOperand(0));
3164 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003165 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003166 break;
3167 }
3168
Chris Lattner3e928bb2005-01-07 07:47:09 +00003169 // The low part is just a zero extension of the input (which degenerates to
3170 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003171 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003172
Chris Lattner3e928bb2005-01-07 07:47:09 +00003173 // The high part is just a zero.
3174 Hi = DAG.getConstant(0, NVT);
3175 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003176 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003177 // These operators cannot be expanded directly, emit them as calls to
3178 // library functions.
3179 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003180 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003181 SDOperand Op;
3182 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3183 case Expand: assert(0 && "cannot expand FP!");
3184 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3185 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3186 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003187
Chris Lattnerf20d1832005-07-30 01:40:57 +00003188 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3189
Chris Lattner80a3e942005-07-29 00:33:32 +00003190 // Now that the custom expander is done, expand the result, which is still
3191 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003192 if (Op.Val) {
3193 ExpandOp(Op, Lo, Hi);
3194 break;
3195 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003196 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003197
Chris Lattner4e6c7462005-01-08 19:27:05 +00003198 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003199 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003200 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003201 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003202 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003203
Chris Lattner4e6c7462005-01-08 19:27:05 +00003204 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003205 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3206 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3207 LegalizeOp(Node->getOperand(0)));
3208 // Now that the custom expander is done, expand the result, which is still
3209 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003210 Op = TLI.LowerOperation(Op, DAG);
3211 if (Op.Val) {
3212 ExpandOp(Op, Lo, Hi);
3213 break;
3214 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003215 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003216
Chris Lattner4e6c7462005-01-08 19:27:05 +00003217 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003218 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003219 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003220 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003221 break;
3222
Chris Lattnere34b3962005-01-19 04:19:40 +00003223 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003224 // If the target wants custom lowering, do so.
3225 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3226 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3227 LegalizeOp(Node->getOperand(1)));
3228 Op = TLI.LowerOperation(Op, DAG);
3229 if (Op.Val) {
3230 // Now that the custom expander is done, expand the result, which is
3231 // still VT.
3232 ExpandOp(Op, Lo, Hi);
3233 break;
3234 }
3235 }
3236
Chris Lattnere34b3962005-01-19 04:19:40 +00003237 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003238 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003239 break;
Chris Lattner47599822005-04-02 03:38:53 +00003240
3241 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003242 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003243 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3244 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003245 break;
3246 }
3247
Chris Lattnere34b3962005-01-19 04:19:40 +00003248 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003249 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003250 break;
3251
3252 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003253 // If the target wants custom lowering, do so.
3254 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3255 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3256 LegalizeOp(Node->getOperand(1)));
3257 Op = TLI.LowerOperation(Op, DAG);
3258 if (Op.Val) {
3259 // Now that the custom expander is done, expand the result, which is
3260 // still VT.
3261 ExpandOp(Op, Lo, Hi);
3262 break;
3263 }
3264 }
3265
Chris Lattnere34b3962005-01-19 04:19:40 +00003266 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003267 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003268 break;
Chris Lattner47599822005-04-02 03:38:53 +00003269
3270 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003271 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003272 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3273 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003274 break;
3275 }
3276
Chris Lattnere34b3962005-01-19 04:19:40 +00003277 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003278 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003279 break;
3280 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003281 // If the target wants custom lowering, do so.
3282 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3283 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3284 LegalizeOp(Node->getOperand(1)));
3285 Op = TLI.LowerOperation(Op, DAG);
3286 if (Op.Val) {
3287 // Now that the custom expander is done, expand the result, which is
3288 // still VT.
3289 ExpandOp(Op, Lo, Hi);
3290 break;
3291 }
3292 }
3293
Chris Lattnere34b3962005-01-19 04:19:40 +00003294 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003295 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003296 break;
Chris Lattner47599822005-04-02 03:38:53 +00003297
3298 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003299 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003300 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3301 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003302 break;
3303 }
3304
Chris Lattnere34b3962005-01-19 04:19:40 +00003305 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003306 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003307 break;
3308
Misha Brukmanedf128a2005-04-21 22:36:52 +00003309 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003310 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3311 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003312 break;
3313 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003314 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3315 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003316 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003317 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003318 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003319 SDOperand LL, LH, RL, RH;
3320 ExpandOp(Node->getOperand(0), LL, LH);
3321 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003322 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3323 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3324 // extended the sign bit of the low half through the upper half, and if so
3325 // emit a MULHS instead of the alternate sequence that is valid for any
3326 // i64 x i64 multiply.
3327 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3328 // is RH an extension of the sign bit of RL?
3329 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3330 RH.getOperand(1).getOpcode() == ISD::Constant &&
3331 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3332 // is LH an extension of the sign bit of LL?
3333 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3334 LH.getOperand(1).getOpcode() == ISD::Constant &&
3335 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3336 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3337 } else {
3338 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3339 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3340 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3341 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3342 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3343 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003344 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3345 } else {
3346 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3347 }
3348 break;
3349 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003350 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3351 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3352 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3353 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003354 }
3355
3356 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003357 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3358 std::make_pair(Lo, Hi))).second;
3359 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003360}
3361
3362
3363// SelectionDAG::Legalize - This is the entry point for the file.
3364//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003365void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003366 /// run - This is the main entry point to this class.
3367 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003368 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003369}
3370