blob: af2c4ba13989fe563b494df6b876617b4bee0275 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +000080 LegalizedNodes.insert(std::make_pair(From, To));
81 // If someone requests legalization of the new node, return itself.
82 if (From != To)
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +000084 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000085 void AddPromotedOperand(SDOperand From, SDOperand To) {
86 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
87 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +000088 // If someone requests legalization of the new node, return itself.
89 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +000090 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000091
Chris Lattnerdc750592005-01-07 07:47:09 +000092public:
93
Chris Lattner4add7e32005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000095
96 /// Run - While there is still lowering to do, perform a pass over the DAG.
97 /// Most regularization can be done in a single pass, but targets that require
98 /// large values to be split into registers multiple times (e.g. i64 -> 4x
99 /// i16) require iteration for these values (the first iteration will demote
100 /// to i32, the second will demote to i16).
101 void Run() {
102 do {
103 NeedsAnotherIteration = false;
104 LegalizeDAG();
105 } while (NeedsAnotherIteration);
106 }
107
108 /// getTypeAction - Return how we should legalize values of this type, either
109 /// it is already legal or we need to expand it into multiple registers of
110 /// smaller integer type, or we need to promote it to a larger type.
111 LegalizeAction getTypeAction(MVT::ValueType VT) const {
112 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
113 }
114
115 /// isTypeLegal - Return true if this type is legal on this target.
116 ///
117 bool isTypeLegal(MVT::ValueType VT) const {
118 return getTypeAction(VT) == Legal;
119 }
120
121private:
122 void LegalizeDAG();
123
124 SDOperand LegalizeOp(SDOperand O);
125 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000127
Chris Lattneraac464e2005-01-21 06:05:23 +0000128 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
129 SDOperand &Hi);
130 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
131 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000132
Chris Lattner36e663d2005-12-23 00:16:34 +0000133 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000134 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
135 SDOperand LegalOp,
136 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000137 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
138 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000139 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
140 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000141
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000142 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000144 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
145 SDOperand &Lo, SDOperand &Hi);
146 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000147 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000148
Chris Lattnera5bf1032005-05-12 04:49:08 +0000149 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
150
Chris Lattnerdc750592005-01-07 07:47:09 +0000151 SDOperand getIntPtrConstant(uint64_t Val) {
152 return DAG.getConstant(Val, TLI.getPointerTy());
153 }
154};
155}
156
Chris Lattner301015a2005-11-19 05:51:46 +0000157static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000158 switch (VecOp) {
159 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000160 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
161 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
162 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
163 }
164}
Chris Lattnerdc750592005-01-07 07:47:09 +0000165
Chris Lattner4add7e32005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000171}
172
Jim Laskeyf2516a92005-08-17 00:39:29 +0000173/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
174/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000175/// we expand it. At this point, we know that the result and operand types are
176/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000177SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
178 SDOperand Op0,
179 MVT::ValueType DestVT) {
180 if (Op0.getValueType() == MVT::i32) {
181 // simple 32-bit [signed|unsigned] integer to float/double expansion
182
183 // get the stack frame index of a 8 byte buffer
184 MachineFunction &MF = DAG.getMachineFunction();
185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
186 // get address of 8 byte buffer
187 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
188 // word offset constant for Hi/Lo address computation
189 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
190 // set up Hi and Lo (into buffer) address based on endian
191 SDOperand Hi, Lo;
192 if (TLI.isLittleEndian()) {
193 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
194 Lo = StackSlot;
195 } else {
196 Hi = StackSlot;
197 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
198 }
199 // if signed map to unsigned space
200 SDOperand Op0Mapped;
201 if (isSigned) {
202 // constant used to invert sign bit (signed to unsigned mapping)
203 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
204 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
205 } else {
206 Op0Mapped = Op0;
207 }
208 // store the lo of the constructed double - based on integer input
209 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
210 Op0Mapped, Lo, DAG.getSrcValue(NULL));
211 // initial hi portion of constructed double
212 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
213 // store the hi of the constructed double - biased exponent
214 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
215 InitialHi, Hi, DAG.getSrcValue(NULL));
216 // load the constructed double
217 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
218 DAG.getSrcValue(NULL));
219 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000220 SDOperand Bias = DAG.getConstantFP(isSigned ?
221 BitsToDouble(0x4330000080000000ULL)
222 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000223 MVT::f64);
224 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000226 // final result
227 SDOperand Result;
228 // handle final rounding
229 if (DestVT == MVT::f64) {
230 // do nothing
231 Result = Sub;
232 } else {
233 // if f32 then cast to f32
234 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
235 }
Chris Lattner2af3ee42005-12-20 00:53:54 +0000236 return LegalizeOp(Result);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000237 }
238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000240
Chris Lattnerd47675e2005-08-09 20:20:18 +0000241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
242 DAG.getConstant(0, Op0.getValueType()),
243 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000244 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
245 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
246 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000247
Jim Laskeyf2516a92005-08-17 00:39:29 +0000248 // If the sign bit of the integer is set, the large number will be treated
249 // as a negative number. To counteract this, the dynamic code adds an
250 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000251 uint64_t FF;
252 switch (Op0.getValueType()) {
253 default: assert(0 && "Unsupported integer type!");
254 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
255 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
256 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
257 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
258 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000259 if (TLI.isLittleEndian()) FF <<= 32;
260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000261
Chris Lattnerc30405e2005-08-26 17:15:30 +0000262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-07-16 00:19:57 +0000263 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
264 SDOperand FudgeInReg;
265 if (DestVT == MVT::f32)
266 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
267 DAG.getSrcValue(NULL));
268 else {
269 assert(DestVT == MVT::f64 && "Unexpected conversion");
270 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
271 DAG.getEntryNode(), CPIdx,
272 DAG.getSrcValue(NULL), MVT::f32));
273 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000274
Chris Lattner2af3ee42005-12-20 00:53:54 +0000275 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000276}
277
Chris Lattner19732782005-08-16 18:17:10 +0000278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000279/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000280/// we promote it. At this point, we know that the result and operand types are
281/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
282/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
284 MVT::ValueType DestVT,
285 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 // First step, figure out the appropriate *INT_TO_FP operation to use.
287 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000288
Chris Lattnere3e847b2005-07-16 00:19:57 +0000289 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000290
Chris Lattnere3e847b2005-07-16 00:19:57 +0000291 // Scan for the appropriate larger type to use.
292 while (1) {
293 NewInTy = (MVT::ValueType)(NewInTy+1);
294 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000295
Chris Lattnere3e847b2005-07-16 00:19:57 +0000296 // If the target supports SINT_TO_FP of this type, use it.
297 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
298 default: break;
299 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000300 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000301 break; // Can't use this datatype.
302 // FALL THROUGH.
303 case TargetLowering::Custom:
304 OpToUse = ISD::SINT_TO_FP;
305 break;
306 }
307 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000308 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000309
Chris Lattnere3e847b2005-07-16 00:19:57 +0000310 // If the target supports UINT_TO_FP of this type, use it.
311 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
312 default: break;
313 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000314 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315 break; // Can't use this datatype.
316 // FALL THROUGH.
317 case TargetLowering::Custom:
318 OpToUse = ISD::UINT_TO_FP;
319 break;
320 }
321 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000322
Chris Lattnere3e847b2005-07-16 00:19:57 +0000323 // Otherwise, try a larger type.
324 }
325
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
Chris Lattner2af3ee42005-12-20 00:53:54 +0000328 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattner2af3ee42005-12-20 00:53:54 +0000331 // Make sure to legalize any nodes we create here.
332 return LegalizeOp(N);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000333}
334
Chris Lattner44fe26f2005-07-29 00:11:56 +0000335/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
336/// FP_TO_*INT operation of the specified operand when the target requests that
337/// we promote it. At this point, we know that the result and operand types are
338/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
339/// operation that returns a larger result.
340SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
341 MVT::ValueType DestVT,
342 bool isSigned) {
343 // First step, figure out the appropriate FP_TO*INT operation to use.
344 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000347
Chris Lattner44fe26f2005-07-29 00:11:56 +0000348 // Scan for the appropriate larger type to use.
349 while (1) {
350 NewOutTy = (MVT::ValueType)(NewOutTy+1);
351 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000352
Chris Lattner44fe26f2005-07-29 00:11:56 +0000353 // If the target supports FP_TO_SINT returning this type, use it.
354 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
355 default: break;
356 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000357 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000358 break; // Can't use this datatype.
359 // FALL THROUGH.
360 case TargetLowering::Custom:
361 OpToUse = ISD::FP_TO_SINT;
362 break;
363 }
364 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000365
Chris Lattner44fe26f2005-07-29 00:11:56 +0000366 // If the target supports FP_TO_UINT of this type, use it.
367 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
368 default: break;
369 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000370 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000371 break; // Can't use this datatype.
372 // FALL THROUGH.
373 case TargetLowering::Custom:
374 OpToUse = ISD::FP_TO_UINT;
375 break;
376 }
377 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000378
Chris Lattner44fe26f2005-07-29 00:11:56 +0000379 // Otherwise, try a larger type.
380 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000381
Chris Lattner44fe26f2005-07-29 00:11:56 +0000382 // Okay, we found the operation and type to use. Truncate the result of the
383 // extended FP_TO_*INT operation to the desired size.
Chris Lattner2af3ee42005-12-20 00:53:54 +0000384 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
385 DAG.getNode(OpToUse, NewOutTy, LegalOp));
386 // Make sure to legalize any nodes we create here in the next pass.
387 return LegalizeOp(N);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000388}
389
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000390/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
391/// not been visited yet and if all of its operands have already been visited.
392static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
393 std::map<SDNode*, unsigned> &Visited) {
394 if (++Visited[N] != N->getNumOperands())
395 return; // Haven't visited all operands yet
396
397 Order.push_back(N);
398
399 if (N->hasOneUse()) { // Tail recurse in common case.
400 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
401 return;
402 }
403
404 // Now that we have N in, add anything that uses it if all of their operands
405 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000406 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
407 ComputeTopDownOrdering(*UI, Order, Visited);
408}
409
Chris Lattner44fe26f2005-07-29 00:11:56 +0000410
Chris Lattnerdc750592005-01-07 07:47:09 +0000411void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000412 // The legalize process is inherently a bottom-up recursive process (users
413 // legalize their uses before themselves). Given infinite stack space, we
414 // could just start legalizing on the root and traverse the whole graph. In
415 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000416 // blocks. To avoid this problem, compute an ordering of the nodes where each
417 // node is only legalized after all of its operands are legalized.
418 std::map<SDNode*, unsigned> Visited;
419 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000420
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000421 // Compute ordering from all of the leaves in the graphs, those (like the
422 // entry node) that have no operands.
423 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
424 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000425 if (I->getNumOperands() == 0) {
426 Visited[I] = 0 - 1U;
427 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000429 }
430
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000431 assert(Order.size() == Visited.size() &&
432 Order.size() ==
433 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000434 "Error: DAG is cyclic!");
435 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000436
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000437 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
438 SDNode *N = Order[i];
439 switch (getTypeAction(N->getValueType(0))) {
440 default: assert(0 && "Bad type action!");
441 case Legal:
442 LegalizeOp(SDOperand(N, 0));
443 break;
444 case Promote:
445 PromoteOp(SDOperand(N, 0));
446 break;
447 case Expand: {
448 SDOperand X, Y;
449 ExpandOp(SDOperand(N, 0), X, Y);
450 break;
451 }
452 }
453 }
454
455 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000456 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000457 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
458 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000459
460 ExpandedNodes.clear();
461 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000462 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000463
464 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000465 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000466}
467
468SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000469 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000470 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000471 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000472
Chris Lattnerdc750592005-01-07 07:47:09 +0000473 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000474 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000475 if (Node->getNumValues() > 1) {
476 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
477 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000478 case Legal: break; // Nothing to do.
479 case Expand: {
480 SDOperand T1, T2;
481 ExpandOp(Op.getValue(i), T1, T2);
482 assert(LegalizedNodes.count(Op) &&
483 "Expansion didn't add legal operands!");
484 return LegalizedNodes[Op];
485 }
486 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000487 PromoteOp(Op.getValue(i));
488 assert(LegalizedNodes.count(Op) &&
489 "Expansion didn't add legal operands!");
490 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000491 }
492 }
493
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000494 // Note that LegalizeOp may be reentered even from single-use nodes, which
495 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000496 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
497 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000498
Nate Begemane5b86d72005-08-10 20:51:12 +0000499 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000500
501 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000502
503 switch (Node->getOpcode()) {
504 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000505 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
506 // If this is a target node, legalize it by legalizing the operands then
507 // passing it through.
508 std::vector<SDOperand> Ops;
509 bool Changed = false;
510 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
511 Ops.push_back(LegalizeOp(Node->getOperand(i)));
512 Changed = Changed || Node->getOperand(i) != Ops.back();
513 }
514 if (Changed)
515 if (Node->getNumValues() == 1)
516 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
517 else {
518 std::vector<MVT::ValueType> VTs(Node->value_begin(),
519 Node->value_end());
520 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
521 }
522
523 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
524 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
525 return Result.getValue(Op.ResNo);
526 }
527 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000528 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
529 assert(0 && "Do not know how to legalize this operator!");
530 abort();
531 case ISD::EntryToken:
532 case ISD::FrameIndex:
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000533 case ISD::TargetFrameIndex:
534 case ISD::Register:
535 case ISD::TargetConstant:
Nate Begeman4e56db62005-12-10 02:36:00 +0000536 case ISD::TargetConstantPool:
Chris Lattnerdc750592005-01-07 07:47:09 +0000537 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000538 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000539 case ISD::ExternalSymbol:
Andrew Lenharth72594262005-12-24 23:42:32 +0000540 case ISD::TargetExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000541 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000542 case ISD::BasicBlock:
543 case ISD::CONDCODE:
544 case ISD::VALUETYPE:
545 case ISD::SRCVALUE:
Chris Lattner435b4022005-11-29 06:21:05 +0000546 case ISD::STRING:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000547 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
548 default: assert(0 && "This action is not supported yet!");
549 case TargetLowering::Custom: {
550 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
551 if (Tmp.Val) {
552 Result = LegalizeOp(Tmp);
553 break;
554 }
555 } // FALLTHROUGH if the target doesn't want to lower this op after all.
556 case TargetLowering::Legal:
557 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
558 break;
559 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000560 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000561 case ISD::AssertSext:
562 case ISD::AssertZext:
563 Tmp1 = LegalizeOp(Node->getOperand(0));
564 if (Tmp1 != Node->getOperand(0))
565 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
566 Node->getOperand(1));
567 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000568 case ISD::MERGE_VALUES:
569 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000570 case ISD::CopyFromReg:
571 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000572 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000573 if (Node->getNumValues() == 2) {
Chris Lattnere3c67e92005-12-18 15:27:43 +0000574 if (Tmp1 != Node->getOperand(0))
575 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattner33182322005-08-16 21:55:35 +0000576 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattnere3c67e92005-12-18 15:27:43 +0000577 Node->getValueType(0));
578 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000579 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
580 if (Node->getNumOperands() == 3)
581 Tmp2 = LegalizeOp(Node->getOperand(2));
582 if (Tmp1 != Node->getOperand(0) ||
583 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattnere3c67e92005-12-18 15:27:43 +0000584 Result = DAG.getCopyFromReg(Tmp1,
585 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
586 Node->getValueType(0), Tmp2);
587 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
588 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000589 // Since CopyFromReg produces two values, make sure to remember that we
590 // legalized both of them.
591 AddLegalizedOperand(Op.getValue(0), Result);
592 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
593 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000594 case ISD::UNDEF: {
595 MVT::ValueType VT = Op.getValueType();
596 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000597 default: assert(0 && "This action is not supported yet!");
598 case TargetLowering::Expand:
599 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000600 if (MVT::isInteger(VT))
601 Result = DAG.getConstant(0, VT);
602 else if (MVT::isFloatingPoint(VT))
603 Result = DAG.getConstantFP(0, VT);
604 else
605 assert(0 && "Unknown value type!");
606 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000607 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000608 break;
609 }
610 break;
611 }
Chris Lattner435b4022005-11-29 06:21:05 +0000612
613 case ISD::LOCATION:
614 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
616
617 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
618 case TargetLowering::Promote:
619 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000620 case TargetLowering::Expand: {
Jim Laskey9e296be2005-12-21 20:51:37 +0000621 if (TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey6f9ff632006-01-04 13:42:59 +0000622 MachineDebugInfo &DebugInfo = getMachineDebugInfo();
Jim Laskey9e296be2005-12-21 20:51:37 +0000623 std::vector<SDOperand> Ops;
624 Ops.push_back(Tmp1); // chain
625 Ops.push_back(Node->getOperand(1)); // line #
626 Ops.push_back(Node->getOperand(2)); // col #
627 const std::string &fname =
628 cast<StringSDNode>(Node->getOperand(3))->getValue();
629 const std::string &dirname =
630 cast<StringSDNode>(Node->getOperand(4))->getValue();
631 unsigned id = DebugInfo.RecordSource(fname, dirname);
632 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
633 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
634 } else {
635 Result = Tmp1; // chain
636 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000637 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner435b4022005-11-29 06:21:05 +0000638 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000639 }
Chris Lattner435b4022005-11-29 06:21:05 +0000640 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000641 if (Tmp1 != Node->getOperand(0) ||
642 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000643 std::vector<SDOperand> Ops;
644 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000645 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
646 Ops.push_back(Node->getOperand(1)); // line # must be legal.
647 Ops.push_back(Node->getOperand(2)); // col # must be legal.
648 } else {
649 // Otherwise promote them.
650 Ops.push_back(PromoteOp(Node->getOperand(1)));
651 Ops.push_back(PromoteOp(Node->getOperand(2)));
652 }
Chris Lattner435b4022005-11-29 06:21:05 +0000653 Ops.push_back(Node->getOperand(3)); // filename must be legal.
654 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
655 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
656 }
657 break;
658 }
659 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000660
661 case ISD::DEBUG_LOC:
662 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
663 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
664 case TargetLowering::Promote:
665 case TargetLowering::Expand:
666 default: assert(0 && "This action is not supported yet!");
667 case TargetLowering::Legal:
668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
669 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
670 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
671 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
672
673 if (Tmp1 != Node->getOperand(0) ||
674 Tmp2 != Node->getOperand(1) ||
675 Tmp3 != Node->getOperand(2) ||
676 Tmp4 != Node->getOperand(3)) {
677 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
678 }
679 break;
680 }
681 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000682
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 case ISD::Constant:
684 // We know we don't need to expand constants here, constants only have one
685 // value and we check that it is fine above.
686
687 // FIXME: Maybe we should handle things like targets that don't support full
688 // 32-bit immediates?
689 break;
690 case ISD::ConstantFP: {
691 // Spill FP immediates to the constant pool if the target cannot directly
692 // codegen them. Targets often have some immediate values that can be
693 // efficiently generated into an FP register without a load. We explicitly
694 // leave these constants as ConstantFP nodes for the target to deal with.
695
696 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
697
698 // Check to see if this FP immediate is already legal.
699 bool isLegal = false;
700 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
701 E = TLI.legal_fpimm_end(); I != E; ++I)
702 if (CFP->isExactlyValue(*I)) {
703 isLegal = true;
704 break;
705 }
706
707 if (!isLegal) {
708 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000709 bool Extend = false;
710
711 // If a FP immediate is precise when represented as a float, we put it
712 // into the constant pool as a float, even if it's is statically typed
713 // as a double.
714 MVT::ValueType VT = CFP->getValueType(0);
715 bool isDouble = VT == MVT::f64;
716 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
717 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000718 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
719 // Only do this if the target has a native EXTLOAD instruction from
720 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000721 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000722 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
723 VT = MVT::f32;
724 Extend = true;
725 }
Misha Brukman835702a2005-04-21 22:36:52 +0000726
Nate Begeman4e56db62005-12-10 02:36:00 +0000727 SDOperand CPIdx =
728 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000729 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000730 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
731 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000732 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000733 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
734 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000735 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 }
737 break;
738 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000739 case ISD::ConstantVec: {
740 // We assume that vector constants are not legal, and will be immediately
741 // spilled to the constant pool.
742 //
743 // FIXME: revisit this when we have some kind of mechanism by which targets
744 // can decided legality of vector constants, of which there may be very
745 // many.
746 //
747 // Create a ConstantPacked, and put it in the constant pool.
748 std::vector<Constant*> CV;
749 MVT::ValueType VT = Node->getValueType(0);
750 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
751 SDOperand OpN = Node->getOperand(I);
752 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
753 if (MVT::isFloatingPoint(VT))
754 CV.push_back(ConstantFP::get(OpNTy,
755 cast<ConstantFPSDNode>(OpN)->getValue()));
756 else
757 CV.push_back(ConstantUInt::get(OpNTy,
758 cast<ConstantSDNode>(OpN)->getValue()));
759 }
760 Constant *CP = ConstantPacked::get(CV);
Nate Begeman956aef42005-12-13 03:03:23 +0000761 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000762 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
763 break;
764 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000765 case ISD::TokenFactor:
766 if (Node->getNumOperands() == 2) {
767 bool Changed = false;
768 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
769 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
770 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
771 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
772 } else {
773 std::vector<SDOperand> Ops;
774 bool Changed = false;
775 // Legalize the operands.
776 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
777 SDOperand Op = Node->getOperand(i);
778 Ops.push_back(LegalizeOp(Op));
779 Changed |= Ops[i] != Op;
780 }
781 if (Changed)
782 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000783 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000784 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000785
Chris Lattner2dce7032005-05-12 23:24:06 +0000786 case ISD::CALLSEQ_START:
787 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000789 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000790 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000791 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000792 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000793
Chris Lattner2dce7032005-05-12 23:24:06 +0000794 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000795 // nodes are treated specially and are mutated in place. This makes the dag
796 // legalization process more efficient and also makes libcall insertion
797 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000798 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000799 case ISD::DYNAMIC_STACKALLOC:
800 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
801 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
802 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
803 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000804 Tmp3 != Node->getOperand(2)) {
805 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
806 std::vector<SDOperand> Ops;
807 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
808 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
809 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000810 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000811
812 // Since this op produces two values, make sure to remember that we
813 // legalized both of them.
814 AddLegalizedOperand(SDOperand(Node, 0), Result);
815 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
816 return Result.getValue(Op.ResNo);
817
Chris Lattnerd0feb642005-05-13 18:43:43 +0000818 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000819 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
821 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000822
823 bool Changed = false;
824 std::vector<SDOperand> Ops;
825 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
826 Ops.push_back(LegalizeOp(Node->getOperand(i)));
827 Changed |= Ops.back() != Node->getOperand(i);
828 }
829
830 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000831 std::vector<MVT::ValueType> RetTyVTs;
832 RetTyVTs.reserve(Node->getNumValues());
833 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000834 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000835 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
836 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000837 } else {
838 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000839 }
Chris Lattner9242c502005-01-09 19:43:23 +0000840 // Since calls produce multiple values, make sure to remember that we
841 // legalized all of them.
842 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
843 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
844 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000845 }
Chris Lattner68a12142005-01-07 22:12:08 +0000846 case ISD::BR:
847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
848 if (Tmp1 != Node->getOperand(0))
849 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
850 break;
851
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000852 case ISD::BRCOND:
853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000854
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000855 switch (getTypeAction(Node->getOperand(1).getValueType())) {
856 case Expand: assert(0 && "It's impossible to expand bools");
857 case Legal:
858 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
859 break;
860 case Promote:
861 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
862 break;
863 }
Nate Begeman371e4952005-08-16 19:49:35 +0000864
865 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
866 default: assert(0 && "This action is not supported yet!");
867 case TargetLowering::Expand:
868 // Expand brcond's setcc into its constituent parts and create a BR_CC
869 // Node.
870 if (Tmp2.getOpcode() == ISD::SETCC) {
871 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
872 Tmp2.getOperand(0), Tmp2.getOperand(1),
873 Node->getOperand(2));
874 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000875 // Make sure the condition is either zero or one. It may have been
876 // promoted from something else.
877 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
878
Nate Begeman371e4952005-08-16 19:49:35 +0000879 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
880 DAG.getCondCode(ISD::SETNE), Tmp2,
881 DAG.getConstant(0, Tmp2.getValueType()),
882 Node->getOperand(2));
883 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000884 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +0000885 break;
Evan Cheng6fc31042005-12-19 23:12:38 +0000886 case TargetLowering::Custom: {
887 SDOperand Tmp =
888 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
889 Tmp1, Tmp2, Node->getOperand(2)), DAG);
890 if (Tmp.Val) {
891 Result = LegalizeOp(Tmp);
892 break;
893 }
894 // FALLTHROUGH if the target thinks it is legal.
895 }
Nate Begeman371e4952005-08-16 19:49:35 +0000896 case TargetLowering::Legal:
897 // Basic block destination (Op#2) is always legal.
898 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
899 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
900 Node->getOperand(2));
901 break;
902 }
903 break;
904 case ISD::BR_CC:
905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000906 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000907 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
908 Node->getOperand(2), // LHS
909 Node->getOperand(3), // RHS
910 Node->getOperand(1)));
911 // If we get a SETCC back from legalizing the SETCC node we just
912 // created, then use its LHS, RHS, and CC directly in creating a new
913 // node. Otherwise, select between the true and false value based on
914 // comparing the result of the legalized with zero.
915 if (Tmp2.getOpcode() == ISD::SETCC) {
916 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
917 Tmp2.getOperand(0), Tmp2.getOperand(1),
918 Node->getOperand(4));
919 } else {
920 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
921 DAG.getCondCode(ISD::SETNE),
922 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
923 Node->getOperand(4));
924 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000925 break;
926 }
927
928 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
929 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
930
931 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
932 default: assert(0 && "Unexpected action for BR_CC!");
933 case TargetLowering::Custom: {
934 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
935 Tmp2, Tmp3, Node->getOperand(4));
936 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
937 if (Tmp4.Val) {
938 Result = LegalizeOp(Tmp4);
939 break;
940 }
941 } // FALLTHROUGH if the target doesn't want to lower this op after all.
942 case TargetLowering::Legal:
943 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
944 Tmp3 != Node->getOperand(3)) {
945 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
946 Tmp2, Tmp3, Node->getOperand(4));
947 }
948 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000949 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000950 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000951 case ISD::BRCONDTWOWAY:
952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
953 switch (getTypeAction(Node->getOperand(1).getValueType())) {
954 case Expand: assert(0 && "It's impossible to expand bools");
955 case Legal:
956 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
957 break;
958 case Promote:
959 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
960 break;
961 }
962 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
963 // pair.
964 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
965 case TargetLowering::Promote:
966 default: assert(0 && "This action is not supported yet!");
967 case TargetLowering::Legal:
968 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
969 std::vector<SDOperand> Ops;
970 Ops.push_back(Tmp1);
971 Ops.push_back(Tmp2);
972 Ops.push_back(Node->getOperand(2));
973 Ops.push_back(Node->getOperand(3));
974 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
975 }
976 break;
977 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000978 // If BRTWOWAY_CC is legal for this target, then simply expand this node
979 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
980 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000981 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000982 if (Tmp2.getOpcode() == ISD::SETCC) {
983 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
984 Tmp2.getOperand(0), Tmp2.getOperand(1),
985 Node->getOperand(2), Node->getOperand(3));
986 } else {
987 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
988 DAG.getConstant(0, Tmp2.getValueType()),
989 Node->getOperand(2), Node->getOperand(3));
990 }
991 } else {
992 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000993 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000994 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
995 }
Chris Lattnerc06da622005-12-18 23:54:29 +0000996 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattnerfd986782005-04-09 03:30:19 +0000997 break;
998 }
999 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001000 case ISD::BRTWOWAY_CC:
1001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001002 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +00001003 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1004 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1005 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1006 Tmp3 != Node->getOperand(3)) {
1007 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1008 Node->getOperand(4), Node->getOperand(5));
1009 }
1010 break;
1011 } else {
1012 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1013 Node->getOperand(2), // LHS
1014 Node->getOperand(3), // RHS
1015 Node->getOperand(1)));
1016 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1017 // pair.
1018 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1019 default: assert(0 && "This action is not supported yet!");
1020 case TargetLowering::Legal:
1021 // If we get a SETCC back from legalizing the SETCC node we just
1022 // created, then use its LHS, RHS, and CC directly in creating a new
1023 // node. Otherwise, select between the true and false value based on
1024 // comparing the result of the legalized with zero.
1025 if (Tmp2.getOpcode() == ISD::SETCC) {
1026 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1027 Tmp2.getOperand(0), Tmp2.getOperand(1),
1028 Node->getOperand(4), Node->getOperand(5));
1029 } else {
1030 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1031 DAG.getConstant(0, Tmp2.getValueType()),
1032 Node->getOperand(4), Node->getOperand(5));
1033 }
1034 break;
1035 case TargetLowering::Expand:
1036 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1037 Node->getOperand(4));
1038 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1039 break;
1040 }
Chris Lattner0fab4592005-12-21 19:40:42 +00001041 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman371e4952005-08-16 19:49:35 +00001042 }
1043 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001044 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001047
Evan Cheng31d15fa2005-12-23 07:29:34 +00001048 MVT::ValueType VT = Node->getValueType(0);
1049 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1050 default: assert(0 && "This action is not supported yet!");
1051 case TargetLowering::Custom: {
1052 SDOperand Op = DAG.getLoad(Node->getValueType(0),
1053 Tmp1, Tmp2, Node->getOperand(2));
1054 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1055 if (Tmp.Val) {
1056 Result = LegalizeOp(Tmp);
1057 // Since loads produce two values, make sure to remember that we legalized
1058 // both of them.
1059 AddLegalizedOperand(SDOperand(Node, 0), Result);
1060 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1061 return Result.getValue(Op.ResNo);
1062 }
1063 // FALLTHROUGH if the target thinks it is legal.
1064 }
1065 case TargetLowering::Legal:
1066 if (Tmp1 != Node->getOperand(0) ||
1067 Tmp2 != Node->getOperand(1))
1068 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1069 Node->getOperand(2));
1070 else
1071 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +00001072
Evan Cheng31d15fa2005-12-23 07:29:34 +00001073 // Since loads produce two values, make sure to remember that we legalized
1074 // both of them.
1075 AddLegalizedOperand(SDOperand(Node, 0), Result);
1076 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1077 return Result.getValue(Op.ResNo);
1078 }
1079 assert(0 && "Unreachable");
1080 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001081 case ISD::EXTLOAD:
1082 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001083 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001084 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1085 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001086
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001087 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001088 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001089 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001090 case TargetLowering::Promote:
1091 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001092 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1093 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001094 // Since loads produce two values, make sure to remember that we legalized
1095 // both of them.
1096 AddLegalizedOperand(SDOperand(Node, 0), Result);
1097 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1098 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +00001099
Evan Cheng31d15fa2005-12-23 07:29:34 +00001100 case TargetLowering::Custom: {
1101 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1102 Tmp1, Tmp2, Node->getOperand(2),
1103 SrcVT);
1104 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1105 if (Tmp.Val) {
1106 Result = LegalizeOp(Tmp);
1107 // Since loads produce two values, make sure to remember that we legalized
1108 // both of them.
1109 AddLegalizedOperand(SDOperand(Node, 0), Result);
1110 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1111 return Result.getValue(Op.ResNo);
1112 }
1113 // FALLTHROUGH if the target thinks it is legal.
1114 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001115 case TargetLowering::Legal:
1116 if (Tmp1 != Node->getOperand(0) ||
1117 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001118 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1119 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001120 else
1121 Result = SDOperand(Node, 0);
1122
1123 // Since loads produce two values, make sure to remember that we legalized
1124 // both of them.
1125 AddLegalizedOperand(SDOperand(Node, 0), Result);
1126 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1127 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001128 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001129 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001130 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1131 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001132 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc06da622005-12-18 23:54:29 +00001133 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001134 Load = LegalizeOp(Load);
1135 AddLegalizedOperand(SDOperand(Node, 0), Result);
1136 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001137 if (Op.ResNo)
1138 return Load.getValue(1);
1139 return Result;
1140 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001141 assert(Node->getOpcode() != ISD::EXTLOAD &&
1142 "EXTLOAD should always be supported!");
1143 // Turn the unsupported load into an EXTLOAD followed by an explicit
1144 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001145 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1146 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001147 SDOperand ValRes;
1148 if (Node->getOpcode() == ISD::SEXTLOAD)
1149 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001150 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001151 else
1152 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc06da622005-12-18 23:54:29 +00001153 Result = LegalizeOp(Result); // Relegalize new nodes.
1154 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner2af3ee42005-12-20 00:53:54 +00001155 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1156 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001157 if (Op.ResNo)
1158 return Result.getValue(1);
1159 return ValRes;
1160 }
1161 assert(0 && "Unreachable");
1162 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001163 case ISD::EXTRACT_ELEMENT: {
1164 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1165 switch (getTypeAction(OpTy)) {
1166 default:
1167 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1168 break;
1169 case Legal:
1170 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1171 // 1 -> Hi
1172 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1173 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1174 TLI.getShiftAmountTy()));
1175 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1176 } else {
1177 // 0 -> Lo
1178 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1179 Node->getOperand(0));
1180 }
1181 Result = LegalizeOp(Result);
1182 break;
1183 case Expand:
1184 // Get both the low and high parts.
1185 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1186 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1187 Result = Tmp2; // 1 -> Hi
1188 else
1189 Result = Tmp1; // 0 -> Lo
1190 break;
1191 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001192 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001193 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001194
1195 case ISD::CopyToReg:
1196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001197
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001198 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001199 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001200 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001201 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001202 if (Node->getNumValues() == 1) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001203 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1204 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1205 Node->getOperand(1), Tmp2);
1206 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001207 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1208 if (Node->getNumOperands() == 4)
1209 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001210 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001211 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattnere3c67e92005-12-18 15:27:43 +00001212 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1213 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1214 }
1215
1216 // Since this produces two values, make sure to remember that we legalized
1217 // both of them.
1218 AddLegalizedOperand(SDOperand(Node, 0), Result);
1219 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1220 return Result.getValue(Op.ResNo);
1221 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001222 break;
1223
1224 case ISD::RET:
1225 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1226 switch (Node->getNumOperands()) {
1227 case 2: // ret val
1228 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1229 case Legal:
1230 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001231 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001232 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1233 break;
1234 case Expand: {
1235 SDOperand Lo, Hi;
1236 ExpandOp(Node->getOperand(1), Lo, Hi);
1237 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001238 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001239 }
1240 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001241 Tmp2 = PromoteOp(Node->getOperand(1));
1242 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1243 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001244 }
1245 break;
1246 case 1: // ret void
1247 if (Tmp1 != Node->getOperand(0))
1248 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1249 break;
1250 default: { // ret <values>
1251 std::vector<SDOperand> NewValues;
1252 NewValues.push_back(Tmp1);
1253 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1254 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1255 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001256 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001257 break;
1258 case Expand: {
1259 SDOperand Lo, Hi;
1260 ExpandOp(Node->getOperand(i), Lo, Hi);
1261 NewValues.push_back(Lo);
1262 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001263 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001264 }
1265 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001266 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001267 }
1268 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1269 break;
1270 }
1271 }
1272 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001273 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001274 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1275 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1276
Chris Lattnere69daaf2005-01-08 06:25:56 +00001277 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001278 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001279 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001280 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001281 DAG.getConstant(FloatToBits(CFP->getValue()),
1282 MVT::i32),
1283 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001284 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001285 } else {
1286 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001287 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001288 DAG.getConstant(DoubleToBits(CFP->getValue()),
1289 MVT::i64),
1290 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001291 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001292 }
Chris Lattnera4743132005-02-22 07:23:39 +00001293 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001294 }
1295
Chris Lattnerdc750592005-01-07 07:47:09 +00001296 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1297 case Legal: {
1298 SDOperand Val = LegalizeOp(Node->getOperand(1));
1299 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1300 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001301 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1302 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001303
1304 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1305 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1306 default: assert(0 && "This action is not supported yet!");
1307 case TargetLowering::Custom: {
1308 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1309 if (Tmp.Val) {
1310 Result = LegalizeOp(Tmp);
1311 break;
1312 }
1313 // FALLTHROUGH if the target thinks it is legal.
1314 }
1315 case TargetLowering::Legal:
1316 // Nothing to do.
1317 break;
1318 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001319 break;
1320 }
1321 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001322 // Truncate the value and store the result.
1323 Tmp3 = PromoteOp(Node->getOperand(1));
1324 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001325 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001326 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001327 break;
1328
Chris Lattnerdc750592005-01-07 07:47:09 +00001329 case Expand:
1330 SDOperand Lo, Hi;
Nate Begemand37c1312005-11-22 18:16:00 +00001331 unsigned IncrementSize;
Chris Lattnerdc750592005-01-07 07:47:09 +00001332 ExpandOp(Node->getOperand(1), Lo, Hi);
1333
1334 if (!TLI.isLittleEndian())
1335 std::swap(Lo, Hi);
1336
Chris Lattner55e9cde2005-05-11 04:51:16 +00001337 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1338 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001339 // If this is a vector type, then we have to calculate the increment as
1340 // the product of the element size in bytes, and the number of elements
1341 // in the high half of the vector.
1342 if (MVT::Vector == Hi.getValueType()) {
1343 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1344 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1345 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1346 } else {
1347 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1348 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001349 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1350 getIntPtrConstant(IncrementSize));
1351 assert(isTypeLegal(Tmp2.getValueType()) &&
1352 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001353 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001354 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1355 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001356 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1357 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001358 }
1359 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001360 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001361 case ISD::PCMARKER:
1362 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001363 if (Tmp1 != Node->getOperand(0))
1364 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001365 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001366 case ISD::READCYCLECOUNTER:
1367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthf9b27d72005-12-02 06:08:08 +00001368 if (Tmp1 != Node->getOperand(0)) {
1369 std::vector<MVT::ValueType> rtypes;
1370 std::vector<SDOperand> rvals;
1371 rtypes.push_back(MVT::i64);
1372 rtypes.push_back(MVT::Other);
1373 rvals.push_back(Tmp1);
1374 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1375 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001376
1377 // Since rdcc produce two values, make sure to remember that we legalized
1378 // both of them.
1379 AddLegalizedOperand(SDOperand(Node, 0), Result);
1380 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1381 return Result.getValue(Op.ResNo);
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001382
Evan Cheng31d15fa2005-12-23 07:29:34 +00001383 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001384 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1385 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1386
1387 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattnerc7037ab2005-12-23 16:12:20 +00001388 case Promote:
1389 case Expand:
1390 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001391 case Legal:
1392 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001393
1394 // The only promote case we handle is TRUNCSTORE:i1 X into
1395 // -> TRUNCSTORE:i8 (and X, 1)
1396 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1397 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1398 TargetLowering::Promote) {
1399 // Promote the bool to a mask then store.
1400 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1401 DAG.getConstant(1, Tmp2.getValueType()));
1402 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1403 Node->getOperand(3), DAG.getValueType(MVT::i8));
1404
1405 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1406 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001407 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001408 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001409 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001410
1411 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1412 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1413 default: assert(0 && "This action is not supported yet!");
1414 case TargetLowering::Custom: {
1415 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1416 if (Tmp.Val) {
1417 Result = LegalizeOp(Tmp);
1418 break;
1419 }
1420 // FALLTHROUGH if the target thinks it is legal.
1421 }
1422 case TargetLowering::Legal:
1423 // Nothing to do.
1424 break;
1425 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001426 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001427 }
1428 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001429 }
Chris Lattner39c67442005-01-14 22:08:15 +00001430 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001431 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1432 case Expand: assert(0 && "It's impossible to expand bools");
1433 case Legal:
1434 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1435 break;
1436 case Promote:
1437 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1438 break;
1439 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001440 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001441 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001442
Nate Begeman987121a2005-08-23 04:29:48 +00001443 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001444 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001445 case TargetLowering::Expand:
1446 if (Tmp1.getOpcode() == ISD::SETCC) {
1447 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1448 Tmp2, Tmp3,
1449 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1450 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001451 // Make sure the condition is either zero or one. It may have been
1452 // promoted from something else.
1453 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001454 Result = DAG.getSelectCC(Tmp1,
1455 DAG.getConstant(0, Tmp1.getValueType()),
1456 Tmp2, Tmp3, ISD::SETNE);
1457 }
Chris Lattnerc06da622005-12-18 23:54:29 +00001458 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begemane5b86d72005-08-10 20:51:12 +00001459 break;
Evan Cheng225a4d02005-12-17 01:21:05 +00001460 case TargetLowering::Custom: {
1461 SDOperand Tmp =
1462 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1463 Tmp1, Tmp2, Tmp3), DAG);
1464 if (Tmp.Val) {
1465 Result = LegalizeOp(Tmp);
1466 break;
1467 }
1468 // FALLTHROUGH if the target thinks it is legal.
1469 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001470 case TargetLowering::Legal:
1471 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1472 Tmp3 != Node->getOperand(2))
1473 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1474 Tmp1, Tmp2, Tmp3);
1475 break;
1476 case TargetLowering::Promote: {
1477 MVT::ValueType NVT =
1478 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1479 unsigned ExtOp, TruncOp;
1480 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001481 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001482 TruncOp = ISD::TRUNCATE;
1483 } else {
1484 ExtOp = ISD::FP_EXTEND;
1485 TruncOp = ISD::FP_ROUND;
1486 }
1487 // Promote each of the values to the new type.
1488 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1489 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1490 // Perform the larger operation, then round down.
1491 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1492 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1493 break;
1494 }
1495 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001496 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001497 case ISD::SELECT_CC:
1498 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1499 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1500
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001501 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001502 // Everything is legal, see if we should expand this op or something.
1503 switch (TLI.getOperationAction(ISD::SELECT_CC,
1504 Node->getOperand(0).getValueType())) {
1505 default: assert(0 && "This action is not supported yet!");
1506 case TargetLowering::Custom: {
1507 SDOperand Tmp =
1508 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1509 Node->getOperand(0),
1510 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001511 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001512 if (Tmp.Val) {
1513 Result = LegalizeOp(Tmp);
1514 break;
1515 }
1516 } // FALLTHROUGH if the target can't lower this operation after all.
1517 case TargetLowering::Legal:
1518 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1519 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1520 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1521 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1408c052005-12-22 05:23:45 +00001522 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner5f573412005-08-26 00:23:59 +00001523 Tmp3, Tmp4, Node->getOperand(4));
1524 }
1525 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001526 }
1527 break;
1528 } else {
1529 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1530 Node->getOperand(0), // LHS
1531 Node->getOperand(1), // RHS
1532 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001533 // If we get a SETCC back from legalizing the SETCC node we just
1534 // created, then use its LHS, RHS, and CC directly in creating a new
1535 // node. Otherwise, select between the true and false value based on
1536 // comparing the result of the legalized with zero.
1537 if (Tmp1.getOpcode() == ISD::SETCC) {
1538 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1539 Tmp1.getOperand(0), Tmp1.getOperand(1),
1540 Tmp3, Tmp4, Tmp1.getOperand(2));
1541 } else {
1542 Result = DAG.getSelectCC(Tmp1,
1543 DAG.getConstant(0, Tmp1.getValueType()),
1544 Tmp3, Tmp4, ISD::SETNE);
1545 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001546 }
1547 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001548 case ISD::SETCC:
1549 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1550 case Legal:
1551 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1552 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001553 break;
1554 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001555 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1556 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1557
1558 // If this is an FP compare, the operands have already been extended.
1559 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1560 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001561 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001562
1563 // Otherwise, we have to insert explicit sign or zero extends. Note
1564 // that we could insert sign extends for ALL conditions, but zero extend
1565 // is cheaper on many machines (an AND instead of two shifts), so prefer
1566 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001567 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001568 default: assert(0 && "Unknown integer comparison!");
1569 case ISD::SETEQ:
1570 case ISD::SETNE:
1571 case ISD::SETUGE:
1572 case ISD::SETUGT:
1573 case ISD::SETULE:
1574 case ISD::SETULT:
1575 // ALL of these operations will work if we either sign or zero extend
1576 // the operands (including the unsigned comparisons!). Zero extend is
1577 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001578 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1579 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001580 break;
1581 case ISD::SETGE:
1582 case ISD::SETGT:
1583 case ISD::SETLT:
1584 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001585 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1586 DAG.getValueType(VT));
1587 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1588 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001589 break;
1590 }
Chris Lattner4d978642005-01-15 22:16:26 +00001591 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001592 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001593 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001594 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1595 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1596 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001597 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001598 case ISD::SETEQ:
1599 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001600 if (RHSLo == RHSHi)
1601 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1602 if (RHSCST->isAllOnesValue()) {
1603 // Comparison to -1.
1604 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001605 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001606 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001607 }
1608
Chris Lattnerdc750592005-01-07 07:47:09 +00001609 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1610 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1611 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001612 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001613 break;
1614 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001615 // If this is a comparison of the sign bit, just look at the top part.
1616 // X > -1, x < 0
1617 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001618 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001619 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001620 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001621 (CST->isAllOnesValue()))) { // X > -1
1622 Tmp1 = LHSHi;
1623 Tmp2 = RHSHi;
1624 break;
1625 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001626
Chris Lattnerdc750592005-01-07 07:47:09 +00001627 // FIXME: This generated code sucks.
1628 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001629 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001630 default: assert(0 && "Unknown integer setcc!");
1631 case ISD::SETLT:
1632 case ISD::SETULT: LowCC = ISD::SETULT; break;
1633 case ISD::SETGT:
1634 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1635 case ISD::SETLE:
1636 case ISD::SETULE: LowCC = ISD::SETULE; break;
1637 case ISD::SETGE:
1638 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1639 }
Misha Brukman835702a2005-04-21 22:36:52 +00001640
Chris Lattnerdc750592005-01-07 07:47:09 +00001641 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1642 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1643 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1644
1645 // NOTE: on targets without efficient SELECT of bools, we can always use
1646 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001647 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1648 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1649 Node->getOperand(2));
1650 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001651 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1652 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001653 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001654 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001655 }
1656 }
Nate Begeman987121a2005-08-23 04:29:48 +00001657
Chris Lattner1408c052005-12-22 05:23:45 +00001658 switch(TLI.getOperationAction(ISD::SETCC,
1659 Node->getOperand(0).getValueType())) {
Nate Begeman987121a2005-08-23 04:29:48 +00001660 default:
1661 assert(0 && "Cannot handle this action for SETCC yet!");
1662 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001663 case TargetLowering::Promote: {
1664 // First step, figure out the appropriate operation to use.
1665 // Allow SETCC to not be supported for all legal data types
1666 // Mostly this targets FP
1667 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1668 MVT::ValueType OldVT = NewInTy;
1669
1670 // Scan for the appropriate larger type to use.
1671 while (1) {
1672 NewInTy = (MVT::ValueType)(NewInTy+1);
1673
1674 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1675 "Fell off of the edge of the integer world");
1676 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1677 "Fell off of the edge of the floating point world");
1678
1679 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001680 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001681 break;
1682 }
1683 if (MVT::isInteger(NewInTy))
1684 assert(0 && "Cannot promote Legal Integer SETCC yet");
1685 else {
1686 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1687 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1688 }
1689
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001690 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1691 Node->getOperand(2));
1692 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001693 }
Evan Chengc1583db2005-12-21 20:21:51 +00001694 case TargetLowering::Custom: {
1695 SDOperand Tmp =
1696 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1697 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1698 if (Tmp.Val) {
1699 Result = LegalizeOp(Tmp);
1700 break;
1701 }
1702 // FALLTHROUGH if the target thinks it is legal.
1703 }
Nate Begeman987121a2005-08-23 04:29:48 +00001704 case TargetLowering::Legal:
1705 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1706 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1707 Node->getOperand(2));
1708 break;
1709 case TargetLowering::Expand:
1710 // Expand a setcc node into a select_cc of the same condition, lhs, and
1711 // rhs that selects between const 1 (true) and const 0 (false).
1712 MVT::ValueType VT = Node->getValueType(0);
1713 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1714 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1715 Node->getOperand(2));
1716 Result = LegalizeOp(Result);
1717 break;
1718 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001719 break;
1720
Chris Lattner85d70c62005-01-11 05:57:22 +00001721 case ISD::MEMSET:
1722 case ISD::MEMCPY:
1723 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001724 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001725 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1726
1727 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1728 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1729 case Expand: assert(0 && "Cannot expand a byte!");
1730 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001731 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001732 break;
1733 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001734 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001735 break;
1736 }
1737 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001738 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001739 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001740
1741 SDOperand Tmp4;
1742 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001743 case Expand: {
1744 // Length is too big, just take the lo-part of the length.
1745 SDOperand HiPart;
1746 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1747 break;
1748 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001749 case Legal:
1750 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001751 break;
1752 case Promote:
1753 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001754 break;
1755 }
1756
1757 SDOperand Tmp5;
1758 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1759 case Expand: assert(0 && "Cannot expand this yet!");
1760 case Legal:
1761 Tmp5 = LegalizeOp(Node->getOperand(4));
1762 break;
1763 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001764 Tmp5 = PromoteOp(Node->getOperand(4));
1765 break;
1766 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001767
1768 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1769 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001770 case TargetLowering::Custom: {
1771 SDOperand Tmp =
1772 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1773 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1774 if (Tmp.Val) {
1775 Result = LegalizeOp(Tmp);
1776 break;
1777 }
1778 // FALLTHROUGH if the target thinks it is legal.
1779 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001780 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001781 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1782 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1783 Tmp5 != Node->getOperand(4)) {
1784 std::vector<SDOperand> Ops;
1785 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1786 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1787 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1788 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001789 break;
1790 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001791 // Otherwise, the target does not support this operation. Lower the
1792 // operation to an explicit libcall as appropriate.
1793 MVT::ValueType IntPtr = TLI.getPointerTy();
1794 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1795 std::vector<std::pair<SDOperand, const Type*> > Args;
1796
Reid Spencer6dced922005-01-12 14:53:45 +00001797 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001798 if (Node->getOpcode() == ISD::MEMSET) {
1799 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1800 // Extend the ubyte argument to be an int value for the call.
1801 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1802 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1803 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1804
1805 FnName = "memset";
1806 } else if (Node->getOpcode() == ISD::MEMCPY ||
1807 Node->getOpcode() == ISD::MEMMOVE) {
1808 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1809 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1810 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1811 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1812 } else {
1813 assert(0 && "Unknown op!");
1814 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001815
Chris Lattner85d70c62005-01-11 05:57:22 +00001816 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001817 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001818 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner2af3ee42005-12-20 00:53:54 +00001819 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001820 break;
1821 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001822 }
1823 break;
1824 }
Chris Lattner5385db52005-05-09 20:23:03 +00001825
1826 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001827 Tmp1 = LegalizeOp(Node->getOperand(0));
1828 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001829
Chris Lattner86535992005-05-14 07:45:46 +00001830 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1831 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1832 std::vector<SDOperand> Ops;
1833 Ops.push_back(Tmp1);
1834 Ops.push_back(Tmp2);
1835 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1836 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001837 Result = SDOperand(Node, 0);
1838 // Since these produce two values, make sure to remember that we legalized
1839 // both of them.
1840 AddLegalizedOperand(SDOperand(Node, 0), Result);
1841 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1842 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001843 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001844 Tmp1 = LegalizeOp(Node->getOperand(0));
1845 Tmp2 = LegalizeOp(Node->getOperand(1));
1846 Tmp3 = LegalizeOp(Node->getOperand(2));
1847 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1848 Tmp3 != Node->getOperand(2))
1849 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1850 break;
1851
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001852 case ISD::READIO:
1853 Tmp1 = LegalizeOp(Node->getOperand(0));
1854 Tmp2 = LegalizeOp(Node->getOperand(1));
1855
1856 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1857 case TargetLowering::Custom:
1858 default: assert(0 && "This action not implemented for this operation!");
1859 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001860 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1861 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1862 std::vector<SDOperand> Ops;
1863 Ops.push_back(Tmp1);
1864 Ops.push_back(Tmp2);
1865 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1866 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001867 Result = SDOperand(Node, 0);
1868 break;
1869 case TargetLowering::Expand:
1870 // Replace this with a load from memory.
1871 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1872 Node->getOperand(1), DAG.getSrcValue(NULL));
1873 Result = LegalizeOp(Result);
1874 break;
1875 }
1876
1877 // Since these produce two values, make sure to remember that we legalized
1878 // both of them.
1879 AddLegalizedOperand(SDOperand(Node, 0), Result);
1880 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1881 return Result.getValue(Op.ResNo);
1882
1883 case ISD::WRITEIO:
1884 Tmp1 = LegalizeOp(Node->getOperand(0));
1885 Tmp2 = LegalizeOp(Node->getOperand(1));
1886 Tmp3 = LegalizeOp(Node->getOperand(2));
1887
1888 switch (TLI.getOperationAction(Node->getOpcode(),
1889 Node->getOperand(1).getValueType())) {
1890 case TargetLowering::Custom:
1891 default: assert(0 && "This action not implemented for this operation!");
1892 case TargetLowering::Legal:
1893 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1894 Tmp3 != Node->getOperand(2))
1895 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1896 break;
1897 case TargetLowering::Expand:
1898 // Replace this with a store to memory.
1899 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1900 Node->getOperand(1), Node->getOperand(2),
1901 DAG.getSrcValue(NULL));
1902 Result = LegalizeOp(Result);
1903 break;
1904 }
1905 break;
1906
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001907 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001908 case ISD::SUB_PARTS:
1909 case ISD::SHL_PARTS:
1910 case ISD::SRA_PARTS:
1911 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001912 std::vector<SDOperand> Ops;
1913 bool Changed = false;
1914 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1915 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1916 Changed |= Ops.back() != Node->getOperand(i);
1917 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001918 if (Changed) {
1919 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1920 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1921 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001922
1923 // Since these produce multiple values, make sure to remember that we
1924 // legalized all of them.
1925 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1926 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1927 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001928 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001929
1930 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001931 case ISD::ADD:
1932 case ISD::SUB:
1933 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001934 case ISD::MULHS:
1935 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001936 case ISD::UDIV:
1937 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001938 case ISD::AND:
1939 case ISD::OR:
1940 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001941 case ISD::SHL:
1942 case ISD::SRL:
1943 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001944 case ISD::FADD:
1945 case ISD::FSUB:
1946 case ISD::FMUL:
1947 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001948 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001949 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1950 case Expand: assert(0 && "Not possible");
1951 case Legal:
1952 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1953 break;
1954 case Promote:
1955 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1956 break;
1957 }
Andrew Lenharth72594262005-12-24 23:42:32 +00001958 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth72594262005-12-24 23:42:32 +00001959 case TargetLowering::Custom: {
1960 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
1961 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1962 if (Tmp.Val) {
1963 Tmp = LegalizeOp(Tmp); // Relegalize input.
1964 AddLegalizedOperand(Op, Tmp);
1965 return Tmp;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001966 } //else it was considered legal and we fall through
Andrew Lenharth72594262005-12-24 23:42:32 +00001967 }
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001968 case TargetLowering::Legal:
1969 if (Tmp1 != Node->getOperand(0) ||
1970 Tmp2 != Node->getOperand(1))
1971 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1972 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001973 default:
1974 assert(0 && "Operation not supported");
1975 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001976 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001977
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001978 case ISD::BUILD_PAIR: {
1979 MVT::ValueType PairTy = Node->getValueType(0);
1980 // TODO: handle the case where the Lo and Hi operands are not of legal type
1981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1982 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1983 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1984 case TargetLowering::Legal:
1985 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1986 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1987 break;
1988 case TargetLowering::Promote:
1989 case TargetLowering::Custom:
1990 assert(0 && "Cannot promote/custom this yet!");
1991 case TargetLowering::Expand:
1992 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1993 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1994 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1995 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1996 TLI.getShiftAmountTy()));
1997 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1998 break;
1999 }
2000 break;
2001 }
2002
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002003 case ISD::UREM:
2004 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002005 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002006 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2007 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2008 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002009 case TargetLowering::Custom: {
2010 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2011 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2012 if (Tmp.Val) {
2013 Tmp = LegalizeOp(Tmp); // Relegalize input.
2014 AddLegalizedOperand(Op, Tmp);
2015 return Tmp;
2016 } //else it was considered legal and we fall through
2017 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002018 case TargetLowering::Legal:
2019 if (Tmp1 != Node->getOperand(0) ||
2020 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00002021 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002022 Tmp2);
2023 break;
2024 case TargetLowering::Promote:
Andrew Lenharth72594262005-12-24 23:42:32 +00002025 assert(0 && "Cannot promote handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00002026 case TargetLowering::Expand:
2027 if (MVT::isInteger(Node->getValueType(0))) {
2028 MVT::ValueType VT = Node->getValueType(0);
2029 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2030 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2031 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2032 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2033 } else {
2034 // Floating point mod -> fmod libcall.
2035 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2036 SDOperand Dummy;
2037 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002038 }
2039 break;
2040 }
2041 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002042
Andrew Lenharth5e177822005-05-03 17:19:30 +00002043 case ISD::CTPOP:
2044 case ISD::CTTZ:
2045 case ISD::CTLZ:
2046 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2047 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2048 case TargetLowering::Legal:
2049 if (Tmp1 != Node->getOperand(0))
2050 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2051 break;
2052 case TargetLowering::Promote: {
2053 MVT::ValueType OVT = Tmp1.getValueType();
2054 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002055
2056 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002057 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2058 // Perform the larger operation, then subtract if needed.
2059 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2060 switch(Node->getOpcode())
2061 {
2062 case ISD::CTPOP:
2063 Result = Tmp1;
2064 break;
2065 case ISD::CTTZ:
2066 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002067 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2068 DAG.getConstant(getSizeInBits(NVT), NVT),
2069 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002070 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002071 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2072 break;
2073 case ISD::CTLZ:
2074 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002075 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2076 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002077 getSizeInBits(OVT), NVT));
2078 break;
2079 }
2080 break;
2081 }
2082 case TargetLowering::Custom:
2083 assert(0 && "Cannot custom handle this yet!");
2084 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002085 switch(Node->getOpcode())
2086 {
2087 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00002088 static const uint64_t mask[6] = {
2089 0x5555555555555555ULL, 0x3333333333333333ULL,
2090 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2091 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2092 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002093 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00002094 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2095 unsigned len = getSizeInBits(VT);
2096 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002097 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00002098 Tmp2 = DAG.getConstant(mask[i], VT);
2099 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002100 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002101 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2102 DAG.getNode(ISD::AND, VT,
2103 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2104 Tmp2));
2105 }
2106 Result = Tmp1;
2107 break;
2108 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002109 case ISD::CTLZ: {
2110 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00002111 x = x | (x >> 1);
2112 x = x | (x >> 2);
2113 ...
2114 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002115 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00002116 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002117
Chris Lattner56add052005-05-11 18:35:21 +00002118 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2119 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002120 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2121 unsigned len = getSizeInBits(VT);
2122 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2123 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002124 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002125 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2126 }
2127 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00002128 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00002129 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002130 }
2131 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002132 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002133 // unless the target has ctlz but not ctpop, in which case we use:
2134 // { return 32 - nlz(~x & (x-1)); }
2135 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00002136 MVT::ValueType VT = Tmp1.getValueType();
2137 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002138 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00002139 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2140 DAG.getNode(ISD::SUB, VT, Tmp1,
2141 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002142 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002143 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2144 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002145 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00002146 DAG.getConstant(getSizeInBits(VT), VT),
2147 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2148 } else {
2149 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2150 }
Chris Lattner72473242005-05-11 05:27:09 +00002151 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00002152 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00002153 default:
2154 assert(0 && "Cannot expand this yet!");
2155 break;
2156 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002157 break;
2158 }
2159 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002160
Chris Lattner13fe99c2005-04-02 05:00:07 +00002161 // Unary operators
2162 case ISD::FABS:
2163 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002164 case ISD::FSQRT:
2165 case ISD::FSIN:
2166 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002167 Tmp1 = LegalizeOp(Node->getOperand(0));
2168 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2169 case TargetLowering::Legal:
2170 if (Tmp1 != Node->getOperand(0))
2171 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2172 break;
2173 case TargetLowering::Promote:
2174 case TargetLowering::Custom:
2175 assert(0 && "Cannot promote/custom handle this yet!");
2176 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00002177 switch(Node->getOpcode()) {
2178 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00002179 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2180 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00002181 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00002182 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00002183 break;
2184 }
2185 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002186 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2187 MVT::ValueType VT = Node->getValueType(0);
2188 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002189 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002190 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2191 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2192 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00002193 break;
2194 }
2195 case ISD::FSQRT:
2196 case ISD::FSIN:
2197 case ISD::FCOS: {
2198 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002199 const char *FnName = 0;
2200 switch(Node->getOpcode()) {
2201 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2202 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2203 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2204 default: assert(0 && "Unreachable!");
2205 }
Nate Begeman77558da2005-08-04 21:43:28 +00002206 SDOperand Dummy;
2207 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002208 break;
2209 }
2210 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002211 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002212 }
2213 break;
2214 }
2215 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002216
2217 case ISD::BIT_CONVERT:
2218 if (!isTypeLegal(Node->getOperand(0).getValueType()))
2219 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2220 else {
2221 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2222 Node->getOperand(0).getValueType())) {
2223 default: assert(0 && "Unknown operation action!");
2224 case TargetLowering::Expand:
2225 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2226 break;
2227 case TargetLowering::Legal:
2228 Tmp1 = LegalizeOp(Node->getOperand(0));
2229 if (Tmp1 != Node->getOperand(0))
2230 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2231 break;
2232 }
2233 }
2234 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002235 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002236 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002237 case ISD::UINT_TO_FP: {
2238 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002239 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2240 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002241 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002242 Node->getOperand(0).getValueType())) {
2243 default: assert(0 && "Unknown operation action!");
2244 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002245 Result = ExpandLegalINT_TO_FP(isSigned,
2246 LegalizeOp(Node->getOperand(0)),
2247 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002248 AddLegalizedOperand(Op, Result);
2249 return Result;
2250 case TargetLowering::Promote:
2251 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2252 Node->getValueType(0),
2253 isSigned);
2254 AddLegalizedOperand(Op, Result);
2255 return Result;
2256 case TargetLowering::Legal:
2257 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002258 case TargetLowering::Custom: {
2259 Tmp1 = LegalizeOp(Node->getOperand(0));
2260 SDOperand Tmp =
2261 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2262 Tmp = TLI.LowerOperation(Tmp, DAG);
2263 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002264 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002265 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002266 return Tmp;
2267 } else {
2268 assert(0 && "Target Must Lower this");
2269 }
2270 }
Andrew Lenharthd74877a2005-06-27 23:28:32 +00002271 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002272
Chris Lattnerdc750592005-01-07 07:47:09 +00002273 Tmp1 = LegalizeOp(Node->getOperand(0));
2274 if (Tmp1 != Node->getOperand(0))
2275 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2276 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002277 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002278 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2279 Node->getValueType(0), Node->getOperand(0));
2280 break;
2281 case Promote:
2282 if (isSigned) {
2283 Result = PromoteOp(Node->getOperand(0));
2284 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2285 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2286 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2287 } else {
2288 Result = PromoteOp(Node->getOperand(0));
2289 Result = DAG.getZeroExtendInReg(Result,
2290 Node->getOperand(0).getValueType());
2291 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00002292 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002293 break;
2294 }
2295 break;
2296 }
2297 case ISD::TRUNCATE:
2298 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2299 case Legal:
2300 Tmp1 = LegalizeOp(Node->getOperand(0));
2301 if (Tmp1 != Node->getOperand(0))
2302 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2303 break;
2304 case Expand:
2305 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2306
2307 // Since the result is legal, we should just be able to truncate the low
2308 // part of the source.
2309 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2310 break;
2311 case Promote:
2312 Result = PromoteOp(Node->getOperand(0));
2313 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2314 break;
2315 }
2316 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002317
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002318 case ISD::FP_TO_SINT:
2319 case ISD::FP_TO_UINT:
2320 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2321 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002322 Tmp1 = LegalizeOp(Node->getOperand(0));
2323
Chris Lattner44fe26f2005-07-29 00:11:56 +00002324 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2325 default: assert(0 && "Unknown operation action!");
2326 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002327 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2328 SDOperand True, False;
2329 MVT::ValueType VT = Node->getOperand(0).getValueType();
2330 MVT::ValueType NVT = Node->getValueType(0);
2331 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2332 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2333 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2334 Node->getOperand(0), Tmp2, ISD::SETLT);
2335 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2336 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002337 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002338 Tmp2));
2339 False = DAG.getNode(ISD::XOR, NVT, False,
2340 DAG.getConstant(1ULL << ShiftAmt, NVT));
2341 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner2af3ee42005-12-20 00:53:54 +00002342 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemand5e739d2005-08-14 18:38:32 +00002343 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002344 } else {
2345 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2346 }
2347 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002348 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002349 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002350 Node->getOpcode() == ISD::FP_TO_SINT);
2351 AddLegalizedOperand(Op, Result);
2352 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002353 case TargetLowering::Custom: {
2354 SDOperand Tmp =
2355 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2356 Tmp = TLI.LowerOperation(Tmp, DAG);
2357 if (Tmp.Val) {
Chris Lattner2af3ee42005-12-20 00:53:54 +00002358 Tmp = LegalizeOp(Tmp);
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002359 AddLegalizedOperand(Op, Tmp);
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002360 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002361 } else {
2362 // The target thinks this is legal afterall.
2363 break;
2364 }
2365 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002366 case TargetLowering::Legal:
2367 break;
2368 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002369
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002370 if (Tmp1 != Node->getOperand(0))
2371 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2372 break;
2373 case Expand:
2374 assert(0 && "Shouldn't need to expand other operators here!");
2375 case Promote:
2376 Result = PromoteOp(Node->getOperand(0));
2377 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2378 break;
2379 }
2380 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002381
Chris Lattner7753f172005-09-02 00:18:10 +00002382 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002383 case ISD::ZERO_EXTEND:
2384 case ISD::SIGN_EXTEND:
2385 case ISD::FP_EXTEND:
2386 case ISD::FP_ROUND:
2387 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2388 case Legal:
2389 Tmp1 = LegalizeOp(Node->getOperand(0));
2390 if (Tmp1 != Node->getOperand(0))
2391 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2392 break;
2393 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002394 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002395
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002396 case Promote:
2397 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002398 case ISD::ANY_EXTEND:
2399 Result = PromoteOp(Node->getOperand(0));
2400 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2401 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002402 case ISD::ZERO_EXTEND:
2403 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002404 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002405 Result = DAG.getZeroExtendInReg(Result,
2406 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002407 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002408 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002409 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002410 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002411 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002412 Result,
2413 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002414 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002415 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002416 Result = PromoteOp(Node->getOperand(0));
2417 if (Result.getValueType() != Op.getValueType())
2418 // Dynamically dead while we have only 2 FP types.
2419 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2420 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002421 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002422 Result = PromoteOp(Node->getOperand(0));
2423 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2424 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002425 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002426 }
2427 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002428 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002429 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002430 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002431 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002432
2433 // If this operation is not supported, convert it to a shl/shr or load/store
2434 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002435 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2436 default: assert(0 && "This action not supported for this op yet!");
2437 case TargetLowering::Legal:
2438 if (Tmp1 != Node->getOperand(0))
2439 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002440 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002441 break;
2442 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002443 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002444 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002445 // NOTE: we could fall back on load/store here too for targets without
2446 // SAR. However, it is doubtful that any exist.
2447 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2448 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002449 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002450 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2451 Node->getOperand(0), ShiftCst);
2452 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2453 Result, ShiftCst);
2454 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2455 // The only way we can lower this is to turn it into a STORETRUNC,
2456 // EXTLOAD pair, targetting a temporary location (a stack slot).
2457
2458 // NOTE: there is a choice here between constantly creating new stack
2459 // slots and always reusing the same one. We currently always create
2460 // new ones, as reuse may inhibit scheduling.
2461 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2462 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2463 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2464 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002465 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002466 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2467 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2468 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002469 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002470 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002471 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2472 Result, StackSlot, DAG.getSrcValue(NULL),
2473 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002474 } else {
2475 assert(0 && "Unknown op");
2476 }
2477 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002478 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002479 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002480 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002481 }
Chris Lattner99222f72005-01-15 07:15:18 +00002482 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002483
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002484 // Note that LegalizeOp may be reentered even from single-use nodes, which
2485 // means that we always must cache transformed nodes.
2486 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002487 return Result;
2488}
2489
Chris Lattner4d978642005-01-15 22:16:26 +00002490/// PromoteOp - Given an operation that produces a value in an invalid type,
2491/// promote it to compute the value into a larger type. The produced value will
2492/// have the correct bits for the low portion of the register, but no guarantee
2493/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002494SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2495 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002496 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002497 assert(getTypeAction(VT) == Promote &&
2498 "Caller should expand or legalize operands that are not promotable!");
2499 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2500 "Cannot promote to smaller type!");
2501
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002502 SDOperand Tmp1, Tmp2, Tmp3;
2503
2504 SDOperand Result;
2505 SDNode *Node = Op.Val;
2506
Chris Lattner1a570f12005-09-02 20:32:45 +00002507 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2508 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002509
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002510 // Promotion needs an optimization step to clean up after it, and is not
2511 // careful to avoid operations the target does not support. Make sure that
2512 // all generated operations are legalized in the next iteration.
2513 NeedsAnotherIteration = true;
2514
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002515 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002516 case ISD::CopyFromReg:
2517 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002518 default:
2519 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2520 assert(0 && "Do not know how to promote this operator!");
2521 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002522 case ISD::UNDEF:
2523 Result = DAG.getNode(ISD::UNDEF, NVT);
2524 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002525 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002526 if (VT != MVT::i1)
2527 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2528 else
2529 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002530 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2531 break;
2532 case ISD::ConstantFP:
2533 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2534 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2535 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002536
Chris Lattner2cb338d2005-01-18 02:59:52 +00002537 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002538 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002539 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2540 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002541 Result = LegalizeOp(Result);
2542 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002543
2544 case ISD::TRUNCATE:
2545 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2546 case Legal:
2547 Result = LegalizeOp(Node->getOperand(0));
2548 assert(Result.getValueType() >= NVT &&
2549 "This truncation doesn't make sense!");
2550 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2551 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2552 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002553 case Promote:
2554 // The truncation is not required, because we don't guarantee anything
2555 // about high bits anyway.
2556 Result = PromoteOp(Node->getOperand(0));
2557 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002558 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002559 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2560 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002561 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002562 }
2563 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002564 case ISD::SIGN_EXTEND:
2565 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002566 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2568 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2569 case Legal:
2570 // Input is legal? Just do extend all the way to the larger type.
2571 Result = LegalizeOp(Node->getOperand(0));
2572 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2573 break;
2574 case Promote:
2575 // Promote the reg if it's smaller.
2576 Result = PromoteOp(Node->getOperand(0));
2577 // The high bits are not guaranteed to be anything. Insert an extend.
2578 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002579 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002580 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002581 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002582 Result = DAG.getZeroExtendInReg(Result,
2583 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002584 break;
2585 }
2586 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002587 case ISD::BIT_CONVERT:
2588 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2589 Result = PromoteOp(Result);
2590 break;
2591
Chris Lattner4d978642005-01-15 22:16:26 +00002592 case ISD::FP_EXTEND:
2593 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2594 case ISD::FP_ROUND:
2595 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2596 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2597 case Promote: assert(0 && "Unreachable with 2 FP types!");
2598 case Legal:
2599 // Input is legal? Do an FP_ROUND_INREG.
2600 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002601 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2602 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002603 break;
2604 }
2605 break;
2606
2607 case ISD::SINT_TO_FP:
2608 case ISD::UINT_TO_FP:
2609 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2610 case Legal:
2611 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002612 // No extra round required here.
2613 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002614 break;
2615
2616 case Promote:
2617 Result = PromoteOp(Node->getOperand(0));
2618 if (Node->getOpcode() == ISD::SINT_TO_FP)
2619 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002620 Result,
2621 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002622 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002623 Result = DAG.getZeroExtendInReg(Result,
2624 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002625 // No extra round required here.
2626 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002627 break;
2628 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002629 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2630 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002631 // Round if we cannot tolerate excess precision.
2632 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002633 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2634 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002635 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002636 }
Chris Lattner4d978642005-01-15 22:16:26 +00002637 break;
2638
Chris Lattner268d4572005-12-09 17:32:47 +00002639 case ISD::SIGN_EXTEND_INREG:
2640 Result = PromoteOp(Node->getOperand(0));
2641 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2642 Node->getOperand(1));
2643 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002644 case ISD::FP_TO_SINT:
2645 case ISD::FP_TO_UINT:
2646 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2647 case Legal:
2648 Tmp1 = LegalizeOp(Node->getOperand(0));
2649 break;
2650 case Promote:
2651 // The input result is prerounded, so we don't have to do anything
2652 // special.
2653 Tmp1 = PromoteOp(Node->getOperand(0));
2654 break;
2655 case Expand:
2656 assert(0 && "not implemented");
2657 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002658 // If we're promoting a UINT to a larger size, check to see if the new node
2659 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2660 // we can use that instead. This allows us to generate better code for
2661 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2662 // legal, such as PowerPC.
2663 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002664 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002665 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2666 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002667 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2668 } else {
2669 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2670 }
Chris Lattner4d978642005-01-15 22:16:26 +00002671 break;
2672
Chris Lattner13fe99c2005-04-02 05:00:07 +00002673 case ISD::FABS:
2674 case ISD::FNEG:
2675 Tmp1 = PromoteOp(Node->getOperand(0));
2676 assert(Tmp1.getValueType() == NVT);
2677 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2678 // NOTE: we do not have to do any extra rounding here for
2679 // NoExcessFPPrecision, because we know the input will have the appropriate
2680 // precision, and these operations don't modify precision at all.
2681 break;
2682
Chris Lattner9d6fa982005-04-28 21:44:33 +00002683 case ISD::FSQRT:
2684 case ISD::FSIN:
2685 case ISD::FCOS:
2686 Tmp1 = PromoteOp(Node->getOperand(0));
2687 assert(Tmp1.getValueType() == NVT);
2688 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2689 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002690 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2691 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002692 break;
2693
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002694 case ISD::AND:
2695 case ISD::OR:
2696 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002697 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002698 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002699 case ISD::MUL:
2700 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002701 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002702 // that too is okay if they are integer operations.
2703 Tmp1 = PromoteOp(Node->getOperand(0));
2704 Tmp2 = PromoteOp(Node->getOperand(1));
2705 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2706 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002707 break;
2708 case ISD::FADD:
2709 case ISD::FSUB:
2710 case ISD::FMUL:
2711 // The input may have strange things in the top bits of the registers, but
2712 // these operations don't care.
2713 Tmp1 = PromoteOp(Node->getOperand(0));
2714 Tmp2 = PromoteOp(Node->getOperand(1));
2715 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2716 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2717
2718 // Floating point operations will give excess precision that we may not be
2719 // able to tolerate. If we DO allow excess precision, just leave it,
2720 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002721 // FIXME: Why would we need to round FP ops more than integer ones?
2722 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002723 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002724 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2725 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002726 break;
2727
Chris Lattner4d978642005-01-15 22:16:26 +00002728 case ISD::SDIV:
2729 case ISD::SREM:
2730 // These operators require that their input be sign extended.
2731 Tmp1 = PromoteOp(Node->getOperand(0));
2732 Tmp2 = PromoteOp(Node->getOperand(1));
2733 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002734 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2735 DAG.getValueType(VT));
2736 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2737 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002738 }
2739 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2740
2741 // Perform FP_ROUND: this is probably overly pessimistic.
2742 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002743 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2744 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002745 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002746 case ISD::FDIV:
2747 case ISD::FREM:
2748 // These operators require that their input be fp extended.
2749 Tmp1 = PromoteOp(Node->getOperand(0));
2750 Tmp2 = PromoteOp(Node->getOperand(1));
2751 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2752
2753 // Perform FP_ROUND: this is probably overly pessimistic.
2754 if (NoExcessFPPrecision)
2755 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2756 DAG.getValueType(VT));
2757 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002758
2759 case ISD::UDIV:
2760 case ISD::UREM:
2761 // These operators require that their input be zero extended.
2762 Tmp1 = PromoteOp(Node->getOperand(0));
2763 Tmp2 = PromoteOp(Node->getOperand(1));
2764 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002765 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2766 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002767 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2768 break;
2769
2770 case ISD::SHL:
2771 Tmp1 = PromoteOp(Node->getOperand(0));
2772 Tmp2 = LegalizeOp(Node->getOperand(1));
2773 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2774 break;
2775 case ISD::SRA:
2776 // The input value must be properly sign extended.
2777 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002778 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2779 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002780 Tmp2 = LegalizeOp(Node->getOperand(1));
2781 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2782 break;
2783 case ISD::SRL:
2784 // The input value must be properly zero extended.
2785 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002786 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002787 Tmp2 = LegalizeOp(Node->getOperand(1));
2788 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2789 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002790 case ISD::LOAD:
2791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2792 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002793 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2794 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002795 // Remember that we legalized the chain.
2796 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2797 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002798 case ISD::SEXTLOAD:
2799 case ISD::ZEXTLOAD:
2800 case ISD::EXTLOAD:
2801 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2802 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002803 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2804 Node->getOperand(2),
2805 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002806 // Remember that we legalized the chain.
2807 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2808 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002809 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002810 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2811 case Expand: assert(0 && "It's impossible to expand bools");
2812 case Legal:
2813 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2814 break;
2815 case Promote:
2816 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2817 break;
2818 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002819 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2820 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2821 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2822 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002823 case ISD::SELECT_CC:
2824 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2825 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2826 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2827 Node->getOperand(1), Tmp2, Tmp3,
2828 Node->getOperand(4));
2829 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002830 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002831 case ISD::CALL: {
2832 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2833 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2834
Chris Lattner3d95c142005-01-19 20:24:35 +00002835 std::vector<SDOperand> Ops;
2836 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2837 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2838
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002839 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2840 "Can only promote single result calls");
2841 std::vector<MVT::ValueType> RetTyVTs;
2842 RetTyVTs.reserve(2);
2843 RetTyVTs.push_back(NVT);
2844 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002845 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2846 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002847 Result = SDOperand(NC, 0);
2848
2849 // Insert the new chain mapping.
2850 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2851 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002852 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002853 case ISD::CTPOP:
2854 case ISD::CTTZ:
2855 case ISD::CTLZ:
2856 Tmp1 = Node->getOperand(0);
2857 //Zero extend the argument
2858 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2859 // Perform the larger operation, then subtract if needed.
2860 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2861 switch(Node->getOpcode())
2862 {
2863 case ISD::CTPOP:
2864 Result = Tmp1;
2865 break;
2866 case ISD::CTTZ:
2867 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002868 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002869 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002870 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002871 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2872 break;
2873 case ISD::CTLZ:
2874 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002875 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2876 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002877 getSizeInBits(VT), NVT));
2878 break;
2879 }
2880 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002881 }
2882
2883 assert(Result.Val && "Didn't set a result!");
2884 AddPromotedOperand(Op, Result);
2885 return Result;
2886}
Chris Lattnerdc750592005-01-07 07:47:09 +00002887
Chris Lattner36e663d2005-12-23 00:16:34 +00002888/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002889/// The resultant code need not be legal. Note that SrcOp is the input operand
2890/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002891SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2892 SDOperand SrcOp) {
2893 // Create the stack frame object.
2894 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2895 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002896 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002897 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2898
2899 // Emit a store to the stack slot.
2900 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002901 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002902 // Result is a load from the stack slot.
2903 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2904}
2905
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002906/// ExpandAddSub - Find a clever way to expand this add operation into
2907/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002908void SelectionDAGLegalize::
2909ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2910 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002911 // Expand the subcomponents.
2912 SDOperand LHSL, LHSH, RHSL, RHSH;
2913 ExpandOp(LHS, LHSL, LHSH);
2914 ExpandOp(RHS, RHSL, RHSH);
2915
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002916 std::vector<SDOperand> Ops;
2917 Ops.push_back(LHSL);
2918 Ops.push_back(LHSH);
2919 Ops.push_back(RHSL);
2920 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002921 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2922 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002923 Hi = Lo.getValue(1);
2924}
2925
Chris Lattner4157c412005-04-02 04:00:59 +00002926void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2927 SDOperand Op, SDOperand Amt,
2928 SDOperand &Lo, SDOperand &Hi) {
2929 // Expand the subcomponents.
2930 SDOperand LHSL, LHSH;
2931 ExpandOp(Op, LHSL, LHSH);
2932
2933 std::vector<SDOperand> Ops;
2934 Ops.push_back(LHSL);
2935 Ops.push_back(LHSH);
2936 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002937 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002938 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002939 Hi = Lo.getValue(1);
2940}
2941
2942
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002943/// ExpandShift - Try to find a clever way to expand this shift operation out to
2944/// smaller elements. If we can't find a way that is more efficient than a
2945/// libcall on this target, return false. Otherwise, return true with the
2946/// low-parts expanded into Lo and Hi.
2947bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2948 SDOperand &Lo, SDOperand &Hi) {
2949 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2950 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002951
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002952 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002953 SDOperand ShAmt = LegalizeOp(Amt);
2954 MVT::ValueType ShTy = ShAmt.getValueType();
2955 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2956 unsigned NVTBits = MVT::getSizeInBits(NVT);
2957
2958 // Handle the case when Amt is an immediate. Other cases are currently broken
2959 // and are disabled.
2960 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2961 unsigned Cst = CN->getValue();
2962 // Expand the incoming operand to be shifted, so that we have its parts
2963 SDOperand InL, InH;
2964 ExpandOp(Op, InL, InH);
2965 switch(Opc) {
2966 case ISD::SHL:
2967 if (Cst > VTBits) {
2968 Lo = DAG.getConstant(0, NVT);
2969 Hi = DAG.getConstant(0, NVT);
2970 } else if (Cst > NVTBits) {
2971 Lo = DAG.getConstant(0, NVT);
2972 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002973 } else if (Cst == NVTBits) {
2974 Lo = DAG.getConstant(0, NVT);
2975 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002976 } else {
2977 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2978 Hi = DAG.getNode(ISD::OR, NVT,
2979 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2980 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2981 }
2982 return true;
2983 case ISD::SRL:
2984 if (Cst > VTBits) {
2985 Lo = DAG.getConstant(0, NVT);
2986 Hi = DAG.getConstant(0, NVT);
2987 } else if (Cst > NVTBits) {
2988 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2989 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002990 } else if (Cst == NVTBits) {
2991 Lo = InH;
2992 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002993 } else {
2994 Lo = DAG.getNode(ISD::OR, NVT,
2995 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2996 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2997 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2998 }
2999 return true;
3000 case ISD::SRA:
3001 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003002 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003003 DAG.getConstant(NVTBits-1, ShTy));
3004 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003005 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003006 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003007 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003008 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003009 } else if (Cst == NVTBits) {
3010 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003011 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003012 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003013 } else {
3014 Lo = DAG.getNode(ISD::OR, NVT,
3015 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3016 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3017 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3018 }
3019 return true;
3020 }
3021 }
3022 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
3023 // so disable it for now. Currently targets are handling this via SHL_PARTS
3024 // and friends.
3025 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003026
3027 // If we have an efficient select operation (or if the selects will all fold
3028 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003029 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003030 return false;
3031
3032 SDOperand InL, InH;
3033 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003034 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3035 DAG.getConstant(NVTBits, ShTy), ShAmt);
3036
Chris Lattner4d25c042005-01-20 20:29:23 +00003037 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00003038 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3039 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00003040
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003041 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3042 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3043 DAG.getConstant(NVTBits-1, ShTy));
3044 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3045 DAG.getConstant(NVTBits-1, ShTy));
3046 }
3047
3048 if (Opc == ISD::SHL) {
3049 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3050 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3051 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00003052 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00003053
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003054 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3055 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3056 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00003057 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003058 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3059 DAG.getConstant(32, ShTy),
3060 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00003061 DAG.getConstant(0, NVT),
3062 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003063 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00003064 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003065 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00003066 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003067
3068 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00003069 if (Opc == ISD::SRA)
3070 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3071 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003072 else
3073 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003074 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00003075 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003076 }
3077 return true;
3078}
Chris Lattneraac464e2005-01-21 06:05:23 +00003079
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003080/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3081/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00003082/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003083static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003084 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00003085
Chris Lattner2dce7032005-05-12 23:24:06 +00003086 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00003087 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003088 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003089 Found = Node;
3090 return;
3091 }
3092
3093 // Otherwise, scan the operands of Node to see if any of them is a call.
3094 assert(Node->getNumOperands() != 0 &&
3095 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00003096 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003097 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00003098
3099 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003100 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00003101 Found);
3102}
3103
3104
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003105/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3106/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00003107/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00003108static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3109 std::set<SDNode*> &Visited) {
3110 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
3111 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00003112
Chris Lattner2dce7032005-05-12 23:24:06 +00003113 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00003114 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00003115 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003116 Found = Node;
3117 return;
3118 }
3119
3120 // Otherwise, scan the operands of Node to see if any of them is a call.
3121 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3122 if (UI == E) return;
3123 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00003124 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003125
3126 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00003127 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003128}
3129
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003130/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003131/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003132static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00003133 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00003134 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00003135 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003136 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00003137
Chris Lattner4add7e32005-01-23 04:42:50 +00003138 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00003139 if (TheChain.getValueType() != MVT::Other)
3140 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00003141 if (TheChain.getValueType() != MVT::Other)
3142 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00003143
3144 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00003145 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00003146
Chris Lattner4add7e32005-01-23 04:42:50 +00003147 // Make sure to only follow users of our token chain.
3148 SDNode *User = *UI;
3149 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3150 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00003151 if (SDNode *Result = FindCallSeqEnd(User))
3152 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00003153 }
Chris Lattnercabdc342005-08-05 16:23:57 +00003154 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00003155}
3156
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003157/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00003158/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003159static SDNode *FindCallSeqStart(SDNode *Node) {
3160 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00003161 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003162
3163 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3164 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003165 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003166}
3167
3168
Chris Lattner4add7e32005-01-23 04:42:50 +00003169/// FindInputOutputChains - If we are replacing an operation with a call we need
3170/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00003171/// properly serialize the calls in the block. The returned operand is the
3172/// input chain value for the new call (e.g. the entry node or the previous
3173/// call), and OutChain is set to be the chain node to update to point to the
3174/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00003175static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3176 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003177 SDNode *LatestCallSeqStart = Entry.Val;
3178 SDNode *LatestCallSeqEnd = 0;
3179 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3180 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00003181
Chris Lattner2dce7032005-05-12 23:24:06 +00003182 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00003183 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00003184 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3185 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00003186 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003187 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00003188 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3189 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003190 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00003191 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003192 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00003193
Chris Lattner06bbeb62005-05-11 19:02:11 +00003194 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003195 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003196 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00003197 std::set<SDNode*> Visited;
3198 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00003199
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003200 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00003201 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003202 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00003203
Chris Lattner5a14c8a2005-05-13 05:09:11 +00003204 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00003205}
3206
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003207/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00003208void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3209 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00003210 // Nothing to splice it into?
3211 if (OutChain == 0) return;
3212
3213 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3214 //OutChain->dump();
3215
3216 // Form a token factor node merging the old inval and the new inval.
3217 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3218 OutChain->getOperand(0));
3219 // Change the node to refer to the new token.
3220 OutChain->setAdjCallChain(InToken);
3221}
Chris Lattner4add7e32005-01-23 04:42:50 +00003222
3223
Chris Lattneraac464e2005-01-21 06:05:23 +00003224// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3225// does not fit into a register, return the lo part and set the hi part to the
3226// by-reg argument. If it does fit into a single register, return the result
3227// and leave the Hi part unset.
3228SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3229 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00003230 SDNode *OutChain;
3231 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3232 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00003233 if (InChain.Val == 0)
3234 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00003235
Chris Lattneraac464e2005-01-21 06:05:23 +00003236 TargetLowering::ArgListTy Args;
3237 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3238 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3239 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3240 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3241 }
3242 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003243
Chris Lattner06bbeb62005-05-11 19:02:11 +00003244 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003245 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003246 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003247 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3248 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003249
Chris Lattner63022662005-09-02 20:26:58 +00003250 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003251 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003252 default: assert(0 && "Unknown thing");
3253 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00003254 Result = CallInfo.first;
3255 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003256 case Promote:
3257 assert(0 && "Cannot promote this yet!");
3258 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003259 ExpandOp(CallInfo.first, Result, Hi);
3260 CallInfo.second = LegalizeOp(CallInfo.second);
3261 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003262 }
Chris Lattner63022662005-09-02 20:26:58 +00003263
3264 SpliceCallInto(CallInfo.second, OutChain);
3265 NeedsAnotherIteration = true;
3266 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003267}
3268
Chris Lattner4add7e32005-01-23 04:42:50 +00003269
Chris Lattneraac464e2005-01-21 06:05:23 +00003270/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3271/// destination type is legal.
3272SDOperand SelectionDAGLegalize::
3273ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003274 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003275 assert(getTypeAction(Source.getValueType()) == Expand &&
3276 "This is not an expansion!");
3277 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3278
Chris Lattner06bbeb62005-05-11 19:02:11 +00003279 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003280 assert(Source.getValueType() == MVT::i64 &&
3281 "This only works for 64-bit -> FP");
3282 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3283 // incoming integer is set. To handle this, we dynamically test to see if
3284 // it is set, and, if so, add a fudge factor.
3285 SDOperand Lo, Hi;
3286 ExpandOp(Source, Lo, Hi);
3287
Chris Lattner2a4f7312005-05-13 04:45:13 +00003288 // If this is unsigned, and not supported, first perform the conversion to
3289 // signed, then adjust the result if the sign bit is set.
3290 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3291 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3292
Chris Lattnerd47675e2005-08-09 20:20:18 +00003293 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3294 DAG.getConstant(0, Hi.getValueType()),
3295 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003296 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3297 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3298 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003299 uint64_t FF = 0x5f800000ULL;
3300 if (TLI.isLittleEndian()) FF <<= 32;
3301 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003302
Chris Lattnerc30405e2005-08-26 17:15:30 +00003303 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003304 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3305 SDOperand FudgeInReg;
3306 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003307 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3308 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003309 else {
3310 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003311 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3312 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003313 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003314 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003315 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003316
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003317 // Check to see if the target has a custom way to lower this. If so, use it.
3318 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3319 default: assert(0 && "This action not implemented for this operation!");
3320 case TargetLowering::Legal:
3321 case TargetLowering::Expand:
3322 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003323 case TargetLowering::Custom: {
3324 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3325 Source), DAG);
3326 if (NV.Val)
3327 return LegalizeOp(NV);
3328 break; // The target decided this was legal after all
3329 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003330 }
3331
Chris Lattner153587e2005-05-12 07:00:44 +00003332 // Expand the source, then glue it back together for the call. We must expand
3333 // the source in case it is shared (this pass of legalize must traverse it).
3334 SDOperand SrcLo, SrcHi;
3335 ExpandOp(Source, SrcLo, SrcHi);
3336 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3337
Chris Lattner06bbeb62005-05-11 19:02:11 +00003338 SDNode *OutChain = 0;
3339 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3340 DAG.getEntryNode());
3341 const char *FnName = 0;
3342 if (DestTy == MVT::f32)
3343 FnName = "__floatdisf";
3344 else {
3345 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3346 FnName = "__floatdidf";
3347 }
3348
Chris Lattneraac464e2005-01-21 06:05:23 +00003349 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3350
3351 TargetLowering::ArgListTy Args;
3352 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003353
Chris Lattneraac464e2005-01-21 06:05:23 +00003354 Args.push_back(std::make_pair(Source, ArgTy));
3355
3356 // We don't care about token chains for libcalls. We just use the entry
3357 // node as our input and ignore the output chain. This allows us to place
3358 // calls wherever we need them to satisfy data dependences.
3359 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003360
3361 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003362 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3363 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003364
Chris Lattnera5bf1032005-05-12 04:49:08 +00003365 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003366 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003367}
Misha Brukman835702a2005-04-21 22:36:52 +00003368
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003369
3370
Chris Lattnerdc750592005-01-07 07:47:09 +00003371/// ExpandOp - Expand the specified SDOperand into its two component pieces
3372/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3373/// LegalizeNodes map is filled in for any results that are not expanded, the
3374/// ExpandedNodes map is filled in for any results that are expanded, and the
3375/// Lo/Hi values are returned.
3376void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3377 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003378 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003379 SDNode *Node = Op.Val;
3380 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003381 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3382 "Cannot expand FP values!");
3383 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003384 "Cannot expand to FP value or to larger int value!");
3385
Chris Lattner1a570f12005-09-02 20:32:45 +00003386 // See if we already expanded it.
3387 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3388 = ExpandedNodes.find(Op);
3389 if (I != ExpandedNodes.end()) {
3390 Lo = I->second.first;
3391 Hi = I->second.second;
3392 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003393 }
3394
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003395 // Expanding to multiple registers needs to perform an optimization step, and
3396 // is not careful to avoid operations the target does not support. Make sure
3397 // that all generated operations are legalized in the next iteration.
3398 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003399
Chris Lattnerdc750592005-01-07 07:47:09 +00003400 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003401 case ISD::CopyFromReg:
3402 assert(0 && "CopyFromReg must be legal!");
3403 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003404 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3405 assert(0 && "Do not know how to expand this operator!");
3406 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003407 case ISD::UNDEF:
3408 Lo = DAG.getNode(ISD::UNDEF, NVT);
3409 Hi = DAG.getNode(ISD::UNDEF, NVT);
3410 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003411 case ISD::Constant: {
3412 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3413 Lo = DAG.getConstant(Cst, NVT);
3414 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3415 break;
3416 }
Nate Begemanae89d862005-12-07 19:48:11 +00003417 case ISD::ConstantVec: {
3418 unsigned NumElements = Node->getNumOperands();
3419 // If we only have two elements left in the constant vector, just break it
3420 // apart into the two scalar constants it contains. Otherwise, bisect the
3421 // ConstantVec, and return each half as a new ConstantVec.
3422 // FIXME: this is hard coded as big endian, it may have to change to support
3423 // SSE and Alpha MVI
3424 if (NumElements == 2) {
3425 Hi = Node->getOperand(0);
3426 Lo = Node->getOperand(1);
3427 } else {
3428 NumElements /= 2;
3429 std::vector<SDOperand> LoOps, HiOps;
3430 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3431 HiOps.push_back(Node->getOperand(I));
3432 LoOps.push_back(Node->getOperand(I+NumElements));
3433 }
3434 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3435 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3436 }
3437 break;
3438 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003439
Chris Lattner32e08b72005-03-28 22:03:13 +00003440 case ISD::BUILD_PAIR:
3441 // Legalize both operands. FIXME: in the future we should handle the case
3442 // where the two elements are not legal.
3443 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3444 Lo = LegalizeOp(Node->getOperand(0));
3445 Hi = LegalizeOp(Node->getOperand(1));
3446 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003447
3448 case ISD::SIGN_EXTEND_INREG:
3449 ExpandOp(Node->getOperand(0), Lo, Hi);
3450 // Sign extend the lo-part.
3451 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3452 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3453 TLI.getShiftAmountTy()));
3454 // sext_inreg the low part if needed.
3455 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3456 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003457
Chris Lattner55e9cde2005-05-11 04:51:16 +00003458 case ISD::CTPOP:
3459 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003460 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3461 DAG.getNode(ISD::CTPOP, NVT, Lo),
3462 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003463 Hi = DAG.getConstant(0, NVT);
3464 break;
3465
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003466 case ISD::CTLZ: {
3467 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003468 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003469 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3470 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003471 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3472 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003473 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3474 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3475
3476 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3477 Hi = DAG.getConstant(0, NVT);
3478 break;
3479 }
3480
3481 case ISD::CTTZ: {
3482 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003483 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003484 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3485 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003486 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3487 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003488 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3489 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3490
3491 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3492 Hi = DAG.getConstant(0, NVT);
3493 break;
3494 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003495
Chris Lattnerdc750592005-01-07 07:47:09 +00003496 case ISD::LOAD: {
3497 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3498 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003499 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003500
3501 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003502 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003503 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3504 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003505 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003506 //are from the same instruction?
3507 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003508
3509 // Build a factor node to remember that this load is independent of the
3510 // other one.
3511 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3512 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003513
Chris Lattnerdc750592005-01-07 07:47:09 +00003514 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003515 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003516 if (!TLI.isLittleEndian())
3517 std::swap(Lo, Hi);
3518 break;
3519 }
Nate Begemand37c1312005-11-22 18:16:00 +00003520 case ISD::VLOAD: {
3521 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3522 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3523 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3524 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3525
3526 // If we only have two elements, turn into a pair of scalar loads.
3527 // FIXME: handle case where a vector of two elements is fine, such as
3528 // 2 x double on SSE2.
3529 if (NumElements == 2) {
3530 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3531 // Increment the pointer to the other half.
3532 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3533 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3534 getIntPtrConstant(IncrementSize));
3535 //Is this safe? declaring that the two parts of the split load
3536 //are from the same instruction?
3537 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3538 } else {
3539 NumElements /= 2; // Split the vector in half
3540 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3541 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3542 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3543 getIntPtrConstant(IncrementSize));
3544 //Is this safe? declaring that the two parts of the split load
3545 //are from the same instruction?
3546 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3547 }
3548
3549 // Build a factor node to remember that this load is independent of the
3550 // other one.
3551 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3552 Hi.getValue(1));
3553
3554 // Remember that we legalized the chain.
3555 AddLegalizedOperand(Op.getValue(1), TF);
3556 if (!TLI.isLittleEndian())
3557 std::swap(Lo, Hi);
3558 break;
3559 }
3560 case ISD::VADD:
3561 case ISD::VSUB:
3562 case ISD::VMUL: {
3563 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3564 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3565 SDOperand LL, LH, RL, RH;
3566
3567 ExpandOp(Node->getOperand(0), LL, LH);
3568 ExpandOp(Node->getOperand(1), RL, RH);
3569
3570 // If we only have two elements, turn into a pair of scalar loads.
3571 // FIXME: handle case where a vector of two elements is fine, such as
3572 // 2 x double on SSE2.
3573 if (NumElements == 2) {
3574 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3575 Lo = DAG.getNode(Opc, EVT, LL, RL);
3576 Hi = DAG.getNode(Opc, EVT, LH, RH);
3577 } else {
3578 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3579 LL.getOperand(3));
3580 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3581 LH.getOperand(3));
3582 }
3583 break;
3584 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003585 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003586 case ISD::CALL: {
3587 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3588 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3589
Chris Lattner3d95c142005-01-19 20:24:35 +00003590 bool Changed = false;
3591 std::vector<SDOperand> Ops;
3592 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3593 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3594 Changed |= Ops.back() != Node->getOperand(i);
3595 }
3596
Chris Lattnerdc750592005-01-07 07:47:09 +00003597 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3598 "Can only expand a call once so far, not i64 -> i16!");
3599
3600 std::vector<MVT::ValueType> RetTyVTs;
3601 RetTyVTs.reserve(3);
3602 RetTyVTs.push_back(NVT);
3603 RetTyVTs.push_back(NVT);
3604 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003605 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3606 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003607 Lo = SDOperand(NC, 0);
3608 Hi = SDOperand(NC, 1);
3609
3610 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003611 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003612 break;
3613 }
3614 case ISD::AND:
3615 case ISD::OR:
3616 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3617 SDOperand LL, LH, RL, RH;
3618 ExpandOp(Node->getOperand(0), LL, LH);
3619 ExpandOp(Node->getOperand(1), RL, RH);
3620 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3621 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3622 break;
3623 }
3624 case ISD::SELECT: {
3625 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003626
3627 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3628 case Expand: assert(0 && "It's impossible to expand bools");
3629 case Legal:
3630 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3631 break;
3632 case Promote:
3633 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3634 break;
3635 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003636 ExpandOp(Node->getOperand(1), LL, LH);
3637 ExpandOp(Node->getOperand(2), RL, RH);
3638 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3639 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3640 break;
3641 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003642 case ISD::SELECT_CC: {
3643 SDOperand TL, TH, FL, FH;
3644 ExpandOp(Node->getOperand(2), TL, TH);
3645 ExpandOp(Node->getOperand(3), FL, FH);
3646 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3647 Node->getOperand(1), TL, FL, Node->getOperand(4));
3648 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3649 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003650 Lo = LegalizeOp(Lo);
3651 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003652 break;
3653 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003654 case ISD::SEXTLOAD: {
3655 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3656 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3657 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3658
3659 if (EVT == NVT)
3660 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3661 else
3662 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3663 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003664
3665 // Remember that we legalized the chain.
3666 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3667
Nate Begemanc3a89c52005-10-13 17:15:37 +00003668 // The high part is obtained by SRA'ing all but one of the bits of the lo
3669 // part.
3670 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3671 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3672 TLI.getShiftAmountTy()));
3673 Lo = LegalizeOp(Lo);
3674 Hi = LegalizeOp(Hi);
3675 break;
3676 }
3677 case ISD::ZEXTLOAD: {
3678 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3679 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3680 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3681
3682 if (EVT == NVT)
3683 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3684 else
3685 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3686 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003687
3688 // Remember that we legalized the chain.
3689 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3690
Nate Begemanc3a89c52005-10-13 17:15:37 +00003691 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003692 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003693 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003694 break;
3695 }
3696 case ISD::EXTLOAD: {
3697 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3698 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3699 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3700
3701 if (EVT == NVT)
3702 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3703 else
3704 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3705 EVT);
3706
3707 // Remember that we legalized the chain.
3708 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3709
3710 // The high part is undefined.
3711 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3712 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003713 break;
3714 }
Chris Lattner7753f172005-09-02 00:18:10 +00003715 case ISD::ANY_EXTEND: {
3716 SDOperand In;
3717 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3718 case Expand: assert(0 && "expand-expand not implemented yet!");
3719 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3720 case Promote:
3721 In = PromoteOp(Node->getOperand(0));
3722 break;
3723 }
3724
3725 // The low part is any extension of the input (which degenerates to a copy).
3726 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3727 // The high part is undefined.
3728 Hi = DAG.getNode(ISD::UNDEF, NVT);
3729 break;
3730 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003731 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003732 SDOperand In;
3733 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3734 case Expand: assert(0 && "expand-expand not implemented yet!");
3735 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3736 case Promote:
3737 In = PromoteOp(Node->getOperand(0));
3738 // Emit the appropriate sign_extend_inreg to get the value we want.
3739 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003740 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003741 break;
3742 }
3743
Chris Lattnerdc750592005-01-07 07:47:09 +00003744 // The low part is just a sign extension of the input (which degenerates to
3745 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003746 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003747
Chris Lattnerdc750592005-01-07 07:47:09 +00003748 // The high part is obtained by SRA'ing all but one of the bits of the lo
3749 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003750 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003751 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3752 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003753 break;
3754 }
Chris Lattner47844892005-04-03 23:41:52 +00003755 case ISD::ZERO_EXTEND: {
3756 SDOperand In;
3757 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3758 case Expand: assert(0 && "expand-expand not implemented yet!");
3759 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3760 case Promote:
3761 In = PromoteOp(Node->getOperand(0));
3762 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003763 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003764 break;
3765 }
3766
Chris Lattnerdc750592005-01-07 07:47:09 +00003767 // The low part is just a zero extension of the input (which degenerates to
3768 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003769 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003770
Chris Lattnerdc750592005-01-07 07:47:09 +00003771 // The high part is just a zero.
3772 Hi = DAG.getConstant(0, NVT);
3773 break;
Chris Lattner47844892005-04-03 23:41:52 +00003774 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003775
3776 case ISD::BIT_CONVERT: {
3777 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3778 Node->getOperand(0));
3779 ExpandOp(Tmp, Lo, Hi);
3780 break;
3781 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003782
Chris Lattner44c28c22005-11-20 22:56:56 +00003783 case ISD::READCYCLECOUNTER: {
3784 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3785 TargetLowering::Custom &&
3786 "Must custom expand ReadCycleCounter");
3787 SDOperand T = TLI.LowerOperation(Op, DAG);
3788 assert(T.Val && "Node must be custom expanded!");
3789 Lo = LegalizeOp(T.getValue(0));
3790 Hi = LegalizeOp(T.getValue(1));
3791 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3792 LegalizeOp(T.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003793 break;
Chris Lattner44c28c22005-11-20 22:56:56 +00003794 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003795
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003796 // These operators cannot be expanded directly, emit them as calls to
3797 // library functions.
3798 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003799 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003800 SDOperand Op;
3801 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3802 case Expand: assert(0 && "cannot expand FP!");
3803 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3804 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3805 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003806
Chris Lattner941d84a2005-07-30 01:40:57 +00003807 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3808
Chris Lattnerfe68d752005-07-29 00:33:32 +00003809 // Now that the custom expander is done, expand the result, which is still
3810 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003811 if (Op.Val) {
3812 ExpandOp(Op, Lo, Hi);
3813 break;
3814 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003815 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003816
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003817 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003818 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003819 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003820 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003821 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003822
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003823 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003824 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3825 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3826 LegalizeOp(Node->getOperand(0)));
3827 // Now that the custom expander is done, expand the result, which is still
3828 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003829 Op = TLI.LowerOperation(Op, DAG);
3830 if (Op.Val) {
3831 ExpandOp(Op, Lo, Hi);
3832 break;
3833 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003834 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003835
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003836 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003837 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003838 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003839 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003840 break;
3841
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003842 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003843 // If the target wants custom lowering, do so.
3844 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3845 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3846 LegalizeOp(Node->getOperand(1)));
3847 Op = TLI.LowerOperation(Op, DAG);
3848 if (Op.Val) {
3849 // Now that the custom expander is done, expand the result, which is
3850 // still VT.
3851 ExpandOp(Op, Lo, Hi);
3852 break;
3853 }
3854 }
3855
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003856 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003857 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003858 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003859
3860 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003861 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003862 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3863 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003864 break;
3865 }
3866
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003867 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003868 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003869 break;
3870
3871 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003872 // If the target wants custom lowering, do so.
3873 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3874 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3875 LegalizeOp(Node->getOperand(1)));
3876 Op = TLI.LowerOperation(Op, DAG);
3877 if (Op.Val) {
3878 // Now that the custom expander is done, expand the result, which is
3879 // still VT.
3880 ExpandOp(Op, Lo, Hi);
3881 break;
3882 }
3883 }
3884
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003885 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003886 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003887 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003888
3889 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003890 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003891 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3892 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003893 break;
3894 }
3895
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003896 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003897 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003898 break;
3899 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003900 // If the target wants custom lowering, do so.
3901 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3902 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3903 LegalizeOp(Node->getOperand(1)));
3904 Op = TLI.LowerOperation(Op, DAG);
3905 if (Op.Val) {
3906 // Now that the custom expander is done, expand the result, which is
3907 // still VT.
3908 ExpandOp(Op, Lo, Hi);
3909 break;
3910 }
3911 }
3912
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003913 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003914 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003915 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003916
3917 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003918 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003919 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3920 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003921 break;
3922 }
3923
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003924 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003925 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003926 break;
3927
Misha Brukman835702a2005-04-21 22:36:52 +00003928 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003929 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3930 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003931 break;
3932 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003933 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3934 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003935 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003936 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003937 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003938 SDOperand LL, LH, RL, RH;
3939 ExpandOp(Node->getOperand(0), LL, LH);
3940 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003941 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3942 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3943 // extended the sign bit of the low half through the upper half, and if so
3944 // emit a MULHS instead of the alternate sequence that is valid for any
3945 // i64 x i64 multiply.
3946 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3947 // is RH an extension of the sign bit of RL?
3948 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3949 RH.getOperand(1).getOpcode() == ISD::Constant &&
3950 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3951 // is LH an extension of the sign bit of LL?
3952 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3953 LH.getOperand(1).getOpcode() == ISD::Constant &&
3954 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3955 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3956 } else {
3957 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3958 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3959 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3960 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3961 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3962 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003963 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3964 } else {
3965 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3966 }
3967 break;
3968 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003969 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3970 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3971 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3972 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003973 }
3974
3975 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003976 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3977 std::make_pair(Lo, Hi))).second;
3978 assert(isNew && "Value already expanded?!?");
Chris Lattner2af3ee42005-12-20 00:53:54 +00003979
Chris Lattnerac12f682005-12-21 18:02:52 +00003980 // Make sure the resultant values have been legalized themselves, unless this
3981 // is a type that requires multi-step expansion.
3982 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3983 Lo = LegalizeOp(Lo);
3984 Hi = LegalizeOp(Hi);
3985 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003986}
3987
3988
3989// SelectionDAG::Legalize - This is the entry point for the file.
3990//
Chris Lattner4add7e32005-01-23 04:42:50 +00003991void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003992 /// run - This is the main entry point to this class.
3993 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003994 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003995}
3996