blob: 7d7c785d9b3a92899d884e14b56425eac0d29325 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman6a648612005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattner3e928bb2005-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 Lattner03c85462005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattner8afc48e2005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-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 Lattner8afc48e2005-01-07 22:28:47 +000084 }
Chris Lattner03c85462005-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 Lattner69a889e2005-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 Lattner03c85462005-01-15 05:21:40 +000090 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000091
Chris Lattner3e928bb2005-01-07 07:47:09 +000092public:
93
Chris Lattner9c32d3b2005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-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 Lattner03c85462005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127
Chris Lattner77e77a62005-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 Lattnercad063f2005-07-16 00:19:57 +0000132
Chris Lattner35481892005-12-23 00:16:34 +0000133 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000134 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
135 SDOperand LegalOp,
136 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000137 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
138 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000139 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
140 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000141
Chris Lattnere34b3962005-01-19 04:19:40 +0000142 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-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 Lattner47599822005-04-02 03:38:53 +0000147 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000148
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000149 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
150
Chris Lattner3e928bb2005-01-07 07:47:09 +0000151 SDOperand getIntPtrConstant(uint64_t Val) {
152 return DAG.getConstant(Val, TLI.getPointerTy());
153 }
154};
155}
156
Chris Lattnerb89175f2005-11-19 05:51:46 +0000157static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000158 switch (VecOp) {
159 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-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 Lattner3e928bb2005-01-07 07:47:09 +0000165
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000171}
172
Jim Laskey6269ed12005-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 Lattnercad063f2005-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 Laskey6269ed12005-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 Laskey02659d22005-08-17 17:42:52 +0000220 SDOperand Bias = DAG.getConstantFP(isSigned ?
221 BitsToDouble(0x4330000080000000ULL)
222 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000223 MVT::f64);
224 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-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 Lattner69a889e2005-12-20 00:53:54 +0000236 return LegalizeOp(Result);
Jim Laskey6269ed12005-08-17 00:39:29 +0000237 }
238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
242 DAG.getConstant(0, Op0.getValueType()),
243 ISD::SETLT);
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000247
Jim Laskey6269ed12005-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 Lattnerf4d32722005-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 Lattnercad063f2005-07-16 00:19:57 +0000259 if (TLI.isLittleEndian()) FF <<= 32;
260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000261
Chris Lattner5839bf22005-08-26 17:15:30 +0000262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000274
Chris Lattner69a889e2005-12-20 00:53:54 +0000275 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnercad063f2005-07-16 00:19:57 +0000276}
277
Chris Lattner149c58c2005-08-16 18:17:10 +0000278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000279/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-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 Begeman5a8441e2005-07-16 02:02:34 +0000283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
284 MVT::ValueType DestVT,
285 bool isSigned) {
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000288
Chris Lattnercad063f2005-07-16 00:19:57 +0000289 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000290
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000295
Chris Lattnercad063f2005-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 Lattnerc9c60f62005-08-24 16:35:28 +0000300 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-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 Begeman5a8441e2005-07-16 02:02:34 +0000308 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000309
Chris Lattnercad063f2005-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 Lattnerc9c60f62005-08-24 16:35:28 +0000314 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000322
Chris Lattnercad063f2005-07-16 00:19:57 +0000323 // Otherwise, try a larger type.
324 }
325
Chris Lattnercad063f2005-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 Lattner69a889e2005-12-20 00:53:54 +0000328 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattner69a889e2005-12-20 00:53:54 +0000331 // Make sure to legalize any nodes we create here.
332 return LegalizeOp(N);
Chris Lattnercad063f2005-07-16 00:19:57 +0000333}
334
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000345
Chris Lattner1618beb2005-07-29 00:11:56 +0000346 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000347
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000352
Chris Lattner1618beb2005-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 Lattnerc9c60f62005-08-24 16:35:28 +0000357 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000365
Chris Lattner1618beb2005-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 Lattnerc9c60f62005-08-24 16:35:28 +0000370 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000378
Chris Lattner1618beb2005-07-29 00:11:56 +0000379 // Otherwise, try a larger type.
380 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000381
Chris Lattner1618beb2005-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 Lattner69a889e2005-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 Lattner1618beb2005-07-29 00:11:56 +0000388}
389
Chris Lattner32fca002005-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 Lattner32fca002005-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 Lattner1618beb2005-07-29 00:11:56 +0000410
Chris Lattner3e928bb2005-01-07 07:47:09 +0000411void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-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 Lattner32fca002005-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 Lattnerab510a72005-10-02 17:49:46 +0000420
Chris Lattner32fca002005-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 Lattnerde202b32005-11-09 23:47:37 +0000425 if (I->getNumOperands() == 0) {
426 Visited[I] = 0 - 1U;
427 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000428 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000429 }
430
Chris Lattnerde202b32005-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 Lattner32fca002005-10-06 01:20:27 +0000434 "Error: DAG is cyclic!");
435 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000436
Chris Lattner32fca002005-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 Lattner3e928bb2005-01-07 07:47:09 +0000456 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000457 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
458 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000459
460 ExpandedNodes.clear();
461 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000462 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463
464 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000465 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466}
467
468SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000469 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000470 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000471 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000472
Chris Lattner3e928bb2005-01-07 07:47:09 +0000473 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000474 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-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 Lattner3e928bb2005-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 Lattner03c85462005-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 Lattner3e928bb2005-01-07 07:47:09 +0000491 }
492 }
493
Chris Lattner45982da2005-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 Lattnere1bd8222005-01-11 05:57:22 +0000496 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
497 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000498
Nate Begeman9373a812005-08-10 20:51:12 +0000499 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000500
501 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000502
503 switch (Node->getOpcode()) {
504 default:
Chris Lattnerd73cc5d2005-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 Lattner3e928bb2005-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 Lattner32fca002005-10-06 01:20:27 +0000533 case ISD::TargetFrameIndex:
534 case ISD::Register:
535 case ISD::TargetConstant:
Nate Begeman28a6b022005-12-10 02:36:00 +0000536 case ISD::TargetConstantPool:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000537 case ISD::GlobalAddress:
Chris Lattnerb9debbf2005-11-17 05:52:24 +0000538 case ISD::TargetGlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000539 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000540 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000541 case ISD::BasicBlock:
542 case ISD::CONDCODE:
543 case ISD::VALUETYPE:
544 case ISD::SRCVALUE:
Chris Lattner36ce6912005-11-29 06:21:05 +0000545 case ISD::STRING:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000546 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
547 default: assert(0 && "This action is not supported yet!");
548 case TargetLowering::Custom: {
549 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
550 if (Tmp.Val) {
551 Result = LegalizeOp(Tmp);
552 break;
553 }
554 } // FALLTHROUGH if the target doesn't want to lower this op after all.
555 case TargetLowering::Legal:
556 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
557 break;
558 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000559 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000560 case ISD::AssertSext:
561 case ISD::AssertZext:
562 Tmp1 = LegalizeOp(Node->getOperand(0));
563 if (Tmp1 != Node->getOperand(0))
564 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
565 Node->getOperand(1));
566 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000567 case ISD::MERGE_VALUES:
568 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner69a52152005-01-14 22:38:01 +0000569 case ISD::CopyFromReg:
570 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000571 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000572 if (Node->getNumValues() == 2) {
Chris Lattner7310fb12005-12-18 15:27:43 +0000573 if (Tmp1 != Node->getOperand(0))
574 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000575 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattner7310fb12005-12-18 15:27:43 +0000576 Node->getValueType(0));
577 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000578 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
579 if (Node->getNumOperands() == 3)
580 Tmp2 = LegalizeOp(Node->getOperand(2));
581 if (Tmp1 != Node->getOperand(0) ||
582 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattner7310fb12005-12-18 15:27:43 +0000583 Result = DAG.getCopyFromReg(Tmp1,
584 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
585 Node->getValueType(0), Tmp2);
586 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
587 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000588 // Since CopyFromReg produces two values, make sure to remember that we
589 // legalized both of them.
590 AddLegalizedOperand(Op.getValue(0), Result);
591 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
592 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000593 case ISD::UNDEF: {
594 MVT::ValueType VT = Op.getValueType();
595 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000596 default: assert(0 && "This action is not supported yet!");
597 case TargetLowering::Expand:
598 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000599 if (MVT::isInteger(VT))
600 Result = DAG.getConstant(0, VT);
601 else if (MVT::isFloatingPoint(VT))
602 Result = DAG.getConstantFP(0, VT);
603 else
604 assert(0 && "Unknown value type!");
605 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000606 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000607 break;
608 }
609 break;
610 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000611
612 case ISD::LOCATION:
613 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
614 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
615
616 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
617 case TargetLowering::Promote:
618 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000619 case TargetLowering::Expand: {
Jim Laskeye81aecb2005-12-21 20:51:37 +0000620 if (TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other)) {
621 MachineDebugInfo &DebugInfo = DAG.getMachineFunction().getDebugInfo();
622 std::vector<SDOperand> Ops;
623 Ops.push_back(Tmp1); // chain
624 Ops.push_back(Node->getOperand(1)); // line #
625 Ops.push_back(Node->getOperand(2)); // col #
626 const std::string &fname =
627 cast<StringSDNode>(Node->getOperand(3))->getValue();
628 const std::string &dirname =
629 cast<StringSDNode>(Node->getOperand(4))->getValue();
630 unsigned id = DebugInfo.RecordSource(fname, dirname);
631 Ops.push_back(DAG.getConstant(id, MVT::i32)); // source file id
632 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
633 } else {
634 Result = Tmp1; // chain
635 }
Chris Lattnere7736732005-12-18 23:54:29 +0000636 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner36ce6912005-11-29 06:21:05 +0000637 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000638 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000639 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000640 if (Tmp1 != Node->getOperand(0) ||
641 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000642 std::vector<SDOperand> Ops;
643 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000644 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
645 Ops.push_back(Node->getOperand(1)); // line # must be legal.
646 Ops.push_back(Node->getOperand(2)); // col # must be legal.
647 } else {
648 // Otherwise promote them.
649 Ops.push_back(PromoteOp(Node->getOperand(1)));
650 Ops.push_back(PromoteOp(Node->getOperand(2)));
651 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000652 Ops.push_back(Node->getOperand(3)); // filename must be legal.
653 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
654 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
655 }
656 break;
657 }
658 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000659
660 case ISD::DEBUG_LOC:
661 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
662 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
663 case TargetLowering::Promote:
664 case TargetLowering::Expand:
665 default: assert(0 && "This action is not supported yet!");
666 case TargetLowering::Legal:
667 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
668 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
669 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
670 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
671
672 if (Tmp1 != Node->getOperand(0) ||
673 Tmp2 != Node->getOperand(1) ||
674 Tmp3 != Node->getOperand(2) ||
675 Tmp4 != Node->getOperand(3)) {
676 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
677 }
678 break;
679 }
680 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000681
Chris Lattner3e928bb2005-01-07 07:47:09 +0000682 case ISD::Constant:
683 // We know we don't need to expand constants here, constants only have one
684 // value and we check that it is fine above.
685
686 // FIXME: Maybe we should handle things like targets that don't support full
687 // 32-bit immediates?
688 break;
689 case ISD::ConstantFP: {
690 // Spill FP immediates to the constant pool if the target cannot directly
691 // codegen them. Targets often have some immediate values that can be
692 // efficiently generated into an FP register without a load. We explicitly
693 // leave these constants as ConstantFP nodes for the target to deal with.
694
695 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
696
697 // Check to see if this FP immediate is already legal.
698 bool isLegal = false;
699 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
700 E = TLI.legal_fpimm_end(); I != E; ++I)
701 if (CFP->isExactlyValue(*I)) {
702 isLegal = true;
703 break;
704 }
705
706 if (!isLegal) {
707 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000708 bool Extend = false;
709
710 // If a FP immediate is precise when represented as a float, we put it
711 // into the constant pool as a float, even if it's is statically typed
712 // as a double.
713 MVT::ValueType VT = CFP->getValueType(0);
714 bool isDouble = VT == MVT::f64;
715 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
716 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000717 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
718 // Only do this if the target has a native EXTLOAD instruction from
719 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000720 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000721 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
722 VT = MVT::f32;
723 Extend = true;
724 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000725
Nate Begeman28a6b022005-12-10 02:36:00 +0000726 SDOperand CPIdx =
727 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000728 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000729 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
730 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000731 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000732 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
733 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000734 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000735 }
736 break;
737 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000738 case ISD::ConstantVec: {
739 // We assume that vector constants are not legal, and will be immediately
740 // spilled to the constant pool.
741 //
742 // FIXME: revisit this when we have some kind of mechanism by which targets
743 // can decided legality of vector constants, of which there may be very
744 // many.
745 //
746 // Create a ConstantPacked, and put it in the constant pool.
747 std::vector<Constant*> CV;
748 MVT::ValueType VT = Node->getValueType(0);
749 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
750 SDOperand OpN = Node->getOperand(I);
751 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
752 if (MVT::isFloatingPoint(VT))
753 CV.push_back(ConstantFP::get(OpNTy,
754 cast<ConstantFPSDNode>(OpN)->getValue()));
755 else
756 CV.push_back(ConstantUInt::get(OpNTy,
757 cast<ConstantSDNode>(OpN)->getValue()));
758 }
759 Constant *CP = ConstantPacked::get(CV);
Nate Begemand7d746f2005-12-13 03:03:23 +0000760 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000761 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
762 break;
763 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000764 case ISD::TokenFactor:
765 if (Node->getNumOperands() == 2) {
766 bool Changed = false;
767 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
768 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
769 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
770 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
771 } else {
772 std::vector<SDOperand> Ops;
773 bool Changed = false;
774 // Legalize the operands.
775 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
776 SDOperand Op = Node->getOperand(i);
777 Ops.push_back(LegalizeOp(Op));
778 Changed |= Ops[i] != Op;
779 }
780 if (Changed)
781 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000782 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000783 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000784
Chris Lattner16cd04d2005-05-12 23:24:06 +0000785 case ISD::CALLSEQ_START:
786 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000787 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000788 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000789 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000790 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000791 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000792
Chris Lattner16cd04d2005-05-12 23:24:06 +0000793 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000794 // nodes are treated specially and are mutated in place. This makes the dag
795 // legalization process more efficient and also makes libcall insertion
796 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000797 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000798 case ISD::DYNAMIC_STACKALLOC:
799 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
800 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
801 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
802 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000803 Tmp3 != Node->getOperand(2)) {
804 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
805 std::vector<SDOperand> Ops;
806 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
807 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
808 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000809 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000810
811 // Since this op produces two values, make sure to remember that we
812 // legalized both of them.
813 AddLegalizedOperand(SDOperand(Node, 0), Result);
814 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
815 return Result.getValue(Op.ResNo);
816
Chris Lattnerd71c0412005-05-13 18:43:43 +0000817 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000818 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000819 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
820 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000821
822 bool Changed = false;
823 std::vector<SDOperand> Ops;
824 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
825 Ops.push_back(LegalizeOp(Node->getOperand(i)));
826 Changed |= Ops.back() != Node->getOperand(i);
827 }
828
829 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000830 std::vector<MVT::ValueType> RetTyVTs;
831 RetTyVTs.reserve(Node->getNumValues());
832 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000833 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000834 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
835 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000836 } else {
837 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000838 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000839 // Since calls produce multiple values, make sure to remember that we
840 // legalized all of them.
841 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
842 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
843 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000844 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000845 case ISD::BR:
846 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
847 if (Tmp1 != Node->getOperand(0))
848 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
849 break;
850
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000851 case ISD::BRCOND:
852 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000853
Chris Lattner47e92232005-01-18 19:27:06 +0000854 switch (getTypeAction(Node->getOperand(1).getValueType())) {
855 case Expand: assert(0 && "It's impossible to expand bools");
856 case Legal:
857 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
858 break;
859 case Promote:
860 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
861 break;
862 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000863
864 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
865 default: assert(0 && "This action is not supported yet!");
866 case TargetLowering::Expand:
867 // Expand brcond's setcc into its constituent parts and create a BR_CC
868 // Node.
869 if (Tmp2.getOpcode() == ISD::SETCC) {
870 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
871 Tmp2.getOperand(0), Tmp2.getOperand(1),
872 Node->getOperand(2));
873 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000874 // Make sure the condition is either zero or one. It may have been
875 // promoted from something else.
876 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
877
Nate Begeman7cbd5252005-08-16 19:49:35 +0000878 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
879 DAG.getCondCode(ISD::SETNE), Tmp2,
880 DAG.getConstant(0, Tmp2.getValueType()),
881 Node->getOperand(2));
882 }
Chris Lattnere7736732005-12-18 23:54:29 +0000883 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000884 break;
Evan Cheng898101c2005-12-19 23:12:38 +0000885 case TargetLowering::Custom: {
886 SDOperand Tmp =
887 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
888 Tmp1, Tmp2, Node->getOperand(2)), DAG);
889 if (Tmp.Val) {
890 Result = LegalizeOp(Tmp);
891 break;
892 }
893 // FALLTHROUGH if the target thinks it is legal.
894 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000895 case TargetLowering::Legal:
896 // Basic block destination (Op#2) is always legal.
897 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
898 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
899 Node->getOperand(2));
900 break;
901 }
902 break;
903 case ISD::BR_CC:
904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000905 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000906 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
907 Node->getOperand(2), // LHS
908 Node->getOperand(3), // RHS
909 Node->getOperand(1)));
910 // If we get a SETCC back from legalizing the SETCC node we just
911 // created, then use its LHS, RHS, and CC directly in creating a new
912 // node. Otherwise, select between the true and false value based on
913 // comparing the result of the legalized with zero.
914 if (Tmp2.getOpcode() == ISD::SETCC) {
915 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
916 Tmp2.getOperand(0), Tmp2.getOperand(1),
917 Node->getOperand(4));
918 } else {
919 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
920 DAG.getCondCode(ISD::SETNE),
921 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
922 Node->getOperand(4));
923 }
Chris Lattner181b7a32005-12-17 23:46:46 +0000924 break;
925 }
926
927 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
928 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
929
930 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
931 default: assert(0 && "Unexpected action for BR_CC!");
932 case TargetLowering::Custom: {
933 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
934 Tmp2, Tmp3, Node->getOperand(4));
935 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
936 if (Tmp4.Val) {
937 Result = LegalizeOp(Tmp4);
938 break;
939 }
940 } // FALLTHROUGH if the target doesn't want to lower this op after all.
941 case TargetLowering::Legal:
942 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
943 Tmp3 != Node->getOperand(3)) {
944 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
945 Tmp2, Tmp3, Node->getOperand(4));
946 }
947 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000948 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000949 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000950 case ISD::BRCONDTWOWAY:
951 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
952 switch (getTypeAction(Node->getOperand(1).getValueType())) {
953 case Expand: assert(0 && "It's impossible to expand bools");
954 case Legal:
955 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
956 break;
957 case Promote:
958 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
959 break;
960 }
961 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
962 // pair.
963 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
964 case TargetLowering::Promote:
965 default: assert(0 && "This action is not supported yet!");
966 case TargetLowering::Legal:
967 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
968 std::vector<SDOperand> Ops;
969 Ops.push_back(Tmp1);
970 Ops.push_back(Tmp2);
971 Ops.push_back(Node->getOperand(2));
972 Ops.push_back(Node->getOperand(3));
973 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
974 }
975 break;
976 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000977 // If BRTWOWAY_CC is legal for this target, then simply expand this node
978 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
979 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000980 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000981 if (Tmp2.getOpcode() == ISD::SETCC) {
982 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
983 Tmp2.getOperand(0), Tmp2.getOperand(1),
984 Node->getOperand(2), Node->getOperand(3));
985 } else {
986 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
987 DAG.getConstant(0, Tmp2.getValueType()),
988 Node->getOperand(2), Node->getOperand(3));
989 }
990 } else {
991 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000992 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000993 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
994 }
Chris Lattnere7736732005-12-18 23:54:29 +0000995 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner411e8882005-04-09 03:30:19 +0000996 break;
997 }
998 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000999 case ISD::BRTWOWAY_CC:
1000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001001 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001002 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1003 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1004 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1005 Tmp3 != Node->getOperand(3)) {
1006 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1007 Node->getOperand(4), Node->getOperand(5));
1008 }
1009 break;
1010 } else {
1011 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1012 Node->getOperand(2), // LHS
1013 Node->getOperand(3), // RHS
1014 Node->getOperand(1)));
1015 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1016 // pair.
1017 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1018 default: assert(0 && "This action is not supported yet!");
1019 case TargetLowering::Legal:
1020 // If we get a SETCC back from legalizing the SETCC node we just
1021 // created, then use its LHS, RHS, and CC directly in creating a new
1022 // node. Otherwise, select between the true and false value based on
1023 // comparing the result of the legalized with zero.
1024 if (Tmp2.getOpcode() == ISD::SETCC) {
1025 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1026 Tmp2.getOperand(0), Tmp2.getOperand(1),
1027 Node->getOperand(4), Node->getOperand(5));
1028 } else {
1029 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1030 DAG.getConstant(0, Tmp2.getValueType()),
1031 Node->getOperand(4), Node->getOperand(5));
1032 }
1033 break;
1034 case TargetLowering::Expand:
1035 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1036 Node->getOperand(4));
1037 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1038 break;
1039 }
Chris Lattnerf9dee6a2005-12-21 19:40:42 +00001040 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001041 }
1042 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001043 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001044 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001046
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001047 MVT::ValueType VT = Node->getValueType(0);
1048 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1049 default: assert(0 && "This action is not supported yet!");
1050 case TargetLowering::Custom: {
1051 SDOperand Op = DAG.getLoad(Node->getValueType(0),
1052 Tmp1, Tmp2, Node->getOperand(2));
1053 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1054 if (Tmp.Val) {
1055 Result = LegalizeOp(Tmp);
1056 // Since loads produce two values, make sure to remember that we legalized
1057 // both of them.
1058 AddLegalizedOperand(SDOperand(Node, 0), Result);
1059 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1060 return Result.getValue(Op.ResNo);
1061 }
1062 // FALLTHROUGH if the target thinks it is legal.
1063 }
1064 case TargetLowering::Legal:
1065 if (Tmp1 != Node->getOperand(0) ||
1066 Tmp2 != Node->getOperand(1))
1067 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1068 Node->getOperand(2));
1069 else
1070 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001071
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001072 // Since loads produce two values, make sure to remember that we legalized
1073 // both of them.
1074 AddLegalizedOperand(SDOperand(Node, 0), Result);
1075 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1076 return Result.getValue(Op.ResNo);
1077 }
1078 assert(0 && "Unreachable");
1079 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001080 case ISD::EXTLOAD:
1081 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001082 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001083 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1084 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001085
Chris Lattner5f056bf2005-07-10 01:55:33 +00001086 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001087 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001088 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001089 case TargetLowering::Promote:
1090 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001091 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1092 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001093 // Since loads produce two values, make sure to remember that we legalized
1094 // both of them.
1095 AddLegalizedOperand(SDOperand(Node, 0), Result);
1096 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1097 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001098
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001099 case TargetLowering::Custom: {
1100 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1101 Tmp1, Tmp2, Node->getOperand(2),
1102 SrcVT);
1103 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1104 if (Tmp.Val) {
1105 Result = LegalizeOp(Tmp);
1106 // Since loads produce two values, make sure to remember that we legalized
1107 // both of them.
1108 AddLegalizedOperand(SDOperand(Node, 0), Result);
1109 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1110 return Result.getValue(Op.ResNo);
1111 }
1112 // FALLTHROUGH if the target thinks it is legal.
1113 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001114 case TargetLowering::Legal:
1115 if (Tmp1 != Node->getOperand(0) ||
1116 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001117 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1118 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001119 else
1120 Result = SDOperand(Node, 0);
1121
1122 // Since loads produce two values, make sure to remember that we legalized
1123 // both of them.
1124 AddLegalizedOperand(SDOperand(Node, 0), Result);
1125 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1126 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +00001127 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001128 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001129 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1130 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001131 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001132 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001133 Load = LegalizeOp(Load);
1134 AddLegalizedOperand(SDOperand(Node, 0), Result);
1135 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001136 if (Op.ResNo)
1137 return Load.getValue(1);
1138 return Result;
1139 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001140 assert(Node->getOpcode() != ISD::EXTLOAD &&
1141 "EXTLOAD should always be supported!");
1142 // Turn the unsupported load into an EXTLOAD followed by an explicit
1143 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001144 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1145 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001146 SDOperand ValRes;
1147 if (Node->getOpcode() == ISD::SEXTLOAD)
1148 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001149 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001150 else
1151 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001152 Result = LegalizeOp(Result); // Relegalize new nodes.
1153 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001154 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1155 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner01ff7212005-04-10 22:54:25 +00001156 if (Op.ResNo)
1157 return Result.getValue(1);
1158 return ValRes;
1159 }
1160 assert(0 && "Unreachable");
1161 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001162 case ISD::EXTRACT_ELEMENT: {
1163 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1164 switch (getTypeAction(OpTy)) {
1165 default:
1166 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1167 break;
1168 case Legal:
1169 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1170 // 1 -> Hi
1171 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1172 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1173 TLI.getShiftAmountTy()));
1174 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1175 } else {
1176 // 0 -> Lo
1177 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1178 Node->getOperand(0));
1179 }
1180 Result = LegalizeOp(Result);
1181 break;
1182 case Expand:
1183 // Get both the low and high parts.
1184 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1185 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1186 Result = Tmp2; // 1 -> Hi
1187 else
1188 Result = Tmp1; // 0 -> Lo
1189 break;
1190 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001191 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001192 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001193
1194 case ISD::CopyToReg:
1195 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001196
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001197 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001198 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001199 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001200 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001201 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001202 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1203 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1204 Node->getOperand(1), Tmp2);
1205 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001206 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1207 if (Node->getNumOperands() == 4)
1208 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001209 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001210 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001211 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1212 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1213 }
1214
1215 // Since this produces two values, make sure to remember that we legalized
1216 // both of them.
1217 AddLegalizedOperand(SDOperand(Node, 0), Result);
1218 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1219 return Result.getValue(Op.ResNo);
1220 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001221 break;
1222
1223 case ISD::RET:
1224 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1225 switch (Node->getNumOperands()) {
1226 case 2: // ret val
1227 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1228 case Legal:
1229 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001230 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001231 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1232 break;
1233 case Expand: {
1234 SDOperand Lo, Hi;
1235 ExpandOp(Node->getOperand(1), Lo, Hi);
1236 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001237 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001238 }
1239 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001240 Tmp2 = PromoteOp(Node->getOperand(1));
1241 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1242 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001243 }
1244 break;
1245 case 1: // ret void
1246 if (Tmp1 != Node->getOperand(0))
1247 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1248 break;
1249 default: { // ret <values>
1250 std::vector<SDOperand> NewValues;
1251 NewValues.push_back(Tmp1);
1252 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1253 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1254 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001255 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001256 break;
1257 case Expand: {
1258 SDOperand Lo, Hi;
1259 ExpandOp(Node->getOperand(i), Lo, Hi);
1260 NewValues.push_back(Lo);
1261 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001262 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001263 }
1264 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001265 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001266 }
1267 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1268 break;
1269 }
1270 }
1271 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001272 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001273 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1274 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1275
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001276 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001277 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001278 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001279 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001280 DAG.getConstant(FloatToBits(CFP->getValue()),
1281 MVT::i32),
1282 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001283 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001284 } else {
1285 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001286 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001287 DAG.getConstant(DoubleToBits(CFP->getValue()),
1288 MVT::i64),
1289 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001290 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001291 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001292 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001293 }
1294
Chris Lattner3e928bb2005-01-07 07:47:09 +00001295 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1296 case Legal: {
1297 SDOperand Val = LegalizeOp(Node->getOperand(1));
1298 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1299 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001300 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1301 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001302
1303 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1304 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1305 default: assert(0 && "This action is not supported yet!");
1306 case TargetLowering::Custom: {
1307 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1308 if (Tmp.Val) {
1309 Result = LegalizeOp(Tmp);
1310 break;
1311 }
1312 // FALLTHROUGH if the target thinks it is legal.
1313 }
1314 case TargetLowering::Legal:
1315 // Nothing to do.
1316 break;
1317 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001318 break;
1319 }
1320 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001321 // Truncate the value and store the result.
1322 Tmp3 = PromoteOp(Node->getOperand(1));
1323 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001324 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001325 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001326 break;
1327
Chris Lattner3e928bb2005-01-07 07:47:09 +00001328 case Expand:
1329 SDOperand Lo, Hi;
Nate Begemanab48be32005-11-22 18:16:00 +00001330 unsigned IncrementSize;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001331 ExpandOp(Node->getOperand(1), Lo, Hi);
1332
1333 if (!TLI.isLittleEndian())
1334 std::swap(Lo, Hi);
1335
Chris Lattneredb1add2005-05-11 04:51:16 +00001336 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1337 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001338 // If this is a vector type, then we have to calculate the increment as
1339 // the product of the element size in bytes, and the number of elements
1340 // in the high half of the vector.
1341 if (MVT::Vector == Hi.getValueType()) {
1342 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1343 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1344 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1345 } else {
1346 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1347 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001348 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1349 getIntPtrConstant(IncrementSize));
1350 assert(isTypeLegal(Tmp2.getValueType()) &&
1351 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001352 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001353 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1354 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001355 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1356 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001357 }
1358 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001359 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001360 case ISD::PCMARKER:
1361 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001362 if (Tmp1 != Node->getOperand(0))
1363 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001364 break;
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001365 case ISD::READCYCLECOUNTER:
1366 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001367 if (Tmp1 != Node->getOperand(0)) {
1368 std::vector<MVT::ValueType> rtypes;
1369 std::vector<SDOperand> rvals;
1370 rtypes.push_back(MVT::i64);
1371 rtypes.push_back(MVT::Other);
1372 rvals.push_back(Tmp1);
1373 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1374 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001375
1376 // Since rdcc produce two values, make sure to remember that we legalized
1377 // both of them.
1378 AddLegalizedOperand(SDOperand(Node, 0), Result);
1379 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1380 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001381
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001382 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1384 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1385
1386 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1387 case Legal:
1388 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001389
1390 // The only promote case we handle is TRUNCSTORE:i1 X into
1391 // -> TRUNCSTORE:i8 (and X, 1)
1392 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1393 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1394 TargetLowering::Promote) {
1395 // Promote the bool to a mask then store.
1396 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1397 DAG.getConstant(1, Tmp2.getValueType()));
1398 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1399 Node->getOperand(3), DAG.getValueType(MVT::i8));
1400
1401 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1402 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001403 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001404 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001405 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001406
1407 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1408 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1409 default: assert(0 && "This action is not supported yet!");
1410 case TargetLowering::Custom: {
1411 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1412 if (Tmp.Val) {
1413 Result = LegalizeOp(Tmp);
1414 break;
1415 }
1416 // FALLTHROUGH if the target thinks it is legal.
1417 }
1418 case TargetLowering::Legal:
1419 // Nothing to do.
1420 break;
1421 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001422 break;
1423 case Promote:
1424 case Expand:
1425 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1426 }
1427 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001428 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001429 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001430 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1431 case Expand: assert(0 && "It's impossible to expand bools");
1432 case Legal:
1433 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1434 break;
1435 case Promote:
1436 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1437 break;
1438 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001439 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001440 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001441
Nate Begemanb942a3d2005-08-23 04:29:48 +00001442 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001443 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001444 case TargetLowering::Expand:
1445 if (Tmp1.getOpcode() == ISD::SETCC) {
1446 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1447 Tmp2, Tmp3,
1448 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1449 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001450 // Make sure the condition is either zero or one. It may have been
1451 // promoted from something else.
1452 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001453 Result = DAG.getSelectCC(Tmp1,
1454 DAG.getConstant(0, Tmp1.getValueType()),
1455 Tmp2, Tmp3, ISD::SETNE);
1456 }
Chris Lattnere7736732005-12-18 23:54:29 +00001457 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman9373a812005-08-10 20:51:12 +00001458 break;
Evan Cheng7df96d62005-12-17 01:21:05 +00001459 case TargetLowering::Custom: {
1460 SDOperand Tmp =
1461 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1462 Tmp1, Tmp2, Tmp3), DAG);
1463 if (Tmp.Val) {
1464 Result = LegalizeOp(Tmp);
1465 break;
1466 }
1467 // FALLTHROUGH if the target thinks it is legal.
1468 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001469 case TargetLowering::Legal:
1470 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1471 Tmp3 != Node->getOperand(2))
1472 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1473 Tmp1, Tmp2, Tmp3);
1474 break;
1475 case TargetLowering::Promote: {
1476 MVT::ValueType NVT =
1477 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1478 unsigned ExtOp, TruncOp;
1479 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001480 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001481 TruncOp = ISD::TRUNCATE;
1482 } else {
1483 ExtOp = ISD::FP_EXTEND;
1484 TruncOp = ISD::FP_ROUND;
1485 }
1486 // Promote each of the values to the new type.
1487 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1488 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1489 // Perform the larger operation, then round down.
1490 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1491 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1492 break;
1493 }
1494 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001495 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001496 case ISD::SELECT_CC:
1497 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1498 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1499
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001500 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001501 // Everything is legal, see if we should expand this op or something.
1502 switch (TLI.getOperationAction(ISD::SELECT_CC,
1503 Node->getOperand(0).getValueType())) {
1504 default: assert(0 && "This action is not supported yet!");
1505 case TargetLowering::Custom: {
1506 SDOperand Tmp =
1507 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1508 Node->getOperand(0),
1509 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001510 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001511 if (Tmp.Val) {
1512 Result = LegalizeOp(Tmp);
1513 break;
1514 }
1515 } // FALLTHROUGH if the target can't lower this operation after all.
1516 case TargetLowering::Legal:
1517 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1518 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1519 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1520 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001521 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner23004e52005-08-26 00:23:59 +00001522 Tmp3, Tmp4, Node->getOperand(4));
1523 }
1524 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001525 }
1526 break;
1527 } else {
1528 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1529 Node->getOperand(0), // LHS
1530 Node->getOperand(1), // RHS
1531 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001532 // If we get a SETCC back from legalizing the SETCC node we just
1533 // created, then use its LHS, RHS, and CC directly in creating a new
1534 // node. Otherwise, select between the true and false value based on
1535 // comparing the result of the legalized with zero.
1536 if (Tmp1.getOpcode() == ISD::SETCC) {
1537 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1538 Tmp1.getOperand(0), Tmp1.getOperand(1),
1539 Tmp3, Tmp4, Tmp1.getOperand(2));
1540 } else {
1541 Result = DAG.getSelectCC(Tmp1,
1542 DAG.getConstant(0, Tmp1.getValueType()),
1543 Tmp3, Tmp4, ISD::SETNE);
1544 }
Nate Begeman9373a812005-08-10 20:51:12 +00001545 }
1546 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001547 case ISD::SETCC:
1548 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1549 case Legal:
1550 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1551 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001552 break;
1553 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001554 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1555 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1556
1557 // If this is an FP compare, the operands have already been extended.
1558 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1559 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001560 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001561
1562 // Otherwise, we have to insert explicit sign or zero extends. Note
1563 // that we could insert sign extends for ALL conditions, but zero extend
1564 // is cheaper on many machines (an AND instead of two shifts), so prefer
1565 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001566 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001567 default: assert(0 && "Unknown integer comparison!");
1568 case ISD::SETEQ:
1569 case ISD::SETNE:
1570 case ISD::SETUGE:
1571 case ISD::SETUGT:
1572 case ISD::SETULE:
1573 case ISD::SETULT:
1574 // ALL of these operations will work if we either sign or zero extend
1575 // the operands (including the unsigned comparisons!). Zero extend is
1576 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001577 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1578 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001579 break;
1580 case ISD::SETGE:
1581 case ISD::SETGT:
1582 case ISD::SETLT:
1583 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001584 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1585 DAG.getValueType(VT));
1586 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1587 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001588 break;
1589 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001590 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001591 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001592 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001593 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1594 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1595 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001596 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001597 case ISD::SETEQ:
1598 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001599 if (RHSLo == RHSHi)
1600 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1601 if (RHSCST->isAllOnesValue()) {
1602 // Comparison to -1.
1603 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001604 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001605 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001606 }
1607
Chris Lattner3e928bb2005-01-07 07:47:09 +00001608 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1609 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1610 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001611 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001612 break;
1613 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001614 // If this is a comparison of the sign bit, just look at the top part.
1615 // X > -1, x < 0
1616 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001617 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001618 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001619 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001620 (CST->isAllOnesValue()))) { // X > -1
1621 Tmp1 = LHSHi;
1622 Tmp2 = RHSHi;
1623 break;
1624 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001625
Chris Lattner3e928bb2005-01-07 07:47:09 +00001626 // FIXME: This generated code sucks.
1627 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001628 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001629 default: assert(0 && "Unknown integer setcc!");
1630 case ISD::SETLT:
1631 case ISD::SETULT: LowCC = ISD::SETULT; break;
1632 case ISD::SETGT:
1633 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1634 case ISD::SETLE:
1635 case ISD::SETULE: LowCC = ISD::SETULE; break;
1636 case ISD::SETGE:
1637 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1638 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001639
Chris Lattner3e928bb2005-01-07 07:47:09 +00001640 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1641 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1642 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1643
1644 // NOTE: on targets without efficient SELECT of bools, we can always use
1645 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001646 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1647 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1648 Node->getOperand(2));
1649 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001650 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1651 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001652 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001653 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001654 }
1655 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001656
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001657 switch(TLI.getOperationAction(ISD::SETCC,
1658 Node->getOperand(0).getValueType())) {
Nate Begemanb942a3d2005-08-23 04:29:48 +00001659 default:
1660 assert(0 && "Cannot handle this action for SETCC yet!");
1661 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001662 case TargetLowering::Promote: {
1663 // First step, figure out the appropriate operation to use.
1664 // Allow SETCC to not be supported for all legal data types
1665 // Mostly this targets FP
1666 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1667 MVT::ValueType OldVT = NewInTy;
1668
1669 // Scan for the appropriate larger type to use.
1670 while (1) {
1671 NewInTy = (MVT::ValueType)(NewInTy+1);
1672
1673 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1674 "Fell off of the edge of the integer world");
1675 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1676 "Fell off of the edge of the floating point world");
1677
1678 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001679 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001680 break;
1681 }
1682 if (MVT::isInteger(NewInTy))
1683 assert(0 && "Cannot promote Legal Integer SETCC yet");
1684 else {
1685 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1686 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1687 }
1688
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001689 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1690 Node->getOperand(2));
1691 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001692 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001693 case TargetLowering::Custom: {
1694 SDOperand Tmp =
1695 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1696 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1697 if (Tmp.Val) {
1698 Result = LegalizeOp(Tmp);
1699 break;
1700 }
1701 // FALLTHROUGH if the target thinks it is legal.
1702 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001703 case TargetLowering::Legal:
1704 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1705 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1706 Node->getOperand(2));
1707 break;
1708 case TargetLowering::Expand:
1709 // Expand a setcc node into a select_cc of the same condition, lhs, and
1710 // rhs that selects between const 1 (true) and const 0 (false).
1711 MVT::ValueType VT = Node->getValueType(0);
1712 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1713 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1714 Node->getOperand(2));
1715 Result = LegalizeOp(Result);
1716 break;
1717 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001718 break;
1719
Chris Lattnere1bd8222005-01-11 05:57:22 +00001720 case ISD::MEMSET:
1721 case ISD::MEMCPY:
1722 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001723 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001724 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1725
1726 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1727 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1728 case Expand: assert(0 && "Cannot expand a byte!");
1729 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001730 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001731 break;
1732 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001733 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001734 break;
1735 }
1736 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001737 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001738 }
Chris Lattner272455b2005-02-02 03:44:41 +00001739
1740 SDOperand Tmp4;
1741 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001742 case Expand: {
1743 // Length is too big, just take the lo-part of the length.
1744 SDOperand HiPart;
1745 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1746 break;
1747 }
Chris Lattnere5605212005-01-28 22:29:18 +00001748 case Legal:
1749 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001750 break;
1751 case Promote:
1752 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001753 break;
1754 }
1755
1756 SDOperand Tmp5;
1757 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1758 case Expand: assert(0 && "Cannot expand this yet!");
1759 case Legal:
1760 Tmp5 = LegalizeOp(Node->getOperand(4));
1761 break;
1762 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001763 Tmp5 = PromoteOp(Node->getOperand(4));
1764 break;
1765 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001766
1767 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1768 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001769 case TargetLowering::Custom: {
1770 SDOperand Tmp =
1771 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1772 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1773 if (Tmp.Val) {
1774 Result = LegalizeOp(Tmp);
1775 break;
1776 }
1777 // FALLTHROUGH if the target thinks it is legal.
1778 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001779 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001780 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1781 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1782 Tmp5 != Node->getOperand(4)) {
1783 std::vector<SDOperand> Ops;
1784 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1785 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1786 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1787 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001788 break;
1789 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001790 // Otherwise, the target does not support this operation. Lower the
1791 // operation to an explicit libcall as appropriate.
1792 MVT::ValueType IntPtr = TLI.getPointerTy();
1793 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1794 std::vector<std::pair<SDOperand, const Type*> > Args;
1795
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001796 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001797 if (Node->getOpcode() == ISD::MEMSET) {
1798 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1799 // Extend the ubyte argument to be an int value for the call.
1800 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1801 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1802 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1803
1804 FnName = "memset";
1805 } else if (Node->getOpcode() == ISD::MEMCPY ||
1806 Node->getOpcode() == ISD::MEMMOVE) {
1807 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1808 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1809 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1810 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1811 } else {
1812 assert(0 && "Unknown op!");
1813 }
Chris Lattner45982da2005-05-12 16:53:42 +00001814
Chris Lattnere1bd8222005-01-11 05:57:22 +00001815 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001816 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001817 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner69a889e2005-12-20 00:53:54 +00001818 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001819 break;
1820 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001821 }
1822 break;
1823 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001824
1825 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001826 Tmp1 = LegalizeOp(Node->getOperand(0));
1827 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001828
Chris Lattner3e011362005-05-14 07:45:46 +00001829 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1830 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1831 std::vector<SDOperand> Ops;
1832 Ops.push_back(Tmp1);
1833 Ops.push_back(Tmp2);
1834 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1835 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001836 Result = SDOperand(Node, 0);
1837 // Since these produce two values, make sure to remember that we legalized
1838 // both of them.
1839 AddLegalizedOperand(SDOperand(Node, 0), Result);
1840 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1841 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001842 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001843 Tmp1 = LegalizeOp(Node->getOperand(0));
1844 Tmp2 = LegalizeOp(Node->getOperand(1));
1845 Tmp3 = LegalizeOp(Node->getOperand(2));
1846 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1847 Tmp3 != Node->getOperand(2))
1848 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1849 break;
1850
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001851 case ISD::READIO:
1852 Tmp1 = LegalizeOp(Node->getOperand(0));
1853 Tmp2 = LegalizeOp(Node->getOperand(1));
1854
1855 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1856 case TargetLowering::Custom:
1857 default: assert(0 && "This action not implemented for this operation!");
1858 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001859 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1860 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1861 std::vector<SDOperand> Ops;
1862 Ops.push_back(Tmp1);
1863 Ops.push_back(Tmp2);
1864 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1865 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001866 Result = SDOperand(Node, 0);
1867 break;
1868 case TargetLowering::Expand:
1869 // Replace this with a load from memory.
1870 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1871 Node->getOperand(1), DAG.getSrcValue(NULL));
1872 Result = LegalizeOp(Result);
1873 break;
1874 }
1875
1876 // Since these produce two values, make sure to remember that we legalized
1877 // both of them.
1878 AddLegalizedOperand(SDOperand(Node, 0), Result);
1879 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1880 return Result.getValue(Op.ResNo);
1881
1882 case ISD::WRITEIO:
1883 Tmp1 = LegalizeOp(Node->getOperand(0));
1884 Tmp2 = LegalizeOp(Node->getOperand(1));
1885 Tmp3 = LegalizeOp(Node->getOperand(2));
1886
1887 switch (TLI.getOperationAction(Node->getOpcode(),
1888 Node->getOperand(1).getValueType())) {
1889 case TargetLowering::Custom:
1890 default: assert(0 && "This action not implemented for this operation!");
1891 case TargetLowering::Legal:
1892 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1893 Tmp3 != Node->getOperand(2))
1894 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1895 break;
1896 case TargetLowering::Expand:
1897 // Replace this with a store to memory.
1898 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1899 Node->getOperand(1), Node->getOperand(2),
1900 DAG.getSrcValue(NULL));
1901 Result = LegalizeOp(Result);
1902 break;
1903 }
1904 break;
1905
Chris Lattner84f67882005-01-20 18:52:28 +00001906 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001907 case ISD::SUB_PARTS:
1908 case ISD::SHL_PARTS:
1909 case ISD::SRA_PARTS:
1910 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001911 std::vector<SDOperand> Ops;
1912 bool Changed = false;
1913 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1914 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1915 Changed |= Ops.back() != Node->getOperand(i);
1916 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001917 if (Changed) {
1918 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1919 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1920 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001921
1922 // Since these produce multiple values, make sure to remember that we
1923 // legalized all of them.
1924 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1925 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1926 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001927 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001928
1929 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 case ISD::ADD:
1931 case ISD::SUB:
1932 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001933 case ISD::MULHS:
1934 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001935 case ISD::UDIV:
1936 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001937 case ISD::AND:
1938 case ISD::OR:
1939 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001940 case ISD::SHL:
1941 case ISD::SRL:
1942 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001943 case ISD::FADD:
1944 case ISD::FSUB:
1945 case ISD::FMUL:
1946 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001947 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001948 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1949 case Expand: assert(0 && "Not possible");
1950 case Legal:
1951 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1952 break;
1953 case Promote:
1954 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1955 break;
1956 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001957 if (Tmp1 != Node->getOperand(0) ||
1958 Tmp2 != Node->getOperand(1))
1959 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1960 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001961
Nate Begeman419f8b62005-10-18 00:27:41 +00001962 case ISD::BUILD_PAIR: {
1963 MVT::ValueType PairTy = Node->getValueType(0);
1964 // TODO: handle the case where the Lo and Hi operands are not of legal type
1965 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1966 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1967 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1968 case TargetLowering::Legal:
1969 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1970 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1971 break;
1972 case TargetLowering::Promote:
1973 case TargetLowering::Custom:
1974 assert(0 && "Cannot promote/custom this yet!");
1975 case TargetLowering::Expand:
1976 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1977 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1978 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1979 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1980 TLI.getShiftAmountTy()));
1981 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1982 break;
1983 }
1984 break;
1985 }
1986
Nate Begemanc105e192005-04-06 00:23:54 +00001987 case ISD::UREM:
1988 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001989 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001990 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1991 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1992 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1993 case TargetLowering::Legal:
1994 if (Tmp1 != Node->getOperand(0) ||
1995 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001996 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001997 Tmp2);
1998 break;
1999 case TargetLowering::Promote:
2000 case TargetLowering::Custom:
2001 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00002002 case TargetLowering::Expand:
2003 if (MVT::isInteger(Node->getValueType(0))) {
2004 MVT::ValueType VT = Node->getValueType(0);
2005 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2006 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2007 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2008 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2009 } else {
2010 // Floating point mod -> fmod libcall.
2011 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2012 SDOperand Dummy;
2013 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002014 }
2015 break;
2016 }
2017 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002018
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002019 case ISD::CTPOP:
2020 case ISD::CTTZ:
2021 case ISD::CTLZ:
2022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2023 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2024 case TargetLowering::Legal:
2025 if (Tmp1 != Node->getOperand(0))
2026 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2027 break;
2028 case TargetLowering::Promote: {
2029 MVT::ValueType OVT = Tmp1.getValueType();
2030 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002031
2032 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002033 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2034 // Perform the larger operation, then subtract if needed.
2035 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2036 switch(Node->getOpcode())
2037 {
2038 case ISD::CTPOP:
2039 Result = Tmp1;
2040 break;
2041 case ISD::CTTZ:
2042 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002043 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2044 DAG.getConstant(getSizeInBits(NVT), NVT),
2045 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002046 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002047 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2048 break;
2049 case ISD::CTLZ:
2050 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002051 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2052 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002053 getSizeInBits(OVT), NVT));
2054 break;
2055 }
2056 break;
2057 }
2058 case TargetLowering::Custom:
2059 assert(0 && "Cannot custom handle this yet!");
2060 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002061 switch(Node->getOpcode())
2062 {
2063 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002064 static const uint64_t mask[6] = {
2065 0x5555555555555555ULL, 0x3333333333333333ULL,
2066 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2067 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2068 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002069 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002070 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2071 unsigned len = getSizeInBits(VT);
2072 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002073 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002074 Tmp2 = DAG.getConstant(mask[i], VT);
2075 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002076 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002077 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2078 DAG.getNode(ISD::AND, VT,
2079 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2080 Tmp2));
2081 }
2082 Result = Tmp1;
2083 break;
2084 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00002085 case ISD::CTLZ: {
2086 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002087 x = x | (x >> 1);
2088 x = x | (x >> 2);
2089 ...
2090 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00002091 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002092 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00002093
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002094 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2095 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00002096 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2097 unsigned len = getSizeInBits(VT);
2098 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2099 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002100 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00002101 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2102 }
2103 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002104 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00002105 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002106 }
2107 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00002108 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002109 // unless the target has ctlz but not ctpop, in which case we use:
2110 // { return 32 - nlz(~x & (x-1)); }
2111 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002112 MVT::ValueType VT = Tmp1.getValueType();
2113 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002114 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002115 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2116 DAG.getNode(ISD::SUB, VT, Tmp1,
2117 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002118 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002119 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2120 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00002121 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002122 DAG.getConstant(getSizeInBits(VT), VT),
2123 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2124 } else {
2125 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2126 }
Chris Lattner18aa6802005-05-11 05:27:09 +00002127 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002128 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002129 default:
2130 assert(0 && "Cannot expand this yet!");
2131 break;
2132 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002133 break;
2134 }
2135 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002136
Chris Lattner2c8086f2005-04-02 05:00:07 +00002137 // Unary operators
2138 case ISD::FABS:
2139 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002140 case ISD::FSQRT:
2141 case ISD::FSIN:
2142 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002143 Tmp1 = LegalizeOp(Node->getOperand(0));
2144 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2145 case TargetLowering::Legal:
2146 if (Tmp1 != Node->getOperand(0))
2147 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2148 break;
2149 case TargetLowering::Promote:
2150 case TargetLowering::Custom:
2151 assert(0 && "Cannot promote/custom handle this yet!");
2152 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002153 switch(Node->getOpcode()) {
2154 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00002155 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2156 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002157 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00002158 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002159 break;
2160 }
2161 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002162 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2163 MVT::ValueType VT = Node->getValueType(0);
2164 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002165 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002166 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2167 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2168 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002169 break;
2170 }
2171 case ISD::FSQRT:
2172 case ISD::FSIN:
2173 case ISD::FCOS: {
2174 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002175 const char *FnName = 0;
2176 switch(Node->getOpcode()) {
2177 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2178 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2179 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2180 default: assert(0 && "Unreachable!");
2181 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002182 SDOperand Dummy;
2183 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002184 break;
2185 }
2186 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002187 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002188 }
2189 break;
2190 }
2191 break;
Chris Lattner35481892005-12-23 00:16:34 +00002192
2193 case ISD::BIT_CONVERT:
2194 if (!isTypeLegal(Node->getOperand(0).getValueType()))
2195 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2196 else {
2197 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2198 Node->getOperand(0).getValueType())) {
2199 default: assert(0 && "Unknown operation action!");
2200 case TargetLowering::Expand:
2201 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2202 break;
2203 case TargetLowering::Legal:
2204 Tmp1 = LegalizeOp(Node->getOperand(0));
2205 if (Tmp1 != Node->getOperand(0))
2206 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2207 break;
2208 }
2209 }
2210 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002211 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002212 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002213 case ISD::UINT_TO_FP: {
2214 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002215 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2216 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002217 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002218 Node->getOperand(0).getValueType())) {
2219 default: assert(0 && "Unknown operation action!");
2220 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002221 Result = ExpandLegalINT_TO_FP(isSigned,
2222 LegalizeOp(Node->getOperand(0)),
2223 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002224 AddLegalizedOperand(Op, Result);
2225 return Result;
2226 case TargetLowering::Promote:
2227 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2228 Node->getValueType(0),
2229 isSigned);
2230 AddLegalizedOperand(Op, Result);
2231 return Result;
2232 case TargetLowering::Legal:
2233 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002234 case TargetLowering::Custom: {
2235 Tmp1 = LegalizeOp(Node->getOperand(0));
2236 SDOperand Tmp =
2237 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2238 Tmp = TLI.LowerOperation(Tmp, DAG);
2239 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002240 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002241 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002242 return Tmp;
2243 } else {
2244 assert(0 && "Target Must Lower this");
2245 }
2246 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00002247 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002248
Chris Lattner3e928bb2005-01-07 07:47:09 +00002249 Tmp1 = LegalizeOp(Node->getOperand(0));
2250 if (Tmp1 != Node->getOperand(0))
2251 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2252 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002253 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002254 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2255 Node->getValueType(0), Node->getOperand(0));
2256 break;
2257 case Promote:
2258 if (isSigned) {
2259 Result = PromoteOp(Node->getOperand(0));
2260 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2261 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2262 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2263 } else {
2264 Result = PromoteOp(Node->getOperand(0));
2265 Result = DAG.getZeroExtendInReg(Result,
2266 Node->getOperand(0).getValueType());
2267 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002268 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002269 break;
2270 }
2271 break;
2272 }
2273 case ISD::TRUNCATE:
2274 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2275 case Legal:
2276 Tmp1 = LegalizeOp(Node->getOperand(0));
2277 if (Tmp1 != Node->getOperand(0))
2278 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2279 break;
2280 case Expand:
2281 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2282
2283 // Since the result is legal, we should just be able to truncate the low
2284 // part of the source.
2285 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2286 break;
2287 case Promote:
2288 Result = PromoteOp(Node->getOperand(0));
2289 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2290 break;
2291 }
2292 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002293
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002294 case ISD::FP_TO_SINT:
2295 case ISD::FP_TO_UINT:
2296 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2297 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002298 Tmp1 = LegalizeOp(Node->getOperand(0));
2299
Chris Lattner1618beb2005-07-29 00:11:56 +00002300 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2301 default: assert(0 && "Unknown operation action!");
2302 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002303 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2304 SDOperand True, False;
2305 MVT::ValueType VT = Node->getOperand(0).getValueType();
2306 MVT::ValueType NVT = Node->getValueType(0);
2307 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2308 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2309 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2310 Node->getOperand(0), Tmp2, ISD::SETLT);
2311 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2312 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002313 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002314 Tmp2));
2315 False = DAG.getNode(ISD::XOR, NVT, False,
2316 DAG.getConstant(1ULL << ShiftAmt, NVT));
2317 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner69a889e2005-12-20 00:53:54 +00002318 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman2d56e722005-08-14 18:38:32 +00002319 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002320 } else {
2321 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2322 }
2323 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002324 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002325 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002326 Node->getOpcode() == ISD::FP_TO_SINT);
2327 AddLegalizedOperand(Op, Result);
2328 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002329 case TargetLowering::Custom: {
2330 SDOperand Tmp =
2331 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2332 Tmp = TLI.LowerOperation(Tmp, DAG);
2333 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002334 Tmp = LegalizeOp(Tmp);
Chris Lattner07dffd62005-08-26 00:14:16 +00002335 AddLegalizedOperand(Op, Tmp);
Chris Lattner507f7522005-08-29 17:30:00 +00002336 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002337 } else {
2338 // The target thinks this is legal afterall.
2339 break;
2340 }
2341 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002342 case TargetLowering::Legal:
2343 break;
2344 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002345
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002346 if (Tmp1 != Node->getOperand(0))
2347 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2348 break;
2349 case Expand:
2350 assert(0 && "Shouldn't need to expand other operators here!");
2351 case Promote:
2352 Result = PromoteOp(Node->getOperand(0));
2353 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2354 break;
2355 }
2356 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002357
Chris Lattner13c78e22005-09-02 00:18:10 +00002358 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002359 case ISD::ZERO_EXTEND:
2360 case ISD::SIGN_EXTEND:
2361 case ISD::FP_EXTEND:
2362 case ISD::FP_ROUND:
2363 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2364 case Legal:
2365 Tmp1 = LegalizeOp(Node->getOperand(0));
2366 if (Tmp1 != Node->getOperand(0))
2367 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2368 break;
2369 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002370 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002371
Chris Lattner03c85462005-01-15 05:21:40 +00002372 case Promote:
2373 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002374 case ISD::ANY_EXTEND:
2375 Result = PromoteOp(Node->getOperand(0));
2376 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2377 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002378 case ISD::ZERO_EXTEND:
2379 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002380 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002381 Result = DAG.getZeroExtendInReg(Result,
2382 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002383 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002384 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002385 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002386 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002387 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002388 Result,
2389 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002390 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002391 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002392 Result = PromoteOp(Node->getOperand(0));
2393 if (Result.getValueType() != Op.getValueType())
2394 // Dynamically dead while we have only 2 FP types.
2395 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2396 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002397 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002398 Result = PromoteOp(Node->getOperand(0));
2399 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2400 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002401 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002402 }
2403 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002404 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002405 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002406 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002407 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002408
2409 // If this operation is not supported, convert it to a shl/shr or load/store
2410 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002411 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2412 default: assert(0 && "This action not supported for this op yet!");
2413 case TargetLowering::Legal:
2414 if (Tmp1 != Node->getOperand(0))
2415 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002416 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002417 break;
2418 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002419 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002420 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002421 // NOTE: we could fall back on load/store here too for targets without
2422 // SAR. However, it is doubtful that any exist.
2423 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2424 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002425 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002426 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2427 Node->getOperand(0), ShiftCst);
2428 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2429 Result, ShiftCst);
2430 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2431 // The only way we can lower this is to turn it into a STORETRUNC,
2432 // EXTLOAD pair, targetting a temporary location (a stack slot).
2433
2434 // NOTE: there is a choice here between constantly creating new stack
2435 // slots and always reusing the same one. We currently always create
2436 // new ones, as reuse may inhibit scheduling.
2437 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2438 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2439 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2440 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002441 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002442 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2443 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2444 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002445 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002446 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002447 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2448 Result, StackSlot, DAG.getSrcValue(NULL),
2449 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002450 } else {
2451 assert(0 && "Unknown op");
2452 }
2453 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002454 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002455 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002456 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002457 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002458 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002459
Chris Lattner45982da2005-05-12 16:53:42 +00002460 // Note that LegalizeOp may be reentered even from single-use nodes, which
2461 // means that we always must cache transformed nodes.
2462 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002463 return Result;
2464}
2465
Chris Lattner8b6fa222005-01-15 22:16:26 +00002466/// PromoteOp - Given an operation that produces a value in an invalid type,
2467/// promote it to compute the value into a larger type. The produced value will
2468/// have the correct bits for the low portion of the register, but no guarantee
2469/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002470SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2471 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002472 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002473 assert(getTypeAction(VT) == Promote &&
2474 "Caller should expand or legalize operands that are not promotable!");
2475 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2476 "Cannot promote to smaller type!");
2477
Chris Lattner03c85462005-01-15 05:21:40 +00002478 SDOperand Tmp1, Tmp2, Tmp3;
2479
2480 SDOperand Result;
2481 SDNode *Node = Op.Val;
2482
Chris Lattner6fdcb252005-09-02 20:32:45 +00002483 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2484 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002485
Chris Lattner0f69b292005-01-15 06:18:18 +00002486 // Promotion needs an optimization step to clean up after it, and is not
2487 // careful to avoid operations the target does not support. Make sure that
2488 // all generated operations are legalized in the next iteration.
2489 NeedsAnotherIteration = true;
2490
Chris Lattner03c85462005-01-15 05:21:40 +00002491 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002492 case ISD::CopyFromReg:
2493 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002494 default:
2495 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2496 assert(0 && "Do not know how to promote this operator!");
2497 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002498 case ISD::UNDEF:
2499 Result = DAG.getNode(ISD::UNDEF, NVT);
2500 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002501 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002502 if (VT != MVT::i1)
2503 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2504 else
2505 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002506 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2507 break;
2508 case ISD::ConstantFP:
2509 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2510 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2511 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002512
Chris Lattner82fbfb62005-01-18 02:59:52 +00002513 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002514 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002515 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2516 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002517 Result = LegalizeOp(Result);
2518 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002519
2520 case ISD::TRUNCATE:
2521 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2522 case Legal:
2523 Result = LegalizeOp(Node->getOperand(0));
2524 assert(Result.getValueType() >= NVT &&
2525 "This truncation doesn't make sense!");
2526 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2527 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2528 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002529 case Promote:
2530 // The truncation is not required, because we don't guarantee anything
2531 // about high bits anyway.
2532 Result = PromoteOp(Node->getOperand(0));
2533 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002534 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002535 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2536 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002537 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002538 }
2539 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002540 case ISD::SIGN_EXTEND:
2541 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002542 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002543 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2544 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2545 case Legal:
2546 // Input is legal? Just do extend all the way to the larger type.
2547 Result = LegalizeOp(Node->getOperand(0));
2548 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2549 break;
2550 case Promote:
2551 // Promote the reg if it's smaller.
2552 Result = PromoteOp(Node->getOperand(0));
2553 // The high bits are not guaranteed to be anything. Insert an extend.
2554 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002555 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002556 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002557 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002558 Result = DAG.getZeroExtendInReg(Result,
2559 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002560 break;
2561 }
2562 break;
Chris Lattner35481892005-12-23 00:16:34 +00002563 case ISD::BIT_CONVERT:
2564 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2565 Result = PromoteOp(Result);
2566 break;
2567
Chris Lattner8b6fa222005-01-15 22:16:26 +00002568 case ISD::FP_EXTEND:
2569 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2570 case ISD::FP_ROUND:
2571 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2572 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2573 case Promote: assert(0 && "Unreachable with 2 FP types!");
2574 case Legal:
2575 // Input is legal? Do an FP_ROUND_INREG.
2576 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002577 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2578 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002579 break;
2580 }
2581 break;
2582
2583 case ISD::SINT_TO_FP:
2584 case ISD::UINT_TO_FP:
2585 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2586 case Legal:
2587 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002588 // No extra round required here.
2589 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002590 break;
2591
2592 case Promote:
2593 Result = PromoteOp(Node->getOperand(0));
2594 if (Node->getOpcode() == ISD::SINT_TO_FP)
2595 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002596 Result,
2597 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002598 else
Chris Lattner23993562005-04-13 02:38:47 +00002599 Result = DAG.getZeroExtendInReg(Result,
2600 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002601 // No extra round required here.
2602 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002603 break;
2604 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002605 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2606 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002607 // Round if we cannot tolerate excess precision.
2608 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002609 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2610 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002611 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002612 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002613 break;
2614
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002615 case ISD::SIGN_EXTEND_INREG:
2616 Result = PromoteOp(Node->getOperand(0));
2617 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2618 Node->getOperand(1));
2619 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002620 case ISD::FP_TO_SINT:
2621 case ISD::FP_TO_UINT:
2622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2623 case Legal:
2624 Tmp1 = LegalizeOp(Node->getOperand(0));
2625 break;
2626 case Promote:
2627 // The input result is prerounded, so we don't have to do anything
2628 // special.
2629 Tmp1 = PromoteOp(Node->getOperand(0));
2630 break;
2631 case Expand:
2632 assert(0 && "not implemented");
2633 }
Nate Begemand2558e32005-08-14 01:20:53 +00002634 // If we're promoting a UINT to a larger size, check to see if the new node
2635 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2636 // we can use that instead. This allows us to generate better code for
2637 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2638 // legal, such as PowerPC.
2639 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002640 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002641 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2642 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002643 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2644 } else {
2645 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2646 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002647 break;
2648
Chris Lattner2c8086f2005-04-02 05:00:07 +00002649 case ISD::FABS:
2650 case ISD::FNEG:
2651 Tmp1 = PromoteOp(Node->getOperand(0));
2652 assert(Tmp1.getValueType() == NVT);
2653 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2654 // NOTE: we do not have to do any extra rounding here for
2655 // NoExcessFPPrecision, because we know the input will have the appropriate
2656 // precision, and these operations don't modify precision at all.
2657 break;
2658
Chris Lattnerda6ba872005-04-28 21:44:33 +00002659 case ISD::FSQRT:
2660 case ISD::FSIN:
2661 case ISD::FCOS:
2662 Tmp1 = PromoteOp(Node->getOperand(0));
2663 assert(Tmp1.getValueType() == NVT);
2664 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2665 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002666 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2667 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002668 break;
2669
Chris Lattner03c85462005-01-15 05:21:40 +00002670 case ISD::AND:
2671 case ISD::OR:
2672 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002673 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002674 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002675 case ISD::MUL:
2676 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002677 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002678 // that too is okay if they are integer operations.
2679 Tmp1 = PromoteOp(Node->getOperand(0));
2680 Tmp2 = PromoteOp(Node->getOperand(1));
2681 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2682 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002683 break;
2684 case ISD::FADD:
2685 case ISD::FSUB:
2686 case ISD::FMUL:
2687 // The input may have strange things in the top bits of the registers, but
2688 // these operations don't care.
2689 Tmp1 = PromoteOp(Node->getOperand(0));
2690 Tmp2 = PromoteOp(Node->getOperand(1));
2691 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2692 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2693
2694 // Floating point operations will give excess precision that we may not be
2695 // able to tolerate. If we DO allow excess precision, just leave it,
2696 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002697 // FIXME: Why would we need to round FP ops more than integer ones?
2698 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002699 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002700 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2701 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002702 break;
2703
Chris Lattner8b6fa222005-01-15 22:16:26 +00002704 case ISD::SDIV:
2705 case ISD::SREM:
2706 // These operators require that their input be sign extended.
2707 Tmp1 = PromoteOp(Node->getOperand(0));
2708 Tmp2 = PromoteOp(Node->getOperand(1));
2709 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002710 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2711 DAG.getValueType(VT));
2712 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2713 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002714 }
2715 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2716
2717 // Perform FP_ROUND: this is probably overly pessimistic.
2718 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002719 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2720 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002721 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002722 case ISD::FDIV:
2723 case ISD::FREM:
2724 // These operators require that their input be fp extended.
2725 Tmp1 = PromoteOp(Node->getOperand(0));
2726 Tmp2 = PromoteOp(Node->getOperand(1));
2727 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2728
2729 // Perform FP_ROUND: this is probably overly pessimistic.
2730 if (NoExcessFPPrecision)
2731 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2732 DAG.getValueType(VT));
2733 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002734
2735 case ISD::UDIV:
2736 case ISD::UREM:
2737 // These operators require that their input be zero extended.
2738 Tmp1 = PromoteOp(Node->getOperand(0));
2739 Tmp2 = PromoteOp(Node->getOperand(1));
2740 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002741 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2742 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002743 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2744 break;
2745
2746 case ISD::SHL:
2747 Tmp1 = PromoteOp(Node->getOperand(0));
2748 Tmp2 = LegalizeOp(Node->getOperand(1));
2749 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2750 break;
2751 case ISD::SRA:
2752 // The input value must be properly sign extended.
2753 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002754 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2755 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002756 Tmp2 = LegalizeOp(Node->getOperand(1));
2757 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2758 break;
2759 case ISD::SRL:
2760 // The input value must be properly zero extended.
2761 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002762 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002763 Tmp2 = LegalizeOp(Node->getOperand(1));
2764 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2765 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002766 case ISD::LOAD:
2767 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2768 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00002769 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2770 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002771 // Remember that we legalized the chain.
2772 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2773 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002774 case ISD::SEXTLOAD:
2775 case ISD::ZEXTLOAD:
2776 case ISD::EXTLOAD:
2777 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2778 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00002779 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2780 Node->getOperand(2),
2781 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002782 // Remember that we legalized the chain.
2783 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2784 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002785 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002786 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2787 case Expand: assert(0 && "It's impossible to expand bools");
2788 case Legal:
2789 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2790 break;
2791 case Promote:
2792 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2793 break;
2794 }
Chris Lattner03c85462005-01-15 05:21:40 +00002795 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2796 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2797 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2798 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002799 case ISD::SELECT_CC:
2800 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2801 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2802 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2803 Node->getOperand(1), Tmp2, Tmp3,
2804 Node->getOperand(4));
2805 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002806 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002807 case ISD::CALL: {
2808 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2809 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2810
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002811 std::vector<SDOperand> Ops;
2812 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2813 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2814
Chris Lattner8ac532c2005-01-16 19:46:48 +00002815 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2816 "Can only promote single result calls");
2817 std::vector<MVT::ValueType> RetTyVTs;
2818 RetTyVTs.reserve(2);
2819 RetTyVTs.push_back(NVT);
2820 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002821 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2822 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002823 Result = SDOperand(NC, 0);
2824
2825 // Insert the new chain mapping.
2826 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2827 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002828 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002829 case ISD::CTPOP:
2830 case ISD::CTTZ:
2831 case ISD::CTLZ:
2832 Tmp1 = Node->getOperand(0);
2833 //Zero extend the argument
2834 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2835 // Perform the larger operation, then subtract if needed.
2836 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2837 switch(Node->getOpcode())
2838 {
2839 case ISD::CTPOP:
2840 Result = Tmp1;
2841 break;
2842 case ISD::CTTZ:
2843 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002844 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002845 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002846 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002847 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2848 break;
2849 case ISD::CTLZ:
2850 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002851 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2852 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002853 getSizeInBits(VT), NVT));
2854 break;
2855 }
2856 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002857 }
2858
2859 assert(Result.Val && "Didn't set a result!");
2860 AddPromotedOperand(Op, Result);
2861 return Result;
2862}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002863
Chris Lattner35481892005-12-23 00:16:34 +00002864/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002865/// The resultant code need not be legal. Note that SrcOp is the input operand
2866/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002867SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2868 SDOperand SrcOp) {
2869 // Create the stack frame object.
2870 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2871 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002872 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002873 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2874
2875 // Emit a store to the stack slot.
2876 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002877 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002878 // Result is a load from the stack slot.
2879 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2880}
2881
Chris Lattner84f67882005-01-20 18:52:28 +00002882/// ExpandAddSub - Find a clever way to expand this add operation into
2883/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002884void SelectionDAGLegalize::
2885ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2886 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002887 // Expand the subcomponents.
2888 SDOperand LHSL, LHSH, RHSL, RHSH;
2889 ExpandOp(LHS, LHSL, LHSH);
2890 ExpandOp(RHS, RHSL, RHSH);
2891
Chris Lattner84f67882005-01-20 18:52:28 +00002892 std::vector<SDOperand> Ops;
2893 Ops.push_back(LHSL);
2894 Ops.push_back(LHSH);
2895 Ops.push_back(RHSL);
2896 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002897 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2898 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002899 Hi = Lo.getValue(1);
2900}
2901
Chris Lattner5b359c62005-04-02 04:00:59 +00002902void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2903 SDOperand Op, SDOperand Amt,
2904 SDOperand &Lo, SDOperand &Hi) {
2905 // Expand the subcomponents.
2906 SDOperand LHSL, LHSH;
2907 ExpandOp(Op, LHSL, LHSH);
2908
2909 std::vector<SDOperand> Ops;
2910 Ops.push_back(LHSL);
2911 Ops.push_back(LHSH);
2912 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002913 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002914 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002915 Hi = Lo.getValue(1);
2916}
2917
2918
Chris Lattnere34b3962005-01-19 04:19:40 +00002919/// ExpandShift - Try to find a clever way to expand this shift operation out to
2920/// smaller elements. If we can't find a way that is more efficient than a
2921/// libcall on this target, return false. Otherwise, return true with the
2922/// low-parts expanded into Lo and Hi.
2923bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2924 SDOperand &Lo, SDOperand &Hi) {
2925 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2926 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002927
Chris Lattnere34b3962005-01-19 04:19:40 +00002928 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002929 SDOperand ShAmt = LegalizeOp(Amt);
2930 MVT::ValueType ShTy = ShAmt.getValueType();
2931 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2932 unsigned NVTBits = MVT::getSizeInBits(NVT);
2933
2934 // Handle the case when Amt is an immediate. Other cases are currently broken
2935 // and are disabled.
2936 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2937 unsigned Cst = CN->getValue();
2938 // Expand the incoming operand to be shifted, so that we have its parts
2939 SDOperand InL, InH;
2940 ExpandOp(Op, InL, InH);
2941 switch(Opc) {
2942 case ISD::SHL:
2943 if (Cst > VTBits) {
2944 Lo = DAG.getConstant(0, NVT);
2945 Hi = DAG.getConstant(0, NVT);
2946 } else if (Cst > NVTBits) {
2947 Lo = DAG.getConstant(0, NVT);
2948 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002949 } else if (Cst == NVTBits) {
2950 Lo = DAG.getConstant(0, NVT);
2951 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002952 } else {
2953 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2954 Hi = DAG.getNode(ISD::OR, NVT,
2955 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2956 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2957 }
2958 return true;
2959 case ISD::SRL:
2960 if (Cst > VTBits) {
2961 Lo = DAG.getConstant(0, NVT);
2962 Hi = DAG.getConstant(0, NVT);
2963 } else if (Cst > NVTBits) {
2964 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2965 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002966 } else if (Cst == NVTBits) {
2967 Lo = InH;
2968 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002969 } else {
2970 Lo = DAG.getNode(ISD::OR, NVT,
2971 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2972 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2973 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2974 }
2975 return true;
2976 case ISD::SRA:
2977 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002978 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002979 DAG.getConstant(NVTBits-1, ShTy));
2980 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002981 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002982 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002983 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002984 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002985 } else if (Cst == NVTBits) {
2986 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002987 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002988 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002989 } else {
2990 Lo = DAG.getNode(ISD::OR, NVT,
2991 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2992 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2993 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2994 }
2995 return true;
2996 }
2997 }
2998 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2999 // so disable it for now. Currently targets are handling this via SHL_PARTS
3000 // and friends.
3001 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003002
3003 // If we have an efficient select operation (or if the selects will all fold
3004 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003005 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00003006 return false;
3007
3008 SDOperand InL, InH;
3009 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00003010 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3011 DAG.getConstant(NVTBits, ShTy), ShAmt);
3012
Chris Lattnere5544f82005-01-20 20:29:23 +00003013 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003014 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3015 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00003016
Chris Lattnere34b3962005-01-19 04:19:40 +00003017 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3018 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3019 DAG.getConstant(NVTBits-1, ShTy));
3020 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3021 DAG.getConstant(NVTBits-1, ShTy));
3022 }
3023
3024 if (Opc == ISD::SHL) {
3025 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3026 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3027 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003028 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00003029
Chris Lattnere34b3962005-01-19 04:19:40 +00003030 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3031 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3032 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00003033 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003034 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3035 DAG.getConstant(32, ShTy),
3036 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00003037 DAG.getConstant(0, NVT),
3038 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00003039 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00003040 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00003041 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003042 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00003043
3044 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00003045 if (Opc == ISD::SRA)
3046 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3047 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00003048 else
3049 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00003050 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00003051 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00003052 }
3053 return true;
3054}
Chris Lattner77e77a62005-01-21 06:05:23 +00003055
Chris Lattner9530ddc2005-05-13 05:09:11 +00003056/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3057/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003058/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003059static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003060 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00003061
Chris Lattner16cd04d2005-05-12 23:24:06 +00003062 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003063 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003064 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003065 Found = Node;
3066 return;
3067 }
3068
3069 // Otherwise, scan the operands of Node to see if any of them is a call.
3070 assert(Node->getNumOperands() != 0 &&
3071 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00003072 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003073 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003074
3075 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003076 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003077 Found);
3078}
3079
3080
Chris Lattner9530ddc2005-05-13 05:09:11 +00003081/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3082/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003083/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00003084static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3085 std::set<SDNode*> &Visited) {
3086 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
3087 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003088
Chris Lattner16cd04d2005-05-12 23:24:06 +00003089 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003090 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003091 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003092 Found = Node;
3093 return;
3094 }
3095
3096 // Otherwise, scan the operands of Node to see if any of them is a call.
3097 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3098 if (UI == E) return;
3099 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00003100 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003101
3102 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00003103 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003104}
3105
Chris Lattner9530ddc2005-05-13 05:09:11 +00003106/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003107/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003108static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00003109 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003110 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00003111 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00003112 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003113
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003114 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00003115 if (TheChain.getValueType() != MVT::Other)
3116 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00003117 if (TheChain.getValueType() != MVT::Other)
3118 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003119
3120 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003121 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003122
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003123 // Make sure to only follow users of our token chain.
3124 SDNode *User = *UI;
3125 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3126 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003127 if (SDNode *Result = FindCallSeqEnd(User))
3128 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003129 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003130 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003131}
3132
Chris Lattner9530ddc2005-05-13 05:09:11 +00003133/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003134/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003135static SDNode *FindCallSeqStart(SDNode *Node) {
3136 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003137 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003138
3139 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3140 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003141 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003142}
3143
3144
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003145/// FindInputOutputChains - If we are replacing an operation with a call we need
3146/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003147/// properly serialize the calls in the block. The returned operand is the
3148/// input chain value for the new call (e.g. the entry node or the previous
3149/// call), and OutChain is set to be the chain node to update to point to the
3150/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003151static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3152 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003153 SDNode *LatestCallSeqStart = Entry.Val;
3154 SDNode *LatestCallSeqEnd = 0;
3155 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
3156 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003157
Chris Lattner16cd04d2005-05-12 23:24:06 +00003158 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003159 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003160 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3161 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003162 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003163 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003164 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3165 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003166 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003167 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003168 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003169
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003170 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003171 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003172 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003173 std::set<SDNode*> Visited;
3174 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003175
Chris Lattner9530ddc2005-05-13 05:09:11 +00003176 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003177 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003178 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003179
Chris Lattner9530ddc2005-05-13 05:09:11 +00003180 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003181}
3182
Jeff Cohen00b168892005-07-27 06:12:32 +00003183/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003184void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3185 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003186 // Nothing to splice it into?
3187 if (OutChain == 0) return;
3188
3189 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3190 //OutChain->dump();
3191
3192 // Form a token factor node merging the old inval and the new inval.
3193 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3194 OutChain->getOperand(0));
3195 // Change the node to refer to the new token.
3196 OutChain->setAdjCallChain(InToken);
3197}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003198
3199
Chris Lattner77e77a62005-01-21 06:05:23 +00003200// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3201// does not fit into a register, return the lo part and set the hi part to the
3202// by-reg argument. If it does fit into a single register, return the result
3203// and leave the Hi part unset.
3204SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3205 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003206 SDNode *OutChain;
3207 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3208 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003209 if (InChain.Val == 0)
3210 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003211
Chris Lattner77e77a62005-01-21 06:05:23 +00003212 TargetLowering::ArgListTy Args;
3213 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3214 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3215 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3216 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3217 }
3218 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003219
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003220 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003221 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003222 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003223 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3224 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003225
Chris Lattner99c25b82005-09-02 20:26:58 +00003226 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003227 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003228 default: assert(0 && "Unknown thing");
3229 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00003230 Result = CallInfo.first;
3231 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003232 case Promote:
3233 assert(0 && "Cannot promote this yet!");
3234 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003235 ExpandOp(CallInfo.first, Result, Hi);
3236 CallInfo.second = LegalizeOp(CallInfo.second);
3237 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003238 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003239
3240 SpliceCallInto(CallInfo.second, OutChain);
3241 NeedsAnotherIteration = true;
3242 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003243}
3244
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003245
Chris Lattner77e77a62005-01-21 06:05:23 +00003246/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3247/// destination type is legal.
3248SDOperand SelectionDAGLegalize::
3249ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003250 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003251 assert(getTypeAction(Source.getValueType()) == Expand &&
3252 "This is not an expansion!");
3253 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3254
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003255 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003256 assert(Source.getValueType() == MVT::i64 &&
3257 "This only works for 64-bit -> FP");
3258 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3259 // incoming integer is set. To handle this, we dynamically test to see if
3260 // it is set, and, if so, add a fudge factor.
3261 SDOperand Lo, Hi;
3262 ExpandOp(Source, Lo, Hi);
3263
Chris Lattner66de05b2005-05-13 04:45:13 +00003264 // If this is unsigned, and not supported, first perform the conversion to
3265 // signed, then adjust the result if the sign bit is set.
3266 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3267 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3268
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003269 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3270 DAG.getConstant(0, Hi.getValueType()),
3271 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003272 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3273 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3274 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003275 uint64_t FF = 0x5f800000ULL;
3276 if (TLI.isLittleEndian()) FF <<= 32;
3277 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003278
Chris Lattner5839bf22005-08-26 17:15:30 +00003279 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003280 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3281 SDOperand FudgeInReg;
3282 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003283 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3284 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003285 else {
3286 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003287 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3288 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003289 }
Chris Lattner473a9902005-09-29 06:44:39 +00003290 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003291 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003292
Chris Lattnera88a2602005-05-14 05:33:54 +00003293 // Check to see if the target has a custom way to lower this. If so, use it.
3294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3295 default: assert(0 && "This action not implemented for this operation!");
3296 case TargetLowering::Legal:
3297 case TargetLowering::Expand:
3298 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003299 case TargetLowering::Custom: {
3300 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3301 Source), DAG);
3302 if (NV.Val)
3303 return LegalizeOp(NV);
3304 break; // The target decided this was legal after all
3305 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003306 }
3307
Chris Lattner13689e22005-05-12 07:00:44 +00003308 // Expand the source, then glue it back together for the call. We must expand
3309 // the source in case it is shared (this pass of legalize must traverse it).
3310 SDOperand SrcLo, SrcHi;
3311 ExpandOp(Source, SrcLo, SrcHi);
3312 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3313
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003314 SDNode *OutChain = 0;
3315 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3316 DAG.getEntryNode());
3317 const char *FnName = 0;
3318 if (DestTy == MVT::f32)
3319 FnName = "__floatdisf";
3320 else {
3321 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3322 FnName = "__floatdidf";
3323 }
3324
Chris Lattner77e77a62005-01-21 06:05:23 +00003325 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3326
3327 TargetLowering::ArgListTy Args;
3328 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003329
Chris Lattner77e77a62005-01-21 06:05:23 +00003330 Args.push_back(std::make_pair(Source, ArgTy));
3331
3332 // We don't care about token chains for libcalls. We just use the entry
3333 // node as our input and ignore the output chain. This allows us to place
3334 // calls wherever we need them to satisfy data dependences.
3335 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003336
3337 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003338 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3339 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003340
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003341 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003342 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003343}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003344
Chris Lattnere34b3962005-01-19 04:19:40 +00003345
3346
Chris Lattner3e928bb2005-01-07 07:47:09 +00003347/// ExpandOp - Expand the specified SDOperand into its two component pieces
3348/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3349/// LegalizeNodes map is filled in for any results that are not expanded, the
3350/// ExpandedNodes map is filled in for any results that are expanded, and the
3351/// Lo/Hi values are returned.
3352void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3353 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003354 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003355 SDNode *Node = Op.Val;
3356 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003357 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3358 "Cannot expand FP values!");
3359 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003360 "Cannot expand to FP value or to larger int value!");
3361
Chris Lattner6fdcb252005-09-02 20:32:45 +00003362 // See if we already expanded it.
3363 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3364 = ExpandedNodes.find(Op);
3365 if (I != ExpandedNodes.end()) {
3366 Lo = I->second.first;
3367 Hi = I->second.second;
3368 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003369 }
3370
Chris Lattner4e6c7462005-01-08 19:27:05 +00003371 // Expanding to multiple registers needs to perform an optimization step, and
3372 // is not careful to avoid operations the target does not support. Make sure
3373 // that all generated operations are legalized in the next iteration.
3374 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003375
Chris Lattner3e928bb2005-01-07 07:47:09 +00003376 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003377 case ISD::CopyFromReg:
3378 assert(0 && "CopyFromReg must be legal!");
3379 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003380 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3381 assert(0 && "Do not know how to expand this operator!");
3382 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003383 case ISD::UNDEF:
3384 Lo = DAG.getNode(ISD::UNDEF, NVT);
3385 Hi = DAG.getNode(ISD::UNDEF, NVT);
3386 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003387 case ISD::Constant: {
3388 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3389 Lo = DAG.getConstant(Cst, NVT);
3390 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3391 break;
3392 }
Nate Begemancc827e62005-12-07 19:48:11 +00003393 case ISD::ConstantVec: {
3394 unsigned NumElements = Node->getNumOperands();
3395 // If we only have two elements left in the constant vector, just break it
3396 // apart into the two scalar constants it contains. Otherwise, bisect the
3397 // ConstantVec, and return each half as a new ConstantVec.
3398 // FIXME: this is hard coded as big endian, it may have to change to support
3399 // SSE and Alpha MVI
3400 if (NumElements == 2) {
3401 Hi = Node->getOperand(0);
3402 Lo = Node->getOperand(1);
3403 } else {
3404 NumElements /= 2;
3405 std::vector<SDOperand> LoOps, HiOps;
3406 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3407 HiOps.push_back(Node->getOperand(I));
3408 LoOps.push_back(Node->getOperand(I+NumElements));
3409 }
3410 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3411 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3412 }
3413 break;
3414 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003415
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003416 case ISD::BUILD_PAIR:
3417 // Legalize both operands. FIXME: in the future we should handle the case
3418 // where the two elements are not legal.
3419 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3420 Lo = LegalizeOp(Node->getOperand(0));
3421 Hi = LegalizeOp(Node->getOperand(1));
3422 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003423
3424 case ISD::SIGN_EXTEND_INREG:
3425 ExpandOp(Node->getOperand(0), Lo, Hi);
3426 // Sign extend the lo-part.
3427 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3428 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3429 TLI.getShiftAmountTy()));
3430 // sext_inreg the low part if needed.
3431 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3432 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003433
Chris Lattneredb1add2005-05-11 04:51:16 +00003434 case ISD::CTPOP:
3435 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003436 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3437 DAG.getNode(ISD::CTPOP, NVT, Lo),
3438 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003439 Hi = DAG.getConstant(0, NVT);
3440 break;
3441
Chris Lattner39a8f332005-05-12 19:05:01 +00003442 case ISD::CTLZ: {
3443 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003444 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003445 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3446 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003447 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3448 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003449 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3450 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3451
3452 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3453 Hi = DAG.getConstant(0, NVT);
3454 break;
3455 }
3456
3457 case ISD::CTTZ: {
3458 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003459 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003460 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3461 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003462 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3463 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003464 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3465 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3466
3467 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3468 Hi = DAG.getConstant(0, NVT);
3469 break;
3470 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003471
Chris Lattner3e928bb2005-01-07 07:47:09 +00003472 case ISD::LOAD: {
3473 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3474 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003475 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003476
3477 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003478 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003479 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3480 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003481 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003482 //are from the same instruction?
3483 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003484
3485 // Build a factor node to remember that this load is independent of the
3486 // other one.
3487 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3488 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003489
Chris Lattner3e928bb2005-01-07 07:47:09 +00003490 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003491 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003492 if (!TLI.isLittleEndian())
3493 std::swap(Lo, Hi);
3494 break;
3495 }
Nate Begemanab48be32005-11-22 18:16:00 +00003496 case ISD::VLOAD: {
3497 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3498 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3499 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3500 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3501
3502 // If we only have two elements, turn into a pair of scalar loads.
3503 // FIXME: handle case where a vector of two elements is fine, such as
3504 // 2 x double on SSE2.
3505 if (NumElements == 2) {
3506 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3507 // Increment the pointer to the other half.
3508 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3509 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3510 getIntPtrConstant(IncrementSize));
3511 //Is this safe? declaring that the two parts of the split load
3512 //are from the same instruction?
3513 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3514 } else {
3515 NumElements /= 2; // Split the vector in half
3516 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3517 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3518 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3519 getIntPtrConstant(IncrementSize));
3520 //Is this safe? declaring that the two parts of the split load
3521 //are from the same instruction?
3522 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3523 }
3524
3525 // Build a factor node to remember that this load is independent of the
3526 // other one.
3527 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3528 Hi.getValue(1));
3529
3530 // Remember that we legalized the chain.
3531 AddLegalizedOperand(Op.getValue(1), TF);
3532 if (!TLI.isLittleEndian())
3533 std::swap(Lo, Hi);
3534 break;
3535 }
3536 case ISD::VADD:
3537 case ISD::VSUB:
3538 case ISD::VMUL: {
3539 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3540 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3541 SDOperand LL, LH, RL, RH;
3542
3543 ExpandOp(Node->getOperand(0), LL, LH);
3544 ExpandOp(Node->getOperand(1), RL, RH);
3545
3546 // If we only have two elements, turn into a pair of scalar loads.
3547 // FIXME: handle case where a vector of two elements is fine, such as
3548 // 2 x double on SSE2.
3549 if (NumElements == 2) {
3550 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3551 Lo = DAG.getNode(Opc, EVT, LL, RL);
3552 Hi = DAG.getNode(Opc, EVT, LH, RH);
3553 } else {
3554 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3555 LL.getOperand(3));
3556 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3557 LH.getOperand(3));
3558 }
3559 break;
3560 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003561 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003562 case ISD::CALL: {
3563 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3564 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3565
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003566 bool Changed = false;
3567 std::vector<SDOperand> Ops;
3568 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3569 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3570 Changed |= Ops.back() != Node->getOperand(i);
3571 }
3572
Chris Lattner3e928bb2005-01-07 07:47:09 +00003573 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3574 "Can only expand a call once so far, not i64 -> i16!");
3575
3576 std::vector<MVT::ValueType> RetTyVTs;
3577 RetTyVTs.reserve(3);
3578 RetTyVTs.push_back(NVT);
3579 RetTyVTs.push_back(NVT);
3580 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003581 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3582 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003583 Lo = SDOperand(NC, 0);
3584 Hi = SDOperand(NC, 1);
3585
3586 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003587 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003588 break;
3589 }
3590 case ISD::AND:
3591 case ISD::OR:
3592 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3593 SDOperand LL, LH, RL, RH;
3594 ExpandOp(Node->getOperand(0), LL, LH);
3595 ExpandOp(Node->getOperand(1), RL, RH);
3596 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3597 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3598 break;
3599 }
3600 case ISD::SELECT: {
3601 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003602
3603 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3604 case Expand: assert(0 && "It's impossible to expand bools");
3605 case Legal:
3606 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3607 break;
3608 case Promote:
3609 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3610 break;
3611 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003612 ExpandOp(Node->getOperand(1), LL, LH);
3613 ExpandOp(Node->getOperand(2), RL, RH);
3614 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3615 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3616 break;
3617 }
Nate Begeman9373a812005-08-10 20:51:12 +00003618 case ISD::SELECT_CC: {
3619 SDOperand TL, TH, FL, FH;
3620 ExpandOp(Node->getOperand(2), TL, TH);
3621 ExpandOp(Node->getOperand(3), FL, FH);
3622 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3623 Node->getOperand(1), TL, FL, Node->getOperand(4));
3624 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3625 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003626 Lo = LegalizeOp(Lo);
3627 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003628 break;
3629 }
Nate Begeman144ff662005-10-13 17:15:37 +00003630 case ISD::SEXTLOAD: {
3631 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3632 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3633 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3634
3635 if (EVT == NVT)
3636 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3637 else
3638 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3639 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003640
3641 // Remember that we legalized the chain.
3642 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3643
Nate Begeman144ff662005-10-13 17:15:37 +00003644 // The high part is obtained by SRA'ing all but one of the bits of the lo
3645 // part.
3646 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3647 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3648 TLI.getShiftAmountTy()));
3649 Lo = LegalizeOp(Lo);
3650 Hi = LegalizeOp(Hi);
3651 break;
3652 }
3653 case ISD::ZEXTLOAD: {
3654 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3655 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3656 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3657
3658 if (EVT == NVT)
3659 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3660 else
3661 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3662 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003663
3664 // Remember that we legalized the chain.
3665 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3666
Nate Begeman144ff662005-10-13 17:15:37 +00003667 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003668 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003669 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003670 break;
3671 }
3672 case ISD::EXTLOAD: {
3673 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3674 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3675 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3676
3677 if (EVT == NVT)
3678 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3679 else
3680 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3681 EVT);
3682
3683 // Remember that we legalized the chain.
3684 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3685
3686 // The high part is undefined.
3687 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3688 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003689 break;
3690 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003691 case ISD::ANY_EXTEND: {
3692 SDOperand In;
3693 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3694 case Expand: assert(0 && "expand-expand not implemented yet!");
3695 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3696 case Promote:
3697 In = PromoteOp(Node->getOperand(0));
3698 break;
3699 }
3700
3701 // The low part is any extension of the input (which degenerates to a copy).
3702 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3703 // The high part is undefined.
3704 Hi = DAG.getNode(ISD::UNDEF, NVT);
3705 break;
3706 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003707 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003708 SDOperand In;
3709 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3710 case Expand: assert(0 && "expand-expand not implemented yet!");
3711 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3712 case Promote:
3713 In = PromoteOp(Node->getOperand(0));
3714 // Emit the appropriate sign_extend_inreg to get the value we want.
3715 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003716 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003717 break;
3718 }
3719
Chris Lattner3e928bb2005-01-07 07:47:09 +00003720 // The low part is just a sign extension of the input (which degenerates to
3721 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003722 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003723
Chris Lattner3e928bb2005-01-07 07:47:09 +00003724 // The high part is obtained by SRA'ing all but one of the bits of the lo
3725 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003726 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003727 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3728 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003729 break;
3730 }
Chris Lattner06098e02005-04-03 23:41:52 +00003731 case ISD::ZERO_EXTEND: {
3732 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 zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003739 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003740 break;
3741 }
3742
Chris Lattner3e928bb2005-01-07 07:47:09 +00003743 // The low part is just a zero extension of the input (which degenerates to
3744 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003745 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003746
Chris Lattner3e928bb2005-01-07 07:47:09 +00003747 // The high part is just a zero.
3748 Hi = DAG.getConstant(0, NVT);
3749 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003750 }
Chris Lattner35481892005-12-23 00:16:34 +00003751
3752 case ISD::BIT_CONVERT: {
3753 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3754 Node->getOperand(0));
3755 ExpandOp(Tmp, Lo, Hi);
3756 break;
3757 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003758
Chris Lattner308575b2005-11-20 22:56:56 +00003759 case ISD::READCYCLECOUNTER: {
3760 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3761 TargetLowering::Custom &&
3762 "Must custom expand ReadCycleCounter");
3763 SDOperand T = TLI.LowerOperation(Op, DAG);
3764 assert(T.Val && "Node must be custom expanded!");
3765 Lo = LegalizeOp(T.getValue(0));
3766 Hi = LegalizeOp(T.getValue(1));
3767 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3768 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003769 break;
Chris Lattner308575b2005-11-20 22:56:56 +00003770 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003771
Chris Lattner4e6c7462005-01-08 19:27:05 +00003772 // These operators cannot be expanded directly, emit them as calls to
3773 // library functions.
3774 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003775 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003776 SDOperand Op;
3777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3778 case Expand: assert(0 && "cannot expand FP!");
3779 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3780 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3781 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003782
Chris Lattnerf20d1832005-07-30 01:40:57 +00003783 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3784
Chris Lattner80a3e942005-07-29 00:33:32 +00003785 // Now that the custom expander is done, expand the result, which is still
3786 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003787 if (Op.Val) {
3788 ExpandOp(Op, Lo, Hi);
3789 break;
3790 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003791 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003792
Chris Lattner4e6c7462005-01-08 19:27:05 +00003793 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003794 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003795 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003796 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003797 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003798
Chris Lattner4e6c7462005-01-08 19:27:05 +00003799 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003800 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3801 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3802 LegalizeOp(Node->getOperand(0)));
3803 // Now that the custom expander is done, expand the result, which is still
3804 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003805 Op = TLI.LowerOperation(Op, DAG);
3806 if (Op.Val) {
3807 ExpandOp(Op, Lo, Hi);
3808 break;
3809 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003810 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003811
Chris Lattner4e6c7462005-01-08 19:27:05 +00003812 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003813 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003814 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003815 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003816 break;
3817
Chris Lattnere34b3962005-01-19 04:19:40 +00003818 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003819 // If the target wants custom lowering, do so.
3820 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3821 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3822 LegalizeOp(Node->getOperand(1)));
3823 Op = TLI.LowerOperation(Op, DAG);
3824 if (Op.Val) {
3825 // Now that the custom expander is done, expand the result, which is
3826 // still VT.
3827 ExpandOp(Op, Lo, Hi);
3828 break;
3829 }
3830 }
3831
Chris Lattnere34b3962005-01-19 04:19:40 +00003832 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003833 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003834 break;
Chris Lattner47599822005-04-02 03:38:53 +00003835
3836 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003837 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003838 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3839 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003840 break;
3841 }
3842
Chris Lattnere34b3962005-01-19 04:19:40 +00003843 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003844 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003845 break;
3846
3847 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003848 // If the target wants custom lowering, do so.
3849 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3850 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3851 LegalizeOp(Node->getOperand(1)));
3852 Op = TLI.LowerOperation(Op, DAG);
3853 if (Op.Val) {
3854 // Now that the custom expander is done, expand the result, which is
3855 // still VT.
3856 ExpandOp(Op, Lo, Hi);
3857 break;
3858 }
3859 }
3860
Chris Lattnere34b3962005-01-19 04:19:40 +00003861 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003862 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003863 break;
Chris Lattner47599822005-04-02 03:38:53 +00003864
3865 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003866 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003867 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3868 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003869 break;
3870 }
3871
Chris Lattnere34b3962005-01-19 04:19:40 +00003872 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003873 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003874 break;
3875 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003876 // If the target wants custom lowering, do so.
3877 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3878 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3879 LegalizeOp(Node->getOperand(1)));
3880 Op = TLI.LowerOperation(Op, DAG);
3881 if (Op.Val) {
3882 // Now that the custom expander is done, expand the result, which is
3883 // still VT.
3884 ExpandOp(Op, Lo, Hi);
3885 break;
3886 }
3887 }
3888
Chris Lattnere34b3962005-01-19 04:19:40 +00003889 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003890 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003891 break;
Chris Lattner47599822005-04-02 03:38:53 +00003892
3893 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003894 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003895 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3896 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003897 break;
3898 }
3899
Chris Lattnere34b3962005-01-19 04:19:40 +00003900 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003901 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003902 break;
3903
Misha Brukmanedf128a2005-04-21 22:36:52 +00003904 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003905 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3906 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003907 break;
3908 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003909 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3910 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003911 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003912 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003913 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003914 SDOperand LL, LH, RL, RH;
3915 ExpandOp(Node->getOperand(0), LL, LH);
3916 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003917 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3918 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3919 // extended the sign bit of the low half through the upper half, and if so
3920 // emit a MULHS instead of the alternate sequence that is valid for any
3921 // i64 x i64 multiply.
3922 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3923 // is RH an extension of the sign bit of RL?
3924 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3925 RH.getOperand(1).getOpcode() == ISD::Constant &&
3926 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3927 // is LH an extension of the sign bit of LL?
3928 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3929 LH.getOperand(1).getOpcode() == ISD::Constant &&
3930 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3931 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3932 } else {
3933 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3934 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3935 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3936 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3937 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3938 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003939 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3940 } else {
3941 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3942 }
3943 break;
3944 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003945 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3946 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3947 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3948 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003949 }
3950
3951 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003952 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3953 std::make_pair(Lo, Hi))).second;
3954 assert(isNew && "Value already expanded?!?");
Chris Lattner69a889e2005-12-20 00:53:54 +00003955
Chris Lattner83397362005-12-21 18:02:52 +00003956 // Make sure the resultant values have been legalized themselves, unless this
3957 // is a type that requires multi-step expansion.
3958 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3959 Lo = LegalizeOp(Lo);
3960 Hi = LegalizeOp(Hi);
3961 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003962}
3963
3964
3965// SelectionDAG::Legalize - This is the entry point for the file.
3966//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003967void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003968 /// run - This is the main entry point to this class.
3969 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003970 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003971}
3972