blob: 3fcf45afe384d51043420de4c814b96b1dd05e98 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
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 Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
152
Chris Lattner4add7e32005-01-23 04:42:50 +0000153SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
154 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
155 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000156 assert(MVT::LAST_VALUETYPE <= 16 &&
157 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000158}
159
Jim Laskeyf2516a92005-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 Lattnere3e847b2005-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 Laskeyf2516a92005-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 Laskey686d6a12005-08-17 17:42:52 +0000207 SDOperand Bias = DAG.getConstantFP(isSigned ?
208 BitsToDouble(0x4330000080000000ULL)
209 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000210 MVT::f64);
211 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000212 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-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 Lattnere3e847b2005-07-16 00:19:57 +0000227 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000228
Chris Lattnerd47675e2005-08-09 20:20:18 +0000229 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
230 DAG.getConstant(0, Op0.getValueType()),
231 ISD::SETLT);
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000235
Jim Laskeyf2516a92005-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 Lattnerb35912e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000247 if (TLI.isLittleEndian()) FF <<= 32;
248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000249
Chris Lattnerc30405e2005-08-26 17:15:30 +0000250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000262
Chris Lattnere3e847b2005-07-16 00:19:57 +0000263 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000264 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000265}
266
Chris Lattner19732782005-08-16 18:17:10 +0000267/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000268/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000272SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
273 MVT::ValueType DestVT,
274 bool isSigned) {
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000277
Chris Lattnere3e847b2005-07-16 00:19:57 +0000278 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000279
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000284
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000289 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000297 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000298
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000303 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000311
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000317
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000321 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
322 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000323}
324
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000335
Chris Lattner44fe26f2005-07-29 00:11:56 +0000336 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000337
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000342
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000347 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000355
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000360 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000368
Chris Lattner44fe26f2005-07-29 00:11:56 +0000369 // Otherwise, try a larger type.
370 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000371
Chris Lattner44fe26f2005-07-29 00:11:56 +0000372 // Make sure to legalize any nodes we create here in the next pass.
373 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000374
Chris Lattner44fe26f2005-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 Lattner4bbbb9e2005-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 Lattner44fe26f2005-07-29 00:11:56 +0000402
Chris Lattnerdc750592005-01-07 07:47:09 +0000403void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner9cfccfb2005-10-02 17:49:46 +0000412
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000413 // Compute ordering from all of the leaves in the graphs, those (like the
414 // entry node) that have no operands.
415 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
416 E = DAG.allnodes_end(); I != E; ++I) {
417 if ((*I)->getNumOperands() == 0) {
418 Visited[*I] = 0 - 1U;
419 ComputeTopDownOrdering(*I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000420 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000421 }
422
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000423 assert(Order.size() == Visited.size() && Order.size() == DAG.allnodes_size()&&
424 "Error: DAG is cyclic!");
425 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000426
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000427 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
428 SDNode *N = Order[i];
429 switch (getTypeAction(N->getValueType(0))) {
430 default: assert(0 && "Bad type action!");
431 case Legal:
432 LegalizeOp(SDOperand(N, 0));
433 break;
434 case Promote:
435 PromoteOp(SDOperand(N, 0));
436 break;
437 case Expand: {
438 SDOperand X, Y;
439 ExpandOp(SDOperand(N, 0), X, Y);
440 break;
441 }
442 }
443 }
444
445 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000446 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000447 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
448 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000449
450 ExpandedNodes.clear();
451 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000452 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000453
454 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000455 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000456}
457
458SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000459 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000460 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000461 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000462
Chris Lattnerdc750592005-01-07 07:47:09 +0000463 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000464 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000465 if (Node->getNumValues() > 1) {
466 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
467 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000468 case Legal: break; // Nothing to do.
469 case Expand: {
470 SDOperand T1, T2;
471 ExpandOp(Op.getValue(i), T1, T2);
472 assert(LegalizedNodes.count(Op) &&
473 "Expansion didn't add legal operands!");
474 return LegalizedNodes[Op];
475 }
476 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000477 PromoteOp(Op.getValue(i));
478 assert(LegalizedNodes.count(Op) &&
479 "Expansion didn't add legal operands!");
480 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000481 }
482 }
483
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000484 // Note that LegalizeOp may be reentered even from single-use nodes, which
485 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000486 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
487 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000488
Nate Begemane5b86d72005-08-10 20:51:12 +0000489 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000490
491 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000492
493 switch (Node->getOpcode()) {
494 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000495 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
496 // If this is a target node, legalize it by legalizing the operands then
497 // passing it through.
498 std::vector<SDOperand> Ops;
499 bool Changed = false;
500 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
501 Ops.push_back(LegalizeOp(Node->getOperand(i)));
502 Changed = Changed || Node->getOperand(i) != Ops.back();
503 }
504 if (Changed)
505 if (Node->getNumValues() == 1)
506 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
507 else {
508 std::vector<MVT::ValueType> VTs(Node->value_begin(),
509 Node->value_end());
510 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
511 }
512
513 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
514 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
515 return Result.getValue(Op.ResNo);
516 }
517 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000518 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
519 assert(0 && "Do not know how to legalize this operator!");
520 abort();
521 case ISD::EntryToken:
522 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000523 case ISD::TargetFrameIndex:
524 case ISD::Register:
525 case ISD::TargetConstant:
Chris Lattnerdc750592005-01-07 07:47:09 +0000526 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000527 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000528 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000529 case ISD::BasicBlock:
530 case ISD::CONDCODE:
531 case ISD::VALUETYPE:
532 case ISD::SRCVALUE:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000533 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000534 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000535 case ISD::AssertSext:
536 case ISD::AssertZext:
537 Tmp1 = LegalizeOp(Node->getOperand(0));
538 if (Tmp1 != Node->getOperand(0))
539 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
540 Node->getOperand(1));
541 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000542 case ISD::CopyFromReg:
543 Tmp1 = LegalizeOp(Node->getOperand(0));
544 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000545 Result = DAG.getCopyFromReg(Tmp1,
546 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
547 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000548 else
549 Result = Op.getValue(0);
550
551 // Since CopyFromReg produces two values, make sure to remember that we
552 // legalized both of them.
553 AddLegalizedOperand(Op.getValue(0), Result);
554 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
555 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000556 case ISD::ImplicitDef:
557 Tmp1 = LegalizeOp(Node->getOperand(0));
558 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000559 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
560 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000561 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000562 case ISD::UNDEF: {
563 MVT::ValueType VT = Op.getValueType();
564 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000565 default: assert(0 && "This action is not supported yet!");
566 case TargetLowering::Expand:
567 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000568 if (MVT::isInteger(VT))
569 Result = DAG.getConstant(0, VT);
570 else if (MVT::isFloatingPoint(VT))
571 Result = DAG.getConstantFP(0, VT);
572 else
573 assert(0 && "Unknown value type!");
574 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000575 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000576 break;
577 }
578 break;
579 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000580 case ISD::Constant:
581 // We know we don't need to expand constants here, constants only have one
582 // value and we check that it is fine above.
583
584 // FIXME: Maybe we should handle things like targets that don't support full
585 // 32-bit immediates?
586 break;
587 case ISD::ConstantFP: {
588 // Spill FP immediates to the constant pool if the target cannot directly
589 // codegen them. Targets often have some immediate values that can be
590 // efficiently generated into an FP register without a load. We explicitly
591 // leave these constants as ConstantFP nodes for the target to deal with.
592
593 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
594
595 // Check to see if this FP immediate is already legal.
596 bool isLegal = false;
597 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
598 E = TLI.legal_fpimm_end(); I != E; ++I)
599 if (CFP->isExactlyValue(*I)) {
600 isLegal = true;
601 break;
602 }
603
604 if (!isLegal) {
605 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000606 bool Extend = false;
607
608 // If a FP immediate is precise when represented as a float, we put it
609 // into the constant pool as a float, even if it's is statically typed
610 // as a double.
611 MVT::ValueType VT = CFP->getValueType(0);
612 bool isDouble = VT == MVT::f64;
613 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
614 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000615 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
616 // Only do this if the target has a native EXTLOAD instruction from
617 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000618 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000619 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
620 VT = MVT::f32;
621 Extend = true;
622 }
Misha Brukman835702a2005-04-21 22:36:52 +0000623
Chris Lattnerc30405e2005-08-26 17:15:30 +0000624 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000625 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000626 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
627 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000628 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000629 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
630 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000631 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000632 }
633 break;
634 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000635 case ISD::TokenFactor: {
636 std::vector<SDOperand> Ops;
637 bool Changed = false;
Nate Begeman02b23c62005-10-13 03:11:28 +0000638 // Legalize the operands
Chris Lattner05b4e372005-01-13 17:59:25 +0000639 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000640 SDOperand Op = Node->getOperand(i);
Nate Begeman02b23c62005-10-13 03:11:28 +0000641 Ops.push_back(LegalizeOp(Op));
642 Changed |= Ops[i] != Op;
Chris Lattner05b4e372005-01-13 17:59:25 +0000643 }
644 if (Changed)
645 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
646 break;
647 }
648
Chris Lattner2dce7032005-05-12 23:24:06 +0000649 case ISD::CALLSEQ_START:
650 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000652 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000653 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000654 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000655 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000656
Chris Lattner2dce7032005-05-12 23:24:06 +0000657 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000658 // nodes are treated specially and are mutated in place. This makes the dag
659 // legalization process more efficient and also makes libcall insertion
660 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000661 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000662 case ISD::DYNAMIC_STACKALLOC:
663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
665 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
666 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000667 Tmp3 != Node->getOperand(2)) {
668 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
669 std::vector<SDOperand> Ops;
670 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
671 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
672 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000673 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000674
675 // Since this op produces two values, make sure to remember that we
676 // legalized both of them.
677 AddLegalizedOperand(SDOperand(Node, 0), Result);
678 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
679 return Result.getValue(Op.ResNo);
680
Chris Lattnerd0feb642005-05-13 18:43:43 +0000681 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000682 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
684 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000685
686 bool Changed = false;
687 std::vector<SDOperand> Ops;
688 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
689 Ops.push_back(LegalizeOp(Node->getOperand(i)));
690 Changed |= Ops.back() != Node->getOperand(i);
691 }
692
693 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000694 std::vector<MVT::ValueType> RetTyVTs;
695 RetTyVTs.reserve(Node->getNumValues());
696 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000697 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000698 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
699 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000700 } else {
701 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000702 }
Chris Lattner9242c502005-01-09 19:43:23 +0000703 // Since calls produce multiple values, make sure to remember that we
704 // legalized all of them.
705 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
706 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
707 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000708 }
Chris Lattner68a12142005-01-07 22:12:08 +0000709 case ISD::BR:
710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
711 if (Tmp1 != Node->getOperand(0))
712 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
713 break;
714
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000715 case ISD::BRCOND:
716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000717
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000718 switch (getTypeAction(Node->getOperand(1).getValueType())) {
719 case Expand: assert(0 && "It's impossible to expand bools");
720 case Legal:
721 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
722 break;
723 case Promote:
724 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
725 break;
726 }
Nate Begeman371e4952005-08-16 19:49:35 +0000727
728 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
729 default: assert(0 && "This action is not supported yet!");
730 case TargetLowering::Expand:
731 // Expand brcond's setcc into its constituent parts and create a BR_CC
732 // Node.
733 if (Tmp2.getOpcode() == ISD::SETCC) {
734 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
735 Tmp2.getOperand(0), Tmp2.getOperand(1),
736 Node->getOperand(2));
737 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000738 // Make sure the condition is either zero or one. It may have been
739 // promoted from something else.
740 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
741
Nate Begeman371e4952005-08-16 19:49:35 +0000742 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
743 DAG.getCondCode(ISD::SETNE), Tmp2,
744 DAG.getConstant(0, Tmp2.getValueType()),
745 Node->getOperand(2));
746 }
747 break;
748 case TargetLowering::Legal:
749 // Basic block destination (Op#2) is always legal.
750 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
751 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
752 Node->getOperand(2));
753 break;
754 }
755 break;
756 case ISD::BR_CC:
757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
758
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000759 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000760 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
761 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
762 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
763 Tmp3 != Node->getOperand(3)) {
764 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
765 Tmp2, Tmp3, Node->getOperand(4));
766 }
767 break;
768 } else {
769 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
770 Node->getOperand(2), // LHS
771 Node->getOperand(3), // RHS
772 Node->getOperand(1)));
773 // If we get a SETCC back from legalizing the SETCC node we just
774 // created, then use its LHS, RHS, and CC directly in creating a new
775 // node. Otherwise, select between the true and false value based on
776 // comparing the result of the legalized with zero.
777 if (Tmp2.getOpcode() == ISD::SETCC) {
778 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
779 Tmp2.getOperand(0), Tmp2.getOperand(1),
780 Node->getOperand(4));
781 } else {
782 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
783 DAG.getCondCode(ISD::SETNE),
784 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
785 Node->getOperand(4));
786 }
787 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000788 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000789 case ISD::BRCONDTWOWAY:
790 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
791 switch (getTypeAction(Node->getOperand(1).getValueType())) {
792 case Expand: assert(0 && "It's impossible to expand bools");
793 case Legal:
794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
795 break;
796 case Promote:
797 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
798 break;
799 }
800 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
801 // pair.
802 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
803 case TargetLowering::Promote:
804 default: assert(0 && "This action is not supported yet!");
805 case TargetLowering::Legal:
806 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
807 std::vector<SDOperand> Ops;
808 Ops.push_back(Tmp1);
809 Ops.push_back(Tmp2);
810 Ops.push_back(Node->getOperand(2));
811 Ops.push_back(Node->getOperand(3));
812 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
813 }
814 break;
815 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000816 // If BRTWOWAY_CC is legal for this target, then simply expand this node
817 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
818 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000819 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000820 if (Tmp2.getOpcode() == ISD::SETCC) {
821 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
822 Tmp2.getOperand(0), Tmp2.getOperand(1),
823 Node->getOperand(2), Node->getOperand(3));
824 } else {
825 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
826 DAG.getConstant(0, Tmp2.getValueType()),
827 Node->getOperand(2), Node->getOperand(3));
828 }
829 } else {
830 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000831 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000832 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
833 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000834 break;
835 }
836 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000837 case ISD::BRTWOWAY_CC:
838 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000839 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000840 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
841 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
842 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
843 Tmp3 != Node->getOperand(3)) {
844 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
845 Node->getOperand(4), Node->getOperand(5));
846 }
847 break;
848 } else {
849 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
850 Node->getOperand(2), // LHS
851 Node->getOperand(3), // RHS
852 Node->getOperand(1)));
853 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
854 // pair.
855 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
856 default: assert(0 && "This action is not supported yet!");
857 case TargetLowering::Legal:
858 // If we get a SETCC back from legalizing the SETCC node we just
859 // created, then use its LHS, RHS, and CC directly in creating a new
860 // node. Otherwise, select between the true and false value based on
861 // comparing the result of the legalized with zero.
862 if (Tmp2.getOpcode() == ISD::SETCC) {
863 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
864 Tmp2.getOperand(0), Tmp2.getOperand(1),
865 Node->getOperand(4), Node->getOperand(5));
866 } else {
867 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
868 DAG.getConstant(0, Tmp2.getValueType()),
869 Node->getOperand(4), Node->getOperand(5));
870 }
871 break;
872 case TargetLowering::Expand:
873 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
874 Node->getOperand(4));
875 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
876 break;
877 }
878 }
879 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000880 case ISD::LOAD:
881 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
882 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000883
Chris Lattnerdc750592005-01-07 07:47:09 +0000884 if (Tmp1 != Node->getOperand(0) ||
885 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000886 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
887 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000888 else
889 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000890
Chris Lattnerea4ca942005-01-07 22:28:47 +0000891 // Since loads produce two values, make sure to remember that we legalized
892 // both of them.
893 AddLegalizedOperand(SDOperand(Node, 0), Result);
894 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
895 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000896
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000897 case ISD::EXTLOAD:
898 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000899 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000900 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
901 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000902
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000903 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000904 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000905 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000906 case TargetLowering::Promote:
907 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000908 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
909 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000910 // Since loads produce two values, make sure to remember that we legalized
911 // both of them.
912 AddLegalizedOperand(SDOperand(Node, 0), Result);
913 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
914 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000915
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000916 case TargetLowering::Legal:
917 if (Tmp1 != Node->getOperand(0) ||
918 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000919 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
920 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000921 else
922 Result = SDOperand(Node, 0);
923
924 // Since loads produce two values, make sure to remember that we legalized
925 // both of them.
926 AddLegalizedOperand(SDOperand(Node, 0), Result);
927 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
928 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000929 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000930 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
931 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
932 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000933 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000934 if (Op.ResNo)
935 return Load.getValue(1);
936 return Result;
937 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000938 assert(Node->getOpcode() != ISD::EXTLOAD &&
939 "EXTLOAD should always be supported!");
940 // Turn the unsupported load into an EXTLOAD followed by an explicit
941 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000942 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
943 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000944 SDOperand ValRes;
945 if (Node->getOpcode() == ISD::SEXTLOAD)
946 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000947 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000948 else
949 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000950 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
951 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
952 if (Op.ResNo)
953 return Result.getValue(1);
954 return ValRes;
955 }
956 assert(0 && "Unreachable");
957 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000958 case ISD::EXTRACT_ELEMENT:
959 // Get both the low and high parts.
960 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
961 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
962 Result = Tmp2; // 1 -> Hi
963 else
964 Result = Tmp1; // 0 -> Lo
965 break;
966
967 case ISD::CopyToReg:
968 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000969
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000970 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +0000971 "Register type must be legal!");
972 // Legalize the incoming value (must be legal).
973 Tmp2 = LegalizeOp(Node->getOperand(2));
974 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
975 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
976 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000977 break;
978
979 case ISD::RET:
980 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
981 switch (Node->getNumOperands()) {
982 case 2: // ret val
983 switch (getTypeAction(Node->getOperand(1).getValueType())) {
984 case Legal:
985 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000986 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000987 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
988 break;
989 case Expand: {
990 SDOperand Lo, Hi;
991 ExpandOp(Node->getOperand(1), Lo, Hi);
992 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000993 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000994 }
995 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000996 Tmp2 = PromoteOp(Node->getOperand(1));
997 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
998 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000999 }
1000 break;
1001 case 1: // ret void
1002 if (Tmp1 != Node->getOperand(0))
1003 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1004 break;
1005 default: { // ret <values>
1006 std::vector<SDOperand> NewValues;
1007 NewValues.push_back(Tmp1);
1008 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1009 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1010 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001011 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001012 break;
1013 case Expand: {
1014 SDOperand Lo, Hi;
1015 ExpandOp(Node->getOperand(i), Lo, Hi);
1016 NewValues.push_back(Lo);
1017 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001018 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001019 }
1020 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001021 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001022 }
1023 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1024 break;
1025 }
1026 }
1027 break;
1028 case ISD::STORE:
1029 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1030 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1031
Chris Lattnere69daaf2005-01-08 06:25:56 +00001032 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001033 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001034 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001035 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001036 DAG.getConstant(FloatToBits(CFP->getValue()),
1037 MVT::i32),
1038 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001039 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001040 } else {
1041 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001042 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001043 DAG.getConstant(DoubleToBits(CFP->getValue()),
1044 MVT::i64),
1045 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001046 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001047 }
Chris Lattnera4743132005-02-22 07:23:39 +00001048 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001049 }
1050
Chris Lattnerdc750592005-01-07 07:47:09 +00001051 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1052 case Legal: {
1053 SDOperand Val = LegalizeOp(Node->getOperand(1));
1054 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1055 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001056 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1057 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001058 break;
1059 }
1060 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001061 // Truncate the value and store the result.
1062 Tmp3 = PromoteOp(Node->getOperand(1));
1063 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001064 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001065 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001066 break;
1067
Chris Lattnerdc750592005-01-07 07:47:09 +00001068 case Expand:
1069 SDOperand Lo, Hi;
1070 ExpandOp(Node->getOperand(1), Lo, Hi);
1071
1072 if (!TLI.isLittleEndian())
1073 std::swap(Lo, Hi);
1074
Chris Lattner55e9cde2005-05-11 04:51:16 +00001075 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1076 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001077 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001078 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1079 getIntPtrConstant(IncrementSize));
1080 assert(isTypeLegal(Tmp2.getValueType()) &&
1081 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001082 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001083 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1084 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001085 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1086 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001087 }
1088 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001089 case ISD::PCMARKER:
1090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001091 if (Tmp1 != Node->getOperand(0))
1092 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001093 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001094 case ISD::TRUNCSTORE:
1095 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1096 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1097
1098 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1099 case Legal:
1100 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001101
1102 // The only promote case we handle is TRUNCSTORE:i1 X into
1103 // -> TRUNCSTORE:i8 (and X, 1)
1104 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1105 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1106 TargetLowering::Promote) {
1107 // Promote the bool to a mask then store.
1108 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1109 DAG.getConstant(1, Tmp2.getValueType()));
1110 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1111 Node->getOperand(3), DAG.getValueType(MVT::i8));
1112
1113 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1114 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001115 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001116 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001117 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001118 break;
1119 case Promote:
1120 case Expand:
1121 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1122 }
1123 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001124 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001125 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1126 case Expand: assert(0 && "It's impossible to expand bools");
1127 case Legal:
1128 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1129 break;
1130 case Promote:
1131 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1132 break;
1133 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001134 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001135 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001136
Nate Begeman987121a2005-08-23 04:29:48 +00001137 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001138 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001139 case TargetLowering::Expand:
1140 if (Tmp1.getOpcode() == ISD::SETCC) {
1141 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1142 Tmp2, Tmp3,
1143 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1144 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001145 // Make sure the condition is either zero or one. It may have been
1146 // promoted from something else.
1147 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001148 Result = DAG.getSelectCC(Tmp1,
1149 DAG.getConstant(0, Tmp1.getValueType()),
1150 Tmp2, Tmp3, ISD::SETNE);
1151 }
1152 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001153 case TargetLowering::Legal:
1154 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1155 Tmp3 != Node->getOperand(2))
1156 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1157 Tmp1, Tmp2, Tmp3);
1158 break;
1159 case TargetLowering::Promote: {
1160 MVT::ValueType NVT =
1161 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1162 unsigned ExtOp, TruncOp;
1163 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001164 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001165 TruncOp = ISD::TRUNCATE;
1166 } else {
1167 ExtOp = ISD::FP_EXTEND;
1168 TruncOp = ISD::FP_ROUND;
1169 }
1170 // Promote each of the values to the new type.
1171 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1172 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1173 // Perform the larger operation, then round down.
1174 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1175 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1176 break;
1177 }
1178 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001179 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001180 case ISD::SELECT_CC:
1181 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1182 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1183
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001184 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001185 // Everything is legal, see if we should expand this op or something.
1186 switch (TLI.getOperationAction(ISD::SELECT_CC,
1187 Node->getOperand(0).getValueType())) {
1188 default: assert(0 && "This action is not supported yet!");
1189 case TargetLowering::Custom: {
1190 SDOperand Tmp =
1191 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1192 Node->getOperand(0),
1193 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001194 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001195 if (Tmp.Val) {
1196 Result = LegalizeOp(Tmp);
1197 break;
1198 }
1199 } // FALLTHROUGH if the target can't lower this operation after all.
1200 case TargetLowering::Legal:
1201 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1202 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1203 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1204 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1205 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1206 Tmp3, Tmp4, Node->getOperand(4));
1207 }
1208 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001209 }
1210 break;
1211 } else {
1212 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1213 Node->getOperand(0), // LHS
1214 Node->getOperand(1), // RHS
1215 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001216 // If we get a SETCC back from legalizing the SETCC node we just
1217 // created, then use its LHS, RHS, and CC directly in creating a new
1218 // node. Otherwise, select between the true and false value based on
1219 // comparing the result of the legalized with zero.
1220 if (Tmp1.getOpcode() == ISD::SETCC) {
1221 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1222 Tmp1.getOperand(0), Tmp1.getOperand(1),
1223 Tmp3, Tmp4, Tmp1.getOperand(2));
1224 } else {
1225 Result = DAG.getSelectCC(Tmp1,
1226 DAG.getConstant(0, Tmp1.getValueType()),
1227 Tmp3, Tmp4, ISD::SETNE);
1228 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001229 }
1230 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001231 case ISD::SETCC:
1232 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1233 case Legal:
1234 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1235 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001236 break;
1237 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001238 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1239 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1240
1241 // If this is an FP compare, the operands have already been extended.
1242 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1243 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001244 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001245
1246 // Otherwise, we have to insert explicit sign or zero extends. Note
1247 // that we could insert sign extends for ALL conditions, but zero extend
1248 // is cheaper on many machines (an AND instead of two shifts), so prefer
1249 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001250 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001251 default: assert(0 && "Unknown integer comparison!");
1252 case ISD::SETEQ:
1253 case ISD::SETNE:
1254 case ISD::SETUGE:
1255 case ISD::SETUGT:
1256 case ISD::SETULE:
1257 case ISD::SETULT:
1258 // ALL of these operations will work if we either sign or zero extend
1259 // the operands (including the unsigned comparisons!). Zero extend is
1260 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001261 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1262 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001263 break;
1264 case ISD::SETGE:
1265 case ISD::SETGT:
1266 case ISD::SETLT:
1267 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001268 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1269 DAG.getValueType(VT));
1270 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1271 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001272 break;
1273 }
Chris Lattner4d978642005-01-15 22:16:26 +00001274 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001275 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001276 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001277 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1278 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1279 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001280 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001281 case ISD::SETEQ:
1282 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001283 if (RHSLo == RHSHi)
1284 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1285 if (RHSCST->isAllOnesValue()) {
1286 // Comparison to -1.
1287 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001288 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001289 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001290 }
1291
Chris Lattnerdc750592005-01-07 07:47:09 +00001292 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1293 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1294 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001295 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001296 break;
1297 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001298 // If this is a comparison of the sign bit, just look at the top part.
1299 // X > -1, x < 0
1300 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001301 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001302 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001303 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001304 (CST->isAllOnesValue()))) { // X > -1
1305 Tmp1 = LHSHi;
1306 Tmp2 = RHSHi;
1307 break;
1308 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001309
Chris Lattnerdc750592005-01-07 07:47:09 +00001310 // FIXME: This generated code sucks.
1311 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001312 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001313 default: assert(0 && "Unknown integer setcc!");
1314 case ISD::SETLT:
1315 case ISD::SETULT: LowCC = ISD::SETULT; break;
1316 case ISD::SETGT:
1317 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1318 case ISD::SETLE:
1319 case ISD::SETULE: LowCC = ISD::SETULE; break;
1320 case ISD::SETGE:
1321 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1322 }
Misha Brukman835702a2005-04-21 22:36:52 +00001323
Chris Lattnerdc750592005-01-07 07:47:09 +00001324 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1325 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1326 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1327
1328 // NOTE: on targets without efficient SELECT of bools, we can always use
1329 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001330 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1331 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1332 Node->getOperand(2));
1333 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001334 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1335 Result, Tmp1, Tmp2));
1336 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001337 }
1338 }
Nate Begeman987121a2005-08-23 04:29:48 +00001339
1340 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1341 default:
1342 assert(0 && "Cannot handle this action for SETCC yet!");
1343 break;
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001344 case TargetLowering::Promote:
1345 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1346 Node->getOperand(2));
1347 break;
Nate Begeman987121a2005-08-23 04:29:48 +00001348 case TargetLowering::Legal:
1349 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1350 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1351 Node->getOperand(2));
1352 break;
1353 case TargetLowering::Expand:
1354 // Expand a setcc node into a select_cc of the same condition, lhs, and
1355 // rhs that selects between const 1 (true) and const 0 (false).
1356 MVT::ValueType VT = Node->getValueType(0);
1357 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1358 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1359 Node->getOperand(2));
1360 Result = LegalizeOp(Result);
1361 break;
1362 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001363 break;
1364
Chris Lattner85d70c62005-01-11 05:57:22 +00001365 case ISD::MEMSET:
1366 case ISD::MEMCPY:
1367 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001368 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001369 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1370
1371 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1372 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1373 case Expand: assert(0 && "Cannot expand a byte!");
1374 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001375 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001376 break;
1377 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001378 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001379 break;
1380 }
1381 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001382 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001383 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001384
1385 SDOperand Tmp4;
1386 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001387 case Expand: {
1388 // Length is too big, just take the lo-part of the length.
1389 SDOperand HiPart;
1390 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1391 break;
1392 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001393 case Legal:
1394 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001395 break;
1396 case Promote:
1397 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001398 break;
1399 }
1400
1401 SDOperand Tmp5;
1402 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1403 case Expand: assert(0 && "Cannot expand this yet!");
1404 case Legal:
1405 Tmp5 = LegalizeOp(Node->getOperand(4));
1406 break;
1407 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001408 Tmp5 = PromoteOp(Node->getOperand(4));
1409 break;
1410 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001411
1412 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1413 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001414 case TargetLowering::Custom: {
1415 SDOperand Tmp =
1416 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1417 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1418 if (Tmp.Val) {
1419 Result = LegalizeOp(Tmp);
1420 break;
1421 }
1422 // FALLTHROUGH if the target thinks it is legal.
1423 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001424 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001425 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1426 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1427 Tmp5 != Node->getOperand(4)) {
1428 std::vector<SDOperand> Ops;
1429 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1430 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1431 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1432 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001433 break;
1434 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001435 // Otherwise, the target does not support this operation. Lower the
1436 // operation to an explicit libcall as appropriate.
1437 MVT::ValueType IntPtr = TLI.getPointerTy();
1438 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1439 std::vector<std::pair<SDOperand, const Type*> > Args;
1440
Reid Spencer6dced922005-01-12 14:53:45 +00001441 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001442 if (Node->getOpcode() == ISD::MEMSET) {
1443 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1444 // Extend the ubyte argument to be an int value for the call.
1445 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1446 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1447 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1448
1449 FnName = "memset";
1450 } else if (Node->getOpcode() == ISD::MEMCPY ||
1451 Node->getOpcode() == ISD::MEMMOVE) {
1452 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1453 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1454 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1455 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1456 } else {
1457 assert(0 && "Unknown op!");
1458 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001459
Chris Lattner85d70c62005-01-11 05:57:22 +00001460 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001461 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001462 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001463 Result = CallResult.second;
1464 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001465 break;
1466 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001467 }
1468 break;
1469 }
Chris Lattner5385db52005-05-09 20:23:03 +00001470
1471 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001472 Tmp1 = LegalizeOp(Node->getOperand(0));
1473 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001474
Chris Lattner86535992005-05-14 07:45:46 +00001475 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1476 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1477 std::vector<SDOperand> Ops;
1478 Ops.push_back(Tmp1);
1479 Ops.push_back(Tmp2);
1480 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1481 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001482 Result = SDOperand(Node, 0);
1483 // Since these produce two values, make sure to remember that we legalized
1484 // both of them.
1485 AddLegalizedOperand(SDOperand(Node, 0), Result);
1486 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1487 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001488 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001489 Tmp1 = LegalizeOp(Node->getOperand(0));
1490 Tmp2 = LegalizeOp(Node->getOperand(1));
1491 Tmp3 = LegalizeOp(Node->getOperand(2));
1492 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1493 Tmp3 != Node->getOperand(2))
1494 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1495 break;
1496
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001497 case ISD::READIO:
1498 Tmp1 = LegalizeOp(Node->getOperand(0));
1499 Tmp2 = LegalizeOp(Node->getOperand(1));
1500
1501 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1502 case TargetLowering::Custom:
1503 default: assert(0 && "This action not implemented for this operation!");
1504 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001505 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1506 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1507 std::vector<SDOperand> Ops;
1508 Ops.push_back(Tmp1);
1509 Ops.push_back(Tmp2);
1510 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1511 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001512 Result = SDOperand(Node, 0);
1513 break;
1514 case TargetLowering::Expand:
1515 // Replace this with a load from memory.
1516 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1517 Node->getOperand(1), DAG.getSrcValue(NULL));
1518 Result = LegalizeOp(Result);
1519 break;
1520 }
1521
1522 // Since these produce two values, make sure to remember that we legalized
1523 // both of them.
1524 AddLegalizedOperand(SDOperand(Node, 0), Result);
1525 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1526 return Result.getValue(Op.ResNo);
1527
1528 case ISD::WRITEIO:
1529 Tmp1 = LegalizeOp(Node->getOperand(0));
1530 Tmp2 = LegalizeOp(Node->getOperand(1));
1531 Tmp3 = LegalizeOp(Node->getOperand(2));
1532
1533 switch (TLI.getOperationAction(Node->getOpcode(),
1534 Node->getOperand(1).getValueType())) {
1535 case TargetLowering::Custom:
1536 default: assert(0 && "This action not implemented for this operation!");
1537 case TargetLowering::Legal:
1538 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1539 Tmp3 != Node->getOperand(2))
1540 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1541 break;
1542 case TargetLowering::Expand:
1543 // Replace this with a store to memory.
1544 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1545 Node->getOperand(1), Node->getOperand(2),
1546 DAG.getSrcValue(NULL));
1547 Result = LegalizeOp(Result);
1548 break;
1549 }
1550 break;
1551
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001552 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001553 case ISD::SUB_PARTS:
1554 case ISD::SHL_PARTS:
1555 case ISD::SRA_PARTS:
1556 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001557 std::vector<SDOperand> Ops;
1558 bool Changed = false;
1559 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1560 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1561 Changed |= Ops.back() != Node->getOperand(i);
1562 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001563 if (Changed) {
1564 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1565 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1566 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001567
1568 // Since these produce multiple values, make sure to remember that we
1569 // legalized all of them.
1570 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1571 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1572 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001573 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001574
1575 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001576 case ISD::ADD:
1577 case ISD::SUB:
1578 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001579 case ISD::MULHS:
1580 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001581 case ISD::UDIV:
1582 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001583 case ISD::AND:
1584 case ISD::OR:
1585 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001586 case ISD::SHL:
1587 case ISD::SRL:
1588 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001589 case ISD::FADD:
1590 case ISD::FSUB:
1591 case ISD::FMUL:
1592 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001593 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001594 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1595 case Expand: assert(0 && "Not possible");
1596 case Legal:
1597 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1598 break;
1599 case Promote:
1600 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1601 break;
1602 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001603 if (Tmp1 != Node->getOperand(0) ||
1604 Tmp2 != Node->getOperand(1))
1605 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1606 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001607
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001608 case ISD::UREM:
1609 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001610 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001611 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1612 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1613 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1614 case TargetLowering::Legal:
1615 if (Tmp1 != Node->getOperand(0) ||
1616 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001617 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001618 Tmp2);
1619 break;
1620 case TargetLowering::Promote:
1621 case TargetLowering::Custom:
1622 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001623 case TargetLowering::Expand:
1624 if (MVT::isInteger(Node->getValueType(0))) {
1625 MVT::ValueType VT = Node->getValueType(0);
1626 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1627 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1628 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1629 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1630 } else {
1631 // Floating point mod -> fmod libcall.
1632 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1633 SDOperand Dummy;
1634 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001635 }
1636 break;
1637 }
1638 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001639
Andrew Lenharth5e177822005-05-03 17:19:30 +00001640 case ISD::CTPOP:
1641 case ISD::CTTZ:
1642 case ISD::CTLZ:
1643 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1644 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1645 case TargetLowering::Legal:
1646 if (Tmp1 != Node->getOperand(0))
1647 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1648 break;
1649 case TargetLowering::Promote: {
1650 MVT::ValueType OVT = Tmp1.getValueType();
1651 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001652
1653 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001654 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1655 // Perform the larger operation, then subtract if needed.
1656 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1657 switch(Node->getOpcode())
1658 {
1659 case ISD::CTPOP:
1660 Result = Tmp1;
1661 break;
1662 case ISD::CTTZ:
1663 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001664 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1665 DAG.getConstant(getSizeInBits(NVT), NVT),
1666 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001667 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001668 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1669 break;
1670 case ISD::CTLZ:
1671 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001672 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1673 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001674 getSizeInBits(OVT), NVT));
1675 break;
1676 }
1677 break;
1678 }
1679 case TargetLowering::Custom:
1680 assert(0 && "Cannot custom handle this yet!");
1681 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001682 switch(Node->getOpcode())
1683 {
1684 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001685 static const uint64_t mask[6] = {
1686 0x5555555555555555ULL, 0x3333333333333333ULL,
1687 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1688 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1689 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001690 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001691 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1692 unsigned len = getSizeInBits(VT);
1693 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001694 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001695 Tmp2 = DAG.getConstant(mask[i], VT);
1696 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001697 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001698 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1699 DAG.getNode(ISD::AND, VT,
1700 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1701 Tmp2));
1702 }
1703 Result = Tmp1;
1704 break;
1705 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001706 case ISD::CTLZ: {
1707 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001708 x = x | (x >> 1);
1709 x = x | (x >> 2);
1710 ...
1711 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001712 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001713 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001714
Chris Lattner56add052005-05-11 18:35:21 +00001715 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1716 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001717 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1718 unsigned len = getSizeInBits(VT);
1719 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1720 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001721 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001722 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1723 }
1724 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001725 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001726 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001727 }
1728 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001729 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001730 // unless the target has ctlz but not ctpop, in which case we use:
1731 // { return 32 - nlz(~x & (x-1)); }
1732 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001733 MVT::ValueType VT = Tmp1.getValueType();
1734 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001735 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001736 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1737 DAG.getNode(ISD::SUB, VT, Tmp1,
1738 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001739 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001740 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1741 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001742 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001743 DAG.getConstant(getSizeInBits(VT), VT),
1744 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1745 } else {
1746 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1747 }
Chris Lattner72473242005-05-11 05:27:09 +00001748 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001749 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001750 default:
1751 assert(0 && "Cannot expand this yet!");
1752 break;
1753 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001754 break;
1755 }
1756 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001757
Chris Lattner13fe99c2005-04-02 05:00:07 +00001758 // Unary operators
1759 case ISD::FABS:
1760 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001761 case ISD::FSQRT:
1762 case ISD::FSIN:
1763 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001764 Tmp1 = LegalizeOp(Node->getOperand(0));
1765 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1766 case TargetLowering::Legal:
1767 if (Tmp1 != Node->getOperand(0))
1768 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1769 break;
1770 case TargetLowering::Promote:
1771 case TargetLowering::Custom:
1772 assert(0 && "Cannot promote/custom handle this yet!");
1773 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001774 switch(Node->getOpcode()) {
1775 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001776 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1777 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001778 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00001779 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001780 break;
1781 }
1782 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001783 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1784 MVT::ValueType VT = Node->getValueType(0);
1785 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001786 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001787 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1788 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1789 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001790 break;
1791 }
1792 case ISD::FSQRT:
1793 case ISD::FSIN:
1794 case ISD::FCOS: {
1795 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001796 const char *FnName = 0;
1797 switch(Node->getOpcode()) {
1798 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1799 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1800 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1801 default: assert(0 && "Unreachable!");
1802 }
Nate Begeman77558da2005-08-04 21:43:28 +00001803 SDOperand Dummy;
1804 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001805 break;
1806 }
1807 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001808 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001809 }
1810 break;
1811 }
1812 break;
1813
1814 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001815 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001816 case ISD::UINT_TO_FP: {
1817 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001818 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1819 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001820 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001821 Node->getOperand(0).getValueType())) {
1822 default: assert(0 && "Unknown operation action!");
1823 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001824 Result = ExpandLegalINT_TO_FP(isSigned,
1825 LegalizeOp(Node->getOperand(0)),
1826 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001827 AddLegalizedOperand(Op, Result);
1828 return Result;
1829 case TargetLowering::Promote:
1830 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1831 Node->getValueType(0),
1832 isSigned);
1833 AddLegalizedOperand(Op, Result);
1834 return Result;
1835 case TargetLowering::Legal:
1836 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001837 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001838
Chris Lattnerdc750592005-01-07 07:47:09 +00001839 Tmp1 = LegalizeOp(Node->getOperand(0));
1840 if (Tmp1 != Node->getOperand(0))
1841 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1842 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001843 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001844 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1845 Node->getValueType(0), Node->getOperand(0));
1846 break;
1847 case Promote:
1848 if (isSigned) {
1849 Result = PromoteOp(Node->getOperand(0));
1850 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1851 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1852 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1853 } else {
1854 Result = PromoteOp(Node->getOperand(0));
1855 Result = DAG.getZeroExtendInReg(Result,
1856 Node->getOperand(0).getValueType());
1857 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001858 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001859 break;
1860 }
1861 break;
1862 }
1863 case ISD::TRUNCATE:
1864 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1865 case Legal:
1866 Tmp1 = LegalizeOp(Node->getOperand(0));
1867 if (Tmp1 != Node->getOperand(0))
1868 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1869 break;
1870 case Expand:
1871 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1872
1873 // Since the result is legal, we should just be able to truncate the low
1874 // part of the source.
1875 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1876 break;
1877 case Promote:
1878 Result = PromoteOp(Node->getOperand(0));
1879 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1880 break;
1881 }
1882 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001883
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001884 case ISD::FP_TO_SINT:
1885 case ISD::FP_TO_UINT:
1886 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1887 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0));
1889
Chris Lattner44fe26f2005-07-29 00:11:56 +00001890 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1891 default: assert(0 && "Unknown operation action!");
1892 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00001893 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1894 SDOperand True, False;
1895 MVT::ValueType VT = Node->getOperand(0).getValueType();
1896 MVT::ValueType NVT = Node->getValueType(0);
1897 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1898 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1899 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1900 Node->getOperand(0), Tmp2, ISD::SETLT);
1901 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1902 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00001903 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00001904 Tmp2));
1905 False = DAG.getNode(ISD::XOR, NVT, False,
1906 DAG.getConstant(1ULL << ShiftAmt, NVT));
1907 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00001908 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00001909 } else {
1910 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1911 }
1912 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001913 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001914 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001915 Node->getOpcode() == ISD::FP_TO_SINT);
1916 AddLegalizedOperand(Op, Result);
1917 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001918 case TargetLowering::Custom: {
1919 SDOperand Tmp =
1920 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1921 Tmp = TLI.LowerOperation(Tmp, DAG);
1922 if (Tmp.Val) {
1923 AddLegalizedOperand(Op, Tmp);
1924 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00001925 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001926 } else {
1927 // The target thinks this is legal afterall.
1928 break;
1929 }
1930 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00001931 case TargetLowering::Legal:
1932 break;
1933 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001934
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001935 if (Tmp1 != Node->getOperand(0))
1936 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1937 break;
1938 case Expand:
1939 assert(0 && "Shouldn't need to expand other operators here!");
1940 case Promote:
1941 Result = PromoteOp(Node->getOperand(0));
1942 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1943 break;
1944 }
1945 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001946
Chris Lattner7753f172005-09-02 00:18:10 +00001947 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001948 case ISD::ZERO_EXTEND:
1949 case ISD::SIGN_EXTEND:
1950 case ISD::FP_EXTEND:
1951 case ISD::FP_ROUND:
1952 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1953 case Legal:
1954 Tmp1 = LegalizeOp(Node->getOperand(0));
1955 if (Tmp1 != Node->getOperand(0))
1956 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1957 break;
1958 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001959 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001960
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001961 case Promote:
1962 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00001963 case ISD::ANY_EXTEND:
1964 Result = PromoteOp(Node->getOperand(0));
1965 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
1966 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001967 case ISD::ZERO_EXTEND:
1968 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00001969 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001970 Result = DAG.getZeroExtendInReg(Result,
1971 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001972 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001973 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001974 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00001975 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001976 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001977 Result,
1978 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001979 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001980 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001981 Result = PromoteOp(Node->getOperand(0));
1982 if (Result.getValueType() != Op.getValueType())
1983 // Dynamically dead while we have only 2 FP types.
1984 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1985 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001986 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001987 Result = PromoteOp(Node->getOperand(0));
1988 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1989 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001990 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001991 }
1992 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001993 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001994 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001995 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001996 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001997
1998 // If this operation is not supported, convert it to a shl/shr or load/store
1999 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002000 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2001 default: assert(0 && "This action not supported for this op yet!");
2002 case TargetLowering::Legal:
2003 if (Tmp1 != Node->getOperand(0))
2004 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002005 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002006 break;
2007 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002008 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002009 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002010 // NOTE: we could fall back on load/store here too for targets without
2011 // SAR. However, it is doubtful that any exist.
2012 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2013 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002014 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002015 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2016 Node->getOperand(0), ShiftCst);
2017 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2018 Result, ShiftCst);
2019 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2020 // The only way we can lower this is to turn it into a STORETRUNC,
2021 // EXTLOAD pair, targetting a temporary location (a stack slot).
2022
2023 // NOTE: there is a choice here between constantly creating new stack
2024 // slots and always reusing the same one. We currently always create
2025 // new ones, as reuse may inhibit scheduling.
2026 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2027 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2028 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2029 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002030 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002031 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2032 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2033 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002034 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002035 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002036 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2037 Result, StackSlot, DAG.getSrcValue(NULL),
2038 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002039 } else {
2040 assert(0 && "Unknown op");
2041 }
2042 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002043 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002044 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002045 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002046 }
Chris Lattner99222f72005-01-15 07:15:18 +00002047 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002048
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002049 // Note that LegalizeOp may be reentered even from single-use nodes, which
2050 // means that we always must cache transformed nodes.
2051 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002052 return Result;
2053}
2054
Chris Lattner4d978642005-01-15 22:16:26 +00002055/// PromoteOp - Given an operation that produces a value in an invalid type,
2056/// promote it to compute the value into a larger type. The produced value will
2057/// have the correct bits for the low portion of the register, but no guarantee
2058/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002059SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2060 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002061 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002062 assert(getTypeAction(VT) == Promote &&
2063 "Caller should expand or legalize operands that are not promotable!");
2064 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2065 "Cannot promote to smaller type!");
2066
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002067 SDOperand Tmp1, Tmp2, Tmp3;
2068
2069 SDOperand Result;
2070 SDNode *Node = Op.Val;
2071
Chris Lattner1a570f12005-09-02 20:32:45 +00002072 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2073 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002074
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002075 // Promotion needs an optimization step to clean up after it, and is not
2076 // careful to avoid operations the target does not support. Make sure that
2077 // all generated operations are legalized in the next iteration.
2078 NeedsAnotherIteration = true;
2079
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002080 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002081 case ISD::CopyFromReg:
2082 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002083 default:
2084 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2085 assert(0 && "Do not know how to promote this operator!");
2086 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002087 case ISD::UNDEF:
2088 Result = DAG.getNode(ISD::UNDEF, NVT);
2089 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002090 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002091 if (VT != MVT::i1)
2092 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2093 else
2094 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002095 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2096 break;
2097 case ISD::ConstantFP:
2098 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2099 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2100 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002101
Chris Lattner2cb338d2005-01-18 02:59:52 +00002102 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002103 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002104 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2105 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002106 Result = LegalizeOp(Result);
2107 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002108
2109 case ISD::TRUNCATE:
2110 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2111 case Legal:
2112 Result = LegalizeOp(Node->getOperand(0));
2113 assert(Result.getValueType() >= NVT &&
2114 "This truncation doesn't make sense!");
2115 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2116 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2117 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002118 case Promote:
2119 // The truncation is not required, because we don't guarantee anything
2120 // about high bits anyway.
2121 Result = PromoteOp(Node->getOperand(0));
2122 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002123 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002124 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2125 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002126 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002127 }
2128 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002129 case ISD::SIGN_EXTEND:
2130 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002131 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002132 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2133 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2134 case Legal:
2135 // Input is legal? Just do extend all the way to the larger type.
2136 Result = LegalizeOp(Node->getOperand(0));
2137 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2138 break;
2139 case Promote:
2140 // Promote the reg if it's smaller.
2141 Result = PromoteOp(Node->getOperand(0));
2142 // The high bits are not guaranteed to be anything. Insert an extend.
2143 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002144 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002145 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002146 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002147 Result = DAG.getZeroExtendInReg(Result,
2148 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002149 break;
2150 }
2151 break;
2152
2153 case ISD::FP_EXTEND:
2154 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2155 case ISD::FP_ROUND:
2156 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2157 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2158 case Promote: assert(0 && "Unreachable with 2 FP types!");
2159 case Legal:
2160 // Input is legal? Do an FP_ROUND_INREG.
2161 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002162 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2163 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002164 break;
2165 }
2166 break;
2167
2168 case ISD::SINT_TO_FP:
2169 case ISD::UINT_TO_FP:
2170 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2171 case Legal:
2172 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002173 // No extra round required here.
2174 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002175 break;
2176
2177 case Promote:
2178 Result = PromoteOp(Node->getOperand(0));
2179 if (Node->getOpcode() == ISD::SINT_TO_FP)
2180 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002181 Result,
2182 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002183 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002184 Result = DAG.getZeroExtendInReg(Result,
2185 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002186 // No extra round required here.
2187 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002188 break;
2189 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002190 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2191 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002192 // Round if we cannot tolerate excess precision.
2193 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002194 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2195 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002196 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002197 }
Chris Lattner4d978642005-01-15 22:16:26 +00002198 break;
2199
2200 case ISD::FP_TO_SINT:
2201 case ISD::FP_TO_UINT:
2202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2203 case Legal:
2204 Tmp1 = LegalizeOp(Node->getOperand(0));
2205 break;
2206 case Promote:
2207 // The input result is prerounded, so we don't have to do anything
2208 // special.
2209 Tmp1 = PromoteOp(Node->getOperand(0));
2210 break;
2211 case Expand:
2212 assert(0 && "not implemented");
2213 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002214 // If we're promoting a UINT to a larger size, check to see if the new node
2215 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2216 // we can use that instead. This allows us to generate better code for
2217 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2218 // legal, such as PowerPC.
2219 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002220 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2221 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begeman36853ee2005-08-14 01:20:53 +00002222 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2223 } else {
2224 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2225 }
Chris Lattner4d978642005-01-15 22:16:26 +00002226 break;
2227
Chris Lattner13fe99c2005-04-02 05:00:07 +00002228 case ISD::FABS:
2229 case ISD::FNEG:
2230 Tmp1 = PromoteOp(Node->getOperand(0));
2231 assert(Tmp1.getValueType() == NVT);
2232 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2233 // NOTE: we do not have to do any extra rounding here for
2234 // NoExcessFPPrecision, because we know the input will have the appropriate
2235 // precision, and these operations don't modify precision at all.
2236 break;
2237
Chris Lattner9d6fa982005-04-28 21:44:33 +00002238 case ISD::FSQRT:
2239 case ISD::FSIN:
2240 case ISD::FCOS:
2241 Tmp1 = PromoteOp(Node->getOperand(0));
2242 assert(Tmp1.getValueType() == NVT);
2243 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2244 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002245 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2246 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002247 break;
2248
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002249 case ISD::AND:
2250 case ISD::OR:
2251 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002252 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002253 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002254 case ISD::MUL:
2255 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002256 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002257 // that too is okay if they are integer operations.
2258 Tmp1 = PromoteOp(Node->getOperand(0));
2259 Tmp2 = PromoteOp(Node->getOperand(1));
2260 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2261 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002262 break;
2263 case ISD::FADD:
2264 case ISD::FSUB:
2265 case ISD::FMUL:
2266 // The input may have strange things in the top bits of the registers, but
2267 // these operations don't care.
2268 Tmp1 = PromoteOp(Node->getOperand(0));
2269 Tmp2 = PromoteOp(Node->getOperand(1));
2270 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2271 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2272
2273 // Floating point operations will give excess precision that we may not be
2274 // able to tolerate. If we DO allow excess precision, just leave it,
2275 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002276 // FIXME: Why would we need to round FP ops more than integer ones?
2277 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002278 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002279 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2280 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002281 break;
2282
Chris Lattner4d978642005-01-15 22:16:26 +00002283 case ISD::SDIV:
2284 case ISD::SREM:
2285 // These operators require that their input be sign extended.
2286 Tmp1 = PromoteOp(Node->getOperand(0));
2287 Tmp2 = PromoteOp(Node->getOperand(1));
2288 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002289 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2290 DAG.getValueType(VT));
2291 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2292 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002293 }
2294 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2295
2296 // Perform FP_ROUND: this is probably overly pessimistic.
2297 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002298 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2299 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002300 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002301 case ISD::FDIV:
2302 case ISD::FREM:
2303 // These operators require that their input be fp extended.
2304 Tmp1 = PromoteOp(Node->getOperand(0));
2305 Tmp2 = PromoteOp(Node->getOperand(1));
2306 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2307
2308 // Perform FP_ROUND: this is probably overly pessimistic.
2309 if (NoExcessFPPrecision)
2310 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2311 DAG.getValueType(VT));
2312 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002313
2314 case ISD::UDIV:
2315 case ISD::UREM:
2316 // These operators require that their input be zero extended.
2317 Tmp1 = PromoteOp(Node->getOperand(0));
2318 Tmp2 = PromoteOp(Node->getOperand(1));
2319 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002320 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2321 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002322 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2323 break;
2324
2325 case ISD::SHL:
2326 Tmp1 = PromoteOp(Node->getOperand(0));
2327 Tmp2 = LegalizeOp(Node->getOperand(1));
2328 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2329 break;
2330 case ISD::SRA:
2331 // The input value must be properly sign extended.
2332 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002333 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2334 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002335 Tmp2 = LegalizeOp(Node->getOperand(1));
2336 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2337 break;
2338 case ISD::SRL:
2339 // The input value must be properly zero extended.
2340 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002341 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002342 Tmp2 = LegalizeOp(Node->getOperand(1));
2343 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2344 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002345 case ISD::LOAD:
2346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002348 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2349 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002350 // Remember that we legalized the chain.
2351 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2352 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002353 case ISD::SEXTLOAD:
2354 case ISD::ZEXTLOAD:
2355 case ISD::EXTLOAD:
2356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2357 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2358 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2,
2359 Node->getOperand(2), Node->getOperand(3));
2360 // Remember that we legalized the chain.
2361 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2362 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002363 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002364 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2365 case Expand: assert(0 && "It's impossible to expand bools");
2366 case Legal:
2367 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2368 break;
2369 case Promote:
2370 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2371 break;
2372 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002373 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2374 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2375 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2376 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002377 case ISD::SELECT_CC:
2378 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2379 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2380 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2381 Node->getOperand(1), Tmp2, Tmp3,
2382 Node->getOperand(4));
2383 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002384 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002385 case ISD::CALL: {
2386 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2387 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2388
Chris Lattner3d95c142005-01-19 20:24:35 +00002389 std::vector<SDOperand> Ops;
2390 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2391 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2392
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002393 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2394 "Can only promote single result calls");
2395 std::vector<MVT::ValueType> RetTyVTs;
2396 RetTyVTs.reserve(2);
2397 RetTyVTs.push_back(NVT);
2398 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002399 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2400 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002401 Result = SDOperand(NC, 0);
2402
2403 // Insert the new chain mapping.
2404 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2405 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002406 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002407 case ISD::CTPOP:
2408 case ISD::CTTZ:
2409 case ISD::CTLZ:
2410 Tmp1 = Node->getOperand(0);
2411 //Zero extend the argument
2412 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2413 // Perform the larger operation, then subtract if needed.
2414 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2415 switch(Node->getOpcode())
2416 {
2417 case ISD::CTPOP:
2418 Result = Tmp1;
2419 break;
2420 case ISD::CTTZ:
2421 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002422 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002423 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002424 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002425 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2426 break;
2427 case ISD::CTLZ:
2428 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002429 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2430 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002431 getSizeInBits(VT), NVT));
2432 break;
2433 }
2434 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002435 }
2436
2437 assert(Result.Val && "Didn't set a result!");
2438 AddPromotedOperand(Op, Result);
2439 return Result;
2440}
Chris Lattnerdc750592005-01-07 07:47:09 +00002441
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002442/// ExpandAddSub - Find a clever way to expand this add operation into
2443/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002444void SelectionDAGLegalize::
2445ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2446 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002447 // Expand the subcomponents.
2448 SDOperand LHSL, LHSH, RHSL, RHSH;
2449 ExpandOp(LHS, LHSL, LHSH);
2450 ExpandOp(RHS, RHSL, RHSH);
2451
Chris Lattner8ffd0042005-04-11 20:29:59 +00002452 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002453 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2454 if (LHSL.getValueType() == MVT::i32) {
Chris Lattner7bf8d062005-10-06 17:39:10 +00002455 SDOperand LowEl = SDOperand(0,0);
Chris Lattner669e8c22005-05-14 07:25:05 +00002456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2457 if (C->getValue() == 0)
2458 LowEl = RHSL;
2459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2460 if (C->getValue() == 0)
2461 LowEl = LHSL;
2462 if (LowEl.Val) {
2463 // Turn this into an add/sub of the high part only.
2464 SDOperand HiEl =
2465 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2466 LowEl.getValueType(), LHSH, RHSH);
2467 Lo = LowEl;
2468 Hi = HiEl;
2469 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002470 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002471 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002472
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002473 std::vector<SDOperand> Ops;
2474 Ops.push_back(LHSL);
2475 Ops.push_back(LHSH);
2476 Ops.push_back(RHSL);
2477 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002478
2479 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2480 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002481 Hi = Lo.getValue(1);
2482}
2483
Chris Lattner4157c412005-04-02 04:00:59 +00002484void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2485 SDOperand Op, SDOperand Amt,
2486 SDOperand &Lo, SDOperand &Hi) {
2487 // Expand the subcomponents.
2488 SDOperand LHSL, LHSH;
2489 ExpandOp(Op, LHSL, LHSH);
2490
2491 std::vector<SDOperand> Ops;
2492 Ops.push_back(LHSL);
2493 Ops.push_back(LHSH);
2494 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002495 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002496 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002497 Hi = Lo.getValue(1);
2498}
2499
2500
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002501/// ExpandShift - Try to find a clever way to expand this shift operation out to
2502/// smaller elements. If we can't find a way that is more efficient than a
2503/// libcall on this target, return false. Otherwise, return true with the
2504/// low-parts expanded into Lo and Hi.
2505bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2506 SDOperand &Lo, SDOperand &Hi) {
2507 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2508 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002509
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002510 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002511 SDOperand ShAmt = LegalizeOp(Amt);
2512 MVT::ValueType ShTy = ShAmt.getValueType();
2513 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2514 unsigned NVTBits = MVT::getSizeInBits(NVT);
2515
2516 // Handle the case when Amt is an immediate. Other cases are currently broken
2517 // and are disabled.
2518 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2519 unsigned Cst = CN->getValue();
2520 // Expand the incoming operand to be shifted, so that we have its parts
2521 SDOperand InL, InH;
2522 ExpandOp(Op, InL, InH);
2523 switch(Opc) {
2524 case ISD::SHL:
2525 if (Cst > VTBits) {
2526 Lo = DAG.getConstant(0, NVT);
2527 Hi = DAG.getConstant(0, NVT);
2528 } else if (Cst > NVTBits) {
2529 Lo = DAG.getConstant(0, NVT);
2530 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002531 } else if (Cst == NVTBits) {
2532 Lo = DAG.getConstant(0, NVT);
2533 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002534 } else {
2535 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2536 Hi = DAG.getNode(ISD::OR, NVT,
2537 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2538 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2539 }
2540 return true;
2541 case ISD::SRL:
2542 if (Cst > VTBits) {
2543 Lo = DAG.getConstant(0, NVT);
2544 Hi = DAG.getConstant(0, NVT);
2545 } else if (Cst > NVTBits) {
2546 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2547 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002548 } else if (Cst == NVTBits) {
2549 Lo = InH;
2550 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002551 } else {
2552 Lo = DAG.getNode(ISD::OR, NVT,
2553 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2554 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2555 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2556 }
2557 return true;
2558 case ISD::SRA:
2559 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002560 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002561 DAG.getConstant(NVTBits-1, ShTy));
2562 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002563 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002564 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002565 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002566 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002567 } else if (Cst == NVTBits) {
2568 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002569 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002570 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002571 } else {
2572 Lo = DAG.getNode(ISD::OR, NVT,
2573 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2574 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2575 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2576 }
2577 return true;
2578 }
2579 }
2580 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2581 // so disable it for now. Currently targets are handling this via SHL_PARTS
2582 // and friends.
2583 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002584
2585 // If we have an efficient select operation (or if the selects will all fold
2586 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002587 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002588 return false;
2589
2590 SDOperand InL, InH;
2591 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002592 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2593 DAG.getConstant(NVTBits, ShTy), ShAmt);
2594
Chris Lattner4d25c042005-01-20 20:29:23 +00002595 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002596 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2597 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002598
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002599 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2600 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2601 DAG.getConstant(NVTBits-1, ShTy));
2602 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2603 DAG.getConstant(NVTBits-1, ShTy));
2604 }
2605
2606 if (Opc == ISD::SHL) {
2607 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2608 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2609 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002610 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002611
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002612 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2613 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2614 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002615 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002616 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2617 DAG.getConstant(32, ShTy),
2618 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002619 DAG.getConstant(0, NVT),
2620 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002621 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002622 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002623 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002624 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002625
2626 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002627 if (Opc == ISD::SRA)
2628 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2629 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002630 else
2631 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002632 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002633 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002634 }
2635 return true;
2636}
Chris Lattneraac464e2005-01-21 06:05:23 +00002637
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002638/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2639/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002640/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002641static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002642 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002643
Chris Lattner2dce7032005-05-12 23:24:06 +00002644 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002645 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002646 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002647 Found = Node;
2648 return;
2649 }
2650
2651 // Otherwise, scan the operands of Node to see if any of them is a call.
2652 assert(Node->getNumOperands() != 0 &&
2653 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002654 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002655 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002656
2657 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002658 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002659 Found);
2660}
2661
2662
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002663/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2664/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002665/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002666static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2667 std::set<SDNode*> &Visited) {
2668 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2669 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002670
Chris Lattner2dce7032005-05-12 23:24:06 +00002671 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002672 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002673 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002674 Found = Node;
2675 return;
2676 }
2677
2678 // Otherwise, scan the operands of Node to see if any of them is a call.
2679 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2680 if (UI == E) return;
2681 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002682 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002683
2684 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002685 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002686}
2687
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002688/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002689/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002690static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002691 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002692 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002693 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002694 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002695
Chris Lattner4add7e32005-01-23 04:42:50 +00002696 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002697 if (TheChain.getValueType() != MVT::Other)
2698 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002699 if (TheChain.getValueType() != MVT::Other)
2700 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002701
2702 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002703 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002704
Chris Lattner4add7e32005-01-23 04:42:50 +00002705 // Make sure to only follow users of our token chain.
2706 SDNode *User = *UI;
2707 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2708 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002709 if (SDNode *Result = FindCallSeqEnd(User))
2710 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002711 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002712 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002713}
2714
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002715/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002716/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002717static SDNode *FindCallSeqStart(SDNode *Node) {
2718 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002719 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002720
2721 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2722 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002723 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002724}
2725
2726
Chris Lattner4add7e32005-01-23 04:42:50 +00002727/// FindInputOutputChains - If we are replacing an operation with a call we need
2728/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002729/// properly serialize the calls in the block. The returned operand is the
2730/// input chain value for the new call (e.g. the entry node or the previous
2731/// call), and OutChain is set to be the chain node to update to point to the
2732/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002733static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2734 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002735 SDNode *LatestCallSeqStart = Entry.Val;
2736 SDNode *LatestCallSeqEnd = 0;
2737 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2738 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002739
Chris Lattner2dce7032005-05-12 23:24:06 +00002740 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002741 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002742 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2743 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002744 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002745 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002746 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2747 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002748 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002749 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002750 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002751
Chris Lattner06bbeb62005-05-11 19:02:11 +00002752 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002753 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002754 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002755 std::set<SDNode*> Visited;
2756 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002757
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002758 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002759 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002760 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002761
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002762 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002763}
2764
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002765/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002766void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2767 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002768 // Nothing to splice it into?
2769 if (OutChain == 0) return;
2770
2771 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2772 //OutChain->dump();
2773
2774 // Form a token factor node merging the old inval and the new inval.
2775 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2776 OutChain->getOperand(0));
2777 // Change the node to refer to the new token.
2778 OutChain->setAdjCallChain(InToken);
2779}
Chris Lattner4add7e32005-01-23 04:42:50 +00002780
2781
Chris Lattneraac464e2005-01-21 06:05:23 +00002782// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2783// does not fit into a register, return the lo part and set the hi part to the
2784// by-reg argument. If it does fit into a single register, return the result
2785// and leave the Hi part unset.
2786SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2787 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002788 SDNode *OutChain;
2789 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2790 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002791 if (InChain.Val == 0)
2792 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002793
Chris Lattneraac464e2005-01-21 06:05:23 +00002794 TargetLowering::ArgListTy Args;
2795 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2796 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2797 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2798 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2799 }
2800 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002801
Chris Lattner06bbeb62005-05-11 19:02:11 +00002802 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002803 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002804 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002805 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2806 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002807
Chris Lattner63022662005-09-02 20:26:58 +00002808 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002809 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002810 default: assert(0 && "Unknown thing");
2811 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00002812 Result = CallInfo.first;
2813 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002814 case Promote:
2815 assert(0 && "Cannot promote this yet!");
2816 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00002817 ExpandOp(CallInfo.first, Result, Hi);
2818 CallInfo.second = LegalizeOp(CallInfo.second);
2819 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002820 }
Chris Lattner63022662005-09-02 20:26:58 +00002821
2822 SpliceCallInto(CallInfo.second, OutChain);
2823 NeedsAnotherIteration = true;
2824 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00002825}
2826
Chris Lattner4add7e32005-01-23 04:42:50 +00002827
Chris Lattneraac464e2005-01-21 06:05:23 +00002828/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2829/// destination type is legal.
2830SDOperand SelectionDAGLegalize::
2831ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002832 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00002833 assert(getTypeAction(Source.getValueType()) == Expand &&
2834 "This is not an expansion!");
2835 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2836
Chris Lattner06bbeb62005-05-11 19:02:11 +00002837 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002838 assert(Source.getValueType() == MVT::i64 &&
2839 "This only works for 64-bit -> FP");
2840 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2841 // incoming integer is set. To handle this, we dynamically test to see if
2842 // it is set, and, if so, add a fudge factor.
2843 SDOperand Lo, Hi;
2844 ExpandOp(Source, Lo, Hi);
2845
Chris Lattner2a4f7312005-05-13 04:45:13 +00002846 // If this is unsigned, and not supported, first perform the conversion to
2847 // signed, then adjust the result if the sign bit is set.
2848 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2849 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2850
Chris Lattnerd47675e2005-08-09 20:20:18 +00002851 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2852 DAG.getConstant(0, Hi.getValueType()),
2853 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002854 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2855 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2856 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002857 uint64_t FF = 0x5f800000ULL;
2858 if (TLI.isLittleEndian()) FF <<= 32;
2859 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002860
Chris Lattnerc30405e2005-08-26 17:15:30 +00002861 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002862 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2863 SDOperand FudgeInReg;
2864 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002865 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2866 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002867 else {
2868 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002869 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2870 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002871 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00002872 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002873 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002874
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002875 // Check to see if the target has a custom way to lower this. If so, use it.
2876 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2877 default: assert(0 && "This action not implemented for this operation!");
2878 case TargetLowering::Legal:
2879 case TargetLowering::Expand:
2880 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002881 case TargetLowering::Custom: {
2882 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2883 Source), DAG);
2884 if (NV.Val)
2885 return LegalizeOp(NV);
2886 break; // The target decided this was legal after all
2887 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002888 }
2889
Chris Lattner153587e2005-05-12 07:00:44 +00002890 // Expand the source, then glue it back together for the call. We must expand
2891 // the source in case it is shared (this pass of legalize must traverse it).
2892 SDOperand SrcLo, SrcHi;
2893 ExpandOp(Source, SrcLo, SrcHi);
2894 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2895
Chris Lattner06bbeb62005-05-11 19:02:11 +00002896 SDNode *OutChain = 0;
2897 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2898 DAG.getEntryNode());
2899 const char *FnName = 0;
2900 if (DestTy == MVT::f32)
2901 FnName = "__floatdisf";
2902 else {
2903 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2904 FnName = "__floatdidf";
2905 }
2906
Chris Lattneraac464e2005-01-21 06:05:23 +00002907 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2908
2909 TargetLowering::ArgListTy Args;
2910 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002911
Chris Lattneraac464e2005-01-21 06:05:23 +00002912 Args.push_back(std::make_pair(Source, ArgTy));
2913
2914 // We don't care about token chains for libcalls. We just use the entry
2915 // node as our input and ignore the output chain. This allows us to place
2916 // calls wherever we need them to satisfy data dependences.
2917 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002918
2919 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002920 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2921 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002922
Chris Lattnera5bf1032005-05-12 04:49:08 +00002923 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002924 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002925}
Misha Brukman835702a2005-04-21 22:36:52 +00002926
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002927
2928
Chris Lattnerdc750592005-01-07 07:47:09 +00002929/// ExpandOp - Expand the specified SDOperand into its two component pieces
2930/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2931/// LegalizeNodes map is filled in for any results that are not expanded, the
2932/// ExpandedNodes map is filled in for any results that are expanded, and the
2933/// Lo/Hi values are returned.
2934void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2935 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002936 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002937 SDNode *Node = Op.Val;
2938 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2939 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2940 assert(MVT::isInteger(NVT) && NVT < VT &&
2941 "Cannot expand to FP value or to larger int value!");
2942
Chris Lattner1a570f12005-09-02 20:32:45 +00002943 // See if we already expanded it.
2944 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2945 = ExpandedNodes.find(Op);
2946 if (I != ExpandedNodes.end()) {
2947 Lo = I->second.first;
2948 Hi = I->second.second;
2949 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00002950 }
2951
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002952 // Expanding to multiple registers needs to perform an optimization step, and
2953 // is not careful to avoid operations the target does not support. Make sure
2954 // that all generated operations are legalized in the next iteration.
2955 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002956
Chris Lattnerdc750592005-01-07 07:47:09 +00002957 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002958 case ISD::CopyFromReg:
2959 assert(0 && "CopyFromReg must be legal!");
2960 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00002961 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2962 assert(0 && "Do not know how to expand this operator!");
2963 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002964 case ISD::UNDEF:
2965 Lo = DAG.getNode(ISD::UNDEF, NVT);
2966 Hi = DAG.getNode(ISD::UNDEF, NVT);
2967 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002968 case ISD::Constant: {
2969 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2970 Lo = DAG.getConstant(Cst, NVT);
2971 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2972 break;
2973 }
2974
Chris Lattner32e08b72005-03-28 22:03:13 +00002975 case ISD::BUILD_PAIR:
2976 // Legalize both operands. FIXME: in the future we should handle the case
2977 // where the two elements are not legal.
2978 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2979 Lo = LegalizeOp(Node->getOperand(0));
2980 Hi = LegalizeOp(Node->getOperand(1));
2981 break;
2982
Chris Lattner55e9cde2005-05-11 04:51:16 +00002983 case ISD::CTPOP:
2984 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002985 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2986 DAG.getNode(ISD::CTPOP, NVT, Lo),
2987 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002988 Hi = DAG.getConstant(0, NVT);
2989 break;
2990
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002991 case ISD::CTLZ: {
2992 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002993 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002994 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2995 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002996 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2997 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002998 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2999 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3000
3001 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3002 Hi = DAG.getConstant(0, NVT);
3003 break;
3004 }
3005
3006 case ISD::CTTZ: {
3007 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003008 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003009 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3010 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003011 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3012 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003013 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3014 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3015
3016 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3017 Hi = DAG.getConstant(0, NVT);
3018 break;
3019 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003020
Chris Lattnerdc750592005-01-07 07:47:09 +00003021 case ISD::LOAD: {
3022 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3023 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003024 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003025
3026 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003027 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003028 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3029 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003030 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003031 //are from the same instruction?
3032 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003033
3034 // Build a factor node to remember that this load is independent of the
3035 // other one.
3036 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3037 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003038
Chris Lattnerdc750592005-01-07 07:47:09 +00003039 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003040 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003041 if (!TLI.isLittleEndian())
3042 std::swap(Lo, Hi);
3043 break;
3044 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003045 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003046 case ISD::CALL: {
3047 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3048 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3049
Chris Lattner3d95c142005-01-19 20:24:35 +00003050 bool Changed = false;
3051 std::vector<SDOperand> Ops;
3052 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3053 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3054 Changed |= Ops.back() != Node->getOperand(i);
3055 }
3056
Chris Lattnerdc750592005-01-07 07:47:09 +00003057 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3058 "Can only expand a call once so far, not i64 -> i16!");
3059
3060 std::vector<MVT::ValueType> RetTyVTs;
3061 RetTyVTs.reserve(3);
3062 RetTyVTs.push_back(NVT);
3063 RetTyVTs.push_back(NVT);
3064 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003065 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3066 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003067 Lo = SDOperand(NC, 0);
3068 Hi = SDOperand(NC, 1);
3069
3070 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003071 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003072 break;
3073 }
3074 case ISD::AND:
3075 case ISD::OR:
3076 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3077 SDOperand LL, LH, RL, RH;
3078 ExpandOp(Node->getOperand(0), LL, LH);
3079 ExpandOp(Node->getOperand(1), RL, RH);
3080 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3081 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3082 break;
3083 }
3084 case ISD::SELECT: {
3085 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003086
3087 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3088 case Expand: assert(0 && "It's impossible to expand bools");
3089 case Legal:
3090 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3091 break;
3092 case Promote:
3093 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3094 break;
3095 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003096 ExpandOp(Node->getOperand(1), LL, LH);
3097 ExpandOp(Node->getOperand(2), RL, RH);
3098 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3099 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3100 break;
3101 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003102 case ISD::SELECT_CC: {
3103 SDOperand TL, TH, FL, FH;
3104 ExpandOp(Node->getOperand(2), TL, TH);
3105 ExpandOp(Node->getOperand(3), FL, FH);
3106 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3107 Node->getOperand(1), TL, FL, Node->getOperand(4));
3108 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3109 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003110 Lo = LegalizeOp(Lo);
3111 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003112 break;
3113 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003114 case ISD::SEXTLOAD: {
3115 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3116 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3117 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3118
3119 if (EVT == NVT)
3120 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3121 else
3122 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3123 EVT);
3124 // The high part is obtained by SRA'ing all but one of the bits of the lo
3125 // part.
3126 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3127 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3128 TLI.getShiftAmountTy()));
3129 Lo = LegalizeOp(Lo);
3130 Hi = LegalizeOp(Hi);
3131 break;
3132 }
3133 case ISD::ZEXTLOAD: {
3134 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3135 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3136 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3137
3138 if (EVT == NVT)
3139 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3140 else
3141 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3142 EVT);
3143 // The high part is just a zero.
3144 Hi = DAG.getConstant(0, NVT);
3145 Lo = LegalizeOp(Lo);
3146 Hi = LegalizeOp(Hi);
3147 break;
3148 }
Chris Lattner7753f172005-09-02 00:18:10 +00003149 case ISD::ANY_EXTEND: {
3150 SDOperand In;
3151 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3152 case Expand: assert(0 && "expand-expand not implemented yet!");
3153 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3154 case Promote:
3155 In = PromoteOp(Node->getOperand(0));
3156 break;
3157 }
3158
3159 // The low part is any extension of the input (which degenerates to a copy).
3160 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3161 // The high part is undefined.
3162 Hi = DAG.getNode(ISD::UNDEF, NVT);
3163 break;
3164 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003165 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003166 SDOperand In;
3167 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3168 case Expand: assert(0 && "expand-expand not implemented yet!");
3169 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3170 case Promote:
3171 In = PromoteOp(Node->getOperand(0));
3172 // Emit the appropriate sign_extend_inreg to get the value we want.
3173 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003174 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003175 break;
3176 }
3177
Chris Lattnerdc750592005-01-07 07:47:09 +00003178 // The low part is just a sign extension of the input (which degenerates to
3179 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003180 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003181
Chris Lattnerdc750592005-01-07 07:47:09 +00003182 // The high part is obtained by SRA'ing all but one of the bits of the lo
3183 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003184 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003185 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3186 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003187 break;
3188 }
Chris Lattner47844892005-04-03 23:41:52 +00003189 case ISD::ZERO_EXTEND: {
3190 SDOperand In;
3191 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3192 case Expand: assert(0 && "expand-expand not implemented yet!");
3193 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3194 case Promote:
3195 In = PromoteOp(Node->getOperand(0));
3196 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003197 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003198 break;
3199 }
3200
Chris Lattnerdc750592005-01-07 07:47:09 +00003201 // The low part is just a zero extension of the input (which degenerates to
3202 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003203 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003204
Chris Lattnerdc750592005-01-07 07:47:09 +00003205 // The high part is just a zero.
3206 Hi = DAG.getConstant(0, NVT);
3207 break;
Chris Lattner47844892005-04-03 23:41:52 +00003208 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003209 // These operators cannot be expanded directly, emit them as calls to
3210 // library functions.
3211 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003212 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003213 SDOperand Op;
3214 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3215 case Expand: assert(0 && "cannot expand FP!");
3216 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3217 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3218 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003219
Chris Lattner941d84a2005-07-30 01:40:57 +00003220 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3221
Chris Lattnerfe68d752005-07-29 00:33:32 +00003222 // Now that the custom expander is done, expand the result, which is still
3223 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003224 if (Op.Val) {
3225 ExpandOp(Op, Lo, Hi);
3226 break;
3227 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003228 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003229
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003230 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003231 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003232 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003233 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003234 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003235
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003236 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003237 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3238 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3239 LegalizeOp(Node->getOperand(0)));
3240 // Now that the custom expander is done, expand the result, which is still
3241 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003242 Op = TLI.LowerOperation(Op, DAG);
3243 if (Op.Val) {
3244 ExpandOp(Op, Lo, Hi);
3245 break;
3246 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003247 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003248
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003249 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003250 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003251 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003252 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003253 break;
3254
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003255 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003256 // If the target wants custom lowering, do so.
3257 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3258 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3259 LegalizeOp(Node->getOperand(1)));
3260 Op = TLI.LowerOperation(Op, DAG);
3261 if (Op.Val) {
3262 // Now that the custom expander is done, expand the result, which is
3263 // still VT.
3264 ExpandOp(Op, Lo, Hi);
3265 break;
3266 }
3267 }
3268
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003269 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003270 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003271 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003272
3273 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003274 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003275 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3276 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003277 break;
3278 }
3279
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003280 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003281 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003282 break;
3283
3284 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003285 // If the target wants custom lowering, do so.
3286 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3287 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3288 LegalizeOp(Node->getOperand(1)));
3289 Op = TLI.LowerOperation(Op, DAG);
3290 if (Op.Val) {
3291 // Now that the custom expander is done, expand the result, which is
3292 // still VT.
3293 ExpandOp(Op, Lo, Hi);
3294 break;
3295 }
3296 }
3297
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003298 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003299 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003300 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003301
3302 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003303 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003304 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3305 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003306 break;
3307 }
3308
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003309 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003310 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003311 break;
3312 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003313 // If the target wants custom lowering, do so.
3314 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3315 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3316 LegalizeOp(Node->getOperand(1)));
3317 Op = TLI.LowerOperation(Op, DAG);
3318 if (Op.Val) {
3319 // Now that the custom expander is done, expand the result, which is
3320 // still VT.
3321 ExpandOp(Op, Lo, Hi);
3322 break;
3323 }
3324 }
3325
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003326 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003327 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003328 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003329
3330 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003331 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003332 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3333 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003334 break;
3335 }
3336
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003337 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003338 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003339 break;
3340
Misha Brukman835702a2005-04-21 22:36:52 +00003341 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003342 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3343 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003344 break;
3345 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003346 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3347 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003348 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003349 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003350 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003351 SDOperand LL, LH, RL, RH;
3352 ExpandOp(Node->getOperand(0), LL, LH);
3353 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003354 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3355 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3356 // extended the sign bit of the low half through the upper half, and if so
3357 // emit a MULHS instead of the alternate sequence that is valid for any
3358 // i64 x i64 multiply.
3359 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3360 // is RH an extension of the sign bit of RL?
3361 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3362 RH.getOperand(1).getOpcode() == ISD::Constant &&
3363 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3364 // is LH an extension of the sign bit of LL?
3365 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3366 LH.getOperand(1).getOpcode() == ISD::Constant &&
3367 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3368 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3369 } else {
3370 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3371 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3372 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3373 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3374 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3375 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003376 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3377 } else {
3378 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3379 }
3380 break;
3381 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003382 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3383 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3384 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3385 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003386 }
3387
3388 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003389 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3390 std::make_pair(Lo, Hi))).second;
3391 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003392}
3393
3394
3395// SelectionDAG::Legalize - This is the entry point for the file.
3396//
Chris Lattner4add7e32005-01-23 04:42:50 +00003397void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003398 /// run - This is the main entry point to this class.
3399 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003400 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003401}
3402