blob: 874bfafab1e3bc2d1dab7f32ac9db892db17c862 [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 Lattner0d902a92005-11-08 23:32:44 +0000412 Order.reserve(DAG.allnodes_end()-DAG.allnodes_begin());
Chris Lattnerab510a72005-10-02 17:49:46 +0000413
Chris Lattner32fca002005-10-06 01:20:27 +0000414 // Compute ordering from all of the leaves in the graphs, those (like the
415 // entry node) that have no operands.
416 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
417 E = DAG.allnodes_end(); I != E; ++I) {
418 if ((*I)->getNumOperands() == 0) {
419 Visited[*I] = 0 - 1U;
420 ComputeTopDownOrdering(*I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000421 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000422 }
423
Chris Lattner32fca002005-10-06 01:20:27 +0000424 assert(Order.size() == Visited.size() && Order.size() == DAG.allnodes_size()&&
425 "Error: DAG is cyclic!");
426 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000427
Chris Lattner32fca002005-10-06 01:20:27 +0000428 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
429 SDNode *N = Order[i];
430 switch (getTypeAction(N->getValueType(0))) {
431 default: assert(0 && "Bad type action!");
432 case Legal:
433 LegalizeOp(SDOperand(N, 0));
434 break;
435 case Promote:
436 PromoteOp(SDOperand(N, 0));
437 break;
438 case Expand: {
439 SDOperand X, Y;
440 ExpandOp(SDOperand(N, 0), X, Y);
441 break;
442 }
443 }
444 }
445
446 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000447 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000448 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
449 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000450
451 ExpandedNodes.clear();
452 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000453 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000454
455 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000456 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000457}
458
459SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000460 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000461 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000462 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000463
Chris Lattner3e928bb2005-01-07 07:47:09 +0000464 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000465 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000466 if (Node->getNumValues() > 1) {
467 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
468 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000469 case Legal: break; // Nothing to do.
470 case Expand: {
471 SDOperand T1, T2;
472 ExpandOp(Op.getValue(i), T1, T2);
473 assert(LegalizedNodes.count(Op) &&
474 "Expansion didn't add legal operands!");
475 return LegalizedNodes[Op];
476 }
477 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000478 PromoteOp(Op.getValue(i));
479 assert(LegalizedNodes.count(Op) &&
480 "Expansion didn't add legal operands!");
481 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000482 }
483 }
484
Chris Lattner45982da2005-05-12 16:53:42 +0000485 // Note that LegalizeOp may be reentered even from single-use nodes, which
486 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000487 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
488 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000489
Nate Begeman9373a812005-08-10 20:51:12 +0000490 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000491
492 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493
494 switch (Node->getOpcode()) {
495 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000496 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
497 // If this is a target node, legalize it by legalizing the operands then
498 // passing it through.
499 std::vector<SDOperand> Ops;
500 bool Changed = false;
501 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
502 Ops.push_back(LegalizeOp(Node->getOperand(i)));
503 Changed = Changed || Node->getOperand(i) != Ops.back();
504 }
505 if (Changed)
506 if (Node->getNumValues() == 1)
507 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
508 else {
509 std::vector<MVT::ValueType> VTs(Node->value_begin(),
510 Node->value_end());
511 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
512 }
513
514 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
515 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
516 return Result.getValue(Op.ResNo);
517 }
518 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000519 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
520 assert(0 && "Do not know how to legalize this operator!");
521 abort();
522 case ISD::EntryToken:
523 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000524 case ISD::TargetFrameIndex:
525 case ISD::Register:
526 case ISD::TargetConstant:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000527 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000528 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000529 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000530 case ISD::BasicBlock:
531 case ISD::CONDCODE:
532 case ISD::VALUETYPE:
533 case ISD::SRCVALUE:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000534 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000535 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000536 case ISD::AssertSext:
537 case ISD::AssertZext:
538 Tmp1 = LegalizeOp(Node->getOperand(0));
539 if (Tmp1 != Node->getOperand(0))
540 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
541 Node->getOperand(1));
542 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000543 case ISD::CopyFromReg:
544 Tmp1 = LegalizeOp(Node->getOperand(0));
545 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000546 Result = DAG.getCopyFromReg(Tmp1,
547 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
548 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000549 else
550 Result = Op.getValue(0);
551
552 // Since CopyFromReg produces two values, make sure to remember that we
553 // legalized both of them.
554 AddLegalizedOperand(Op.getValue(0), Result);
555 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
556 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000557 case ISD::ImplicitDef:
558 Tmp1 = LegalizeOp(Node->getOperand(0));
559 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000560 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
561 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000562 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000563 case ISD::UNDEF: {
564 MVT::ValueType VT = Op.getValueType();
565 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000566 default: assert(0 && "This action is not supported yet!");
567 case TargetLowering::Expand:
568 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000569 if (MVT::isInteger(VT))
570 Result = DAG.getConstant(0, VT);
571 else if (MVT::isFloatingPoint(VT))
572 Result = DAG.getConstantFP(0, VT);
573 else
574 assert(0 && "Unknown value type!");
575 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000576 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000577 break;
578 }
579 break;
580 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000581 case ISD::Constant:
582 // We know we don't need to expand constants here, constants only have one
583 // value and we check that it is fine above.
584
585 // FIXME: Maybe we should handle things like targets that don't support full
586 // 32-bit immediates?
587 break;
588 case ISD::ConstantFP: {
589 // Spill FP immediates to the constant pool if the target cannot directly
590 // codegen them. Targets often have some immediate values that can be
591 // efficiently generated into an FP register without a load. We explicitly
592 // leave these constants as ConstantFP nodes for the target to deal with.
593
594 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
595
596 // Check to see if this FP immediate is already legal.
597 bool isLegal = false;
598 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
599 E = TLI.legal_fpimm_end(); I != E; ++I)
600 if (CFP->isExactlyValue(*I)) {
601 isLegal = true;
602 break;
603 }
604
605 if (!isLegal) {
606 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000607 bool Extend = false;
608
609 // If a FP immediate is precise when represented as a float, we put it
610 // into the constant pool as a float, even if it's is statically typed
611 // as a double.
612 MVT::ValueType VT = CFP->getValueType(0);
613 bool isDouble = VT == MVT::f64;
614 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
615 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000616 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
617 // Only do this if the target has a native EXTLOAD instruction from
618 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000619 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000620 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
621 VT = MVT::f32;
622 Extend = true;
623 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000624
Chris Lattner5839bf22005-08-26 17:15:30 +0000625 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000626 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000627 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
628 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000629 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000630 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
631 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000632 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000633 }
634 break;
635 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000636 case ISD::TokenFactor:
637 if (Node->getNumOperands() == 2) {
638 bool Changed = false;
639 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
640 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
641 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
642 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
643 } else {
644 std::vector<SDOperand> Ops;
645 bool Changed = false;
646 // Legalize the operands.
647 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
648 SDOperand Op = Node->getOperand(i);
649 Ops.push_back(LegalizeOp(Op));
650 Changed |= Ops[i] != Op;
651 }
652 if (Changed)
653 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000654 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000655 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000656
Chris Lattner16cd04d2005-05-12 23:24:06 +0000657 case ISD::CALLSEQ_START:
658 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000659 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000660 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000661 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000662 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000663 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000664
Chris Lattner16cd04d2005-05-12 23:24:06 +0000665 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000666 // nodes are treated specially and are mutated in place. This makes the dag
667 // legalization process more efficient and also makes libcall insertion
668 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000669 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000670 case ISD::DYNAMIC_STACKALLOC:
671 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
672 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
673 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
674 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000675 Tmp3 != Node->getOperand(2)) {
676 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
677 std::vector<SDOperand> Ops;
678 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
679 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
680 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000681 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000682
683 // Since this op produces two values, make sure to remember that we
684 // legalized both of them.
685 AddLegalizedOperand(SDOperand(Node, 0), Result);
686 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
687 return Result.getValue(Op.ResNo);
688
Chris Lattnerd71c0412005-05-13 18:43:43 +0000689 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000690 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
692 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000693
694 bool Changed = false;
695 std::vector<SDOperand> Ops;
696 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
697 Ops.push_back(LegalizeOp(Node->getOperand(i)));
698 Changed |= Ops.back() != Node->getOperand(i);
699 }
700
701 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000702 std::vector<MVT::ValueType> RetTyVTs;
703 RetTyVTs.reserve(Node->getNumValues());
704 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000705 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000706 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
707 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000708 } else {
709 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000710 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000711 // Since calls produce multiple values, make sure to remember that we
712 // legalized all of them.
713 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
714 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
715 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000716 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000717 case ISD::BR:
718 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
719 if (Tmp1 != Node->getOperand(0))
720 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
721 break;
722
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000723 case ISD::BRCOND:
724 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000725
Chris Lattner47e92232005-01-18 19:27:06 +0000726 switch (getTypeAction(Node->getOperand(1).getValueType())) {
727 case Expand: assert(0 && "It's impossible to expand bools");
728 case Legal:
729 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
730 break;
731 case Promote:
732 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
733 break;
734 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000735
736 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
737 default: assert(0 && "This action is not supported yet!");
738 case TargetLowering::Expand:
739 // Expand brcond's setcc into its constituent parts and create a BR_CC
740 // Node.
741 if (Tmp2.getOpcode() == ISD::SETCC) {
742 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
743 Tmp2.getOperand(0), Tmp2.getOperand(1),
744 Node->getOperand(2));
745 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000746 // Make sure the condition is either zero or one. It may have been
747 // promoted from something else.
748 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
749
Nate Begeman7cbd5252005-08-16 19:49:35 +0000750 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
751 DAG.getCondCode(ISD::SETNE), Tmp2,
752 DAG.getConstant(0, Tmp2.getValueType()),
753 Node->getOperand(2));
754 }
755 break;
756 case TargetLowering::Legal:
757 // Basic block destination (Op#2) is always legal.
758 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
759 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
760 Node->getOperand(2));
761 break;
762 }
763 break;
764 case ISD::BR_CC:
765 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
766
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000767 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000768 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
769 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
770 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
771 Tmp3 != Node->getOperand(3)) {
772 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
773 Tmp2, Tmp3, Node->getOperand(4));
774 }
775 break;
776 } else {
777 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
778 Node->getOperand(2), // LHS
779 Node->getOperand(3), // RHS
780 Node->getOperand(1)));
781 // If we get a SETCC back from legalizing the SETCC node we just
782 // created, then use its LHS, RHS, and CC directly in creating a new
783 // node. Otherwise, select between the true and false value based on
784 // comparing the result of the legalized with zero.
785 if (Tmp2.getOpcode() == ISD::SETCC) {
786 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
787 Tmp2.getOperand(0), Tmp2.getOperand(1),
788 Node->getOperand(4));
789 } else {
790 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
791 DAG.getCondCode(ISD::SETNE),
792 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
793 Node->getOperand(4));
794 }
795 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000796 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000797 case ISD::BRCONDTWOWAY:
798 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
799 switch (getTypeAction(Node->getOperand(1).getValueType())) {
800 case Expand: assert(0 && "It's impossible to expand bools");
801 case Legal:
802 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
803 break;
804 case Promote:
805 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
806 break;
807 }
808 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
809 // pair.
810 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
811 case TargetLowering::Promote:
812 default: assert(0 && "This action is not supported yet!");
813 case TargetLowering::Legal:
814 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
815 std::vector<SDOperand> Ops;
816 Ops.push_back(Tmp1);
817 Ops.push_back(Tmp2);
818 Ops.push_back(Node->getOperand(2));
819 Ops.push_back(Node->getOperand(3));
820 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
821 }
822 break;
823 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000824 // If BRTWOWAY_CC is legal for this target, then simply expand this node
825 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
826 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000827 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000828 if (Tmp2.getOpcode() == ISD::SETCC) {
829 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
830 Tmp2.getOperand(0), Tmp2.getOperand(1),
831 Node->getOperand(2), Node->getOperand(3));
832 } else {
833 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
834 DAG.getConstant(0, Tmp2.getValueType()),
835 Node->getOperand(2), Node->getOperand(3));
836 }
837 } else {
838 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000839 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000840 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
841 }
Chris Lattner411e8882005-04-09 03:30:19 +0000842 break;
843 }
844 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000845 case ISD::BRTWOWAY_CC:
846 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000847 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000848 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
849 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
850 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
851 Tmp3 != Node->getOperand(3)) {
852 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
853 Node->getOperand(4), Node->getOperand(5));
854 }
855 break;
856 } else {
857 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
858 Node->getOperand(2), // LHS
859 Node->getOperand(3), // RHS
860 Node->getOperand(1)));
861 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
862 // pair.
863 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
864 default: assert(0 && "This action is not supported yet!");
865 case TargetLowering::Legal:
866 // If we get a SETCC back from legalizing the SETCC node we just
867 // created, then use its LHS, RHS, and CC directly in creating a new
868 // node. Otherwise, select between the true and false value based on
869 // comparing the result of the legalized with zero.
870 if (Tmp2.getOpcode() == ISD::SETCC) {
871 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
872 Tmp2.getOperand(0), Tmp2.getOperand(1),
873 Node->getOperand(4), Node->getOperand(5));
874 } else {
875 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
876 DAG.getConstant(0, Tmp2.getValueType()),
877 Node->getOperand(4), Node->getOperand(5));
878 }
879 break;
880 case TargetLowering::Expand:
881 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
882 Node->getOperand(4));
883 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
884 break;
885 }
886 }
887 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000888 case ISD::LOAD:
889 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
890 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000891
Chris Lattner3e928bb2005-01-07 07:47:09 +0000892 if (Tmp1 != Node->getOperand(0) ||
893 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000894 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
895 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000896 else
897 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000898
Chris Lattner8afc48e2005-01-07 22:28:47 +0000899 // Since loads produce two values, make sure to remember that we legalized
900 // both of them.
901 AddLegalizedOperand(SDOperand(Node, 0), Result);
902 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
903 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000904
Chris Lattner0f69b292005-01-15 06:18:18 +0000905 case ISD::EXTLOAD:
906 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000907 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000910
Chris Lattner5f056bf2005-07-10 01:55:33 +0000911 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000912 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000913 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000914 case TargetLowering::Promote:
915 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000916 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
917 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000918 // Since loads produce two values, make sure to remember that we legalized
919 // both of them.
920 AddLegalizedOperand(SDOperand(Node, 0), Result);
921 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
922 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000923
Chris Lattner01ff7212005-04-10 22:54:25 +0000924 case TargetLowering::Legal:
925 if (Tmp1 != Node->getOperand(0) ||
926 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000927 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
928 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000929 else
930 Result = SDOperand(Node, 0);
931
932 // Since loads produce two values, make sure to remember that we legalized
933 // both of them.
934 AddLegalizedOperand(SDOperand(Node, 0), Result);
935 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
936 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000937 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000938 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
939 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
940 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000941 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000942 if (Op.ResNo)
943 return Load.getValue(1);
944 return Result;
945 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000946 assert(Node->getOpcode() != ISD::EXTLOAD &&
947 "EXTLOAD should always be supported!");
948 // Turn the unsupported load into an EXTLOAD followed by an explicit
949 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000950 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
951 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000952 SDOperand ValRes;
953 if (Node->getOpcode() == ISD::SEXTLOAD)
954 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000955 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000956 else
957 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000958 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
959 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
960 if (Op.ResNo)
961 return Result.getValue(1);
962 return ValRes;
963 }
964 assert(0 && "Unreachable");
965 }
Nate Begeman5dc897b2005-10-19 00:06:56 +0000966 case ISD::EXTRACT_ELEMENT: {
967 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
968 switch (getTypeAction(OpTy)) {
969 default:
970 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
971 break;
972 case Legal:
973 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
974 // 1 -> Hi
975 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
976 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
977 TLI.getShiftAmountTy()));
978 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
979 } else {
980 // 0 -> Lo
981 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
982 Node->getOperand(0));
983 }
984 Result = LegalizeOp(Result);
985 break;
986 case Expand:
987 // Get both the low and high parts.
988 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
989 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
990 Result = Tmp2; // 1 -> Hi
991 else
992 Result = Tmp1; // 0 -> Lo
993 break;
994 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000995 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +0000996 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000997
998 case ISD::CopyToReg:
999 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001000
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001001 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001002 "Register type must be legal!");
1003 // Legalize the incoming value (must be legal).
1004 Tmp2 = LegalizeOp(Node->getOperand(2));
1005 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1006 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1007 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001008 break;
1009
1010 case ISD::RET:
1011 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1012 switch (Node->getNumOperands()) {
1013 case 2: // ret val
1014 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1015 case Legal:
1016 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001017 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001018 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1019 break;
1020 case Expand: {
1021 SDOperand Lo, Hi;
1022 ExpandOp(Node->getOperand(1), Lo, Hi);
1023 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001024 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001025 }
1026 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001027 Tmp2 = PromoteOp(Node->getOperand(1));
1028 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1029 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001030 }
1031 break;
1032 case 1: // ret void
1033 if (Tmp1 != Node->getOperand(0))
1034 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1035 break;
1036 default: { // ret <values>
1037 std::vector<SDOperand> NewValues;
1038 NewValues.push_back(Tmp1);
1039 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1040 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1041 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001042 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001043 break;
1044 case Expand: {
1045 SDOperand Lo, Hi;
1046 ExpandOp(Node->getOperand(i), Lo, Hi);
1047 NewValues.push_back(Lo);
1048 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001049 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001050 }
1051 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001052 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001053 }
1054 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1055 break;
1056 }
1057 }
1058 break;
1059 case ISD::STORE:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1061 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1062
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001063 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001064 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001065 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001066 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001067 DAG.getConstant(FloatToBits(CFP->getValue()),
1068 MVT::i32),
1069 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001070 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001071 } else {
1072 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001073 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001074 DAG.getConstant(DoubleToBits(CFP->getValue()),
1075 MVT::i64),
1076 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001077 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001078 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001079 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001080 }
1081
Chris Lattner3e928bb2005-01-07 07:47:09 +00001082 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1083 case Legal: {
1084 SDOperand Val = LegalizeOp(Node->getOperand(1));
1085 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1086 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001087 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1088 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001089 break;
1090 }
1091 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001092 // Truncate the value and store the result.
1093 Tmp3 = PromoteOp(Node->getOperand(1));
1094 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001095 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001096 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001097 break;
1098
Chris Lattner3e928bb2005-01-07 07:47:09 +00001099 case Expand:
1100 SDOperand Lo, Hi;
1101 ExpandOp(Node->getOperand(1), Lo, Hi);
1102
1103 if (!TLI.isLittleEndian())
1104 std::swap(Lo, Hi);
1105
Chris Lattneredb1add2005-05-11 04:51:16 +00001106 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1107 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001108 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001109 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1110 getIntPtrConstant(IncrementSize));
1111 assert(isTypeLegal(Tmp2.getValueType()) &&
1112 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001113 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001114 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1115 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001116 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1117 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001118 }
1119 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001120 case ISD::PCMARKER:
1121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001122 if (Tmp1 != Node->getOperand(0))
1123 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001124 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001125 case ISD::TRUNCSTORE:
1126 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1127 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1128
1129 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1130 case Legal:
1131 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001132
1133 // The only promote case we handle is TRUNCSTORE:i1 X into
1134 // -> TRUNCSTORE:i8 (and X, 1)
1135 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1136 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1137 TargetLowering::Promote) {
1138 // Promote the bool to a mask then store.
1139 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1140 DAG.getConstant(1, Tmp2.getValueType()));
1141 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1142 Node->getOperand(3), DAG.getValueType(MVT::i8));
1143
1144 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1145 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001146 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001147 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001148 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001149 break;
1150 case Promote:
1151 case Expand:
1152 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1153 }
1154 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001155 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001156 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1157 case Expand: assert(0 && "It's impossible to expand bools");
1158 case Legal:
1159 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1160 break;
1161 case Promote:
1162 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1163 break;
1164 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001165 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001166 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001167
Nate Begemanb942a3d2005-08-23 04:29:48 +00001168 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001169 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001170 case TargetLowering::Expand:
1171 if (Tmp1.getOpcode() == ISD::SETCC) {
1172 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1173 Tmp2, Tmp3,
1174 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1175 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001176 // Make sure the condition is either zero or one. It may have been
1177 // promoted from something else.
1178 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001179 Result = DAG.getSelectCC(Tmp1,
1180 DAG.getConstant(0, Tmp1.getValueType()),
1181 Tmp2, Tmp3, ISD::SETNE);
1182 }
1183 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001184 case TargetLowering::Legal:
1185 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1186 Tmp3 != Node->getOperand(2))
1187 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1188 Tmp1, Tmp2, Tmp3);
1189 break;
1190 case TargetLowering::Promote: {
1191 MVT::ValueType NVT =
1192 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1193 unsigned ExtOp, TruncOp;
1194 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001195 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001196 TruncOp = ISD::TRUNCATE;
1197 } else {
1198 ExtOp = ISD::FP_EXTEND;
1199 TruncOp = ISD::FP_ROUND;
1200 }
1201 // Promote each of the values to the new type.
1202 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1203 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1204 // Perform the larger operation, then round down.
1205 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1206 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1207 break;
1208 }
1209 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001210 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001211 case ISD::SELECT_CC:
1212 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1213 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1214
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001215 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001216 // Everything is legal, see if we should expand this op or something.
1217 switch (TLI.getOperationAction(ISD::SELECT_CC,
1218 Node->getOperand(0).getValueType())) {
1219 default: assert(0 && "This action is not supported yet!");
1220 case TargetLowering::Custom: {
1221 SDOperand Tmp =
1222 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1223 Node->getOperand(0),
1224 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001225 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001226 if (Tmp.Val) {
1227 Result = LegalizeOp(Tmp);
1228 break;
1229 }
1230 } // FALLTHROUGH if the target can't lower this operation after all.
1231 case TargetLowering::Legal:
1232 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1233 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1234 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1235 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1236 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1237 Tmp3, Tmp4, Node->getOperand(4));
1238 }
1239 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001240 }
1241 break;
1242 } else {
1243 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1244 Node->getOperand(0), // LHS
1245 Node->getOperand(1), // RHS
1246 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001247 // If we get a SETCC back from legalizing the SETCC node we just
1248 // created, then use its LHS, RHS, and CC directly in creating a new
1249 // node. Otherwise, select between the true and false value based on
1250 // comparing the result of the legalized with zero.
1251 if (Tmp1.getOpcode() == ISD::SETCC) {
1252 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1253 Tmp1.getOperand(0), Tmp1.getOperand(1),
1254 Tmp3, Tmp4, Tmp1.getOperand(2));
1255 } else {
1256 Result = DAG.getSelectCC(Tmp1,
1257 DAG.getConstant(0, Tmp1.getValueType()),
1258 Tmp3, Tmp4, ISD::SETNE);
1259 }
Nate Begeman9373a812005-08-10 20:51:12 +00001260 }
1261 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001262 case ISD::SETCC:
1263 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1264 case Legal:
1265 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1266 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001267 break;
1268 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001269 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1270 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1271
1272 // If this is an FP compare, the operands have already been extended.
1273 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1274 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001275 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001276
1277 // Otherwise, we have to insert explicit sign or zero extends. Note
1278 // that we could insert sign extends for ALL conditions, but zero extend
1279 // is cheaper on many machines (an AND instead of two shifts), so prefer
1280 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001281 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001282 default: assert(0 && "Unknown integer comparison!");
1283 case ISD::SETEQ:
1284 case ISD::SETNE:
1285 case ISD::SETUGE:
1286 case ISD::SETUGT:
1287 case ISD::SETULE:
1288 case ISD::SETULT:
1289 // ALL of these operations will work if we either sign or zero extend
1290 // the operands (including the unsigned comparisons!). Zero extend is
1291 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001292 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1293 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001294 break;
1295 case ISD::SETGE:
1296 case ISD::SETGT:
1297 case ISD::SETLT:
1298 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001299 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1300 DAG.getValueType(VT));
1301 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1302 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001303 break;
1304 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001305 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001306 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001307 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001308 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1309 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1310 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001311 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001312 case ISD::SETEQ:
1313 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001314 if (RHSLo == RHSHi)
1315 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1316 if (RHSCST->isAllOnesValue()) {
1317 // Comparison to -1.
1318 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001319 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001320 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001321 }
1322
Chris Lattner3e928bb2005-01-07 07:47:09 +00001323 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1324 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1325 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001326 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001327 break;
1328 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001329 // If this is a comparison of the sign bit, just look at the top part.
1330 // X > -1, x < 0
1331 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001332 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001333 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001334 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001335 (CST->isAllOnesValue()))) { // X > -1
1336 Tmp1 = LHSHi;
1337 Tmp2 = RHSHi;
1338 break;
1339 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001340
Chris Lattner3e928bb2005-01-07 07:47:09 +00001341 // FIXME: This generated code sucks.
1342 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001343 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001344 default: assert(0 && "Unknown integer setcc!");
1345 case ISD::SETLT:
1346 case ISD::SETULT: LowCC = ISD::SETULT; break;
1347 case ISD::SETGT:
1348 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1349 case ISD::SETLE:
1350 case ISD::SETULE: LowCC = ISD::SETULE; break;
1351 case ISD::SETGE:
1352 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1353 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001354
Chris Lattner3e928bb2005-01-07 07:47:09 +00001355 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1356 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1357 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1358
1359 // NOTE: on targets without efficient SELECT of bools, we can always use
1360 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001361 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1362 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1363 Node->getOperand(2));
1364 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001365 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1366 Result, Tmp1, Tmp2));
1367 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001368 }
1369 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001370
1371 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1372 default:
1373 assert(0 && "Cannot handle this action for SETCC yet!");
1374 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001375 case TargetLowering::Promote:
1376 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1377 Node->getOperand(2));
1378 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001379 case TargetLowering::Legal:
1380 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1381 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1382 Node->getOperand(2));
1383 break;
1384 case TargetLowering::Expand:
1385 // Expand a setcc node into a select_cc of the same condition, lhs, and
1386 // rhs that selects between const 1 (true) and const 0 (false).
1387 MVT::ValueType VT = Node->getValueType(0);
1388 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1389 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1390 Node->getOperand(2));
1391 Result = LegalizeOp(Result);
1392 break;
1393 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001394 break;
1395
Chris Lattnere1bd8222005-01-11 05:57:22 +00001396 case ISD::MEMSET:
1397 case ISD::MEMCPY:
1398 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001400 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1401
1402 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1403 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1404 case Expand: assert(0 && "Cannot expand a byte!");
1405 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001406 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001407 break;
1408 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001409 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001410 break;
1411 }
1412 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001413 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001414 }
Chris Lattner272455b2005-02-02 03:44:41 +00001415
1416 SDOperand Tmp4;
1417 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001418 case Expand: {
1419 // Length is too big, just take the lo-part of the length.
1420 SDOperand HiPart;
1421 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1422 break;
1423 }
Chris Lattnere5605212005-01-28 22:29:18 +00001424 case Legal:
1425 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001426 break;
1427 case Promote:
1428 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001429 break;
1430 }
1431
1432 SDOperand Tmp5;
1433 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1434 case Expand: assert(0 && "Cannot expand this yet!");
1435 case Legal:
1436 Tmp5 = LegalizeOp(Node->getOperand(4));
1437 break;
1438 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001439 Tmp5 = PromoteOp(Node->getOperand(4));
1440 break;
1441 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001442
1443 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1444 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001445 case TargetLowering::Custom: {
1446 SDOperand Tmp =
1447 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1448 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1449 if (Tmp.Val) {
1450 Result = LegalizeOp(Tmp);
1451 break;
1452 }
1453 // FALLTHROUGH if the target thinks it is legal.
1454 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001455 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001456 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1457 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1458 Tmp5 != Node->getOperand(4)) {
1459 std::vector<SDOperand> Ops;
1460 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1461 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1462 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1463 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001464 break;
1465 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001466 // Otherwise, the target does not support this operation. Lower the
1467 // operation to an explicit libcall as appropriate.
1468 MVT::ValueType IntPtr = TLI.getPointerTy();
1469 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1470 std::vector<std::pair<SDOperand, const Type*> > Args;
1471
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001472 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001473 if (Node->getOpcode() == ISD::MEMSET) {
1474 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1475 // Extend the ubyte argument to be an int value for the call.
1476 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1477 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1478 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1479
1480 FnName = "memset";
1481 } else if (Node->getOpcode() == ISD::MEMCPY ||
1482 Node->getOpcode() == ISD::MEMMOVE) {
1483 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1484 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1485 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1486 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1487 } else {
1488 assert(0 && "Unknown op!");
1489 }
Chris Lattner45982da2005-05-12 16:53:42 +00001490
Chris Lattnere1bd8222005-01-11 05:57:22 +00001491 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001492 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001493 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001494 Result = CallResult.second;
1495 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001496 break;
1497 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001498 }
1499 break;
1500 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001501
1502 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001503 Tmp1 = LegalizeOp(Node->getOperand(0));
1504 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001505
Chris Lattner3e011362005-05-14 07:45:46 +00001506 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1507 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1508 std::vector<SDOperand> Ops;
1509 Ops.push_back(Tmp1);
1510 Ops.push_back(Tmp2);
1511 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1512 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001513 Result = SDOperand(Node, 0);
1514 // Since these produce two values, make sure to remember that we legalized
1515 // both of them.
1516 AddLegalizedOperand(SDOperand(Node, 0), Result);
1517 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1518 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001519 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001520 Tmp1 = LegalizeOp(Node->getOperand(0));
1521 Tmp2 = LegalizeOp(Node->getOperand(1));
1522 Tmp3 = LegalizeOp(Node->getOperand(2));
1523 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1524 Tmp3 != Node->getOperand(2))
1525 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1526 break;
1527
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001528 case ISD::READIO:
1529 Tmp1 = LegalizeOp(Node->getOperand(0));
1530 Tmp2 = LegalizeOp(Node->getOperand(1));
1531
1532 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1533 case TargetLowering::Custom:
1534 default: assert(0 && "This action not implemented for this operation!");
1535 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001536 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1537 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1538 std::vector<SDOperand> Ops;
1539 Ops.push_back(Tmp1);
1540 Ops.push_back(Tmp2);
1541 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1542 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001543 Result = SDOperand(Node, 0);
1544 break;
1545 case TargetLowering::Expand:
1546 // Replace this with a load from memory.
1547 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1548 Node->getOperand(1), DAG.getSrcValue(NULL));
1549 Result = LegalizeOp(Result);
1550 break;
1551 }
1552
1553 // Since these produce two values, make sure to remember that we legalized
1554 // both of them.
1555 AddLegalizedOperand(SDOperand(Node, 0), Result);
1556 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1557 return Result.getValue(Op.ResNo);
1558
1559 case ISD::WRITEIO:
1560 Tmp1 = LegalizeOp(Node->getOperand(0));
1561 Tmp2 = LegalizeOp(Node->getOperand(1));
1562 Tmp3 = LegalizeOp(Node->getOperand(2));
1563
1564 switch (TLI.getOperationAction(Node->getOpcode(),
1565 Node->getOperand(1).getValueType())) {
1566 case TargetLowering::Custom:
1567 default: assert(0 && "This action not implemented for this operation!");
1568 case TargetLowering::Legal:
1569 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1570 Tmp3 != Node->getOperand(2))
1571 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1572 break;
1573 case TargetLowering::Expand:
1574 // Replace this with a store to memory.
1575 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1576 Node->getOperand(1), Node->getOperand(2),
1577 DAG.getSrcValue(NULL));
1578 Result = LegalizeOp(Result);
1579 break;
1580 }
1581 break;
1582
Chris Lattner84f67882005-01-20 18:52:28 +00001583 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001584 case ISD::SUB_PARTS:
1585 case ISD::SHL_PARTS:
1586 case ISD::SRA_PARTS:
1587 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001588 std::vector<SDOperand> Ops;
1589 bool Changed = false;
1590 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1591 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1592 Changed |= Ops.back() != Node->getOperand(i);
1593 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001594 if (Changed) {
1595 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1596 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1597 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001598
1599 // Since these produce multiple values, make sure to remember that we
1600 // legalized all of them.
1601 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1602 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1603 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001604 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001605
1606 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001607 case ISD::ADD:
1608 case ISD::SUB:
1609 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001610 case ISD::MULHS:
1611 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001612 case ISD::UDIV:
1613 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001614 case ISD::AND:
1615 case ISD::OR:
1616 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001617 case ISD::SHL:
1618 case ISD::SRL:
1619 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001620 case ISD::FADD:
1621 case ISD::FSUB:
1622 case ISD::FMUL:
1623 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001624 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001625 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1626 case Expand: assert(0 && "Not possible");
1627 case Legal:
1628 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1629 break;
1630 case Promote:
1631 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1632 break;
1633 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001634 if (Tmp1 != Node->getOperand(0) ||
1635 Tmp2 != Node->getOperand(1))
1636 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1637 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001638
Nate Begeman419f8b62005-10-18 00:27:41 +00001639 case ISD::BUILD_PAIR: {
1640 MVT::ValueType PairTy = Node->getValueType(0);
1641 // TODO: handle the case where the Lo and Hi operands are not of legal type
1642 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1643 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1644 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1645 case TargetLowering::Legal:
1646 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1647 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1648 break;
1649 case TargetLowering::Promote:
1650 case TargetLowering::Custom:
1651 assert(0 && "Cannot promote/custom this yet!");
1652 case TargetLowering::Expand:
1653 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1654 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1655 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1656 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1657 TLI.getShiftAmountTy()));
1658 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1659 break;
1660 }
1661 break;
1662 }
1663
Nate Begemanc105e192005-04-06 00:23:54 +00001664 case ISD::UREM:
1665 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001666 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001667 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1668 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1669 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1670 case TargetLowering::Legal:
1671 if (Tmp1 != Node->getOperand(0) ||
1672 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001673 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001674 Tmp2);
1675 break;
1676 case TargetLowering::Promote:
1677 case TargetLowering::Custom:
1678 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001679 case TargetLowering::Expand:
1680 if (MVT::isInteger(Node->getValueType(0))) {
1681 MVT::ValueType VT = Node->getValueType(0);
1682 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1683 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1684 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1685 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1686 } else {
1687 // Floating point mod -> fmod libcall.
1688 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1689 SDOperand Dummy;
1690 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001691 }
1692 break;
1693 }
1694 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001695
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001696 case ISD::CTPOP:
1697 case ISD::CTTZ:
1698 case ISD::CTLZ:
1699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1700 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1701 case TargetLowering::Legal:
1702 if (Tmp1 != Node->getOperand(0))
1703 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1704 break;
1705 case TargetLowering::Promote: {
1706 MVT::ValueType OVT = Tmp1.getValueType();
1707 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001708
1709 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001710 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1711 // Perform the larger operation, then subtract if needed.
1712 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1713 switch(Node->getOpcode())
1714 {
1715 case ISD::CTPOP:
1716 Result = Tmp1;
1717 break;
1718 case ISD::CTTZ:
1719 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001720 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1721 DAG.getConstant(getSizeInBits(NVT), NVT),
1722 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001723 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001724 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1725 break;
1726 case ISD::CTLZ:
1727 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001728 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1729 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001730 getSizeInBits(OVT), NVT));
1731 break;
1732 }
1733 break;
1734 }
1735 case TargetLowering::Custom:
1736 assert(0 && "Cannot custom handle this yet!");
1737 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001738 switch(Node->getOpcode())
1739 {
1740 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001741 static const uint64_t mask[6] = {
1742 0x5555555555555555ULL, 0x3333333333333333ULL,
1743 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1744 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1745 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001746 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001747 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1748 unsigned len = getSizeInBits(VT);
1749 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001750 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001751 Tmp2 = DAG.getConstant(mask[i], VT);
1752 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001753 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001754 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1755 DAG.getNode(ISD::AND, VT,
1756 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1757 Tmp2));
1758 }
1759 Result = Tmp1;
1760 break;
1761 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001762 case ISD::CTLZ: {
1763 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001764 x = x | (x >> 1);
1765 x = x | (x >> 2);
1766 ...
1767 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001768 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001769 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001770
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001771 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1772 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001773 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1774 unsigned len = getSizeInBits(VT);
1775 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1776 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001777 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001778 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1779 }
1780 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001781 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001782 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001783 }
1784 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001785 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001786 // unless the target has ctlz but not ctpop, in which case we use:
1787 // { return 32 - nlz(~x & (x-1)); }
1788 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001789 MVT::ValueType VT = Tmp1.getValueType();
1790 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001791 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001792 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1793 DAG.getNode(ISD::SUB, VT, Tmp1,
1794 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001795 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001796 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1797 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001798 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001799 DAG.getConstant(getSizeInBits(VT), VT),
1800 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1801 } else {
1802 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1803 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001804 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001805 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001806 default:
1807 assert(0 && "Cannot expand this yet!");
1808 break;
1809 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001810 break;
1811 }
1812 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001813
Chris Lattner2c8086f2005-04-02 05:00:07 +00001814 // Unary operators
1815 case ISD::FABS:
1816 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001817 case ISD::FSQRT:
1818 case ISD::FSIN:
1819 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001820 Tmp1 = LegalizeOp(Node->getOperand(0));
1821 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1822 case TargetLowering::Legal:
1823 if (Tmp1 != Node->getOperand(0))
1824 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1825 break;
1826 case TargetLowering::Promote:
1827 case TargetLowering::Custom:
1828 assert(0 && "Cannot promote/custom handle this yet!");
1829 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001830 switch(Node->getOpcode()) {
1831 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001832 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1833 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001834 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00001835 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001836 break;
1837 }
1838 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001839 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1840 MVT::ValueType VT = Node->getValueType(0);
1841 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001842 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001843 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1844 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1845 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001846 break;
1847 }
1848 case ISD::FSQRT:
1849 case ISD::FSIN:
1850 case ISD::FCOS: {
1851 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001852 const char *FnName = 0;
1853 switch(Node->getOpcode()) {
1854 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1855 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1856 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1857 default: assert(0 && "Unreachable!");
1858 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001859 SDOperand Dummy;
1860 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001861 break;
1862 }
1863 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001864 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001865 }
1866 break;
1867 }
1868 break;
1869
1870 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001871 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001872 case ISD::UINT_TO_FP: {
1873 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001874 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1875 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001876 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001877 Node->getOperand(0).getValueType())) {
1878 default: assert(0 && "Unknown operation action!");
1879 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001880 Result = ExpandLegalINT_TO_FP(isSigned,
1881 LegalizeOp(Node->getOperand(0)),
1882 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001883 AddLegalizedOperand(Op, Result);
1884 return Result;
1885 case TargetLowering::Promote:
1886 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1887 Node->getValueType(0),
1888 isSigned);
1889 AddLegalizedOperand(Op, Result);
1890 return Result;
1891 case TargetLowering::Legal:
1892 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001893 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001894
Chris Lattner3e928bb2005-01-07 07:47:09 +00001895 Tmp1 = LegalizeOp(Node->getOperand(0));
1896 if (Tmp1 != Node->getOperand(0))
1897 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1898 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001899 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001900 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1901 Node->getValueType(0), Node->getOperand(0));
1902 break;
1903 case Promote:
1904 if (isSigned) {
1905 Result = PromoteOp(Node->getOperand(0));
1906 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1907 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1908 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1909 } else {
1910 Result = PromoteOp(Node->getOperand(0));
1911 Result = DAG.getZeroExtendInReg(Result,
1912 Node->getOperand(0).getValueType());
1913 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001914 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001915 break;
1916 }
1917 break;
1918 }
1919 case ISD::TRUNCATE:
1920 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1921 case Legal:
1922 Tmp1 = LegalizeOp(Node->getOperand(0));
1923 if (Tmp1 != Node->getOperand(0))
1924 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1925 break;
1926 case Expand:
1927 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1928
1929 // Since the result is legal, we should just be able to truncate the low
1930 // part of the source.
1931 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1932 break;
1933 case Promote:
1934 Result = PromoteOp(Node->getOperand(0));
1935 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1936 break;
1937 }
1938 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001939
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001940 case ISD::FP_TO_SINT:
1941 case ISD::FP_TO_UINT:
1942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1943 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001944 Tmp1 = LegalizeOp(Node->getOperand(0));
1945
Chris Lattner1618beb2005-07-29 00:11:56 +00001946 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1947 default: assert(0 && "Unknown operation action!");
1948 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001949 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1950 SDOperand True, False;
1951 MVT::ValueType VT = Node->getOperand(0).getValueType();
1952 MVT::ValueType NVT = Node->getValueType(0);
1953 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1954 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1955 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1956 Node->getOperand(0), Tmp2, ISD::SETLT);
1957 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1958 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00001959 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00001960 Tmp2));
1961 False = DAG.getNode(ISD::XOR, NVT, False,
1962 DAG.getConstant(1ULL << ShiftAmt, NVT));
1963 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001964 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001965 } else {
1966 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1967 }
1968 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001969 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001970 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001971 Node->getOpcode() == ISD::FP_TO_SINT);
1972 AddLegalizedOperand(Op, Result);
1973 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001974 case TargetLowering::Custom: {
1975 SDOperand Tmp =
1976 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1977 Tmp = TLI.LowerOperation(Tmp, DAG);
1978 if (Tmp.Val) {
1979 AddLegalizedOperand(Op, Tmp);
1980 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001981 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001982 } else {
1983 // The target thinks this is legal afterall.
1984 break;
1985 }
1986 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001987 case TargetLowering::Legal:
1988 break;
1989 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001990
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001991 if (Tmp1 != Node->getOperand(0))
1992 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1993 break;
1994 case Expand:
1995 assert(0 && "Shouldn't need to expand other operators here!");
1996 case Promote:
1997 Result = PromoteOp(Node->getOperand(0));
1998 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1999 break;
2000 }
2001 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002002
Chris Lattner13c78e22005-09-02 00:18:10 +00002003 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002004 case ISD::ZERO_EXTEND:
2005 case ISD::SIGN_EXTEND:
2006 case ISD::FP_EXTEND:
2007 case ISD::FP_ROUND:
2008 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2009 case Legal:
2010 Tmp1 = LegalizeOp(Node->getOperand(0));
2011 if (Tmp1 != Node->getOperand(0))
2012 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2013 break;
2014 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002015 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002016
Chris Lattner03c85462005-01-15 05:21:40 +00002017 case Promote:
2018 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002019 case ISD::ANY_EXTEND:
2020 Result = PromoteOp(Node->getOperand(0));
2021 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2022 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002023 case ISD::ZERO_EXTEND:
2024 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002025 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002026 Result = DAG.getZeroExtendInReg(Result,
2027 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002028 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002029 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002030 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002031 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002032 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002033 Result,
2034 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002035 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002036 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002037 Result = PromoteOp(Node->getOperand(0));
2038 if (Result.getValueType() != Op.getValueType())
2039 // Dynamically dead while we have only 2 FP types.
2040 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2041 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002042 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002043 Result = PromoteOp(Node->getOperand(0));
2044 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2045 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002046 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002047 }
2048 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002049 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002050 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002051 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002052 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002053
2054 // If this operation is not supported, convert it to a shl/shr or load/store
2055 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002056 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2057 default: assert(0 && "This action not supported for this op yet!");
2058 case TargetLowering::Legal:
2059 if (Tmp1 != Node->getOperand(0))
2060 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002061 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002062 break;
2063 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002064 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002065 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002066 // NOTE: we could fall back on load/store here too for targets without
2067 // SAR. However, it is doubtful that any exist.
2068 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2069 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002070 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002071 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2072 Node->getOperand(0), ShiftCst);
2073 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2074 Result, ShiftCst);
2075 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2076 // The only way we can lower this is to turn it into a STORETRUNC,
2077 // EXTLOAD pair, targetting a temporary location (a stack slot).
2078
2079 // NOTE: there is a choice here between constantly creating new stack
2080 // slots and always reusing the same one. We currently always create
2081 // new ones, as reuse may inhibit scheduling.
2082 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2083 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2084 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2085 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002086 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002087 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2088 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2089 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002090 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002091 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002092 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2093 Result, StackSlot, DAG.getSrcValue(NULL),
2094 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002095 } else {
2096 assert(0 && "Unknown op");
2097 }
2098 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002099 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002100 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002101 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002102 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002103 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002104
Chris Lattner45982da2005-05-12 16:53:42 +00002105 // Note that LegalizeOp may be reentered even from single-use nodes, which
2106 // means that we always must cache transformed nodes.
2107 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002108 return Result;
2109}
2110
Chris Lattner8b6fa222005-01-15 22:16:26 +00002111/// PromoteOp - Given an operation that produces a value in an invalid type,
2112/// promote it to compute the value into a larger type. The produced value will
2113/// have the correct bits for the low portion of the register, but no guarantee
2114/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002115SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2116 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002117 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002118 assert(getTypeAction(VT) == Promote &&
2119 "Caller should expand or legalize operands that are not promotable!");
2120 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2121 "Cannot promote to smaller type!");
2122
Chris Lattner03c85462005-01-15 05:21:40 +00002123 SDOperand Tmp1, Tmp2, Tmp3;
2124
2125 SDOperand Result;
2126 SDNode *Node = Op.Val;
2127
Chris Lattner6fdcb252005-09-02 20:32:45 +00002128 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2129 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002130
Chris Lattner0f69b292005-01-15 06:18:18 +00002131 // Promotion needs an optimization step to clean up after it, and is not
2132 // careful to avoid operations the target does not support. Make sure that
2133 // all generated operations are legalized in the next iteration.
2134 NeedsAnotherIteration = true;
2135
Chris Lattner03c85462005-01-15 05:21:40 +00002136 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002137 case ISD::CopyFromReg:
2138 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002139 default:
2140 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2141 assert(0 && "Do not know how to promote this operator!");
2142 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002143 case ISD::UNDEF:
2144 Result = DAG.getNode(ISD::UNDEF, NVT);
2145 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002146 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002147 if (VT != MVT::i1)
2148 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2149 else
2150 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002151 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2152 break;
2153 case ISD::ConstantFP:
2154 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2155 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2156 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002157
Chris Lattner82fbfb62005-01-18 02:59:52 +00002158 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002159 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002160 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2161 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002162 Result = LegalizeOp(Result);
2163 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002164
2165 case ISD::TRUNCATE:
2166 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2167 case Legal:
2168 Result = LegalizeOp(Node->getOperand(0));
2169 assert(Result.getValueType() >= NVT &&
2170 "This truncation doesn't make sense!");
2171 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2172 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2173 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002174 case Promote:
2175 // The truncation is not required, because we don't guarantee anything
2176 // about high bits anyway.
2177 Result = PromoteOp(Node->getOperand(0));
2178 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002179 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002180 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2181 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002182 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002183 }
2184 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002185 case ISD::SIGN_EXTEND:
2186 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002187 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002188 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2189 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2190 case Legal:
2191 // Input is legal? Just do extend all the way to the larger type.
2192 Result = LegalizeOp(Node->getOperand(0));
2193 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2194 break;
2195 case Promote:
2196 // Promote the reg if it's smaller.
2197 Result = PromoteOp(Node->getOperand(0));
2198 // The high bits are not guaranteed to be anything. Insert an extend.
2199 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002200 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002201 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002202 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002203 Result = DAG.getZeroExtendInReg(Result,
2204 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002205 break;
2206 }
2207 break;
2208
2209 case ISD::FP_EXTEND:
2210 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2211 case ISD::FP_ROUND:
2212 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2213 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2214 case Promote: assert(0 && "Unreachable with 2 FP types!");
2215 case Legal:
2216 // Input is legal? Do an FP_ROUND_INREG.
2217 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002218 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2219 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002220 break;
2221 }
2222 break;
2223
2224 case ISD::SINT_TO_FP:
2225 case ISD::UINT_TO_FP:
2226 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2227 case Legal:
2228 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002229 // No extra round required here.
2230 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002231 break;
2232
2233 case Promote:
2234 Result = PromoteOp(Node->getOperand(0));
2235 if (Node->getOpcode() == ISD::SINT_TO_FP)
2236 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002237 Result,
2238 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002239 else
Chris Lattner23993562005-04-13 02:38:47 +00002240 Result = DAG.getZeroExtendInReg(Result,
2241 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002242 // No extra round required here.
2243 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002244 break;
2245 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002246 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2247 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002248 // Round if we cannot tolerate excess precision.
2249 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002250 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2251 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002252 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002253 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002254 break;
2255
2256 case ISD::FP_TO_SINT:
2257 case ISD::FP_TO_UINT:
2258 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2259 case Legal:
2260 Tmp1 = LegalizeOp(Node->getOperand(0));
2261 break;
2262 case Promote:
2263 // The input result is prerounded, so we don't have to do anything
2264 // special.
2265 Tmp1 = PromoteOp(Node->getOperand(0));
2266 break;
2267 case Expand:
2268 assert(0 && "not implemented");
2269 }
Nate Begemand2558e32005-08-14 01:20:53 +00002270 // If we're promoting a UINT to a larger size, check to see if the new node
2271 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2272 // we can use that instead. This allows us to generate better code for
2273 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2274 // legal, such as PowerPC.
2275 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002276 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002277 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2278 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002279 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2280 } else {
2281 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2282 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002283 break;
2284
Chris Lattner2c8086f2005-04-02 05:00:07 +00002285 case ISD::FABS:
2286 case ISD::FNEG:
2287 Tmp1 = PromoteOp(Node->getOperand(0));
2288 assert(Tmp1.getValueType() == NVT);
2289 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2290 // NOTE: we do not have to do any extra rounding here for
2291 // NoExcessFPPrecision, because we know the input will have the appropriate
2292 // precision, and these operations don't modify precision at all.
2293 break;
2294
Chris Lattnerda6ba872005-04-28 21:44:33 +00002295 case ISD::FSQRT:
2296 case ISD::FSIN:
2297 case ISD::FCOS:
2298 Tmp1 = PromoteOp(Node->getOperand(0));
2299 assert(Tmp1.getValueType() == NVT);
2300 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2301 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002302 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2303 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002304 break;
2305
Chris Lattner03c85462005-01-15 05:21:40 +00002306 case ISD::AND:
2307 case ISD::OR:
2308 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002309 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002310 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002311 case ISD::MUL:
2312 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002313 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002314 // that too is okay if they are integer operations.
2315 Tmp1 = PromoteOp(Node->getOperand(0));
2316 Tmp2 = PromoteOp(Node->getOperand(1));
2317 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2318 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002319 break;
2320 case ISD::FADD:
2321 case ISD::FSUB:
2322 case ISD::FMUL:
2323 // The input may have strange things in the top bits of the registers, but
2324 // these operations don't care.
2325 Tmp1 = PromoteOp(Node->getOperand(0));
2326 Tmp2 = PromoteOp(Node->getOperand(1));
2327 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2328 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2329
2330 // Floating point operations will give excess precision that we may not be
2331 // able to tolerate. If we DO allow excess precision, just leave it,
2332 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002333 // FIXME: Why would we need to round FP ops more than integer ones?
2334 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002335 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002336 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2337 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002338 break;
2339
Chris Lattner8b6fa222005-01-15 22:16:26 +00002340 case ISD::SDIV:
2341 case ISD::SREM:
2342 // These operators require that their input be sign extended.
2343 Tmp1 = PromoteOp(Node->getOperand(0));
2344 Tmp2 = PromoteOp(Node->getOperand(1));
2345 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002346 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2347 DAG.getValueType(VT));
2348 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2349 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002350 }
2351 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2352
2353 // Perform FP_ROUND: this is probably overly pessimistic.
2354 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002355 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2356 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002357 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002358 case ISD::FDIV:
2359 case ISD::FREM:
2360 // These operators require that their input be fp extended.
2361 Tmp1 = PromoteOp(Node->getOperand(0));
2362 Tmp2 = PromoteOp(Node->getOperand(1));
2363 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2364
2365 // Perform FP_ROUND: this is probably overly pessimistic.
2366 if (NoExcessFPPrecision)
2367 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2368 DAG.getValueType(VT));
2369 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002370
2371 case ISD::UDIV:
2372 case ISD::UREM:
2373 // These operators require that their input be zero extended.
2374 Tmp1 = PromoteOp(Node->getOperand(0));
2375 Tmp2 = PromoteOp(Node->getOperand(1));
2376 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002377 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2378 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002379 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2380 break;
2381
2382 case ISD::SHL:
2383 Tmp1 = PromoteOp(Node->getOperand(0));
2384 Tmp2 = LegalizeOp(Node->getOperand(1));
2385 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2386 break;
2387 case ISD::SRA:
2388 // The input value must be properly sign extended.
2389 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002390 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2391 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002392 Tmp2 = LegalizeOp(Node->getOperand(1));
2393 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2394 break;
2395 case ISD::SRL:
2396 // The input value must be properly zero extended.
2397 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002398 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002399 Tmp2 = LegalizeOp(Node->getOperand(1));
2400 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2401 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002402 case ISD::LOAD:
2403 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2404 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00002405 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2406 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002407 // Remember that we legalized the chain.
2408 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2409 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002410 case ISD::SEXTLOAD:
2411 case ISD::ZEXTLOAD:
2412 case ISD::EXTLOAD:
2413 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2414 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00002415 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2416 Node->getOperand(2),
2417 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002418 // Remember that we legalized the chain.
2419 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2420 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002421 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002422 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2423 case Expand: assert(0 && "It's impossible to expand bools");
2424 case Legal:
2425 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2426 break;
2427 case Promote:
2428 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2429 break;
2430 }
Chris Lattner03c85462005-01-15 05:21:40 +00002431 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2432 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2433 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2434 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002435 case ISD::SELECT_CC:
2436 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2437 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2438 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2439 Node->getOperand(1), Tmp2, Tmp3,
2440 Node->getOperand(4));
2441 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002442 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002443 case ISD::CALL: {
2444 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2445 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2446
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002447 std::vector<SDOperand> Ops;
2448 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2449 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2450
Chris Lattner8ac532c2005-01-16 19:46:48 +00002451 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2452 "Can only promote single result calls");
2453 std::vector<MVT::ValueType> RetTyVTs;
2454 RetTyVTs.reserve(2);
2455 RetTyVTs.push_back(NVT);
2456 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002457 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2458 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002459 Result = SDOperand(NC, 0);
2460
2461 // Insert the new chain mapping.
2462 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2463 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002464 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002465 case ISD::CTPOP:
2466 case ISD::CTTZ:
2467 case ISD::CTLZ:
2468 Tmp1 = Node->getOperand(0);
2469 //Zero extend the argument
2470 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2471 // Perform the larger operation, then subtract if needed.
2472 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2473 switch(Node->getOpcode())
2474 {
2475 case ISD::CTPOP:
2476 Result = Tmp1;
2477 break;
2478 case ISD::CTTZ:
2479 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002480 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002481 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002482 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002483 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2484 break;
2485 case ISD::CTLZ:
2486 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002487 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2488 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002489 getSizeInBits(VT), NVT));
2490 break;
2491 }
2492 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002493 }
2494
2495 assert(Result.Val && "Didn't set a result!");
2496 AddPromotedOperand(Op, Result);
2497 return Result;
2498}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002499
Chris Lattner84f67882005-01-20 18:52:28 +00002500/// ExpandAddSub - Find a clever way to expand this add operation into
2501/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002502void SelectionDAGLegalize::
2503ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2504 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002505 // Expand the subcomponents.
2506 SDOperand LHSL, LHSH, RHSL, RHSH;
2507 ExpandOp(LHS, LHSL, LHSH);
2508 ExpandOp(RHS, RHSL, RHSH);
2509
Chris Lattner84f67882005-01-20 18:52:28 +00002510 std::vector<SDOperand> Ops;
2511 Ops.push_back(LHSL);
2512 Ops.push_back(LHSH);
2513 Ops.push_back(RHSL);
2514 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002515 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2516 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002517 Hi = Lo.getValue(1);
2518}
2519
Chris Lattner5b359c62005-04-02 04:00:59 +00002520void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2521 SDOperand Op, SDOperand Amt,
2522 SDOperand &Lo, SDOperand &Hi) {
2523 // Expand the subcomponents.
2524 SDOperand LHSL, LHSH;
2525 ExpandOp(Op, LHSL, LHSH);
2526
2527 std::vector<SDOperand> Ops;
2528 Ops.push_back(LHSL);
2529 Ops.push_back(LHSH);
2530 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002531 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002532 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002533 Hi = Lo.getValue(1);
2534}
2535
2536
Chris Lattnere34b3962005-01-19 04:19:40 +00002537/// ExpandShift - Try to find a clever way to expand this shift operation out to
2538/// smaller elements. If we can't find a way that is more efficient than a
2539/// libcall on this target, return false. Otherwise, return true with the
2540/// low-parts expanded into Lo and Hi.
2541bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2542 SDOperand &Lo, SDOperand &Hi) {
2543 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2544 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002545
Chris Lattnere34b3962005-01-19 04:19:40 +00002546 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002547 SDOperand ShAmt = LegalizeOp(Amt);
2548 MVT::ValueType ShTy = ShAmt.getValueType();
2549 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2550 unsigned NVTBits = MVT::getSizeInBits(NVT);
2551
2552 // Handle the case when Amt is an immediate. Other cases are currently broken
2553 // and are disabled.
2554 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2555 unsigned Cst = CN->getValue();
2556 // Expand the incoming operand to be shifted, so that we have its parts
2557 SDOperand InL, InH;
2558 ExpandOp(Op, InL, InH);
2559 switch(Opc) {
2560 case ISD::SHL:
2561 if (Cst > VTBits) {
2562 Lo = DAG.getConstant(0, NVT);
2563 Hi = DAG.getConstant(0, NVT);
2564 } else if (Cst > NVTBits) {
2565 Lo = DAG.getConstant(0, NVT);
2566 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002567 } else if (Cst == NVTBits) {
2568 Lo = DAG.getConstant(0, NVT);
2569 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002570 } else {
2571 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2572 Hi = DAG.getNode(ISD::OR, NVT,
2573 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2574 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2575 }
2576 return true;
2577 case ISD::SRL:
2578 if (Cst > VTBits) {
2579 Lo = DAG.getConstant(0, NVT);
2580 Hi = DAG.getConstant(0, NVT);
2581 } else if (Cst > NVTBits) {
2582 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2583 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002584 } else if (Cst == NVTBits) {
2585 Lo = InH;
2586 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002587 } else {
2588 Lo = DAG.getNode(ISD::OR, NVT,
2589 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2590 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2591 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2592 }
2593 return true;
2594 case ISD::SRA:
2595 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002596 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002597 DAG.getConstant(NVTBits-1, ShTy));
2598 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002599 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002600 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002601 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002602 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002603 } else if (Cst == NVTBits) {
2604 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002605 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002606 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002607 } else {
2608 Lo = DAG.getNode(ISD::OR, NVT,
2609 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2610 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2611 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2612 }
2613 return true;
2614 }
2615 }
2616 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2617 // so disable it for now. Currently targets are handling this via SHL_PARTS
2618 // and friends.
2619 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002620
2621 // If we have an efficient select operation (or if the selects will all fold
2622 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002623 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002624 return false;
2625
2626 SDOperand InL, InH;
2627 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002628 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2629 DAG.getConstant(NVTBits, ShTy), ShAmt);
2630
Chris Lattnere5544f82005-01-20 20:29:23 +00002631 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002632 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2633 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002634
Chris Lattnere34b3962005-01-19 04:19:40 +00002635 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2636 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2637 DAG.getConstant(NVTBits-1, ShTy));
2638 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2639 DAG.getConstant(NVTBits-1, ShTy));
2640 }
2641
2642 if (Opc == ISD::SHL) {
2643 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2644 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2645 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002646 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002647
Chris Lattnere34b3962005-01-19 04:19:40 +00002648 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2649 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2650 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002651 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002652 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2653 DAG.getConstant(32, ShTy),
2654 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002655 DAG.getConstant(0, NVT),
2656 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002657 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002658 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002659 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002660 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002661
2662 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002663 if (Opc == ISD::SRA)
2664 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2665 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002666 else
2667 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002668 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002669 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002670 }
2671 return true;
2672}
Chris Lattner77e77a62005-01-21 06:05:23 +00002673
Chris Lattner9530ddc2005-05-13 05:09:11 +00002674/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2675/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002676/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002677static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002678 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002679
Chris Lattner16cd04d2005-05-12 23:24:06 +00002680 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002681 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002682 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002683 Found = Node;
2684 return;
2685 }
2686
2687 // Otherwise, scan the operands of Node to see if any of them is a call.
2688 assert(Node->getNumOperands() != 0 &&
2689 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002690 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002691 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002692
2693 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002694 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002695 Found);
2696}
2697
2698
Chris Lattner9530ddc2005-05-13 05:09:11 +00002699/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2700/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002701/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002702static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2703 std::set<SDNode*> &Visited) {
2704 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2705 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002706
Chris Lattner16cd04d2005-05-12 23:24:06 +00002707 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002708 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002709 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002710 Found = Node;
2711 return;
2712 }
2713
2714 // Otherwise, scan the operands of Node to see if any of them is a call.
2715 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2716 if (UI == E) return;
2717 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002718 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002719
2720 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002721 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002722}
2723
Chris Lattner9530ddc2005-05-13 05:09:11 +00002724/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002725/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002726static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002727 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002728 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002729 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002730 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002731
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002732 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002733 if (TheChain.getValueType() != MVT::Other)
2734 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00002735 if (TheChain.getValueType() != MVT::Other)
2736 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002737
2738 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002739 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002740
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002741 // Make sure to only follow users of our token chain.
2742 SDNode *User = *UI;
2743 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2744 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002745 if (SDNode *Result = FindCallSeqEnd(User))
2746 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002747 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002748 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002749}
2750
Chris Lattner9530ddc2005-05-13 05:09:11 +00002751/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002752/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002753static SDNode *FindCallSeqStart(SDNode *Node) {
2754 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002755 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002756
2757 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2758 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002759 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002760}
2761
2762
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002763/// FindInputOutputChains - If we are replacing an operation with a call we need
2764/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002765/// properly serialize the calls in the block. The returned operand is the
2766/// input chain value for the new call (e.g. the entry node or the previous
2767/// call), and OutChain is set to be the chain node to update to point to the
2768/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002769static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2770 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002771 SDNode *LatestCallSeqStart = Entry.Val;
2772 SDNode *LatestCallSeqEnd = 0;
2773 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2774 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002775
Chris Lattner16cd04d2005-05-12 23:24:06 +00002776 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002777 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002778 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2779 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00002780 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002781 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00002782 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2783 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002784 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00002785 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00002786 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002787
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002788 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002789 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002790 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002791 std::set<SDNode*> Visited;
2792 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002793
Chris Lattner9530ddc2005-05-13 05:09:11 +00002794 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002795 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002796 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002797
Chris Lattner9530ddc2005-05-13 05:09:11 +00002798 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002799}
2800
Jeff Cohen00b168892005-07-27 06:12:32 +00002801/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002802void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2803 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002804 // Nothing to splice it into?
2805 if (OutChain == 0) return;
2806
2807 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2808 //OutChain->dump();
2809
2810 // Form a token factor node merging the old inval and the new inval.
2811 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2812 OutChain->getOperand(0));
2813 // Change the node to refer to the new token.
2814 OutChain->setAdjCallChain(InToken);
2815}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002816
2817
Chris Lattner77e77a62005-01-21 06:05:23 +00002818// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2819// does not fit into a register, return the lo part and set the hi part to the
2820// by-reg argument. If it does fit into a single register, return the result
2821// and leave the Hi part unset.
2822SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2823 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002824 SDNode *OutChain;
2825 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2826 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002827 if (InChain.Val == 0)
2828 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002829
Chris Lattner77e77a62005-01-21 06:05:23 +00002830 TargetLowering::ArgListTy Args;
2831 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2832 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2833 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2834 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2835 }
2836 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002837
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002838 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002839 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002840 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002841 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2842 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002843
Chris Lattner99c25b82005-09-02 20:26:58 +00002844 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002845 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002846 default: assert(0 && "Unknown thing");
2847 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002848 Result = CallInfo.first;
2849 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002850 case Promote:
2851 assert(0 && "Cannot promote this yet!");
2852 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002853 ExpandOp(CallInfo.first, Result, Hi);
2854 CallInfo.second = LegalizeOp(CallInfo.second);
2855 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002856 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002857
2858 SpliceCallInto(CallInfo.second, OutChain);
2859 NeedsAnotherIteration = true;
2860 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002861}
2862
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002863
Chris Lattner77e77a62005-01-21 06:05:23 +00002864/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2865/// destination type is legal.
2866SDOperand SelectionDAGLegalize::
2867ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002868 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002869 assert(getTypeAction(Source.getValueType()) == Expand &&
2870 "This is not an expansion!");
2871 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2872
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002873 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002874 assert(Source.getValueType() == MVT::i64 &&
2875 "This only works for 64-bit -> FP");
2876 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2877 // incoming integer is set. To handle this, we dynamically test to see if
2878 // it is set, and, if so, add a fudge factor.
2879 SDOperand Lo, Hi;
2880 ExpandOp(Source, Lo, Hi);
2881
Chris Lattner66de05b2005-05-13 04:45:13 +00002882 // If this is unsigned, and not supported, first perform the conversion to
2883 // signed, then adjust the result if the sign bit is set.
2884 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2885 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2886
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002887 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2888 DAG.getConstant(0, Hi.getValueType()),
2889 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002890 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2891 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2892 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002893 uint64_t FF = 0x5f800000ULL;
2894 if (TLI.isLittleEndian()) FF <<= 32;
2895 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002896
Chris Lattner5839bf22005-08-26 17:15:30 +00002897 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002898 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2899 SDOperand FudgeInReg;
2900 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002901 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2902 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002903 else {
2904 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002905 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2906 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002907 }
Chris Lattner473a9902005-09-29 06:44:39 +00002908 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002909 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002910
Chris Lattnera88a2602005-05-14 05:33:54 +00002911 // Check to see if the target has a custom way to lower this. If so, use it.
2912 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2913 default: assert(0 && "This action not implemented for this operation!");
2914 case TargetLowering::Legal:
2915 case TargetLowering::Expand:
2916 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002917 case TargetLowering::Custom: {
2918 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2919 Source), DAG);
2920 if (NV.Val)
2921 return LegalizeOp(NV);
2922 break; // The target decided this was legal after all
2923 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002924 }
2925
Chris Lattner13689e22005-05-12 07:00:44 +00002926 // Expand the source, then glue it back together for the call. We must expand
2927 // the source in case it is shared (this pass of legalize must traverse it).
2928 SDOperand SrcLo, SrcHi;
2929 ExpandOp(Source, SrcLo, SrcHi);
2930 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2931
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002932 SDNode *OutChain = 0;
2933 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2934 DAG.getEntryNode());
2935 const char *FnName = 0;
2936 if (DestTy == MVT::f32)
2937 FnName = "__floatdisf";
2938 else {
2939 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2940 FnName = "__floatdidf";
2941 }
2942
Chris Lattner77e77a62005-01-21 06:05:23 +00002943 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2944
2945 TargetLowering::ArgListTy Args;
2946 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002947
Chris Lattner77e77a62005-01-21 06:05:23 +00002948 Args.push_back(std::make_pair(Source, ArgTy));
2949
2950 // We don't care about token chains for libcalls. We just use the entry
2951 // node as our input and ignore the output chain. This allows us to place
2952 // calls wherever we need them to satisfy data dependences.
2953 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002954
2955 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002956 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2957 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002958
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002959 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002960 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002961}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002962
Chris Lattnere34b3962005-01-19 04:19:40 +00002963
2964
Chris Lattner3e928bb2005-01-07 07:47:09 +00002965/// ExpandOp - Expand the specified SDOperand into its two component pieces
2966/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2967/// LegalizeNodes map is filled in for any results that are not expanded, the
2968/// ExpandedNodes map is filled in for any results that are expanded, and the
2969/// Lo/Hi values are returned.
2970void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2971 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002972 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002973 SDNode *Node = Op.Val;
2974 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2975 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2976 assert(MVT::isInteger(NVT) && NVT < VT &&
2977 "Cannot expand to FP value or to larger int value!");
2978
Chris Lattner6fdcb252005-09-02 20:32:45 +00002979 // See if we already expanded it.
2980 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2981 = ExpandedNodes.find(Op);
2982 if (I != ExpandedNodes.end()) {
2983 Lo = I->second.first;
2984 Hi = I->second.second;
2985 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002986 }
2987
Chris Lattner4e6c7462005-01-08 19:27:05 +00002988 // Expanding to multiple registers needs to perform an optimization step, and
2989 // is not careful to avoid operations the target does not support. Make sure
2990 // that all generated operations are legalized in the next iteration.
2991 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002992
Chris Lattner3e928bb2005-01-07 07:47:09 +00002993 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002994 case ISD::CopyFromReg:
2995 assert(0 && "CopyFromReg must be legal!");
2996 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002997 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2998 assert(0 && "Do not know how to expand this operator!");
2999 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003000 case ISD::UNDEF:
3001 Lo = DAG.getNode(ISD::UNDEF, NVT);
3002 Hi = DAG.getNode(ISD::UNDEF, NVT);
3003 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003004 case ISD::Constant: {
3005 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3006 Lo = DAG.getConstant(Cst, NVT);
3007 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3008 break;
3009 }
3010
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003011 case ISD::BUILD_PAIR:
3012 // Legalize both operands. FIXME: in the future we should handle the case
3013 // where the two elements are not legal.
3014 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3015 Lo = LegalizeOp(Node->getOperand(0));
3016 Hi = LegalizeOp(Node->getOperand(1));
3017 break;
3018
Chris Lattneredb1add2005-05-11 04:51:16 +00003019 case ISD::CTPOP:
3020 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003021 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3022 DAG.getNode(ISD::CTPOP, NVT, Lo),
3023 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003024 Hi = DAG.getConstant(0, NVT);
3025 break;
3026
Chris Lattner39a8f332005-05-12 19:05:01 +00003027 case ISD::CTLZ: {
3028 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003029 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003030 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3031 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003032 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3033 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003034 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3035 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3036
3037 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3038 Hi = DAG.getConstant(0, NVT);
3039 break;
3040 }
3041
3042 case ISD::CTTZ: {
3043 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003044 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003045 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3046 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003047 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3048 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003049 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3050 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3051
3052 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3053 Hi = DAG.getConstant(0, NVT);
3054 break;
3055 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003056
Chris Lattner3e928bb2005-01-07 07:47:09 +00003057 case ISD::LOAD: {
3058 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3059 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003060 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003061
3062 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003063 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003064 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3065 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003066 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003067 //are from the same instruction?
3068 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003069
3070 // Build a factor node to remember that this load is independent of the
3071 // other one.
3072 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3073 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003074
Chris Lattner3e928bb2005-01-07 07:47:09 +00003075 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003076 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003077 if (!TLI.isLittleEndian())
3078 std::swap(Lo, Hi);
3079 break;
3080 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003081 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003082 case ISD::CALL: {
3083 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3084 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3085
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003086 bool Changed = false;
3087 std::vector<SDOperand> Ops;
3088 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3089 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3090 Changed |= Ops.back() != Node->getOperand(i);
3091 }
3092
Chris Lattner3e928bb2005-01-07 07:47:09 +00003093 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3094 "Can only expand a call once so far, not i64 -> i16!");
3095
3096 std::vector<MVT::ValueType> RetTyVTs;
3097 RetTyVTs.reserve(3);
3098 RetTyVTs.push_back(NVT);
3099 RetTyVTs.push_back(NVT);
3100 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003101 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3102 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003103 Lo = SDOperand(NC, 0);
3104 Hi = SDOperand(NC, 1);
3105
3106 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003107 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003108 break;
3109 }
3110 case ISD::AND:
3111 case ISD::OR:
3112 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3113 SDOperand LL, LH, RL, RH;
3114 ExpandOp(Node->getOperand(0), LL, LH);
3115 ExpandOp(Node->getOperand(1), RL, RH);
3116 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3117 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3118 break;
3119 }
3120 case ISD::SELECT: {
3121 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003122
3123 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3124 case Expand: assert(0 && "It's impossible to expand bools");
3125 case Legal:
3126 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3127 break;
3128 case Promote:
3129 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3130 break;
3131 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003132 ExpandOp(Node->getOperand(1), LL, LH);
3133 ExpandOp(Node->getOperand(2), RL, RH);
3134 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3135 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3136 break;
3137 }
Nate Begeman9373a812005-08-10 20:51:12 +00003138 case ISD::SELECT_CC: {
3139 SDOperand TL, TH, FL, FH;
3140 ExpandOp(Node->getOperand(2), TL, TH);
3141 ExpandOp(Node->getOperand(3), FL, FH);
3142 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3143 Node->getOperand(1), TL, FL, Node->getOperand(4));
3144 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3145 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003146 Lo = LegalizeOp(Lo);
3147 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003148 break;
3149 }
Nate Begeman144ff662005-10-13 17:15:37 +00003150 case ISD::SEXTLOAD: {
3151 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3152 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3153 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3154
3155 if (EVT == NVT)
3156 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3157 else
3158 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3159 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003160
3161 // Remember that we legalized the chain.
3162 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3163
Nate Begeman144ff662005-10-13 17:15:37 +00003164 // The high part is obtained by SRA'ing all but one of the bits of the lo
3165 // part.
3166 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3167 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3168 TLI.getShiftAmountTy()));
3169 Lo = LegalizeOp(Lo);
3170 Hi = LegalizeOp(Hi);
3171 break;
3172 }
3173 case ISD::ZEXTLOAD: {
3174 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3175 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3176 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3177
3178 if (EVT == NVT)
3179 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3180 else
3181 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3182 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003183
3184 // Remember that we legalized the chain.
3185 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3186
Nate Begeman144ff662005-10-13 17:15:37 +00003187 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003188 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003189 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003190 break;
3191 }
3192 case ISD::EXTLOAD: {
3193 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3194 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3195 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3196
3197 if (EVT == NVT)
3198 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3199 else
3200 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3201 EVT);
3202
3203 // Remember that we legalized the chain.
3204 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3205
3206 // The high part is undefined.
3207 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3208 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003209 break;
3210 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003211 case ISD::ANY_EXTEND: {
3212 SDOperand In;
3213 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3214 case Expand: assert(0 && "expand-expand not implemented yet!");
3215 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3216 case Promote:
3217 In = PromoteOp(Node->getOperand(0));
3218 break;
3219 }
3220
3221 // The low part is any extension of the input (which degenerates to a copy).
3222 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3223 // The high part is undefined.
3224 Hi = DAG.getNode(ISD::UNDEF, NVT);
3225 break;
3226 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003227 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003228 SDOperand In;
3229 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3230 case Expand: assert(0 && "expand-expand not implemented yet!");
3231 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3232 case Promote:
3233 In = PromoteOp(Node->getOperand(0));
3234 // Emit the appropriate sign_extend_inreg to get the value we want.
3235 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003236 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003237 break;
3238 }
3239
Chris Lattner3e928bb2005-01-07 07:47:09 +00003240 // The low part is just a sign extension of the input (which degenerates to
3241 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003242 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003243
Chris Lattner3e928bb2005-01-07 07:47:09 +00003244 // The high part is obtained by SRA'ing all but one of the bits of the lo
3245 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003246 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003247 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3248 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003249 break;
3250 }
Chris Lattner06098e02005-04-03 23:41:52 +00003251 case ISD::ZERO_EXTEND: {
3252 SDOperand In;
3253 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3254 case Expand: assert(0 && "expand-expand not implemented yet!");
3255 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3256 case Promote:
3257 In = PromoteOp(Node->getOperand(0));
3258 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003259 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003260 break;
3261 }
3262
Chris Lattner3e928bb2005-01-07 07:47:09 +00003263 // The low part is just a zero extension of the input (which degenerates to
3264 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003265 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003266
Chris Lattner3e928bb2005-01-07 07:47:09 +00003267 // The high part is just a zero.
3268 Hi = DAG.getConstant(0, NVT);
3269 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003270 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003271 // These operators cannot be expanded directly, emit them as calls to
3272 // library functions.
3273 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003274 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003275 SDOperand Op;
3276 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3277 case Expand: assert(0 && "cannot expand FP!");
3278 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3279 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3280 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003281
Chris Lattnerf20d1832005-07-30 01:40:57 +00003282 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3283
Chris Lattner80a3e942005-07-29 00:33:32 +00003284 // Now that the custom expander is done, expand the result, which is still
3285 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003286 if (Op.Val) {
3287 ExpandOp(Op, Lo, Hi);
3288 break;
3289 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003290 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003291
Chris Lattner4e6c7462005-01-08 19:27:05 +00003292 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003293 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003294 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003295 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003296 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003297
Chris Lattner4e6c7462005-01-08 19:27:05 +00003298 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003299 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3300 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3301 LegalizeOp(Node->getOperand(0)));
3302 // Now that the custom expander is done, expand the result, which is still
3303 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003304 Op = TLI.LowerOperation(Op, DAG);
3305 if (Op.Val) {
3306 ExpandOp(Op, Lo, Hi);
3307 break;
3308 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003309 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003310
Chris Lattner4e6c7462005-01-08 19:27:05 +00003311 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003312 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003313 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003314 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003315 break;
3316
Chris Lattnere34b3962005-01-19 04:19:40 +00003317 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003318 // If the target wants custom lowering, do so.
3319 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3320 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3321 LegalizeOp(Node->getOperand(1)));
3322 Op = TLI.LowerOperation(Op, DAG);
3323 if (Op.Val) {
3324 // Now that the custom expander is done, expand the result, which is
3325 // still VT.
3326 ExpandOp(Op, Lo, Hi);
3327 break;
3328 }
3329 }
3330
Chris Lattnere34b3962005-01-19 04:19:40 +00003331 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003332 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003333 break;
Chris Lattner47599822005-04-02 03:38:53 +00003334
3335 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003336 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003337 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3338 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003339 break;
3340 }
3341
Chris Lattnere34b3962005-01-19 04:19:40 +00003342 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003343 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003344 break;
3345
3346 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003347 // If the target wants custom lowering, do so.
3348 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3349 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3350 LegalizeOp(Node->getOperand(1)));
3351 Op = TLI.LowerOperation(Op, DAG);
3352 if (Op.Val) {
3353 // Now that the custom expander is done, expand the result, which is
3354 // still VT.
3355 ExpandOp(Op, Lo, Hi);
3356 break;
3357 }
3358 }
3359
Chris Lattnere34b3962005-01-19 04:19:40 +00003360 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003361 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003362 break;
Chris Lattner47599822005-04-02 03:38:53 +00003363
3364 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003365 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003366 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3367 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003368 break;
3369 }
3370
Chris Lattnere34b3962005-01-19 04:19:40 +00003371 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003372 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003373 break;
3374 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003375 // If the target wants custom lowering, do so.
3376 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3377 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3378 LegalizeOp(Node->getOperand(1)));
3379 Op = TLI.LowerOperation(Op, DAG);
3380 if (Op.Val) {
3381 // Now that the custom expander is done, expand the result, which is
3382 // still VT.
3383 ExpandOp(Op, Lo, Hi);
3384 break;
3385 }
3386 }
3387
Chris Lattnere34b3962005-01-19 04:19:40 +00003388 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003389 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003390 break;
Chris Lattner47599822005-04-02 03:38:53 +00003391
3392 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003393 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003394 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3395 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003396 break;
3397 }
3398
Chris Lattnere34b3962005-01-19 04:19:40 +00003399 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003400 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003401 break;
3402
Misha Brukmanedf128a2005-04-21 22:36:52 +00003403 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003404 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3405 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003406 break;
3407 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003408 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3409 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003410 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003411 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003412 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003413 SDOperand LL, LH, RL, RH;
3414 ExpandOp(Node->getOperand(0), LL, LH);
3415 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003416 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3417 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3418 // extended the sign bit of the low half through the upper half, and if so
3419 // emit a MULHS instead of the alternate sequence that is valid for any
3420 // i64 x i64 multiply.
3421 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3422 // is RH an extension of the sign bit of RL?
3423 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3424 RH.getOperand(1).getOpcode() == ISD::Constant &&
3425 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3426 // is LH an extension of the sign bit of LL?
3427 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3428 LH.getOperand(1).getOpcode() == ISD::Constant &&
3429 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3430 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3431 } else {
3432 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3433 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3434 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3435 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3436 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3437 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003438 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3439 } else {
3440 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3441 }
3442 break;
3443 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003444 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3445 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3446 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3447 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003448 }
3449
3450 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003451 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3452 std::make_pair(Lo, Hi))).second;
3453 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003454}
3455
3456
3457// SelectionDAG::Legalize - This is the entry point for the file.
3458//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003459void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003460 /// run - This is the main entry point to this class.
3461 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003462 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003463}
3464