blob: 2250c7f431c0065977ca4b3b81fbd5924bfa2dad [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
55 unsigned ValueTypeActions;
56
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner03c85462005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattner8afc48e2005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner03c85462005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000087
Chris Lattner3e928bb2005-01-07 07:47:09 +000088public:
89
Chris Lattner9c32d3b2005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner77e77a62005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000128
Jim Laskey6269ed12005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000136
Chris Lattnere34b3962005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000143
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
152
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000153SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
154 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
155 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156 assert(MVT::LAST_VALUETYPE <= 16 &&
157 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
Jim Laskey6269ed12005-08-17 00:39:29 +0000160/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
161/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000162/// we expand it. At this point, we know that the result and operand types are
163/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000164SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
165 SDOperand Op0,
166 MVT::ValueType DestVT) {
167 if (Op0.getValueType() == MVT::i32) {
168 // simple 32-bit [signed|unsigned] integer to float/double expansion
169
170 // get the stack frame index of a 8 byte buffer
171 MachineFunction &MF = DAG.getMachineFunction();
172 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
173 // get address of 8 byte buffer
174 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
175 // word offset constant for Hi/Lo address computation
176 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
177 // set up Hi and Lo (into buffer) address based on endian
178 SDOperand Hi, Lo;
179 if (TLI.isLittleEndian()) {
180 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
181 Lo = StackSlot;
182 } else {
183 Hi = StackSlot;
184 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
185 }
186 // if signed map to unsigned space
187 SDOperand Op0Mapped;
188 if (isSigned) {
189 // constant used to invert sign bit (signed to unsigned mapping)
190 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
191 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
192 } else {
193 Op0Mapped = Op0;
194 }
195 // store the lo of the constructed double - based on integer input
196 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
197 Op0Mapped, Lo, DAG.getSrcValue(NULL));
198 // initial hi portion of constructed double
199 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
200 // store the hi of the constructed double - biased exponent
201 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
202 InitialHi, Hi, DAG.getSrcValue(NULL));
203 // load the constructed double
204 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
205 DAG.getSrcValue(NULL));
206 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000207 SDOperand Bias = DAG.getConstantFP(isSigned ?
208 BitsToDouble(0x4330000080000000ULL)
209 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000210 MVT::f64);
211 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000212 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000213 // final result
214 SDOperand Result;
215 // handle final rounding
216 if (DestVT == MVT::f64) {
217 // do nothing
218 Result = Sub;
219 } else {
220 // if f32 then cast to f32
221 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
222 }
223 NeedsAnotherIteration = true;
224 return Result;
225 }
226 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000227 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000229 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
230 DAG.getConstant(0, Op0.getValueType()),
231 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000232 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
233 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
234 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000235
Jim Laskey6269ed12005-08-17 00:39:29 +0000236 // If the sign bit of the integer is set, the large number will be treated
237 // as a negative number. To counteract this, the dynamic code adds an
238 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000239 uint64_t FF;
240 switch (Op0.getValueType()) {
241 default: assert(0 && "Unsupported integer type!");
242 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
243 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
244 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
245 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
246 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000247 if (TLI.isLittleEndian()) FF <<= 32;
248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000249
Chris Lattner5839bf22005-08-26 17:15:30 +0000250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000251 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
252 SDOperand FudgeInReg;
253 if (DestVT == MVT::f32)
254 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
255 DAG.getSrcValue(NULL));
256 else {
257 assert(DestVT == MVT::f64 && "Unexpected conversion");
258 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
259 DAG.getEntryNode(), CPIdx,
260 DAG.getSrcValue(NULL), MVT::f32));
261 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000262
Chris Lattnercad063f2005-07-16 00:19:57 +0000263 NeedsAnotherIteration = true;
Chris Lattner473a9902005-09-29 06:44:39 +0000264 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnercad063f2005-07-16 00:19:57 +0000265}
266
Chris Lattner149c58c2005-08-16 18:17:10 +0000267/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000268/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000269/// we promote it. At this point, we know that the result and operand types are
270/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
271/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000272SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
273 MVT::ValueType DestVT,
274 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000275 // First step, figure out the appropriate *INT_TO_FP operation to use.
276 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000277
Chris Lattnercad063f2005-07-16 00:19:57 +0000278 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000279
Chris Lattnercad063f2005-07-16 00:19:57 +0000280 // Scan for the appropriate larger type to use.
281 while (1) {
282 NewInTy = (MVT::ValueType)(NewInTy+1);
283 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000284
Chris Lattnercad063f2005-07-16 00:19:57 +0000285 // If the target supports SINT_TO_FP of this type, use it.
286 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
287 default: break;
288 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000289 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000290 break; // Can't use this datatype.
291 // FALL THROUGH.
292 case TargetLowering::Custom:
293 OpToUse = ISD::SINT_TO_FP;
294 break;
295 }
296 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000297 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000298
Chris Lattnercad063f2005-07-16 00:19:57 +0000299 // If the target supports UINT_TO_FP of this type, use it.
300 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
301 default: break;
302 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000303 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000304 break; // Can't use this datatype.
305 // FALL THROUGH.
306 case TargetLowering::Custom:
307 OpToUse = ISD::UINT_TO_FP;
308 break;
309 }
310 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000311
Chris Lattnercad063f2005-07-16 00:19:57 +0000312 // Otherwise, try a larger type.
313 }
314
315 // Make sure to legalize any nodes we create here in the next pass.
316 NeedsAnotherIteration = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000317
Chris Lattnercad063f2005-07-16 00:19:57 +0000318 // Okay, we found the operation and type to use. Zero extend our input to the
319 // desired type then run the operation on it.
320 return DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000321 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
322 NewInTy, LegalOp));
Chris Lattnercad063f2005-07-16 00:19:57 +0000323}
324
Chris Lattner1618beb2005-07-29 00:11:56 +0000325/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
326/// FP_TO_*INT operation of the specified operand when the target requests that
327/// we promote it. At this point, we know that the result and operand types are
328/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
329/// operation that returns a larger result.
330SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
331 MVT::ValueType DestVT,
332 bool isSigned) {
333 // First step, figure out the appropriate FP_TO*INT operation to use.
334 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000335
Chris Lattner1618beb2005-07-29 00:11:56 +0000336 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000337
Chris Lattner1618beb2005-07-29 00:11:56 +0000338 // Scan for the appropriate larger type to use.
339 while (1) {
340 NewOutTy = (MVT::ValueType)(NewOutTy+1);
341 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000342
Chris Lattner1618beb2005-07-29 00:11:56 +0000343 // If the target supports FP_TO_SINT returning this type, use it.
344 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
345 default: break;
346 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000347 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000348 break; // Can't use this datatype.
349 // FALL THROUGH.
350 case TargetLowering::Custom:
351 OpToUse = ISD::FP_TO_SINT;
352 break;
353 }
354 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000355
Chris Lattner1618beb2005-07-29 00:11:56 +0000356 // If the target supports FP_TO_UINT of this type, use it.
357 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
358 default: break;
359 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000360 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000361 break; // Can't use this datatype.
362 // FALL THROUGH.
363 case TargetLowering::Custom:
364 OpToUse = ISD::FP_TO_UINT;
365 break;
366 }
367 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000368
Chris Lattner1618beb2005-07-29 00:11:56 +0000369 // Otherwise, try a larger type.
370 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000371
Chris Lattner1618beb2005-07-29 00:11:56 +0000372 // Make sure to legalize any nodes we create here in the next pass.
373 NeedsAnotherIteration = true;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000374
Chris Lattner1618beb2005-07-29 00:11:56 +0000375 // Okay, we found the operation and type to use. Truncate the result of the
376 // extended FP_TO_*INT operation to the desired size.
377 return DAG.getNode(ISD::TRUNCATE, DestVT,
378 DAG.getNode(OpToUse, NewOutTy, LegalOp));
379}
380
381
Chris Lattner3e928bb2005-01-07 07:47:09 +0000382void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000383 // The legalize process is inherently a bottom-up recursive process (users
384 // legalize their uses before themselves). Given infinite stack space, we
385 // could just start legalizing on the root and traverse the whole graph. In
386 // practice however, this causes us to run out of stack space on large basic
387 // blocks. To avoid this problem, legalize the entry node, then all its uses
388 // iteratively instead of recursively.
389 std::vector<SDOperand> Worklist;
390 Worklist.push_back(DAG.getEntryNode());
391
392 while (!Worklist.empty()) {
393 SDOperand Node = Worklist.back();
394 Worklist.pop_back();
395
396 if (LegalizedNodes.count(Node)) continue;
397
398 for (SDNode::use_iterator UI = Node.Val->use_begin(),
399 E = Node.Val->use_end(); UI != E; ++UI) {
400 // Scan the values. If this use has a value that is a token chain, add it
401 // to the worklist.
402 SDNode *User = *UI;
403 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
404 if (User->getValueType(i) == MVT::Other) {
405 Worklist.push_back(SDOperand(User, i));
406 break;
407 }
408 }
409
410 // Finally, legalize this node.
411 LegalizeOp(Node);
412 }
413
414
415 // Finally, legalize from the root up, to make sure we have legalized
416 // everything possible.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000417 SDOperand OldRoot = DAG.getRoot();
418 SDOperand NewRoot = LegalizeOp(OldRoot);
Chris Lattnerab510a72005-10-02 17:49:46 +0000419
Chris Lattner3e928bb2005-01-07 07:47:09 +0000420 DAG.setRoot(NewRoot);
421
422 ExpandedNodes.clear();
423 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000424 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000425
426 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000427 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000428}
429
430SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000431 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000432 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000433 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000434
Chris Lattner3e928bb2005-01-07 07:47:09 +0000435 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000436 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000437 if (Node->getNumValues() > 1) {
438 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
439 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000440 case Legal: break; // Nothing to do.
441 case Expand: {
442 SDOperand T1, T2;
443 ExpandOp(Op.getValue(i), T1, T2);
444 assert(LegalizedNodes.count(Op) &&
445 "Expansion didn't add legal operands!");
446 return LegalizedNodes[Op];
447 }
448 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000449 PromoteOp(Op.getValue(i));
450 assert(LegalizedNodes.count(Op) &&
451 "Expansion didn't add legal operands!");
452 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000453 }
454 }
455
Chris Lattner45982da2005-05-12 16:53:42 +0000456 // Note that LegalizeOp may be reentered even from single-use nodes, which
457 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000458 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
459 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000460
Nate Begeman9373a812005-08-10 20:51:12 +0000461 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000462
463 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000464
465 switch (Node->getOpcode()) {
466 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000467 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
468 // If this is a target node, legalize it by legalizing the operands then
469 // passing it through.
470 std::vector<SDOperand> Ops;
471 bool Changed = false;
472 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
473 Ops.push_back(LegalizeOp(Node->getOperand(i)));
474 Changed = Changed || Node->getOperand(i) != Ops.back();
475 }
476 if (Changed)
477 if (Node->getNumValues() == 1)
478 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
479 else {
480 std::vector<MVT::ValueType> VTs(Node->value_begin(),
481 Node->value_end());
482 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
483 }
484
485 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
486 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
487 return Result.getValue(Op.ResNo);
488 }
489 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
491 assert(0 && "Do not know how to legalize this operator!");
492 abort();
493 case ISD::EntryToken:
494 case ISD::FrameIndex:
495 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000496 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000497 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000498 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000499 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000500 case ISD::AssertSext:
501 case ISD::AssertZext:
502 Tmp1 = LegalizeOp(Node->getOperand(0));
503 if (Tmp1 != Node->getOperand(0))
504 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
505 Node->getOperand(1));
506 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000507 case ISD::CopyFromReg:
508 Tmp1 = LegalizeOp(Node->getOperand(0));
509 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000510 Result = DAG.getCopyFromReg(Tmp1,
511 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
512 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000513 else
514 Result = Op.getValue(0);
515
516 // Since CopyFromReg produces two values, make sure to remember that we
517 // legalized both of them.
518 AddLegalizedOperand(Op.getValue(0), Result);
519 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
520 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000521 case ISD::ImplicitDef:
522 Tmp1 = LegalizeOp(Node->getOperand(0));
523 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000524 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
525 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000526 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000527 case ISD::UNDEF: {
528 MVT::ValueType VT = Op.getValueType();
529 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000530 default: assert(0 && "This action is not supported yet!");
531 case TargetLowering::Expand:
532 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000533 if (MVT::isInteger(VT))
534 Result = DAG.getConstant(0, VT);
535 else if (MVT::isFloatingPoint(VT))
536 Result = DAG.getConstantFP(0, VT);
537 else
538 assert(0 && "Unknown value type!");
539 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000540 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000541 break;
542 }
543 break;
544 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000545 case ISD::Constant:
546 // We know we don't need to expand constants here, constants only have one
547 // value and we check that it is fine above.
548
549 // FIXME: Maybe we should handle things like targets that don't support full
550 // 32-bit immediates?
551 break;
552 case ISD::ConstantFP: {
553 // Spill FP immediates to the constant pool if the target cannot directly
554 // codegen them. Targets often have some immediate values that can be
555 // efficiently generated into an FP register without a load. We explicitly
556 // leave these constants as ConstantFP nodes for the target to deal with.
557
558 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
559
560 // Check to see if this FP immediate is already legal.
561 bool isLegal = false;
562 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
563 E = TLI.legal_fpimm_end(); I != E; ++I)
564 if (CFP->isExactlyValue(*I)) {
565 isLegal = true;
566 break;
567 }
568
569 if (!isLegal) {
570 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000571 bool Extend = false;
572
573 // If a FP immediate is precise when represented as a float, we put it
574 // into the constant pool as a float, even if it's is statically typed
575 // as a double.
576 MVT::ValueType VT = CFP->getValueType(0);
577 bool isDouble = VT == MVT::f64;
578 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
579 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000580 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
581 // Only do this if the target has a native EXTLOAD instruction from
582 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000583 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000584 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
585 VT = MVT::f32;
586 Extend = true;
587 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000588
Chris Lattner5839bf22005-08-26 17:15:30 +0000589 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000590 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000591 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
592 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000593 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000594 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
595 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000596 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000597 }
598 break;
599 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000600 case ISD::TokenFactor: {
601 std::vector<SDOperand> Ops;
602 bool Changed = false;
603 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000604 SDOperand Op = Node->getOperand(i);
605 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000606 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000607 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
608 Changed = true;
609 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
610 Ops.push_back(LegalizeOp(Op.getOperand(j)));
611 } else {
612 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
613 Changed |= Ops[i] != Op;
614 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000615 }
616 if (Changed)
617 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
618 break;
619 }
620
Chris Lattner16cd04d2005-05-12 23:24:06 +0000621 case ISD::CALLSEQ_START:
622 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000624 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000625 Tmp2 = Node->getOperand(0);
Nate Begeman27d404c2005-10-04 00:37:37 +0000626 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000627 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000628
629 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
630 // this will cause the maps used to memoize results to get confused.
631 // Create and add a dummy use, just to increase its use count. This will
632 // be removed at the end of legalize when dead nodes are removed.
633 if (Tmp2.Val->hasOneUse()) {
634 // FIXME: find out why this code is necessary
635 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
636 DAG.getConstant(0, MVT::i32));
637 }
638 }
639
Chris Lattner16cd04d2005-05-12 23:24:06 +0000640 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000641 // nodes are treated specially and are mutated in place. This makes the dag
642 // legalization process more efficient and also makes libcall insertion
643 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000644 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000645 case ISD::DYNAMIC_STACKALLOC:
646 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
647 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
648 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
649 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000650 Tmp3 != Node->getOperand(2)) {
651 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
652 std::vector<SDOperand> Ops;
653 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
654 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
655 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000656 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000657
658 // Since this op produces two values, make sure to remember that we
659 // legalized both of them.
660 AddLegalizedOperand(SDOperand(Node, 0), Result);
661 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
662 return Result.getValue(Op.ResNo);
663
Chris Lattnerd71c0412005-05-13 18:43:43 +0000664 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000665 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000668
669 bool Changed = false;
670 std::vector<SDOperand> Ops;
671 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
672 Ops.push_back(LegalizeOp(Node->getOperand(i)));
673 Changed |= Ops.back() != Node->getOperand(i);
674 }
675
676 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000677 std::vector<MVT::ValueType> RetTyVTs;
678 RetTyVTs.reserve(Node->getNumValues());
679 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000680 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000681 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
682 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000683 } else {
684 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000685 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000686 // Since calls produce multiple values, make sure to remember that we
687 // legalized all of them.
688 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
689 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
690 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000691 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000692 case ISD::BR:
693 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
694 if (Tmp1 != Node->getOperand(0))
695 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
696 break;
697
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000698 case ISD::BRCOND:
699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000700
Chris Lattner47e92232005-01-18 19:27:06 +0000701 switch (getTypeAction(Node->getOperand(1).getValueType())) {
702 case Expand: assert(0 && "It's impossible to expand bools");
703 case Legal:
704 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
705 break;
706 case Promote:
707 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
708 break;
709 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000710
711 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
712 default: assert(0 && "This action is not supported yet!");
713 case TargetLowering::Expand:
714 // Expand brcond's setcc into its constituent parts and create a BR_CC
715 // Node.
716 if (Tmp2.getOpcode() == ISD::SETCC) {
717 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
718 Tmp2.getOperand(0), Tmp2.getOperand(1),
719 Node->getOperand(2));
720 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000721 // Make sure the condition is either zero or one. It may have been
722 // promoted from something else.
723 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
724
Nate Begeman7cbd5252005-08-16 19:49:35 +0000725 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
726 DAG.getCondCode(ISD::SETNE), Tmp2,
727 DAG.getConstant(0, Tmp2.getValueType()),
728 Node->getOperand(2));
729 }
730 break;
731 case TargetLowering::Legal:
732 // Basic block destination (Op#2) is always legal.
733 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
734 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
735 Node->getOperand(2));
736 break;
737 }
738 break;
739 case ISD::BR_CC:
740 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
741
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000742 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000743 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
744 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
745 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
746 Tmp3 != Node->getOperand(3)) {
747 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
748 Tmp2, Tmp3, Node->getOperand(4));
749 }
750 break;
751 } else {
752 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
753 Node->getOperand(2), // LHS
754 Node->getOperand(3), // RHS
755 Node->getOperand(1)));
756 // If we get a SETCC back from legalizing the SETCC node we just
757 // created, then use its LHS, RHS, and CC directly in creating a new
758 // node. Otherwise, select between the true and false value based on
759 // comparing the result of the legalized with zero.
760 if (Tmp2.getOpcode() == ISD::SETCC) {
761 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
762 Tmp2.getOperand(0), Tmp2.getOperand(1),
763 Node->getOperand(4));
764 } else {
765 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
766 DAG.getCondCode(ISD::SETNE),
767 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
768 Node->getOperand(4));
769 }
770 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000771 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000772 case ISD::BRCONDTWOWAY:
773 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
774 switch (getTypeAction(Node->getOperand(1).getValueType())) {
775 case Expand: assert(0 && "It's impossible to expand bools");
776 case Legal:
777 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
778 break;
779 case Promote:
780 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
781 break;
782 }
783 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
784 // pair.
785 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
786 case TargetLowering::Promote:
787 default: assert(0 && "This action is not supported yet!");
788 case TargetLowering::Legal:
789 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
790 std::vector<SDOperand> Ops;
791 Ops.push_back(Tmp1);
792 Ops.push_back(Tmp2);
793 Ops.push_back(Node->getOperand(2));
794 Ops.push_back(Node->getOperand(3));
795 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
796 }
797 break;
798 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000799 // If BRTWOWAY_CC is legal for this target, then simply expand this node
800 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
801 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000802 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000803 if (Tmp2.getOpcode() == ISD::SETCC) {
804 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
805 Tmp2.getOperand(0), Tmp2.getOperand(1),
806 Node->getOperand(2), Node->getOperand(3));
807 } else {
808 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
809 DAG.getConstant(0, Tmp2.getValueType()),
810 Node->getOperand(2), Node->getOperand(3));
811 }
812 } else {
813 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000814 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000815 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
816 }
Chris Lattner411e8882005-04-09 03:30:19 +0000817 break;
818 }
819 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000820 case ISD::BRTWOWAY_CC:
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000822 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000823 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
824 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
825 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
826 Tmp3 != Node->getOperand(3)) {
827 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
828 Node->getOperand(4), Node->getOperand(5));
829 }
830 break;
831 } else {
832 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
833 Node->getOperand(2), // LHS
834 Node->getOperand(3), // RHS
835 Node->getOperand(1)));
836 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
837 // pair.
838 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
839 default: assert(0 && "This action is not supported yet!");
840 case TargetLowering::Legal:
841 // If we get a SETCC back from legalizing the SETCC node we just
842 // created, then use its LHS, RHS, and CC directly in creating a new
843 // node. Otherwise, select between the true and false value based on
844 // comparing the result of the legalized with zero.
845 if (Tmp2.getOpcode() == ISD::SETCC) {
846 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
847 Tmp2.getOperand(0), Tmp2.getOperand(1),
848 Node->getOperand(4), Node->getOperand(5));
849 } else {
850 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
851 DAG.getConstant(0, Tmp2.getValueType()),
852 Node->getOperand(4), Node->getOperand(5));
853 }
854 break;
855 case TargetLowering::Expand:
856 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
857 Node->getOperand(4));
858 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
859 break;
860 }
861 }
862 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000863 case ISD::LOAD:
864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
865 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000866
Chris Lattner3e928bb2005-01-07 07:47:09 +0000867 if (Tmp1 != Node->getOperand(0) ||
868 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000869 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
870 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000871 else
872 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000873
Chris Lattner8afc48e2005-01-07 22:28:47 +0000874 // Since loads produce two values, make sure to remember that we legalized
875 // both of them.
876 AddLegalizedOperand(SDOperand(Node, 0), Result);
877 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
878 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000879
Chris Lattner0f69b292005-01-15 06:18:18 +0000880 case ISD::EXTLOAD:
881 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000882 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000885
Chris Lattner5f056bf2005-07-10 01:55:33 +0000886 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000887 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000888 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000889 case TargetLowering::Promote:
890 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000891 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
892 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000893 // Since loads produce two values, make sure to remember that we legalized
894 // both of them.
895 AddLegalizedOperand(SDOperand(Node, 0), Result);
896 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
897 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000898
Chris Lattner01ff7212005-04-10 22:54:25 +0000899 case TargetLowering::Legal:
900 if (Tmp1 != Node->getOperand(0) ||
901 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000902 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
903 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000904 else
905 Result = SDOperand(Node, 0);
906
907 // Since loads produce two values, make sure to remember that we legalized
908 // both of them.
909 AddLegalizedOperand(SDOperand(Node, 0), Result);
910 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
911 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000912 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000913 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
914 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
915 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000916 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000917 if (Op.ResNo)
918 return Load.getValue(1);
919 return Result;
920 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000921 assert(Node->getOpcode() != ISD::EXTLOAD &&
922 "EXTLOAD should always be supported!");
923 // Turn the unsupported load into an EXTLOAD followed by an explicit
924 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000925 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
926 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000927 SDOperand ValRes;
928 if (Node->getOpcode() == ISD::SEXTLOAD)
929 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000930 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000931 else
932 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000933 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
934 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
935 if (Op.ResNo)
936 return Result.getValue(1);
937 return ValRes;
938 }
939 assert(0 && "Unreachable");
940 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000941 case ISD::EXTRACT_ELEMENT:
942 // Get both the low and high parts.
943 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
944 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
945 Result = Tmp2; // 1 -> Hi
946 else
947 Result = Tmp1; // 0 -> Lo
948 break;
949
950 case ISD::CopyToReg:
951 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000952
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000953 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000954 "Register type must be legal!");
955 // Legalize the incoming value (must be legal).
956 Tmp2 = LegalizeOp(Node->getOperand(2));
957 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
958 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
959 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000960 break;
961
962 case ISD::RET:
963 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
964 switch (Node->getNumOperands()) {
965 case 2: // ret val
966 switch (getTypeAction(Node->getOperand(1).getValueType())) {
967 case Legal:
968 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000969 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000970 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
971 break;
972 case Expand: {
973 SDOperand Lo, Hi;
974 ExpandOp(Node->getOperand(1), Lo, Hi);
975 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000976 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000977 }
978 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000979 Tmp2 = PromoteOp(Node->getOperand(1));
980 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
981 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000982 }
983 break;
984 case 1: // ret void
985 if (Tmp1 != Node->getOperand(0))
986 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
987 break;
988 default: { // ret <values>
989 std::vector<SDOperand> NewValues;
990 NewValues.push_back(Tmp1);
991 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
992 switch (getTypeAction(Node->getOperand(i).getValueType())) {
993 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000994 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000995 break;
996 case Expand: {
997 SDOperand Lo, Hi;
998 ExpandOp(Node->getOperand(i), Lo, Hi);
999 NewValues.push_back(Lo);
1000 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001001 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001002 }
1003 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001004 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001005 }
1006 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1007 break;
1008 }
1009 }
1010 break;
1011 case ISD::STORE:
1012 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1013 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1014
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001015 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001016 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001017 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001018 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001019 DAG.getConstant(FloatToBits(CFP->getValue()),
1020 MVT::i32),
1021 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001022 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001023 } else {
1024 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001025 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001026 DAG.getConstant(DoubleToBits(CFP->getValue()),
1027 MVT::i64),
1028 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001029 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001030 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001031 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001032 }
1033
Chris Lattner3e928bb2005-01-07 07:47:09 +00001034 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1035 case Legal: {
1036 SDOperand Val = LegalizeOp(Node->getOperand(1));
1037 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1038 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001039 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1040 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001041 break;
1042 }
1043 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001044 // Truncate the value and store the result.
1045 Tmp3 = PromoteOp(Node->getOperand(1));
1046 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001047 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001048 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001049 break;
1050
Chris Lattner3e928bb2005-01-07 07:47:09 +00001051 case Expand:
1052 SDOperand Lo, Hi;
1053 ExpandOp(Node->getOperand(1), Lo, Hi);
1054
1055 if (!TLI.isLittleEndian())
1056 std::swap(Lo, Hi);
1057
Chris Lattneredb1add2005-05-11 04:51:16 +00001058 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1059 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001060 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001061 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1062 getIntPtrConstant(IncrementSize));
1063 assert(isTypeLegal(Tmp2.getValueType()) &&
1064 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001065 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001066 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1067 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001068 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1069 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001070 }
1071 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001072 case ISD::PCMARKER:
1073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001074 if (Tmp1 != Node->getOperand(0))
1075 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001076 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001077 case ISD::TRUNCSTORE:
1078 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1079 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1080
1081 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1082 case Legal:
1083 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001084
1085 // The only promote case we handle is TRUNCSTORE:i1 X into
1086 // -> TRUNCSTORE:i8 (and X, 1)
1087 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1088 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1089 TargetLowering::Promote) {
1090 // Promote the bool to a mask then store.
1091 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1092 DAG.getConstant(1, Tmp2.getValueType()));
1093 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1094 Node->getOperand(3), DAG.getValueType(MVT::i8));
1095
1096 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1097 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001098 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001099 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001100 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001101 break;
1102 case Promote:
1103 case Expand:
1104 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1105 }
1106 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001107 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001108 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1109 case Expand: assert(0 && "It's impossible to expand bools");
1110 case Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1112 break;
1113 case Promote:
1114 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1115 break;
1116 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001117 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001118 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001119
Nate Begemanb942a3d2005-08-23 04:29:48 +00001120 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001121 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001122 case TargetLowering::Expand:
1123 if (Tmp1.getOpcode() == ISD::SETCC) {
1124 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1125 Tmp2, Tmp3,
1126 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1127 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001128 // Make sure the condition is either zero or one. It may have been
1129 // promoted from something else.
1130 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001131 Result = DAG.getSelectCC(Tmp1,
1132 DAG.getConstant(0, Tmp1.getValueType()),
1133 Tmp2, Tmp3, ISD::SETNE);
1134 }
1135 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001136 case TargetLowering::Legal:
1137 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1138 Tmp3 != Node->getOperand(2))
1139 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1140 Tmp1, Tmp2, Tmp3);
1141 break;
1142 case TargetLowering::Promote: {
1143 MVT::ValueType NVT =
1144 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1145 unsigned ExtOp, TruncOp;
1146 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001147 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001148 TruncOp = ISD::TRUNCATE;
1149 } else {
1150 ExtOp = ISD::FP_EXTEND;
1151 TruncOp = ISD::FP_ROUND;
1152 }
1153 // Promote each of the values to the new type.
1154 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1155 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1156 // Perform the larger operation, then round down.
1157 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1158 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1159 break;
1160 }
1161 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001162 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001163 case ISD::SELECT_CC:
1164 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1165 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1166
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001167 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001168 // Everything is legal, see if we should expand this op or something.
1169 switch (TLI.getOperationAction(ISD::SELECT_CC,
1170 Node->getOperand(0).getValueType())) {
1171 default: assert(0 && "This action is not supported yet!");
1172 case TargetLowering::Custom: {
1173 SDOperand Tmp =
1174 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1175 Node->getOperand(0),
1176 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001177 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001178 if (Tmp.Val) {
1179 Result = LegalizeOp(Tmp);
1180 break;
1181 }
1182 } // FALLTHROUGH if the target can't lower this operation after all.
1183 case TargetLowering::Legal:
1184 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1185 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1186 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1187 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1188 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1189 Tmp3, Tmp4, Node->getOperand(4));
1190 }
1191 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001192 }
1193 break;
1194 } else {
1195 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1196 Node->getOperand(0), // LHS
1197 Node->getOperand(1), // RHS
1198 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001199 // If we get a SETCC back from legalizing the SETCC node we just
1200 // created, then use its LHS, RHS, and CC directly in creating a new
1201 // node. Otherwise, select between the true and false value based on
1202 // comparing the result of the legalized with zero.
1203 if (Tmp1.getOpcode() == ISD::SETCC) {
1204 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1205 Tmp1.getOperand(0), Tmp1.getOperand(1),
1206 Tmp3, Tmp4, Tmp1.getOperand(2));
1207 } else {
1208 Result = DAG.getSelectCC(Tmp1,
1209 DAG.getConstant(0, Tmp1.getValueType()),
1210 Tmp3, Tmp4, ISD::SETNE);
1211 }
Nate Begeman9373a812005-08-10 20:51:12 +00001212 }
1213 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001214 case ISD::SETCC:
1215 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1216 case Legal:
1217 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1218 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001219 break;
1220 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001221 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1222 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1223
1224 // If this is an FP compare, the operands have already been extended.
1225 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1226 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001227 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001228
1229 // Otherwise, we have to insert explicit sign or zero extends. Note
1230 // that we could insert sign extends for ALL conditions, but zero extend
1231 // is cheaper on many machines (an AND instead of two shifts), so prefer
1232 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001233 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001234 default: assert(0 && "Unknown integer comparison!");
1235 case ISD::SETEQ:
1236 case ISD::SETNE:
1237 case ISD::SETUGE:
1238 case ISD::SETUGT:
1239 case ISD::SETULE:
1240 case ISD::SETULT:
1241 // ALL of these operations will work if we either sign or zero extend
1242 // the operands (including the unsigned comparisons!). Zero extend is
1243 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001244 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1245 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001246 break;
1247 case ISD::SETGE:
1248 case ISD::SETGT:
1249 case ISD::SETLT:
1250 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001251 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1252 DAG.getValueType(VT));
1253 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1254 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001255 break;
1256 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001257 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001258 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001259 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001260 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1261 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1262 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001263 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001264 case ISD::SETEQ:
1265 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001266 if (RHSLo == RHSHi)
1267 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1268 if (RHSCST->isAllOnesValue()) {
1269 // Comparison to -1.
1270 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001271 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001272 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001273 }
1274
Chris Lattner3e928bb2005-01-07 07:47:09 +00001275 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1276 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1277 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001278 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001279 break;
1280 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001281 // If this is a comparison of the sign bit, just look at the top part.
1282 // X > -1, x < 0
1283 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001284 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001285 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001286 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001287 (CST->isAllOnesValue()))) { // X > -1
1288 Tmp1 = LHSHi;
1289 Tmp2 = RHSHi;
1290 break;
1291 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001292
Chris Lattner3e928bb2005-01-07 07:47:09 +00001293 // FIXME: This generated code sucks.
1294 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001295 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001296 default: assert(0 && "Unknown integer setcc!");
1297 case ISD::SETLT:
1298 case ISD::SETULT: LowCC = ISD::SETULT; break;
1299 case ISD::SETGT:
1300 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1301 case ISD::SETLE:
1302 case ISD::SETULE: LowCC = ISD::SETULE; break;
1303 case ISD::SETGE:
1304 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1305 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001306
Chris Lattner3e928bb2005-01-07 07:47:09 +00001307 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1308 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1309 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1310
1311 // NOTE: on targets without efficient SELECT of bools, we can always use
1312 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001313 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1314 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1315 Node->getOperand(2));
1316 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001317 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1318 Result, Tmp1, Tmp2));
1319 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001320 }
1321 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001322
1323 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1324 default:
1325 assert(0 && "Cannot handle this action for SETCC yet!");
1326 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001327 case TargetLowering::Promote:
1328 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1329 Node->getOperand(2));
1330 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001331 case TargetLowering::Legal:
1332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1333 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1334 Node->getOperand(2));
1335 break;
1336 case TargetLowering::Expand:
1337 // Expand a setcc node into a select_cc of the same condition, lhs, and
1338 // rhs that selects between const 1 (true) and const 0 (false).
1339 MVT::ValueType VT = Node->getValueType(0);
1340 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1341 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1342 Node->getOperand(2));
1343 Result = LegalizeOp(Result);
1344 break;
1345 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001346 break;
1347
Chris Lattnere1bd8222005-01-11 05:57:22 +00001348 case ISD::MEMSET:
1349 case ISD::MEMCPY:
1350 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001351 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001352 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1353
1354 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1355 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1356 case Expand: assert(0 && "Cannot expand a byte!");
1357 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001358 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001359 break;
1360 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001361 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001362 break;
1363 }
1364 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001365 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001366 }
Chris Lattner272455b2005-02-02 03:44:41 +00001367
1368 SDOperand Tmp4;
1369 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001370 case Expand: {
1371 // Length is too big, just take the lo-part of the length.
1372 SDOperand HiPart;
1373 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1374 break;
1375 }
Chris Lattnere5605212005-01-28 22:29:18 +00001376 case Legal:
1377 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001378 break;
1379 case Promote:
1380 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001381 break;
1382 }
1383
1384 SDOperand Tmp5;
1385 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1386 case Expand: assert(0 && "Cannot expand this yet!");
1387 case Legal:
1388 Tmp5 = LegalizeOp(Node->getOperand(4));
1389 break;
1390 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001391 Tmp5 = PromoteOp(Node->getOperand(4));
1392 break;
1393 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001394
1395 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1396 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001397 case TargetLowering::Custom: {
1398 SDOperand Tmp =
1399 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1400 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1401 if (Tmp.Val) {
1402 Result = LegalizeOp(Tmp);
1403 break;
1404 }
1405 // FALLTHROUGH if the target thinks it is legal.
1406 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001407 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001408 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1409 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1410 Tmp5 != Node->getOperand(4)) {
1411 std::vector<SDOperand> Ops;
1412 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1413 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1414 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1415 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001416 break;
1417 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001418 // Otherwise, the target does not support this operation. Lower the
1419 // operation to an explicit libcall as appropriate.
1420 MVT::ValueType IntPtr = TLI.getPointerTy();
1421 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1422 std::vector<std::pair<SDOperand, const Type*> > Args;
1423
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001424 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001425 if (Node->getOpcode() == ISD::MEMSET) {
1426 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1427 // Extend the ubyte argument to be an int value for the call.
1428 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1429 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1430 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1431
1432 FnName = "memset";
1433 } else if (Node->getOpcode() == ISD::MEMCPY ||
1434 Node->getOpcode() == ISD::MEMMOVE) {
1435 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1436 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1437 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1438 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1439 } else {
1440 assert(0 && "Unknown op!");
1441 }
Chris Lattner45982da2005-05-12 16:53:42 +00001442
Chris Lattnere1bd8222005-01-11 05:57:22 +00001443 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001444 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001445 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001446 Result = CallResult.second;
1447 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001448 break;
1449 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001450 }
1451 break;
1452 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001453
1454 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001455 Tmp1 = LegalizeOp(Node->getOperand(0));
1456 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001457
Chris Lattner3e011362005-05-14 07:45:46 +00001458 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1459 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1460 std::vector<SDOperand> Ops;
1461 Ops.push_back(Tmp1);
1462 Ops.push_back(Tmp2);
1463 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1464 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001465 Result = SDOperand(Node, 0);
1466 // Since these produce two values, make sure to remember that we legalized
1467 // both of them.
1468 AddLegalizedOperand(SDOperand(Node, 0), Result);
1469 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1470 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001471 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001472 Tmp1 = LegalizeOp(Node->getOperand(0));
1473 Tmp2 = LegalizeOp(Node->getOperand(1));
1474 Tmp3 = LegalizeOp(Node->getOperand(2));
1475 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1476 Tmp3 != Node->getOperand(2))
1477 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1478 break;
1479
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001480 case ISD::READIO:
1481 Tmp1 = LegalizeOp(Node->getOperand(0));
1482 Tmp2 = LegalizeOp(Node->getOperand(1));
1483
1484 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1485 case TargetLowering::Custom:
1486 default: assert(0 && "This action not implemented for this operation!");
1487 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001488 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1489 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1490 std::vector<SDOperand> Ops;
1491 Ops.push_back(Tmp1);
1492 Ops.push_back(Tmp2);
1493 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1494 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001495 Result = SDOperand(Node, 0);
1496 break;
1497 case TargetLowering::Expand:
1498 // Replace this with a load from memory.
1499 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1500 Node->getOperand(1), DAG.getSrcValue(NULL));
1501 Result = LegalizeOp(Result);
1502 break;
1503 }
1504
1505 // Since these produce two values, make sure to remember that we legalized
1506 // both of them.
1507 AddLegalizedOperand(SDOperand(Node, 0), Result);
1508 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1509 return Result.getValue(Op.ResNo);
1510
1511 case ISD::WRITEIO:
1512 Tmp1 = LegalizeOp(Node->getOperand(0));
1513 Tmp2 = LegalizeOp(Node->getOperand(1));
1514 Tmp3 = LegalizeOp(Node->getOperand(2));
1515
1516 switch (TLI.getOperationAction(Node->getOpcode(),
1517 Node->getOperand(1).getValueType())) {
1518 case TargetLowering::Custom:
1519 default: assert(0 && "This action not implemented for this operation!");
1520 case TargetLowering::Legal:
1521 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1522 Tmp3 != Node->getOperand(2))
1523 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1524 break;
1525 case TargetLowering::Expand:
1526 // Replace this with a store to memory.
1527 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1528 Node->getOperand(1), Node->getOperand(2),
1529 DAG.getSrcValue(NULL));
1530 Result = LegalizeOp(Result);
1531 break;
1532 }
1533 break;
1534
Chris Lattner84f67882005-01-20 18:52:28 +00001535 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001536 case ISD::SUB_PARTS:
1537 case ISD::SHL_PARTS:
1538 case ISD::SRA_PARTS:
1539 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001540 std::vector<SDOperand> Ops;
1541 bool Changed = false;
1542 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1543 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1544 Changed |= Ops.back() != Node->getOperand(i);
1545 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001546 if (Changed) {
1547 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1548 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1549 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001550
1551 // Since these produce multiple values, make sure to remember that we
1552 // legalized all of them.
1553 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1554 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1555 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001556 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001557
1558 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001559 case ISD::ADD:
1560 case ISD::SUB:
1561 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001562 case ISD::MULHS:
1563 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001564 case ISD::UDIV:
1565 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001566 case ISD::AND:
1567 case ISD::OR:
1568 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001569 case ISD::SHL:
1570 case ISD::SRL:
1571 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001572 case ISD::FADD:
1573 case ISD::FSUB:
1574 case ISD::FMUL:
1575 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001576 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001577 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1578 case Expand: assert(0 && "Not possible");
1579 case Legal:
1580 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1581 break;
1582 case Promote:
1583 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1584 break;
1585 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001586 if (Tmp1 != Node->getOperand(0) ||
1587 Tmp2 != Node->getOperand(1))
1588 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1589 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001590
Nate Begemanc105e192005-04-06 00:23:54 +00001591 case ISD::UREM:
1592 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001593 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001594 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1595 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1596 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1597 case TargetLowering::Legal:
1598 if (Tmp1 != Node->getOperand(0) ||
1599 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001600 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001601 Tmp2);
1602 break;
1603 case TargetLowering::Promote:
1604 case TargetLowering::Custom:
1605 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001606 case TargetLowering::Expand:
1607 if (MVT::isInteger(Node->getValueType(0))) {
1608 MVT::ValueType VT = Node->getValueType(0);
1609 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1610 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1611 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1612 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1613 } else {
1614 // Floating point mod -> fmod libcall.
1615 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1616 SDOperand Dummy;
1617 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001618 }
1619 break;
1620 }
1621 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001622
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001623 case ISD::CTPOP:
1624 case ISD::CTTZ:
1625 case ISD::CTLZ:
1626 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1627 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1628 case TargetLowering::Legal:
1629 if (Tmp1 != Node->getOperand(0))
1630 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1631 break;
1632 case TargetLowering::Promote: {
1633 MVT::ValueType OVT = Tmp1.getValueType();
1634 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001635
1636 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001637 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1638 // Perform the larger operation, then subtract if needed.
1639 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1640 switch(Node->getOpcode())
1641 {
1642 case ISD::CTPOP:
1643 Result = Tmp1;
1644 break;
1645 case ISD::CTTZ:
1646 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001647 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1648 DAG.getConstant(getSizeInBits(NVT), NVT),
1649 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001650 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001651 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1652 break;
1653 case ISD::CTLZ:
1654 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001655 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1656 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001657 getSizeInBits(OVT), NVT));
1658 break;
1659 }
1660 break;
1661 }
1662 case TargetLowering::Custom:
1663 assert(0 && "Cannot custom handle this yet!");
1664 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001665 switch(Node->getOpcode())
1666 {
1667 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001668 static const uint64_t mask[6] = {
1669 0x5555555555555555ULL, 0x3333333333333333ULL,
1670 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1671 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1672 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001673 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001674 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1675 unsigned len = getSizeInBits(VT);
1676 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001677 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001678 Tmp2 = DAG.getConstant(mask[i], VT);
1679 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001680 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001681 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1682 DAG.getNode(ISD::AND, VT,
1683 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1684 Tmp2));
1685 }
1686 Result = Tmp1;
1687 break;
1688 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001689 case ISD::CTLZ: {
1690 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001691 x = x | (x >> 1);
1692 x = x | (x >> 2);
1693 ...
1694 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001695 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001696 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001697
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001698 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1699 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001700 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1701 unsigned len = getSizeInBits(VT);
1702 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1703 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001704 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001705 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1706 }
1707 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001708 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001709 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001710 }
1711 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001712 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001713 // unless the target has ctlz but not ctpop, in which case we use:
1714 // { return 32 - nlz(~x & (x-1)); }
1715 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001716 MVT::ValueType VT = Tmp1.getValueType();
1717 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001718 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001719 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1720 DAG.getNode(ISD::SUB, VT, Tmp1,
1721 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001722 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001723 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1724 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001725 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001726 DAG.getConstant(getSizeInBits(VT), VT),
1727 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1728 } else {
1729 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1730 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001731 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001732 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001733 default:
1734 assert(0 && "Cannot expand this yet!");
1735 break;
1736 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001737 break;
1738 }
1739 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001740
Chris Lattner2c8086f2005-04-02 05:00:07 +00001741 // Unary operators
1742 case ISD::FABS:
1743 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001744 case ISD::FSQRT:
1745 case ISD::FSIN:
1746 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001747 Tmp1 = LegalizeOp(Node->getOperand(0));
1748 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1749 case TargetLowering::Legal:
1750 if (Tmp1 != Node->getOperand(0))
1751 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1752 break;
1753 case TargetLowering::Promote:
1754 case TargetLowering::Custom:
1755 assert(0 && "Cannot promote/custom handle this yet!");
1756 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001757 switch(Node->getOpcode()) {
1758 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001759 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1760 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001761 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00001762 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001763 break;
1764 }
1765 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001766 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1767 MVT::ValueType VT = Node->getValueType(0);
1768 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001769 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001770 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1771 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1772 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001773 break;
1774 }
1775 case ISD::FSQRT:
1776 case ISD::FSIN:
1777 case ISD::FCOS: {
1778 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001779 const char *FnName = 0;
1780 switch(Node->getOpcode()) {
1781 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1782 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1783 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1784 default: assert(0 && "Unreachable!");
1785 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001786 SDOperand Dummy;
1787 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001788 break;
1789 }
1790 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001791 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001792 }
1793 break;
1794 }
1795 break;
1796
1797 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001798 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001799 case ISD::UINT_TO_FP: {
1800 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001801 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1802 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001803 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001804 Node->getOperand(0).getValueType())) {
1805 default: assert(0 && "Unknown operation action!");
1806 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001807 Result = ExpandLegalINT_TO_FP(isSigned,
1808 LegalizeOp(Node->getOperand(0)),
1809 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001810 AddLegalizedOperand(Op, Result);
1811 return Result;
1812 case TargetLowering::Promote:
1813 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1814 Node->getValueType(0),
1815 isSigned);
1816 AddLegalizedOperand(Op, Result);
1817 return Result;
1818 case TargetLowering::Legal:
1819 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001820 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001821
Chris Lattner3e928bb2005-01-07 07:47:09 +00001822 Tmp1 = LegalizeOp(Node->getOperand(0));
1823 if (Tmp1 != Node->getOperand(0))
1824 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1825 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001826 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001827 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1828 Node->getValueType(0), Node->getOperand(0));
1829 break;
1830 case Promote:
1831 if (isSigned) {
1832 Result = PromoteOp(Node->getOperand(0));
1833 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1834 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1835 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1836 } else {
1837 Result = PromoteOp(Node->getOperand(0));
1838 Result = DAG.getZeroExtendInReg(Result,
1839 Node->getOperand(0).getValueType());
1840 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001841 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001842 break;
1843 }
1844 break;
1845 }
1846 case ISD::TRUNCATE:
1847 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1848 case Legal:
1849 Tmp1 = LegalizeOp(Node->getOperand(0));
1850 if (Tmp1 != Node->getOperand(0))
1851 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1852 break;
1853 case Expand:
1854 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1855
1856 // Since the result is legal, we should just be able to truncate the low
1857 // part of the source.
1858 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1859 break;
1860 case Promote:
1861 Result = PromoteOp(Node->getOperand(0));
1862 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1863 break;
1864 }
1865 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001866
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001867 case ISD::FP_TO_SINT:
1868 case ISD::FP_TO_UINT:
1869 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1870 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001871 Tmp1 = LegalizeOp(Node->getOperand(0));
1872
Chris Lattner1618beb2005-07-29 00:11:56 +00001873 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1874 default: assert(0 && "Unknown operation action!");
1875 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001876 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1877 SDOperand True, False;
1878 MVT::ValueType VT = Node->getOperand(0).getValueType();
1879 MVT::ValueType NVT = Node->getValueType(0);
1880 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1881 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1882 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1883 Node->getOperand(0), Tmp2, ISD::SETLT);
1884 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1885 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00001886 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00001887 Tmp2));
1888 False = DAG.getNode(ISD::XOR, NVT, False,
1889 DAG.getConstant(1ULL << ShiftAmt, NVT));
1890 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001891 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001892 } else {
1893 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1894 }
1895 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001896 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001897 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001898 Node->getOpcode() == ISD::FP_TO_SINT);
1899 AddLegalizedOperand(Op, Result);
1900 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001901 case TargetLowering::Custom: {
1902 SDOperand Tmp =
1903 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1904 Tmp = TLI.LowerOperation(Tmp, DAG);
1905 if (Tmp.Val) {
1906 AddLegalizedOperand(Op, Tmp);
1907 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001908 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001909 } else {
1910 // The target thinks this is legal afterall.
1911 break;
1912 }
1913 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001914 case TargetLowering::Legal:
1915 break;
1916 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001917
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001918 if (Tmp1 != Node->getOperand(0))
1919 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1920 break;
1921 case Expand:
1922 assert(0 && "Shouldn't need to expand other operators here!");
1923 case Promote:
1924 Result = PromoteOp(Node->getOperand(0));
1925 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1926 break;
1927 }
1928 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001929
Chris Lattner13c78e22005-09-02 00:18:10 +00001930 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001931 case ISD::ZERO_EXTEND:
1932 case ISD::SIGN_EXTEND:
1933 case ISD::FP_EXTEND:
1934 case ISD::FP_ROUND:
1935 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1936 case Legal:
1937 Tmp1 = LegalizeOp(Node->getOperand(0));
1938 if (Tmp1 != Node->getOperand(0))
1939 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1940 break;
1941 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001942 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001943
Chris Lattner03c85462005-01-15 05:21:40 +00001944 case Promote:
1945 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001946 case ISD::ANY_EXTEND:
1947 Result = PromoteOp(Node->getOperand(0));
1948 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
1949 break;
Chris Lattner1713e732005-01-16 00:38:00 +00001950 case ISD::ZERO_EXTEND:
1951 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001952 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001953 Result = DAG.getZeroExtendInReg(Result,
1954 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001955 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001956 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001957 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001958 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001959 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001960 Result,
1961 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001962 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001963 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001964 Result = PromoteOp(Node->getOperand(0));
1965 if (Result.getValueType() != Op.getValueType())
1966 // Dynamically dead while we have only 2 FP types.
1967 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1968 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001969 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001970 Result = PromoteOp(Node->getOperand(0));
1971 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1972 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001973 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001974 }
1975 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001976 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001977 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001978 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001979 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001980
1981 // If this operation is not supported, convert it to a shl/shr or load/store
1982 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001983 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1984 default: assert(0 && "This action not supported for this op yet!");
1985 case TargetLowering::Legal:
1986 if (Tmp1 != Node->getOperand(0))
1987 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001988 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001989 break;
1990 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001991 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001992 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001993 // NOTE: we could fall back on load/store here too for targets without
1994 // SAR. However, it is doubtful that any exist.
1995 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1996 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001997 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001998 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1999 Node->getOperand(0), ShiftCst);
2000 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2001 Result, ShiftCst);
2002 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2003 // The only way we can lower this is to turn it into a STORETRUNC,
2004 // EXTLOAD pair, targetting a temporary location (a stack slot).
2005
2006 // NOTE: there is a choice here between constantly creating new stack
2007 // slots and always reusing the same one. We currently always create
2008 // new ones, as reuse may inhibit scheduling.
2009 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2010 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2011 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2012 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002013 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002014 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2015 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2016 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002017 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002018 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002019 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2020 Result, StackSlot, DAG.getSrcValue(NULL),
2021 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002022 } else {
2023 assert(0 && "Unknown op");
2024 }
2025 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002026 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002027 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002028 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002029 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002030 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002031
Chris Lattner45982da2005-05-12 16:53:42 +00002032 // Note that LegalizeOp may be reentered even from single-use nodes, which
2033 // means that we always must cache transformed nodes.
2034 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002035 return Result;
2036}
2037
Chris Lattner8b6fa222005-01-15 22:16:26 +00002038/// PromoteOp - Given an operation that produces a value in an invalid type,
2039/// promote it to compute the value into a larger type. The produced value will
2040/// have the correct bits for the low portion of the register, but no guarantee
2041/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002042SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2043 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002044 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002045 assert(getTypeAction(VT) == Promote &&
2046 "Caller should expand or legalize operands that are not promotable!");
2047 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2048 "Cannot promote to smaller type!");
2049
Chris Lattner03c85462005-01-15 05:21:40 +00002050 SDOperand Tmp1, Tmp2, Tmp3;
2051
2052 SDOperand Result;
2053 SDNode *Node = Op.Val;
2054
Chris Lattner6fdcb252005-09-02 20:32:45 +00002055 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2056 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002057
Chris Lattner0f69b292005-01-15 06:18:18 +00002058 // Promotion needs an optimization step to clean up after it, and is not
2059 // careful to avoid operations the target does not support. Make sure that
2060 // all generated operations are legalized in the next iteration.
2061 NeedsAnotherIteration = true;
2062
Chris Lattner03c85462005-01-15 05:21:40 +00002063 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002064 case ISD::CopyFromReg:
2065 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002066 default:
2067 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2068 assert(0 && "Do not know how to promote this operator!");
2069 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002070 case ISD::UNDEF:
2071 Result = DAG.getNode(ISD::UNDEF, NVT);
2072 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002073 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002074 if (VT != MVT::i1)
2075 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2076 else
2077 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002078 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2079 break;
2080 case ISD::ConstantFP:
2081 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2082 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2083 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002084
Chris Lattner82fbfb62005-01-18 02:59:52 +00002085 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002086 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002087 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2088 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002089 Result = LegalizeOp(Result);
2090 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002091
2092 case ISD::TRUNCATE:
2093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2094 case Legal:
2095 Result = LegalizeOp(Node->getOperand(0));
2096 assert(Result.getValueType() >= NVT &&
2097 "This truncation doesn't make sense!");
2098 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2099 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2100 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002101 case Promote:
2102 // The truncation is not required, because we don't guarantee anything
2103 // about high bits anyway.
2104 Result = PromoteOp(Node->getOperand(0));
2105 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002106 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002107 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2108 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002109 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002110 }
2111 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002112 case ISD::SIGN_EXTEND:
2113 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002114 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2116 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2117 case Legal:
2118 // Input is legal? Just do extend all the way to the larger type.
2119 Result = LegalizeOp(Node->getOperand(0));
2120 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2121 break;
2122 case Promote:
2123 // Promote the reg if it's smaller.
2124 Result = PromoteOp(Node->getOperand(0));
2125 // The high bits are not guaranteed to be anything. Insert an extend.
2126 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002127 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002128 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002129 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002130 Result = DAG.getZeroExtendInReg(Result,
2131 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002132 break;
2133 }
2134 break;
2135
2136 case ISD::FP_EXTEND:
2137 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2138 case ISD::FP_ROUND:
2139 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2140 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2141 case Promote: assert(0 && "Unreachable with 2 FP types!");
2142 case Legal:
2143 // Input is legal? Do an FP_ROUND_INREG.
2144 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002145 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2146 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002147 break;
2148 }
2149 break;
2150
2151 case ISD::SINT_TO_FP:
2152 case ISD::UINT_TO_FP:
2153 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2154 case Legal:
2155 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002156 // No extra round required here.
2157 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002158 break;
2159
2160 case Promote:
2161 Result = PromoteOp(Node->getOperand(0));
2162 if (Node->getOpcode() == ISD::SINT_TO_FP)
2163 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002164 Result,
2165 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002166 else
Chris Lattner23993562005-04-13 02:38:47 +00002167 Result = DAG.getZeroExtendInReg(Result,
2168 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002169 // No extra round required here.
2170 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002171 break;
2172 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002173 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2174 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002175 // Round if we cannot tolerate excess precision.
2176 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002177 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2178 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002179 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002180 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002181 break;
2182
2183 case ISD::FP_TO_SINT:
2184 case ISD::FP_TO_UINT:
2185 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2186 case Legal:
2187 Tmp1 = LegalizeOp(Node->getOperand(0));
2188 break;
2189 case Promote:
2190 // The input result is prerounded, so we don't have to do anything
2191 // special.
2192 Tmp1 = PromoteOp(Node->getOperand(0));
2193 break;
2194 case Expand:
2195 assert(0 && "not implemented");
2196 }
Nate Begemand2558e32005-08-14 01:20:53 +00002197 // If we're promoting a UINT to a larger size, check to see if the new node
2198 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2199 // we can use that instead. This allows us to generate better code for
2200 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2201 // legal, such as PowerPC.
2202 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002203 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2204 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begemand2558e32005-08-14 01:20:53 +00002205 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2206 } else {
2207 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2208 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002209 break;
2210
Chris Lattner2c8086f2005-04-02 05:00:07 +00002211 case ISD::FABS:
2212 case ISD::FNEG:
2213 Tmp1 = PromoteOp(Node->getOperand(0));
2214 assert(Tmp1.getValueType() == NVT);
2215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2216 // NOTE: we do not have to do any extra rounding here for
2217 // NoExcessFPPrecision, because we know the input will have the appropriate
2218 // precision, and these operations don't modify precision at all.
2219 break;
2220
Chris Lattnerda6ba872005-04-28 21:44:33 +00002221 case ISD::FSQRT:
2222 case ISD::FSIN:
2223 case ISD::FCOS:
2224 Tmp1 = PromoteOp(Node->getOperand(0));
2225 assert(Tmp1.getValueType() == NVT);
2226 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2227 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002228 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2229 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002230 break;
2231
Chris Lattner03c85462005-01-15 05:21:40 +00002232 case ISD::AND:
2233 case ISD::OR:
2234 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002235 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002236 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002237 case ISD::MUL:
2238 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002239 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002240 // that too is okay if they are integer operations.
2241 Tmp1 = PromoteOp(Node->getOperand(0));
2242 Tmp2 = PromoteOp(Node->getOperand(1));
2243 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2244 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002245 break;
2246 case ISD::FADD:
2247 case ISD::FSUB:
2248 case ISD::FMUL:
2249 // The input may have strange things in the top bits of the registers, but
2250 // these operations don't care.
2251 Tmp1 = PromoteOp(Node->getOperand(0));
2252 Tmp2 = PromoteOp(Node->getOperand(1));
2253 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2254 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2255
2256 // Floating point operations will give excess precision that we may not be
2257 // able to tolerate. If we DO allow excess precision, just leave it,
2258 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002259 // FIXME: Why would we need to round FP ops more than integer ones?
2260 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002261 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002262 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2263 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002264 break;
2265
Chris Lattner8b6fa222005-01-15 22:16:26 +00002266 case ISD::SDIV:
2267 case ISD::SREM:
2268 // These operators require that their input be sign extended.
2269 Tmp1 = PromoteOp(Node->getOperand(0));
2270 Tmp2 = PromoteOp(Node->getOperand(1));
2271 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002272 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2273 DAG.getValueType(VT));
2274 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2275 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002276 }
2277 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2278
2279 // Perform FP_ROUND: this is probably overly pessimistic.
2280 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002281 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2282 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002283 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002284 case ISD::FDIV:
2285 case ISD::FREM:
2286 // These operators require that their input be fp extended.
2287 Tmp1 = PromoteOp(Node->getOperand(0));
2288 Tmp2 = PromoteOp(Node->getOperand(1));
2289 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2290
2291 // Perform FP_ROUND: this is probably overly pessimistic.
2292 if (NoExcessFPPrecision)
2293 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2294 DAG.getValueType(VT));
2295 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002296
2297 case ISD::UDIV:
2298 case ISD::UREM:
2299 // These operators require that their input be zero extended.
2300 Tmp1 = PromoteOp(Node->getOperand(0));
2301 Tmp2 = PromoteOp(Node->getOperand(1));
2302 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002303 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2304 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002305 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2306 break;
2307
2308 case ISD::SHL:
2309 Tmp1 = PromoteOp(Node->getOperand(0));
2310 Tmp2 = LegalizeOp(Node->getOperand(1));
2311 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2312 break;
2313 case ISD::SRA:
2314 // The input value must be properly sign extended.
2315 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002316 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2317 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002318 Tmp2 = LegalizeOp(Node->getOperand(1));
2319 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2320 break;
2321 case ISD::SRL:
2322 // The input value must be properly zero extended.
2323 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002324 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002325 Tmp2 = LegalizeOp(Node->getOperand(1));
2326 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2327 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002328 case ISD::LOAD:
2329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002331 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002332 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002333 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2334 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002335 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002336 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2337 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002338
2339 // Remember that we legalized the chain.
2340 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2341 break;
2342 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002343 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2344 case Expand: assert(0 && "It's impossible to expand bools");
2345 case Legal:
2346 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2347 break;
2348 case Promote:
2349 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2350 break;
2351 }
Chris Lattner03c85462005-01-15 05:21:40 +00002352 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2353 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2354 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2355 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002356 case ISD::SELECT_CC:
2357 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2358 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2359 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2360 Node->getOperand(1), Tmp2, Tmp3,
2361 Node->getOperand(4));
2362 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002363 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002364 case ISD::CALL: {
2365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2366 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2367
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002368 std::vector<SDOperand> Ops;
2369 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2370 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2371
Chris Lattner8ac532c2005-01-16 19:46:48 +00002372 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2373 "Can only promote single result calls");
2374 std::vector<MVT::ValueType> RetTyVTs;
2375 RetTyVTs.reserve(2);
2376 RetTyVTs.push_back(NVT);
2377 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002378 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2379 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002380 Result = SDOperand(NC, 0);
2381
2382 // Insert the new chain mapping.
2383 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2384 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002385 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002386 case ISD::CTPOP:
2387 case ISD::CTTZ:
2388 case ISD::CTLZ:
2389 Tmp1 = Node->getOperand(0);
2390 //Zero extend the argument
2391 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2392 // Perform the larger operation, then subtract if needed.
2393 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2394 switch(Node->getOpcode())
2395 {
2396 case ISD::CTPOP:
2397 Result = Tmp1;
2398 break;
2399 case ISD::CTTZ:
2400 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002401 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002402 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002403 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002404 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2405 break;
2406 case ISD::CTLZ:
2407 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002408 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2409 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002410 getSizeInBits(VT), NVT));
2411 break;
2412 }
2413 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002414 }
2415
2416 assert(Result.Val && "Didn't set a result!");
2417 AddPromotedOperand(Op, Result);
2418 return Result;
2419}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002420
Chris Lattner84f67882005-01-20 18:52:28 +00002421/// ExpandAddSub - Find a clever way to expand this add operation into
2422/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002423void SelectionDAGLegalize::
2424ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2425 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002426 // Expand the subcomponents.
2427 SDOperand LHSL, LHSH, RHSL, RHSH;
2428 ExpandOp(LHS, LHSL, LHSH);
2429 ExpandOp(RHS, RHSL, RHSH);
2430
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002431 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002432 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2433 if (LHSL.getValueType() == MVT::i32) {
2434 SDOperand LowEl;
2435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2436 if (C->getValue() == 0)
2437 LowEl = RHSL;
2438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2439 if (C->getValue() == 0)
2440 LowEl = LHSL;
2441 if (LowEl.Val) {
2442 // Turn this into an add/sub of the high part only.
2443 SDOperand HiEl =
2444 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2445 LowEl.getValueType(), LHSH, RHSH);
2446 Lo = LowEl;
2447 Hi = HiEl;
2448 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002449 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002450 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002451
Chris Lattner84f67882005-01-20 18:52:28 +00002452 std::vector<SDOperand> Ops;
2453 Ops.push_back(LHSL);
2454 Ops.push_back(LHSH);
2455 Ops.push_back(RHSL);
2456 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002457
2458 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2459 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002460 Hi = Lo.getValue(1);
2461}
2462
Chris Lattner5b359c62005-04-02 04:00:59 +00002463void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2464 SDOperand Op, SDOperand Amt,
2465 SDOperand &Lo, SDOperand &Hi) {
2466 // Expand the subcomponents.
2467 SDOperand LHSL, LHSH;
2468 ExpandOp(Op, LHSL, LHSH);
2469
2470 std::vector<SDOperand> Ops;
2471 Ops.push_back(LHSL);
2472 Ops.push_back(LHSH);
2473 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002474 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002475 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002476 Hi = Lo.getValue(1);
2477}
2478
2479
Chris Lattnere34b3962005-01-19 04:19:40 +00002480/// ExpandShift - Try to find a clever way to expand this shift operation out to
2481/// smaller elements. If we can't find a way that is more efficient than a
2482/// libcall on this target, return false. Otherwise, return true with the
2483/// low-parts expanded into Lo and Hi.
2484bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2485 SDOperand &Lo, SDOperand &Hi) {
2486 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2487 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002488
Chris Lattnere34b3962005-01-19 04:19:40 +00002489 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002490 SDOperand ShAmt = LegalizeOp(Amt);
2491 MVT::ValueType ShTy = ShAmt.getValueType();
2492 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2493 unsigned NVTBits = MVT::getSizeInBits(NVT);
2494
2495 // Handle the case when Amt is an immediate. Other cases are currently broken
2496 // and are disabled.
2497 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2498 unsigned Cst = CN->getValue();
2499 // Expand the incoming operand to be shifted, so that we have its parts
2500 SDOperand InL, InH;
2501 ExpandOp(Op, InL, InH);
2502 switch(Opc) {
2503 case ISD::SHL:
2504 if (Cst > VTBits) {
2505 Lo = DAG.getConstant(0, NVT);
2506 Hi = DAG.getConstant(0, NVT);
2507 } else if (Cst > NVTBits) {
2508 Lo = DAG.getConstant(0, NVT);
2509 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002510 } else if (Cst == NVTBits) {
2511 Lo = DAG.getConstant(0, NVT);
2512 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002513 } else {
2514 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2515 Hi = DAG.getNode(ISD::OR, NVT,
2516 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2517 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2518 }
2519 return true;
2520 case ISD::SRL:
2521 if (Cst > VTBits) {
2522 Lo = DAG.getConstant(0, NVT);
2523 Hi = DAG.getConstant(0, NVT);
2524 } else if (Cst > NVTBits) {
2525 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2526 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002527 } else if (Cst == NVTBits) {
2528 Lo = InH;
2529 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002530 } else {
2531 Lo = DAG.getNode(ISD::OR, NVT,
2532 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2533 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2534 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2535 }
2536 return true;
2537 case ISD::SRA:
2538 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002539 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002540 DAG.getConstant(NVTBits-1, ShTy));
2541 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002542 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002543 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002544 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002545 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002546 } else if (Cst == NVTBits) {
2547 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002548 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002549 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002550 } else {
2551 Lo = DAG.getNode(ISD::OR, NVT,
2552 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2553 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2554 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2555 }
2556 return true;
2557 }
2558 }
2559 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2560 // so disable it for now. Currently targets are handling this via SHL_PARTS
2561 // and friends.
2562 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002563
2564 // If we have an efficient select operation (or if the selects will all fold
2565 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002566 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002567 return false;
2568
2569 SDOperand InL, InH;
2570 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002571 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2572 DAG.getConstant(NVTBits, ShTy), ShAmt);
2573
Chris Lattnere5544f82005-01-20 20:29:23 +00002574 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002575 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2576 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002577
Chris Lattnere34b3962005-01-19 04:19:40 +00002578 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2579 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2580 DAG.getConstant(NVTBits-1, ShTy));
2581 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2582 DAG.getConstant(NVTBits-1, ShTy));
2583 }
2584
2585 if (Opc == ISD::SHL) {
2586 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2587 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2588 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002589 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002590
Chris Lattnere34b3962005-01-19 04:19:40 +00002591 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2592 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2593 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002594 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002595 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2596 DAG.getConstant(32, ShTy),
2597 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002598 DAG.getConstant(0, NVT),
2599 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002600 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002601 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002602 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002603 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002604
2605 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002606 if (Opc == ISD::SRA)
2607 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2608 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002609 else
2610 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002611 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002612 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002613 }
2614 return true;
2615}
Chris Lattner77e77a62005-01-21 06:05:23 +00002616
Chris Lattner9530ddc2005-05-13 05:09:11 +00002617/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2618/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002619/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002620static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002621 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002622
Chris Lattner16cd04d2005-05-12 23:24:06 +00002623 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002624 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002625 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002626 Found = Node;
2627 return;
2628 }
2629
2630 // Otherwise, scan the operands of Node to see if any of them is a call.
2631 assert(Node->getNumOperands() != 0 &&
2632 "All leaves should have depth equal to the entry node!");
2633 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002634 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002635
2636 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002637 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002638 Found);
2639}
2640
2641
Chris Lattner9530ddc2005-05-13 05:09:11 +00002642/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2643/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002644/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002645static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2646 std::set<SDNode*> &Visited) {
2647 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2648 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002649
Chris Lattner16cd04d2005-05-12 23:24:06 +00002650 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002651 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002652 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002653 Found = Node;
2654 return;
2655 }
2656
2657 // Otherwise, scan the operands of Node to see if any of them is a call.
2658 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2659 if (UI == E) return;
2660 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002661 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002662
2663 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002664 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002665}
2666
Chris Lattner9530ddc2005-05-13 05:09:11 +00002667/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002668/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002669static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002670 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002671 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002672 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002673 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002674
2675 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002676 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002677
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002678 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002679 if (TheChain.getValueType() != MVT::Other)
2680 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002681 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002682
2683 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002684 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002685
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002686 // Make sure to only follow users of our token chain.
2687 SDNode *User = *UI;
2688 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2689 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002690 if (SDNode *Result = FindCallSeqEnd(User))
2691 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002692 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002693 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002694}
2695
Chris Lattner9530ddc2005-05-13 05:09:11 +00002696/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002697/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002698static SDNode *FindCallSeqStart(SDNode *Node) {
2699 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002700 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002701
2702 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2703 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002704 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002705}
2706
2707
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002708/// FindInputOutputChains - If we are replacing an operation with a call we need
2709/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002710/// properly serialize the calls in the block. The returned operand is the
2711/// input chain value for the new call (e.g. the entry node or the previous
2712/// call), and OutChain is set to be the chain node to update to point to the
2713/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002714static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2715 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002716 SDNode *LatestCallSeqStart = Entry.Val;
2717 SDNode *LatestCallSeqEnd = 0;
2718 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2719 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002720
Chris Lattner16cd04d2005-05-12 23:24:06 +00002721 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002722 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002723 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2724 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002725 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2726 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002727 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002728 LatestCallSeqEnd = Entry.Val;
2729 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002730
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002731 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002732 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002733 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002734 std::set<SDNode*> Visited;
2735 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002736
Chris Lattner9530ddc2005-05-13 05:09:11 +00002737 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002738 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002739 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002740
Chris Lattner9530ddc2005-05-13 05:09:11 +00002741 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002742}
2743
Jeff Cohen00b168892005-07-27 06:12:32 +00002744/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002745void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2746 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002747 // Nothing to splice it into?
2748 if (OutChain == 0) return;
2749
2750 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2751 //OutChain->dump();
2752
2753 // Form a token factor node merging the old inval and the new inval.
2754 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2755 OutChain->getOperand(0));
2756 // Change the node to refer to the new token.
2757 OutChain->setAdjCallChain(InToken);
2758}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002759
2760
Chris Lattner77e77a62005-01-21 06:05:23 +00002761// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2762// does not fit into a register, return the lo part and set the hi part to the
2763// by-reg argument. If it does fit into a single register, return the result
2764// and leave the Hi part unset.
2765SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2766 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002767 SDNode *OutChain;
2768 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2769 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002770 if (InChain.Val == 0)
2771 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002772
Chris Lattner77e77a62005-01-21 06:05:23 +00002773 TargetLowering::ArgListTy Args;
2774 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2775 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2776 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2777 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2778 }
2779 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002780
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002781 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002782 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002783 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002784 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2785 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002786
Chris Lattner99c25b82005-09-02 20:26:58 +00002787 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002788 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002789 default: assert(0 && "Unknown thing");
2790 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002791 Result = CallInfo.first;
2792 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002793 case Promote:
2794 assert(0 && "Cannot promote this yet!");
2795 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002796 ExpandOp(CallInfo.first, Result, Hi);
2797 CallInfo.second = LegalizeOp(CallInfo.second);
2798 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002799 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002800
2801 SpliceCallInto(CallInfo.second, OutChain);
2802 NeedsAnotherIteration = true;
2803 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002804}
2805
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002806
Chris Lattner77e77a62005-01-21 06:05:23 +00002807/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2808/// destination type is legal.
2809SDOperand SelectionDAGLegalize::
2810ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002811 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002812 assert(getTypeAction(Source.getValueType()) == Expand &&
2813 "This is not an expansion!");
2814 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2815
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002816 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002817 assert(Source.getValueType() == MVT::i64 &&
2818 "This only works for 64-bit -> FP");
2819 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2820 // incoming integer is set. To handle this, we dynamically test to see if
2821 // it is set, and, if so, add a fudge factor.
2822 SDOperand Lo, Hi;
2823 ExpandOp(Source, Lo, Hi);
2824
Chris Lattner66de05b2005-05-13 04:45:13 +00002825 // If this is unsigned, and not supported, first perform the conversion to
2826 // signed, then adjust the result if the sign bit is set.
2827 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2828 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2829
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002830 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2831 DAG.getConstant(0, Hi.getValueType()),
2832 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002833 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2834 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2835 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002836 uint64_t FF = 0x5f800000ULL;
2837 if (TLI.isLittleEndian()) FF <<= 32;
2838 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002839
Chris Lattner5839bf22005-08-26 17:15:30 +00002840 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002841 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2842 SDOperand FudgeInReg;
2843 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002844 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2845 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002846 else {
2847 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002848 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2849 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002850 }
Chris Lattner473a9902005-09-29 06:44:39 +00002851 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002852 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002853
Chris Lattnera88a2602005-05-14 05:33:54 +00002854 // Check to see if the target has a custom way to lower this. If so, use it.
2855 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2856 default: assert(0 && "This action not implemented for this operation!");
2857 case TargetLowering::Legal:
2858 case TargetLowering::Expand:
2859 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002860 case TargetLowering::Custom: {
2861 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2862 Source), DAG);
2863 if (NV.Val)
2864 return LegalizeOp(NV);
2865 break; // The target decided this was legal after all
2866 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002867 }
2868
Chris Lattner13689e22005-05-12 07:00:44 +00002869 // Expand the source, then glue it back together for the call. We must expand
2870 // the source in case it is shared (this pass of legalize must traverse it).
2871 SDOperand SrcLo, SrcHi;
2872 ExpandOp(Source, SrcLo, SrcHi);
2873 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2874
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002875 SDNode *OutChain = 0;
2876 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2877 DAG.getEntryNode());
2878 const char *FnName = 0;
2879 if (DestTy == MVT::f32)
2880 FnName = "__floatdisf";
2881 else {
2882 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2883 FnName = "__floatdidf";
2884 }
2885
Chris Lattner77e77a62005-01-21 06:05:23 +00002886 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2887
2888 TargetLowering::ArgListTy Args;
2889 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002890
Chris Lattner77e77a62005-01-21 06:05:23 +00002891 Args.push_back(std::make_pair(Source, ArgTy));
2892
2893 // We don't care about token chains for libcalls. We just use the entry
2894 // node as our input and ignore the output chain. This allows us to place
2895 // calls wherever we need them to satisfy data dependences.
2896 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002897
2898 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002899 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2900 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002901
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002902 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002903 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002904}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002905
Chris Lattnere34b3962005-01-19 04:19:40 +00002906
2907
Chris Lattner3e928bb2005-01-07 07:47:09 +00002908/// ExpandOp - Expand the specified SDOperand into its two component pieces
2909/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2910/// LegalizeNodes map is filled in for any results that are not expanded, the
2911/// ExpandedNodes map is filled in for any results that are expanded, and the
2912/// Lo/Hi values are returned.
2913void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2914 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002915 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002916 SDNode *Node = Op.Val;
2917 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2918 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2919 assert(MVT::isInteger(NVT) && NVT < VT &&
2920 "Cannot expand to FP value or to larger int value!");
2921
Chris Lattner6fdcb252005-09-02 20:32:45 +00002922 // See if we already expanded it.
2923 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2924 = ExpandedNodes.find(Op);
2925 if (I != ExpandedNodes.end()) {
2926 Lo = I->second.first;
2927 Hi = I->second.second;
2928 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002929 }
2930
Chris Lattner4e6c7462005-01-08 19:27:05 +00002931 // Expanding to multiple registers needs to perform an optimization step, and
2932 // is not careful to avoid operations the target does not support. Make sure
2933 // that all generated operations are legalized in the next iteration.
2934 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002935
Chris Lattner3e928bb2005-01-07 07:47:09 +00002936 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002937 case ISD::CopyFromReg:
2938 assert(0 && "CopyFromReg must be legal!");
2939 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002940 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2941 assert(0 && "Do not know how to expand this operator!");
2942 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002943 case ISD::UNDEF:
2944 Lo = DAG.getNode(ISD::UNDEF, NVT);
2945 Hi = DAG.getNode(ISD::UNDEF, NVT);
2946 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002947 case ISD::Constant: {
2948 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2949 Lo = DAG.getConstant(Cst, NVT);
2950 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2951 break;
2952 }
2953
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002954 case ISD::BUILD_PAIR:
2955 // Legalize both operands. FIXME: in the future we should handle the case
2956 // where the two elements are not legal.
2957 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2958 Lo = LegalizeOp(Node->getOperand(0));
2959 Hi = LegalizeOp(Node->getOperand(1));
2960 break;
2961
Chris Lattneredb1add2005-05-11 04:51:16 +00002962 case ISD::CTPOP:
2963 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002964 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2965 DAG.getNode(ISD::CTPOP, NVT, Lo),
2966 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002967 Hi = DAG.getConstant(0, NVT);
2968 break;
2969
Chris Lattner39a8f332005-05-12 19:05:01 +00002970 case ISD::CTLZ: {
2971 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002972 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002973 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2974 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002975 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2976 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002977 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2978 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2979
2980 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2981 Hi = DAG.getConstant(0, NVT);
2982 break;
2983 }
2984
2985 case ISD::CTTZ: {
2986 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002987 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002988 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2989 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002990 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2991 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002992 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2993 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2994
2995 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2996 Hi = DAG.getConstant(0, NVT);
2997 break;
2998 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002999
Chris Lattner3e928bb2005-01-07 07:47:09 +00003000 case ISD::LOAD: {
3001 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3002 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003003 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003004
3005 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003006 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003007 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3008 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003009 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003010 //are from the same instruction?
3011 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003012
3013 // Build a factor node to remember that this load is independent of the
3014 // other one.
3015 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3016 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003017
Chris Lattner3e928bb2005-01-07 07:47:09 +00003018 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003019 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003020 if (!TLI.isLittleEndian())
3021 std::swap(Lo, Hi);
3022 break;
3023 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003024 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003025 case ISD::CALL: {
3026 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3027 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3028
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003029 bool Changed = false;
3030 std::vector<SDOperand> Ops;
3031 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3032 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3033 Changed |= Ops.back() != Node->getOperand(i);
3034 }
3035
Chris Lattner3e928bb2005-01-07 07:47:09 +00003036 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3037 "Can only expand a call once so far, not i64 -> i16!");
3038
3039 std::vector<MVT::ValueType> RetTyVTs;
3040 RetTyVTs.reserve(3);
3041 RetTyVTs.push_back(NVT);
3042 RetTyVTs.push_back(NVT);
3043 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003044 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3045 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003046 Lo = SDOperand(NC, 0);
3047 Hi = SDOperand(NC, 1);
3048
3049 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003050 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003051 break;
3052 }
3053 case ISD::AND:
3054 case ISD::OR:
3055 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3056 SDOperand LL, LH, RL, RH;
3057 ExpandOp(Node->getOperand(0), LL, LH);
3058 ExpandOp(Node->getOperand(1), RL, RH);
3059 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3060 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3061 break;
3062 }
3063 case ISD::SELECT: {
3064 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003065
3066 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3067 case Expand: assert(0 && "It's impossible to expand bools");
3068 case Legal:
3069 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3070 break;
3071 case Promote:
3072 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3073 break;
3074 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003075 ExpandOp(Node->getOperand(1), LL, LH);
3076 ExpandOp(Node->getOperand(2), RL, RH);
3077 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3078 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3079 break;
3080 }
Nate Begeman9373a812005-08-10 20:51:12 +00003081 case ISD::SELECT_CC: {
3082 SDOperand TL, TH, FL, FH;
3083 ExpandOp(Node->getOperand(2), TL, TH);
3084 ExpandOp(Node->getOperand(3), FL, FH);
3085 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3086 Node->getOperand(1), TL, FL, Node->getOperand(4));
3087 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3088 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003089 Lo = LegalizeOp(Lo);
3090 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003091 break;
3092 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003093 case ISD::ANY_EXTEND: {
3094 SDOperand In;
3095 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3096 case Expand: assert(0 && "expand-expand not implemented yet!");
3097 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3098 case Promote:
3099 In = PromoteOp(Node->getOperand(0));
3100 break;
3101 }
3102
3103 // The low part is any extension of the input (which degenerates to a copy).
3104 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3105 // The high part is undefined.
3106 Hi = DAG.getNode(ISD::UNDEF, NVT);
3107 break;
3108 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003109 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003110 SDOperand In;
3111 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3112 case Expand: assert(0 && "expand-expand not implemented yet!");
3113 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3114 case Promote:
3115 In = PromoteOp(Node->getOperand(0));
3116 // Emit the appropriate sign_extend_inreg to get the value we want.
3117 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003118 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003119 break;
3120 }
3121
Chris Lattner3e928bb2005-01-07 07:47:09 +00003122 // The low part is just a sign extension of the input (which degenerates to
3123 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003124 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003125
Chris Lattner3e928bb2005-01-07 07:47:09 +00003126 // The high part is obtained by SRA'ing all but one of the bits of the lo
3127 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003128 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003129 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3130 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003131 break;
3132 }
Chris Lattner06098e02005-04-03 23:41:52 +00003133 case ISD::ZERO_EXTEND: {
3134 SDOperand In;
3135 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3136 case Expand: assert(0 && "expand-expand not implemented yet!");
3137 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3138 case Promote:
3139 In = PromoteOp(Node->getOperand(0));
3140 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003141 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003142 break;
3143 }
3144
Chris Lattner3e928bb2005-01-07 07:47:09 +00003145 // The low part is just a zero extension of the input (which degenerates to
3146 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003147 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003148
Chris Lattner3e928bb2005-01-07 07:47:09 +00003149 // The high part is just a zero.
3150 Hi = DAG.getConstant(0, NVT);
3151 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003152 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003153 // These operators cannot be expanded directly, emit them as calls to
3154 // library functions.
3155 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003156 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003157 SDOperand Op;
3158 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3159 case Expand: assert(0 && "cannot expand FP!");
3160 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3161 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3162 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003163
Chris Lattnerf20d1832005-07-30 01:40:57 +00003164 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3165
Chris Lattner80a3e942005-07-29 00:33:32 +00003166 // Now that the custom expander is done, expand the result, which is still
3167 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003168 if (Op.Val) {
3169 ExpandOp(Op, Lo, Hi);
3170 break;
3171 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003172 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003173
Chris Lattner4e6c7462005-01-08 19:27:05 +00003174 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003175 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003176 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003177 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003178 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003179
Chris Lattner4e6c7462005-01-08 19:27:05 +00003180 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003181 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3182 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3183 LegalizeOp(Node->getOperand(0)));
3184 // Now that the custom expander is done, expand the result, which is still
3185 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003186 Op = TLI.LowerOperation(Op, DAG);
3187 if (Op.Val) {
3188 ExpandOp(Op, Lo, Hi);
3189 break;
3190 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003191 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003192
Chris Lattner4e6c7462005-01-08 19:27:05 +00003193 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003194 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003195 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003196 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003197 break;
3198
Chris Lattnere34b3962005-01-19 04:19:40 +00003199 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003200 // If the target wants custom lowering, do so.
3201 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3202 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3203 LegalizeOp(Node->getOperand(1)));
3204 Op = TLI.LowerOperation(Op, DAG);
3205 if (Op.Val) {
3206 // Now that the custom expander is done, expand the result, which is
3207 // still VT.
3208 ExpandOp(Op, Lo, Hi);
3209 break;
3210 }
3211 }
3212
Chris Lattnere34b3962005-01-19 04:19:40 +00003213 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003214 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003215 break;
Chris Lattner47599822005-04-02 03:38:53 +00003216
3217 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003218 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003219 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3220 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003221 break;
3222 }
3223
Chris Lattnere34b3962005-01-19 04:19:40 +00003224 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003225 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003226 break;
3227
3228 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003229 // If the target wants custom lowering, do so.
3230 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3231 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3232 LegalizeOp(Node->getOperand(1)));
3233 Op = TLI.LowerOperation(Op, DAG);
3234 if (Op.Val) {
3235 // Now that the custom expander is done, expand the result, which is
3236 // still VT.
3237 ExpandOp(Op, Lo, Hi);
3238 break;
3239 }
3240 }
3241
Chris Lattnere34b3962005-01-19 04:19:40 +00003242 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003243 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003244 break;
Chris Lattner47599822005-04-02 03:38:53 +00003245
3246 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003247 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003248 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3249 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003250 break;
3251 }
3252
Chris Lattnere34b3962005-01-19 04:19:40 +00003253 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003254 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003255 break;
3256 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003257 // If the target wants custom lowering, do so.
3258 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3259 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3260 LegalizeOp(Node->getOperand(1)));
3261 Op = TLI.LowerOperation(Op, DAG);
3262 if (Op.Val) {
3263 // Now that the custom expander is done, expand the result, which is
3264 // still VT.
3265 ExpandOp(Op, Lo, Hi);
3266 break;
3267 }
3268 }
3269
Chris Lattnere34b3962005-01-19 04:19:40 +00003270 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003271 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003272 break;
Chris Lattner47599822005-04-02 03:38:53 +00003273
3274 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003275 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003276 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3277 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003278 break;
3279 }
3280
Chris Lattnere34b3962005-01-19 04:19:40 +00003281 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003282 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003283 break;
3284
Misha Brukmanedf128a2005-04-21 22:36:52 +00003285 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003286 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3287 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003288 break;
3289 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003290 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3291 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003292 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003293 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003294 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003295 SDOperand LL, LH, RL, RH;
3296 ExpandOp(Node->getOperand(0), LL, LH);
3297 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003298 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3299 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3300 // extended the sign bit of the low half through the upper half, and if so
3301 // emit a MULHS instead of the alternate sequence that is valid for any
3302 // i64 x i64 multiply.
3303 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3304 // is RH an extension of the sign bit of RL?
3305 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3306 RH.getOperand(1).getOpcode() == ISD::Constant &&
3307 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3308 // is LH an extension of the sign bit of LL?
3309 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3310 LH.getOperand(1).getOpcode() == ISD::Constant &&
3311 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3312 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3313 } else {
3314 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3315 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3316 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3317 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3318 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3319 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003320 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3321 } else {
3322 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3323 }
3324 break;
3325 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003326 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3327 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3328 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3329 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003330 }
3331
3332 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003333 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3334 std::make_pair(Lo, Hi))).second;
3335 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003336}
3337
3338
3339// SelectionDAG::Legalize - This is the entry point for the file.
3340//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003341void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003342 /// run - This is the main entry point to this class.
3343 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003344 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003345}
3346