blob: a59325e4f2f13a75abc624b32c3406cf05f0f41c [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);
Chris Lattnerab510a72005-10-02 17:49:46 +0000626 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000627 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000628
Chris Lattner16cd04d2005-05-12 23:24:06 +0000629 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000630 // nodes are treated specially and are mutated in place. This makes the dag
631 // legalization process more efficient and also makes libcall insertion
632 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000633 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000634 case ISD::DYNAMIC_STACKALLOC:
635 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
636 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
637 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
638 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000639 Tmp3 != Node->getOperand(2)) {
640 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
641 std::vector<SDOperand> Ops;
642 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
643 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
644 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000645 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000646
647 // Since this op produces two values, make sure to remember that we
648 // legalized both of them.
649 AddLegalizedOperand(SDOperand(Node, 0), Result);
650 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
651 return Result.getValue(Op.ResNo);
652
Chris Lattnerd71c0412005-05-13 18:43:43 +0000653 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000654 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
656 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000657
658 bool Changed = false;
659 std::vector<SDOperand> Ops;
660 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
661 Ops.push_back(LegalizeOp(Node->getOperand(i)));
662 Changed |= Ops.back() != Node->getOperand(i);
663 }
664
665 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000666 std::vector<MVT::ValueType> RetTyVTs;
667 RetTyVTs.reserve(Node->getNumValues());
668 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000669 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000670 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
671 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000672 } else {
673 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000674 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000675 // Since calls produce multiple values, make sure to remember that we
676 // legalized all of them.
677 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
678 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
679 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000680 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000681 case ISD::BR:
682 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
683 if (Tmp1 != Node->getOperand(0))
684 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
685 break;
686
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000687 case ISD::BRCOND:
688 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000689
Chris Lattner47e92232005-01-18 19:27:06 +0000690 switch (getTypeAction(Node->getOperand(1).getValueType())) {
691 case Expand: assert(0 && "It's impossible to expand bools");
692 case Legal:
693 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
694 break;
695 case Promote:
696 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
697 break;
698 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000699
700 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
701 default: assert(0 && "This action is not supported yet!");
702 case TargetLowering::Expand:
703 // Expand brcond's setcc into its constituent parts and create a BR_CC
704 // Node.
705 if (Tmp2.getOpcode() == ISD::SETCC) {
706 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
707 Tmp2.getOperand(0), Tmp2.getOperand(1),
708 Node->getOperand(2));
709 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000710 // Make sure the condition is either zero or one. It may have been
711 // promoted from something else.
712 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
713
Nate Begeman7cbd5252005-08-16 19:49:35 +0000714 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
715 DAG.getCondCode(ISD::SETNE), Tmp2,
716 DAG.getConstant(0, Tmp2.getValueType()),
717 Node->getOperand(2));
718 }
719 break;
720 case TargetLowering::Legal:
721 // Basic block destination (Op#2) is always legal.
722 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
723 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
724 Node->getOperand(2));
725 break;
726 }
727 break;
728 case ISD::BR_CC:
729 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
730
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000731 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000732 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
733 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
734 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
735 Tmp3 != Node->getOperand(3)) {
736 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
737 Tmp2, Tmp3, Node->getOperand(4));
738 }
739 break;
740 } else {
741 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
742 Node->getOperand(2), // LHS
743 Node->getOperand(3), // RHS
744 Node->getOperand(1)));
745 // If we get a SETCC back from legalizing the SETCC node we just
746 // created, then use its LHS, RHS, and CC directly in creating a new
747 // node. Otherwise, select between the true and false value based on
748 // comparing the result of the legalized with zero.
749 if (Tmp2.getOpcode() == ISD::SETCC) {
750 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
751 Tmp2.getOperand(0), Tmp2.getOperand(1),
752 Node->getOperand(4));
753 } else {
754 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
755 DAG.getCondCode(ISD::SETNE),
756 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
757 Node->getOperand(4));
758 }
759 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000760 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000761 case ISD::BRCONDTWOWAY:
762 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
763 switch (getTypeAction(Node->getOperand(1).getValueType())) {
764 case Expand: assert(0 && "It's impossible to expand bools");
765 case Legal:
766 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
767 break;
768 case Promote:
769 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
770 break;
771 }
772 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
773 // pair.
774 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
775 case TargetLowering::Promote:
776 default: assert(0 && "This action is not supported yet!");
777 case TargetLowering::Legal:
778 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
779 std::vector<SDOperand> Ops;
780 Ops.push_back(Tmp1);
781 Ops.push_back(Tmp2);
782 Ops.push_back(Node->getOperand(2));
783 Ops.push_back(Node->getOperand(3));
784 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
785 }
786 break;
787 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000788 // If BRTWOWAY_CC is legal for this target, then simply expand this node
789 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
790 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000791 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000792 if (Tmp2.getOpcode() == ISD::SETCC) {
793 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
794 Tmp2.getOperand(0), Tmp2.getOperand(1),
795 Node->getOperand(2), Node->getOperand(3));
796 } else {
797 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
798 DAG.getConstant(0, Tmp2.getValueType()),
799 Node->getOperand(2), Node->getOperand(3));
800 }
801 } else {
802 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000803 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000804 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
805 }
Chris Lattner411e8882005-04-09 03:30:19 +0000806 break;
807 }
808 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000809 case ISD::BRTWOWAY_CC:
810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000811 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000812 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
813 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
814 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
815 Tmp3 != Node->getOperand(3)) {
816 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
817 Node->getOperand(4), Node->getOperand(5));
818 }
819 break;
820 } else {
821 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
822 Node->getOperand(2), // LHS
823 Node->getOperand(3), // RHS
824 Node->getOperand(1)));
825 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
826 // pair.
827 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
828 default: assert(0 && "This action is not supported yet!");
829 case TargetLowering::Legal:
830 // If we get a SETCC back from legalizing the SETCC node we just
831 // created, then use its LHS, RHS, and CC directly in creating a new
832 // node. Otherwise, select between the true and false value based on
833 // comparing the result of the legalized with zero.
834 if (Tmp2.getOpcode() == ISD::SETCC) {
835 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
836 Tmp2.getOperand(0), Tmp2.getOperand(1),
837 Node->getOperand(4), Node->getOperand(5));
838 } else {
839 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
840 DAG.getConstant(0, Tmp2.getValueType()),
841 Node->getOperand(4), Node->getOperand(5));
842 }
843 break;
844 case TargetLowering::Expand:
845 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
846 Node->getOperand(4));
847 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
848 break;
849 }
850 }
851 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000852 case ISD::LOAD:
853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
854 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000855
Chris Lattner3e928bb2005-01-07 07:47:09 +0000856 if (Tmp1 != Node->getOperand(0) ||
857 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000858 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
859 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000860 else
861 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000862
Chris Lattner8afc48e2005-01-07 22:28:47 +0000863 // Since loads produce two values, make sure to remember that we legalized
864 // both of them.
865 AddLegalizedOperand(SDOperand(Node, 0), Result);
866 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
867 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000868
Chris Lattner0f69b292005-01-15 06:18:18 +0000869 case ISD::EXTLOAD:
870 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000871 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000872 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
873 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000874
Chris Lattner5f056bf2005-07-10 01:55:33 +0000875 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000876 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000877 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000878 case TargetLowering::Promote:
879 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000880 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
881 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000882 // Since loads produce two values, make sure to remember that we legalized
883 // both of them.
884 AddLegalizedOperand(SDOperand(Node, 0), Result);
885 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
886 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000887
Chris Lattner01ff7212005-04-10 22:54:25 +0000888 case TargetLowering::Legal:
889 if (Tmp1 != Node->getOperand(0) ||
890 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000891 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
892 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000893 else
894 Result = SDOperand(Node, 0);
895
896 // Since loads produce two values, make sure to remember that we legalized
897 // both of them.
898 AddLegalizedOperand(SDOperand(Node, 0), Result);
899 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
900 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000901 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000902 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
903 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
904 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000905 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000906 if (Op.ResNo)
907 return Load.getValue(1);
908 return Result;
909 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000910 assert(Node->getOpcode() != ISD::EXTLOAD &&
911 "EXTLOAD should always be supported!");
912 // Turn the unsupported load into an EXTLOAD followed by an explicit
913 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000914 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
915 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000916 SDOperand ValRes;
917 if (Node->getOpcode() == ISD::SEXTLOAD)
918 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000919 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000920 else
921 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000922 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
923 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
924 if (Op.ResNo)
925 return Result.getValue(1);
926 return ValRes;
927 }
928 assert(0 && "Unreachable");
929 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000930 case ISD::EXTRACT_ELEMENT:
931 // Get both the low and high parts.
932 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
933 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
934 Result = Tmp2; // 1 -> Hi
935 else
936 Result = Tmp1; // 0 -> Lo
937 break;
938
939 case ISD::CopyToReg:
940 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000941
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000942 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000943 "Register type must be legal!");
944 // Legalize the incoming value (must be legal).
945 Tmp2 = LegalizeOp(Node->getOperand(2));
946 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
947 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
948 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000949 break;
950
951 case ISD::RET:
952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
953 switch (Node->getNumOperands()) {
954 case 2: // ret val
955 switch (getTypeAction(Node->getOperand(1).getValueType())) {
956 case Legal:
957 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000958 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000959 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
960 break;
961 case Expand: {
962 SDOperand Lo, Hi;
963 ExpandOp(Node->getOperand(1), Lo, Hi);
964 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000965 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000966 }
967 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000968 Tmp2 = PromoteOp(Node->getOperand(1));
969 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
970 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000971 }
972 break;
973 case 1: // ret void
974 if (Tmp1 != Node->getOperand(0))
975 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
976 break;
977 default: { // ret <values>
978 std::vector<SDOperand> NewValues;
979 NewValues.push_back(Tmp1);
980 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
981 switch (getTypeAction(Node->getOperand(i).getValueType())) {
982 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000983 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000984 break;
985 case Expand: {
986 SDOperand Lo, Hi;
987 ExpandOp(Node->getOperand(i), Lo, Hi);
988 NewValues.push_back(Lo);
989 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000990 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000991 }
992 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000993 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000994 }
995 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
996 break;
997 }
998 }
999 break;
1000 case ISD::STORE:
1001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1002 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1003
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001004 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001005 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001006 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001007 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001008 DAG.getConstant(FloatToBits(CFP->getValue()),
1009 MVT::i32),
1010 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001011 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001012 } else {
1013 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001014 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001015 DAG.getConstant(DoubleToBits(CFP->getValue()),
1016 MVT::i64),
1017 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001018 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001019 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001020 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001021 }
1022
Chris Lattner3e928bb2005-01-07 07:47:09 +00001023 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1024 case Legal: {
1025 SDOperand Val = LegalizeOp(Node->getOperand(1));
1026 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1027 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001028 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1029 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001030 break;
1031 }
1032 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001033 // Truncate the value and store the result.
1034 Tmp3 = PromoteOp(Node->getOperand(1));
1035 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001036 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001037 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001038 break;
1039
Chris Lattner3e928bb2005-01-07 07:47:09 +00001040 case Expand:
1041 SDOperand Lo, Hi;
1042 ExpandOp(Node->getOperand(1), Lo, Hi);
1043
1044 if (!TLI.isLittleEndian())
1045 std::swap(Lo, Hi);
1046
Chris Lattneredb1add2005-05-11 04:51:16 +00001047 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1048 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001049 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001050 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1051 getIntPtrConstant(IncrementSize));
1052 assert(isTypeLegal(Tmp2.getValueType()) &&
1053 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001054 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001055 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1056 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001057 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1058 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001059 }
1060 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001061 case ISD::PCMARKER:
1062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001063 if (Tmp1 != Node->getOperand(0))
1064 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001065 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001066 case ISD::TRUNCSTORE:
1067 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1068 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1069
1070 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1071 case Legal:
1072 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001073
1074 // The only promote case we handle is TRUNCSTORE:i1 X into
1075 // -> TRUNCSTORE:i8 (and X, 1)
1076 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1077 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1078 TargetLowering::Promote) {
1079 // Promote the bool to a mask then store.
1080 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1081 DAG.getConstant(1, Tmp2.getValueType()));
1082 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1083 Node->getOperand(3), DAG.getValueType(MVT::i8));
1084
1085 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1086 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001087 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001088 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001089 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001090 break;
1091 case Promote:
1092 case Expand:
1093 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1094 }
1095 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001096 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1098 case Expand: assert(0 && "It's impossible to expand bools");
1099 case Legal:
1100 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1101 break;
1102 case Promote:
1103 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1104 break;
1105 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001106 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001107 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001108
Nate Begemanb942a3d2005-08-23 04:29:48 +00001109 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001110 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001111 case TargetLowering::Expand:
1112 if (Tmp1.getOpcode() == ISD::SETCC) {
1113 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1114 Tmp2, Tmp3,
1115 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1116 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001117 // Make sure the condition is either zero or one. It may have been
1118 // promoted from something else.
1119 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001120 Result = DAG.getSelectCC(Tmp1,
1121 DAG.getConstant(0, Tmp1.getValueType()),
1122 Tmp2, Tmp3, ISD::SETNE);
1123 }
1124 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001125 case TargetLowering::Legal:
1126 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1127 Tmp3 != Node->getOperand(2))
1128 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1129 Tmp1, Tmp2, Tmp3);
1130 break;
1131 case TargetLowering::Promote: {
1132 MVT::ValueType NVT =
1133 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1134 unsigned ExtOp, TruncOp;
1135 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001136 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001137 TruncOp = ISD::TRUNCATE;
1138 } else {
1139 ExtOp = ISD::FP_EXTEND;
1140 TruncOp = ISD::FP_ROUND;
1141 }
1142 // Promote each of the values to the new type.
1143 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1144 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1145 // Perform the larger operation, then round down.
1146 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1147 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1148 break;
1149 }
1150 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001151 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001152 case ISD::SELECT_CC:
1153 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1154 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1155
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001156 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001157 // Everything is legal, see if we should expand this op or something.
1158 switch (TLI.getOperationAction(ISD::SELECT_CC,
1159 Node->getOperand(0).getValueType())) {
1160 default: assert(0 && "This action is not supported yet!");
1161 case TargetLowering::Custom: {
1162 SDOperand Tmp =
1163 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1164 Node->getOperand(0),
1165 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001166 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001167 if (Tmp.Val) {
1168 Result = LegalizeOp(Tmp);
1169 break;
1170 }
1171 } // FALLTHROUGH if the target can't lower this operation after all.
1172 case TargetLowering::Legal:
1173 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1174 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1175 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1176 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1177 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1178 Tmp3, Tmp4, Node->getOperand(4));
1179 }
1180 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001181 }
1182 break;
1183 } else {
1184 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1185 Node->getOperand(0), // LHS
1186 Node->getOperand(1), // RHS
1187 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001188 // If we get a SETCC back from legalizing the SETCC node we just
1189 // created, then use its LHS, RHS, and CC directly in creating a new
1190 // node. Otherwise, select between the true and false value based on
1191 // comparing the result of the legalized with zero.
1192 if (Tmp1.getOpcode() == ISD::SETCC) {
1193 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1194 Tmp1.getOperand(0), Tmp1.getOperand(1),
1195 Tmp3, Tmp4, Tmp1.getOperand(2));
1196 } else {
1197 Result = DAG.getSelectCC(Tmp1,
1198 DAG.getConstant(0, Tmp1.getValueType()),
1199 Tmp3, Tmp4, ISD::SETNE);
1200 }
Nate Begeman9373a812005-08-10 20:51:12 +00001201 }
1202 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001203 case ISD::SETCC:
1204 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1205 case Legal:
1206 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1207 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001208 break;
1209 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001210 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1211 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1212
1213 // If this is an FP compare, the operands have already been extended.
1214 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1215 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001216 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001217
1218 // Otherwise, we have to insert explicit sign or zero extends. Note
1219 // that we could insert sign extends for ALL conditions, but zero extend
1220 // is cheaper on many machines (an AND instead of two shifts), so prefer
1221 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001222 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001223 default: assert(0 && "Unknown integer comparison!");
1224 case ISD::SETEQ:
1225 case ISD::SETNE:
1226 case ISD::SETUGE:
1227 case ISD::SETUGT:
1228 case ISD::SETULE:
1229 case ISD::SETULT:
1230 // ALL of these operations will work if we either sign or zero extend
1231 // the operands (including the unsigned comparisons!). Zero extend is
1232 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001233 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1234 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001235 break;
1236 case ISD::SETGE:
1237 case ISD::SETGT:
1238 case ISD::SETLT:
1239 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001240 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1241 DAG.getValueType(VT));
1242 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1243 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001244 break;
1245 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001246 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001247 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001248 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001249 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1250 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1251 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001252 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001253 case ISD::SETEQ:
1254 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001255 if (RHSLo == RHSHi)
1256 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1257 if (RHSCST->isAllOnesValue()) {
1258 // Comparison to -1.
1259 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001260 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001261 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001262 }
1263
Chris Lattner3e928bb2005-01-07 07:47:09 +00001264 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1265 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1266 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001267 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001268 break;
1269 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001270 // If this is a comparison of the sign bit, just look at the top part.
1271 // X > -1, x < 0
1272 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001273 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001274 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001275 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001276 (CST->isAllOnesValue()))) { // X > -1
1277 Tmp1 = LHSHi;
1278 Tmp2 = RHSHi;
1279 break;
1280 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001281
Chris Lattner3e928bb2005-01-07 07:47:09 +00001282 // FIXME: This generated code sucks.
1283 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001284 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001285 default: assert(0 && "Unknown integer setcc!");
1286 case ISD::SETLT:
1287 case ISD::SETULT: LowCC = ISD::SETULT; break;
1288 case ISD::SETGT:
1289 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1290 case ISD::SETLE:
1291 case ISD::SETULE: LowCC = ISD::SETULE; break;
1292 case ISD::SETGE:
1293 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1294 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001295
Chris Lattner3e928bb2005-01-07 07:47:09 +00001296 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1297 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1298 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1299
1300 // NOTE: on targets without efficient SELECT of bools, we can always use
1301 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001302 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1303 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1304 Node->getOperand(2));
1305 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001306 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1307 Result, Tmp1, Tmp2));
1308 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001309 }
1310 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001311
1312 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1313 default:
1314 assert(0 && "Cannot handle this action for SETCC yet!");
1315 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001316 case TargetLowering::Promote:
1317 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1318 Node->getOperand(2));
1319 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001320 case TargetLowering::Legal:
1321 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1322 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1323 Node->getOperand(2));
1324 break;
1325 case TargetLowering::Expand:
1326 // Expand a setcc node into a select_cc of the same condition, lhs, and
1327 // rhs that selects between const 1 (true) and const 0 (false).
1328 MVT::ValueType VT = Node->getValueType(0);
1329 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1330 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1331 Node->getOperand(2));
1332 Result = LegalizeOp(Result);
1333 break;
1334 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001335 break;
1336
Chris Lattnere1bd8222005-01-11 05:57:22 +00001337 case ISD::MEMSET:
1338 case ISD::MEMCPY:
1339 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001341 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1342
1343 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1344 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1345 case Expand: assert(0 && "Cannot expand a byte!");
1346 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001347 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001348 break;
1349 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001350 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001351 break;
1352 }
1353 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001354 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001355 }
Chris Lattner272455b2005-02-02 03:44:41 +00001356
1357 SDOperand Tmp4;
1358 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001359 case Expand: {
1360 // Length is too big, just take the lo-part of the length.
1361 SDOperand HiPart;
1362 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1363 break;
1364 }
Chris Lattnere5605212005-01-28 22:29:18 +00001365 case Legal:
1366 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001367 break;
1368 case Promote:
1369 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001370 break;
1371 }
1372
1373 SDOperand Tmp5;
1374 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1375 case Expand: assert(0 && "Cannot expand this yet!");
1376 case Legal:
1377 Tmp5 = LegalizeOp(Node->getOperand(4));
1378 break;
1379 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001380 Tmp5 = PromoteOp(Node->getOperand(4));
1381 break;
1382 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001383
1384 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1385 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001386 case TargetLowering::Custom: {
1387 SDOperand Tmp =
1388 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1389 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1390 if (Tmp.Val) {
1391 Result = LegalizeOp(Tmp);
1392 break;
1393 }
1394 // FALLTHROUGH if the target thinks it is legal.
1395 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001396 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001397 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1398 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1399 Tmp5 != Node->getOperand(4)) {
1400 std::vector<SDOperand> Ops;
1401 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1402 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1403 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1404 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001405 break;
1406 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001407 // Otherwise, the target does not support this operation. Lower the
1408 // operation to an explicit libcall as appropriate.
1409 MVT::ValueType IntPtr = TLI.getPointerTy();
1410 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1411 std::vector<std::pair<SDOperand, const Type*> > Args;
1412
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001413 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001414 if (Node->getOpcode() == ISD::MEMSET) {
1415 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1416 // Extend the ubyte argument to be an int value for the call.
1417 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1418 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1419 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1420
1421 FnName = "memset";
1422 } else if (Node->getOpcode() == ISD::MEMCPY ||
1423 Node->getOpcode() == ISD::MEMMOVE) {
1424 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1425 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1426 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1427 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1428 } else {
1429 assert(0 && "Unknown op!");
1430 }
Chris Lattner45982da2005-05-12 16:53:42 +00001431
Chris Lattnere1bd8222005-01-11 05:57:22 +00001432 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001433 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001434 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001435 Result = CallResult.second;
1436 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001437 break;
1438 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001439 }
1440 break;
1441 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001442
1443 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001444 Tmp1 = LegalizeOp(Node->getOperand(0));
1445 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001446
Chris Lattner3e011362005-05-14 07:45:46 +00001447 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1448 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1449 std::vector<SDOperand> Ops;
1450 Ops.push_back(Tmp1);
1451 Ops.push_back(Tmp2);
1452 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1453 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001454 Result = SDOperand(Node, 0);
1455 // Since these produce two values, make sure to remember that we legalized
1456 // both of them.
1457 AddLegalizedOperand(SDOperand(Node, 0), Result);
1458 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1459 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001460 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001461 Tmp1 = LegalizeOp(Node->getOperand(0));
1462 Tmp2 = LegalizeOp(Node->getOperand(1));
1463 Tmp3 = LegalizeOp(Node->getOperand(2));
1464 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1465 Tmp3 != Node->getOperand(2))
1466 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1467 break;
1468
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001469 case ISD::READIO:
1470 Tmp1 = LegalizeOp(Node->getOperand(0));
1471 Tmp2 = LegalizeOp(Node->getOperand(1));
1472
1473 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1474 case TargetLowering::Custom:
1475 default: assert(0 && "This action not implemented for this operation!");
1476 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001477 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1478 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1479 std::vector<SDOperand> Ops;
1480 Ops.push_back(Tmp1);
1481 Ops.push_back(Tmp2);
1482 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1483 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001484 Result = SDOperand(Node, 0);
1485 break;
1486 case TargetLowering::Expand:
1487 // Replace this with a load from memory.
1488 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1489 Node->getOperand(1), DAG.getSrcValue(NULL));
1490 Result = LegalizeOp(Result);
1491 break;
1492 }
1493
1494 // Since these produce two values, make sure to remember that we legalized
1495 // both of them.
1496 AddLegalizedOperand(SDOperand(Node, 0), Result);
1497 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1498 return Result.getValue(Op.ResNo);
1499
1500 case ISD::WRITEIO:
1501 Tmp1 = LegalizeOp(Node->getOperand(0));
1502 Tmp2 = LegalizeOp(Node->getOperand(1));
1503 Tmp3 = LegalizeOp(Node->getOperand(2));
1504
1505 switch (TLI.getOperationAction(Node->getOpcode(),
1506 Node->getOperand(1).getValueType())) {
1507 case TargetLowering::Custom:
1508 default: assert(0 && "This action not implemented for this operation!");
1509 case TargetLowering::Legal:
1510 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1511 Tmp3 != Node->getOperand(2))
1512 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1513 break;
1514 case TargetLowering::Expand:
1515 // Replace this with a store to memory.
1516 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1517 Node->getOperand(1), Node->getOperand(2),
1518 DAG.getSrcValue(NULL));
1519 Result = LegalizeOp(Result);
1520 break;
1521 }
1522 break;
1523
Chris Lattner84f67882005-01-20 18:52:28 +00001524 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001525 case ISD::SUB_PARTS:
1526 case ISD::SHL_PARTS:
1527 case ISD::SRA_PARTS:
1528 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001529 std::vector<SDOperand> Ops;
1530 bool Changed = false;
1531 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1532 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1533 Changed |= Ops.back() != Node->getOperand(i);
1534 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001535 if (Changed) {
1536 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1537 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1538 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001539
1540 // Since these produce multiple values, make sure to remember that we
1541 // legalized all of them.
1542 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1543 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1544 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001545 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001546
1547 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001548 case ISD::ADD:
1549 case ISD::SUB:
1550 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001551 case ISD::MULHS:
1552 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001553 case ISD::UDIV:
1554 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001555 case ISD::AND:
1556 case ISD::OR:
1557 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001558 case ISD::SHL:
1559 case ISD::SRL:
1560 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001561 case ISD::FADD:
1562 case ISD::FSUB:
1563 case ISD::FMUL:
1564 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001565 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001566 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1567 case Expand: assert(0 && "Not possible");
1568 case Legal:
1569 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1570 break;
1571 case Promote:
1572 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1573 break;
1574 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001575 if (Tmp1 != Node->getOperand(0) ||
1576 Tmp2 != Node->getOperand(1))
1577 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1578 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001579
Nate Begemanc105e192005-04-06 00:23:54 +00001580 case ISD::UREM:
1581 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001582 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001583 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1584 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1585 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1586 case TargetLowering::Legal:
1587 if (Tmp1 != Node->getOperand(0) ||
1588 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001589 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001590 Tmp2);
1591 break;
1592 case TargetLowering::Promote:
1593 case TargetLowering::Custom:
1594 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001595 case TargetLowering::Expand:
1596 if (MVT::isInteger(Node->getValueType(0))) {
1597 MVT::ValueType VT = Node->getValueType(0);
1598 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1599 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1600 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1601 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1602 } else {
1603 // Floating point mod -> fmod libcall.
1604 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1605 SDOperand Dummy;
1606 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001607 }
1608 break;
1609 }
1610 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001611
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001612 case ISD::CTPOP:
1613 case ISD::CTTZ:
1614 case ISD::CTLZ:
1615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1616 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1617 case TargetLowering::Legal:
1618 if (Tmp1 != Node->getOperand(0))
1619 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1620 break;
1621 case TargetLowering::Promote: {
1622 MVT::ValueType OVT = Tmp1.getValueType();
1623 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001624
1625 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001626 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1627 // Perform the larger operation, then subtract if needed.
1628 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1629 switch(Node->getOpcode())
1630 {
1631 case ISD::CTPOP:
1632 Result = Tmp1;
1633 break;
1634 case ISD::CTTZ:
1635 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001636 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1637 DAG.getConstant(getSizeInBits(NVT), NVT),
1638 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001639 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001640 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1641 break;
1642 case ISD::CTLZ:
1643 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001644 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1645 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001646 getSizeInBits(OVT), NVT));
1647 break;
1648 }
1649 break;
1650 }
1651 case TargetLowering::Custom:
1652 assert(0 && "Cannot custom handle this yet!");
1653 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001654 switch(Node->getOpcode())
1655 {
1656 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001657 static const uint64_t mask[6] = {
1658 0x5555555555555555ULL, 0x3333333333333333ULL,
1659 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1660 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1661 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001662 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001663 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1664 unsigned len = getSizeInBits(VT);
1665 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001666 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001667 Tmp2 = DAG.getConstant(mask[i], VT);
1668 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001669 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001670 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1671 DAG.getNode(ISD::AND, VT,
1672 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1673 Tmp2));
1674 }
1675 Result = Tmp1;
1676 break;
1677 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001678 case ISD::CTLZ: {
1679 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001680 x = x | (x >> 1);
1681 x = x | (x >> 2);
1682 ...
1683 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001684 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001685 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001686
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001687 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1688 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001689 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1690 unsigned len = getSizeInBits(VT);
1691 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1692 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001693 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001694 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1695 }
1696 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001697 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001698 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001699 }
1700 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001701 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001702 // unless the target has ctlz but not ctpop, in which case we use:
1703 // { return 32 - nlz(~x & (x-1)); }
1704 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001705 MVT::ValueType VT = Tmp1.getValueType();
1706 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001707 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001708 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1709 DAG.getNode(ISD::SUB, VT, Tmp1,
1710 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001711 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001712 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1713 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001714 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001715 DAG.getConstant(getSizeInBits(VT), VT),
1716 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1717 } else {
1718 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1719 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001720 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001721 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001722 default:
1723 assert(0 && "Cannot expand this yet!");
1724 break;
1725 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001726 break;
1727 }
1728 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001729
Chris Lattner2c8086f2005-04-02 05:00:07 +00001730 // Unary operators
1731 case ISD::FABS:
1732 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001733 case ISD::FSQRT:
1734 case ISD::FSIN:
1735 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001736 Tmp1 = LegalizeOp(Node->getOperand(0));
1737 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1738 case TargetLowering::Legal:
1739 if (Tmp1 != Node->getOperand(0))
1740 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1741 break;
1742 case TargetLowering::Promote:
1743 case TargetLowering::Custom:
1744 assert(0 && "Cannot promote/custom handle this yet!");
1745 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001746 switch(Node->getOpcode()) {
1747 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001748 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1749 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001750 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00001751 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001752 break;
1753 }
1754 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001755 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1756 MVT::ValueType VT = Node->getValueType(0);
1757 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001758 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001759 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1760 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1761 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001762 break;
1763 }
1764 case ISD::FSQRT:
1765 case ISD::FSIN:
1766 case ISD::FCOS: {
1767 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001768 const char *FnName = 0;
1769 switch(Node->getOpcode()) {
1770 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1771 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1772 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1773 default: assert(0 && "Unreachable!");
1774 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001775 SDOperand Dummy;
1776 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001777 break;
1778 }
1779 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001780 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001781 }
1782 break;
1783 }
1784 break;
1785
1786 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001787 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001788 case ISD::UINT_TO_FP: {
1789 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001790 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1791 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001792 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001793 Node->getOperand(0).getValueType())) {
1794 default: assert(0 && "Unknown operation action!");
1795 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001796 Result = ExpandLegalINT_TO_FP(isSigned,
1797 LegalizeOp(Node->getOperand(0)),
1798 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001799 AddLegalizedOperand(Op, Result);
1800 return Result;
1801 case TargetLowering::Promote:
1802 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1803 Node->getValueType(0),
1804 isSigned);
1805 AddLegalizedOperand(Op, Result);
1806 return Result;
1807 case TargetLowering::Legal:
1808 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001809 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001810
Chris Lattner3e928bb2005-01-07 07:47:09 +00001811 Tmp1 = LegalizeOp(Node->getOperand(0));
1812 if (Tmp1 != Node->getOperand(0))
1813 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1814 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001815 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001816 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1817 Node->getValueType(0), Node->getOperand(0));
1818 break;
1819 case Promote:
1820 if (isSigned) {
1821 Result = PromoteOp(Node->getOperand(0));
1822 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1823 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1824 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1825 } else {
1826 Result = PromoteOp(Node->getOperand(0));
1827 Result = DAG.getZeroExtendInReg(Result,
1828 Node->getOperand(0).getValueType());
1829 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001830 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001831 break;
1832 }
1833 break;
1834 }
1835 case ISD::TRUNCATE:
1836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1837 case Legal:
1838 Tmp1 = LegalizeOp(Node->getOperand(0));
1839 if (Tmp1 != Node->getOperand(0))
1840 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1841 break;
1842 case Expand:
1843 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1844
1845 // Since the result is legal, we should just be able to truncate the low
1846 // part of the source.
1847 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1848 break;
1849 case Promote:
1850 Result = PromoteOp(Node->getOperand(0));
1851 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1852 break;
1853 }
1854 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001855
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001856 case ISD::FP_TO_SINT:
1857 case ISD::FP_TO_UINT:
1858 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1859 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001860 Tmp1 = LegalizeOp(Node->getOperand(0));
1861
Chris Lattner1618beb2005-07-29 00:11:56 +00001862 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1863 default: assert(0 && "Unknown operation action!");
1864 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001865 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1866 SDOperand True, False;
1867 MVT::ValueType VT = Node->getOperand(0).getValueType();
1868 MVT::ValueType NVT = Node->getValueType(0);
1869 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1870 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1871 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1872 Node->getOperand(0), Tmp2, ISD::SETLT);
1873 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1874 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00001875 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00001876 Tmp2));
1877 False = DAG.getNode(ISD::XOR, NVT, False,
1878 DAG.getConstant(1ULL << ShiftAmt, NVT));
1879 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001880 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001881 } else {
1882 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1883 }
1884 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001885 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001886 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001887 Node->getOpcode() == ISD::FP_TO_SINT);
1888 AddLegalizedOperand(Op, Result);
1889 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001890 case TargetLowering::Custom: {
1891 SDOperand Tmp =
1892 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1893 Tmp = TLI.LowerOperation(Tmp, DAG);
1894 if (Tmp.Val) {
1895 AddLegalizedOperand(Op, Tmp);
1896 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001897 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001898 } else {
1899 // The target thinks this is legal afterall.
1900 break;
1901 }
1902 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001903 case TargetLowering::Legal:
1904 break;
1905 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001906
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001907 if (Tmp1 != Node->getOperand(0))
1908 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1909 break;
1910 case Expand:
1911 assert(0 && "Shouldn't need to expand other operators here!");
1912 case Promote:
1913 Result = PromoteOp(Node->getOperand(0));
1914 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1915 break;
1916 }
1917 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001918
Chris Lattner13c78e22005-09-02 00:18:10 +00001919 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001920 case ISD::ZERO_EXTEND:
1921 case ISD::SIGN_EXTEND:
1922 case ISD::FP_EXTEND:
1923 case ISD::FP_ROUND:
1924 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1925 case Legal:
1926 Tmp1 = LegalizeOp(Node->getOperand(0));
1927 if (Tmp1 != Node->getOperand(0))
1928 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1929 break;
1930 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001931 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001932
Chris Lattner03c85462005-01-15 05:21:40 +00001933 case Promote:
1934 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001935 case ISD::ANY_EXTEND:
1936 Result = PromoteOp(Node->getOperand(0));
1937 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
1938 break;
Chris Lattner1713e732005-01-16 00:38:00 +00001939 case ISD::ZERO_EXTEND:
1940 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001941 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001942 Result = DAG.getZeroExtendInReg(Result,
1943 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001944 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001945 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001946 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00001947 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001948 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001949 Result,
1950 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001951 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001952 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001953 Result = PromoteOp(Node->getOperand(0));
1954 if (Result.getValueType() != Op.getValueType())
1955 // Dynamically dead while we have only 2 FP types.
1956 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1957 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001958 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001959 Result = PromoteOp(Node->getOperand(0));
1960 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1961 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001962 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001963 }
1964 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001965 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001966 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001967 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001968 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001969
1970 // If this operation is not supported, convert it to a shl/shr or load/store
1971 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001972 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1973 default: assert(0 && "This action not supported for this op yet!");
1974 case TargetLowering::Legal:
1975 if (Tmp1 != Node->getOperand(0))
1976 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001977 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001978 break;
1979 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001980 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001981 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001982 // NOTE: we could fall back on load/store here too for targets without
1983 // SAR. However, it is doubtful that any exist.
1984 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1985 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001986 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001987 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1988 Node->getOperand(0), ShiftCst);
1989 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1990 Result, ShiftCst);
1991 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1992 // The only way we can lower this is to turn it into a STORETRUNC,
1993 // EXTLOAD pair, targetting a temporary location (a stack slot).
1994
1995 // NOTE: there is a choice here between constantly creating new stack
1996 // slots and always reusing the same one. We currently always create
1997 // new ones, as reuse may inhibit scheduling.
1998 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1999 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2000 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2001 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002002 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002003 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2004 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2005 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002006 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002007 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002008 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2009 Result, StackSlot, DAG.getSrcValue(NULL),
2010 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002011 } else {
2012 assert(0 && "Unknown op");
2013 }
2014 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002015 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002016 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002017 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002018 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002019 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002020
Chris Lattner45982da2005-05-12 16:53:42 +00002021 // Note that LegalizeOp may be reentered even from single-use nodes, which
2022 // means that we always must cache transformed nodes.
2023 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002024 return Result;
2025}
2026
Chris Lattner8b6fa222005-01-15 22:16:26 +00002027/// PromoteOp - Given an operation that produces a value in an invalid type,
2028/// promote it to compute the value into a larger type. The produced value will
2029/// have the correct bits for the low portion of the register, but no guarantee
2030/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002031SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2032 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002033 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002034 assert(getTypeAction(VT) == Promote &&
2035 "Caller should expand or legalize operands that are not promotable!");
2036 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2037 "Cannot promote to smaller type!");
2038
Chris Lattner03c85462005-01-15 05:21:40 +00002039 SDOperand Tmp1, Tmp2, Tmp3;
2040
2041 SDOperand Result;
2042 SDNode *Node = Op.Val;
2043
Chris Lattner6fdcb252005-09-02 20:32:45 +00002044 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2045 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002046
Chris Lattner0f69b292005-01-15 06:18:18 +00002047 // Promotion needs an optimization step to clean up after it, and is not
2048 // careful to avoid operations the target does not support. Make sure that
2049 // all generated operations are legalized in the next iteration.
2050 NeedsAnotherIteration = true;
2051
Chris Lattner03c85462005-01-15 05:21:40 +00002052 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002053 case ISD::CopyFromReg:
2054 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002055 default:
2056 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2057 assert(0 && "Do not know how to promote this operator!");
2058 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002059 case ISD::UNDEF:
2060 Result = DAG.getNode(ISD::UNDEF, NVT);
2061 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002062 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002063 if (VT != MVT::i1)
2064 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2065 else
2066 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002067 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2068 break;
2069 case ISD::ConstantFP:
2070 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2071 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2072 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002073
Chris Lattner82fbfb62005-01-18 02:59:52 +00002074 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002075 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002076 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2077 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002078 Result = LegalizeOp(Result);
2079 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002080
2081 case ISD::TRUNCATE:
2082 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2083 case Legal:
2084 Result = LegalizeOp(Node->getOperand(0));
2085 assert(Result.getValueType() >= NVT &&
2086 "This truncation doesn't make sense!");
2087 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2088 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2089 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002090 case Promote:
2091 // The truncation is not required, because we don't guarantee anything
2092 // about high bits anyway.
2093 Result = PromoteOp(Node->getOperand(0));
2094 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002095 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002096 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2097 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002098 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002099 }
2100 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002101 case ISD::SIGN_EXTEND:
2102 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002103 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002104 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2105 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2106 case Legal:
2107 // Input is legal? Just do extend all the way to the larger type.
2108 Result = LegalizeOp(Node->getOperand(0));
2109 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2110 break;
2111 case Promote:
2112 // Promote the reg if it's smaller.
2113 Result = PromoteOp(Node->getOperand(0));
2114 // The high bits are not guaranteed to be anything. Insert an extend.
2115 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002116 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002117 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002118 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002119 Result = DAG.getZeroExtendInReg(Result,
2120 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002121 break;
2122 }
2123 break;
2124
2125 case ISD::FP_EXTEND:
2126 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2127 case ISD::FP_ROUND:
2128 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2129 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2130 case Promote: assert(0 && "Unreachable with 2 FP types!");
2131 case Legal:
2132 // Input is legal? Do an FP_ROUND_INREG.
2133 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002134 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2135 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002136 break;
2137 }
2138 break;
2139
2140 case ISD::SINT_TO_FP:
2141 case ISD::UINT_TO_FP:
2142 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2143 case Legal:
2144 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002145 // No extra round required here.
2146 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002147 break;
2148
2149 case Promote:
2150 Result = PromoteOp(Node->getOperand(0));
2151 if (Node->getOpcode() == ISD::SINT_TO_FP)
2152 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002153 Result,
2154 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002155 else
Chris Lattner23993562005-04-13 02:38:47 +00002156 Result = DAG.getZeroExtendInReg(Result,
2157 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002158 // No extra round required here.
2159 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002160 break;
2161 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002162 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2163 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002164 // Round if we cannot tolerate excess precision.
2165 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002166 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2167 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002168 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002169 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002170 break;
2171
2172 case ISD::FP_TO_SINT:
2173 case ISD::FP_TO_UINT:
2174 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2175 case Legal:
2176 Tmp1 = LegalizeOp(Node->getOperand(0));
2177 break;
2178 case Promote:
2179 // The input result is prerounded, so we don't have to do anything
2180 // special.
2181 Tmp1 = PromoteOp(Node->getOperand(0));
2182 break;
2183 case Expand:
2184 assert(0 && "not implemented");
2185 }
Nate Begemand2558e32005-08-14 01:20:53 +00002186 // If we're promoting a UINT to a larger size, check to see if the new node
2187 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2188 // we can use that instead. This allows us to generate better code for
2189 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2190 // legal, such as PowerPC.
2191 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002192 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2193 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begemand2558e32005-08-14 01:20:53 +00002194 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2195 } else {
2196 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2197 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002198 break;
2199
Chris Lattner2c8086f2005-04-02 05:00:07 +00002200 case ISD::FABS:
2201 case ISD::FNEG:
2202 Tmp1 = PromoteOp(Node->getOperand(0));
2203 assert(Tmp1.getValueType() == NVT);
2204 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2205 // NOTE: we do not have to do any extra rounding here for
2206 // NoExcessFPPrecision, because we know the input will have the appropriate
2207 // precision, and these operations don't modify precision at all.
2208 break;
2209
Chris Lattnerda6ba872005-04-28 21:44:33 +00002210 case ISD::FSQRT:
2211 case ISD::FSIN:
2212 case ISD::FCOS:
2213 Tmp1 = PromoteOp(Node->getOperand(0));
2214 assert(Tmp1.getValueType() == NVT);
2215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2216 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002217 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2218 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002219 break;
2220
Chris Lattner03c85462005-01-15 05:21:40 +00002221 case ISD::AND:
2222 case ISD::OR:
2223 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002224 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002225 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002226 case ISD::MUL:
2227 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002228 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002229 // that too is okay if they are integer operations.
2230 Tmp1 = PromoteOp(Node->getOperand(0));
2231 Tmp2 = PromoteOp(Node->getOperand(1));
2232 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2233 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002234 break;
2235 case ISD::FADD:
2236 case ISD::FSUB:
2237 case ISD::FMUL:
2238 // The input may have strange things in the top bits of the registers, but
2239 // these operations don't care.
2240 Tmp1 = PromoteOp(Node->getOperand(0));
2241 Tmp2 = PromoteOp(Node->getOperand(1));
2242 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2243 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2244
2245 // Floating point operations will give excess precision that we may not be
2246 // able to tolerate. If we DO allow excess precision, just leave it,
2247 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002248 // FIXME: Why would we need to round FP ops more than integer ones?
2249 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002250 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002251 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2252 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002253 break;
2254
Chris Lattner8b6fa222005-01-15 22:16:26 +00002255 case ISD::SDIV:
2256 case ISD::SREM:
2257 // These operators require that their input be sign extended.
2258 Tmp1 = PromoteOp(Node->getOperand(0));
2259 Tmp2 = PromoteOp(Node->getOperand(1));
2260 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002261 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2262 DAG.getValueType(VT));
2263 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2264 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002265 }
2266 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2267
2268 // Perform FP_ROUND: this is probably overly pessimistic.
2269 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002270 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2271 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002272 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002273 case ISD::FDIV:
2274 case ISD::FREM:
2275 // These operators require that their input be fp extended.
2276 Tmp1 = PromoteOp(Node->getOperand(0));
2277 Tmp2 = PromoteOp(Node->getOperand(1));
2278 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2279
2280 // Perform FP_ROUND: this is probably overly pessimistic.
2281 if (NoExcessFPPrecision)
2282 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2283 DAG.getValueType(VT));
2284 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002285
2286 case ISD::UDIV:
2287 case ISD::UREM:
2288 // These operators require that their input be zero extended.
2289 Tmp1 = PromoteOp(Node->getOperand(0));
2290 Tmp2 = PromoteOp(Node->getOperand(1));
2291 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002292 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2293 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002294 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2295 break;
2296
2297 case ISD::SHL:
2298 Tmp1 = PromoteOp(Node->getOperand(0));
2299 Tmp2 = LegalizeOp(Node->getOperand(1));
2300 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2301 break;
2302 case ISD::SRA:
2303 // The input value must be properly sign extended.
2304 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002305 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2306 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002307 Tmp2 = LegalizeOp(Node->getOperand(1));
2308 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2309 break;
2310 case ISD::SRL:
2311 // The input value must be properly zero extended.
2312 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002313 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002314 Tmp2 = LegalizeOp(Node->getOperand(1));
2315 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2316 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002317 case ISD::LOAD:
2318 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2319 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002320 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002321 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002322 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2323 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002324 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002325 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2326 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002327
2328 // Remember that we legalized the chain.
2329 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2330 break;
2331 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002332 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2333 case Expand: assert(0 && "It's impossible to expand bools");
2334 case Legal:
2335 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2336 break;
2337 case Promote:
2338 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2339 break;
2340 }
Chris Lattner03c85462005-01-15 05:21:40 +00002341 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2342 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2343 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2344 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002345 case ISD::SELECT_CC:
2346 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2347 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2348 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2349 Node->getOperand(1), Tmp2, Tmp3,
2350 Node->getOperand(4));
2351 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002352 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002353 case ISD::CALL: {
2354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2355 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2356
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002357 std::vector<SDOperand> Ops;
2358 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2359 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2360
Chris Lattner8ac532c2005-01-16 19:46:48 +00002361 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2362 "Can only promote single result calls");
2363 std::vector<MVT::ValueType> RetTyVTs;
2364 RetTyVTs.reserve(2);
2365 RetTyVTs.push_back(NVT);
2366 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002367 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2368 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002369 Result = SDOperand(NC, 0);
2370
2371 // Insert the new chain mapping.
2372 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2373 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002374 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002375 case ISD::CTPOP:
2376 case ISD::CTTZ:
2377 case ISD::CTLZ:
2378 Tmp1 = Node->getOperand(0);
2379 //Zero extend the argument
2380 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2381 // Perform the larger operation, then subtract if needed.
2382 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2383 switch(Node->getOpcode())
2384 {
2385 case ISD::CTPOP:
2386 Result = Tmp1;
2387 break;
2388 case ISD::CTTZ:
2389 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002390 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002391 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002392 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002393 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2394 break;
2395 case ISD::CTLZ:
2396 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002397 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2398 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002399 getSizeInBits(VT), NVT));
2400 break;
2401 }
2402 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002403 }
2404
2405 assert(Result.Val && "Didn't set a result!");
2406 AddPromotedOperand(Op, Result);
2407 return Result;
2408}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002409
Chris Lattner84f67882005-01-20 18:52:28 +00002410/// ExpandAddSub - Find a clever way to expand this add operation into
2411/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002412void SelectionDAGLegalize::
2413ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2414 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002415 // Expand the subcomponents.
2416 SDOperand LHSL, LHSH, RHSL, RHSH;
2417 ExpandOp(LHS, LHSL, LHSH);
2418 ExpandOp(RHS, RHSL, RHSH);
2419
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002420 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002421 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2422 if (LHSL.getValueType() == MVT::i32) {
2423 SDOperand LowEl;
2424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2425 if (C->getValue() == 0)
2426 LowEl = RHSL;
2427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2428 if (C->getValue() == 0)
2429 LowEl = LHSL;
2430 if (LowEl.Val) {
2431 // Turn this into an add/sub of the high part only.
2432 SDOperand HiEl =
2433 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2434 LowEl.getValueType(), LHSH, RHSH);
2435 Lo = LowEl;
2436 Hi = HiEl;
2437 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002438 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002439 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002440
Chris Lattner84f67882005-01-20 18:52:28 +00002441 std::vector<SDOperand> Ops;
2442 Ops.push_back(LHSL);
2443 Ops.push_back(LHSH);
2444 Ops.push_back(RHSL);
2445 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002446
2447 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2448 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002449 Hi = Lo.getValue(1);
2450}
2451
Chris Lattner5b359c62005-04-02 04:00:59 +00002452void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2453 SDOperand Op, SDOperand Amt,
2454 SDOperand &Lo, SDOperand &Hi) {
2455 // Expand the subcomponents.
2456 SDOperand LHSL, LHSH;
2457 ExpandOp(Op, LHSL, LHSH);
2458
2459 std::vector<SDOperand> Ops;
2460 Ops.push_back(LHSL);
2461 Ops.push_back(LHSH);
2462 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002463 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002464 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002465 Hi = Lo.getValue(1);
2466}
2467
2468
Chris Lattnere34b3962005-01-19 04:19:40 +00002469/// ExpandShift - Try to find a clever way to expand this shift operation out to
2470/// smaller elements. If we can't find a way that is more efficient than a
2471/// libcall on this target, return false. Otherwise, return true with the
2472/// low-parts expanded into Lo and Hi.
2473bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2474 SDOperand &Lo, SDOperand &Hi) {
2475 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2476 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002477
Chris Lattnere34b3962005-01-19 04:19:40 +00002478 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002479 SDOperand ShAmt = LegalizeOp(Amt);
2480 MVT::ValueType ShTy = ShAmt.getValueType();
2481 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2482 unsigned NVTBits = MVT::getSizeInBits(NVT);
2483
2484 // Handle the case when Amt is an immediate. Other cases are currently broken
2485 // and are disabled.
2486 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2487 unsigned Cst = CN->getValue();
2488 // Expand the incoming operand to be shifted, so that we have its parts
2489 SDOperand InL, InH;
2490 ExpandOp(Op, InL, InH);
2491 switch(Opc) {
2492 case ISD::SHL:
2493 if (Cst > VTBits) {
2494 Lo = DAG.getConstant(0, NVT);
2495 Hi = DAG.getConstant(0, NVT);
2496 } else if (Cst > NVTBits) {
2497 Lo = DAG.getConstant(0, NVT);
2498 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002499 } else if (Cst == NVTBits) {
2500 Lo = DAG.getConstant(0, NVT);
2501 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002502 } else {
2503 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2504 Hi = DAG.getNode(ISD::OR, NVT,
2505 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2506 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2507 }
2508 return true;
2509 case ISD::SRL:
2510 if (Cst > VTBits) {
2511 Lo = DAG.getConstant(0, NVT);
2512 Hi = DAG.getConstant(0, NVT);
2513 } else if (Cst > NVTBits) {
2514 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2515 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002516 } else if (Cst == NVTBits) {
2517 Lo = InH;
2518 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002519 } else {
2520 Lo = DAG.getNode(ISD::OR, NVT,
2521 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2522 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2523 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2524 }
2525 return true;
2526 case ISD::SRA:
2527 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002528 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002529 DAG.getConstant(NVTBits-1, ShTy));
2530 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002531 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002532 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002533 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002534 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002535 } else if (Cst == NVTBits) {
2536 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002537 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002538 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002539 } else {
2540 Lo = DAG.getNode(ISD::OR, NVT,
2541 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2542 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2543 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2544 }
2545 return true;
2546 }
2547 }
2548 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2549 // so disable it for now. Currently targets are handling this via SHL_PARTS
2550 // and friends.
2551 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002552
2553 // If we have an efficient select operation (or if the selects will all fold
2554 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002555 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002556 return false;
2557
2558 SDOperand InL, InH;
2559 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002560 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2561 DAG.getConstant(NVTBits, ShTy), ShAmt);
2562
Chris Lattnere5544f82005-01-20 20:29:23 +00002563 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002564 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2565 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002566
Chris Lattnere34b3962005-01-19 04:19:40 +00002567 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2568 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2569 DAG.getConstant(NVTBits-1, ShTy));
2570 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2571 DAG.getConstant(NVTBits-1, ShTy));
2572 }
2573
2574 if (Opc == ISD::SHL) {
2575 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2576 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2577 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002578 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002579
Chris Lattnere34b3962005-01-19 04:19:40 +00002580 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2581 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2582 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002583 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002584 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2585 DAG.getConstant(32, ShTy),
2586 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002587 DAG.getConstant(0, NVT),
2588 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002589 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002590 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002591 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002592 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002593
2594 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002595 if (Opc == ISD::SRA)
2596 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2597 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002598 else
2599 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002600 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002601 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002602 }
2603 return true;
2604}
Chris Lattner77e77a62005-01-21 06:05:23 +00002605
Chris Lattner9530ddc2005-05-13 05:09:11 +00002606/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2607/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002608/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002609static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002610 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002611
Chris Lattner16cd04d2005-05-12 23:24:06 +00002612 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002613 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002614 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002615 Found = Node;
2616 return;
2617 }
2618
2619 // Otherwise, scan the operands of Node to see if any of them is a call.
2620 assert(Node->getNumOperands() != 0 &&
2621 "All leaves should have depth equal to the entry node!");
2622 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002623 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002624
2625 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002626 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002627 Found);
2628}
2629
2630
Chris Lattner9530ddc2005-05-13 05:09:11 +00002631/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2632/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002633/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002634static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2635 std::set<SDNode*> &Visited) {
2636 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2637 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002638
Chris Lattner16cd04d2005-05-12 23:24:06 +00002639 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002640 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002641 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002642 Found = Node;
2643 return;
2644 }
2645
2646 // Otherwise, scan the operands of Node to see if any of them is a call.
2647 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2648 if (UI == E) return;
2649 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002650 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002651
2652 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002653 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002654}
2655
Chris Lattner9530ddc2005-05-13 05:09:11 +00002656/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002657/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002658static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002659 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002660 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002661 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002662 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002663
2664 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002665 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002666
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002667 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002668 if (TheChain.getValueType() != MVT::Other)
2669 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002670 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002671
2672 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002673 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002674
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002675 // Make sure to only follow users of our token chain.
2676 SDNode *User = *UI;
2677 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2678 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002679 if (SDNode *Result = FindCallSeqEnd(User))
2680 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002681 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002682 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002683}
2684
Chris Lattner9530ddc2005-05-13 05:09:11 +00002685/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002686/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002687static SDNode *FindCallSeqStart(SDNode *Node) {
2688 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002689 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002690
2691 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2692 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002693 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002694}
2695
2696
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002697/// FindInputOutputChains - If we are replacing an operation with a call we need
2698/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002699/// properly serialize the calls in the block. The returned operand is the
2700/// input chain value for the new call (e.g. the entry node or the previous
2701/// call), and OutChain is set to be the chain node to update to point to the
2702/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002703static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2704 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002705 SDNode *LatestCallSeqStart = Entry.Val;
2706 SDNode *LatestCallSeqEnd = 0;
2707 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2708 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002709
Chris Lattner16cd04d2005-05-12 23:24:06 +00002710 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002711 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002712 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2713 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002714 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2715 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002716 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002717 LatestCallSeqEnd = Entry.Val;
2718 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002719
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002720 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002721 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002722 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002723 std::set<SDNode*> Visited;
2724 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002725
Chris Lattner9530ddc2005-05-13 05:09:11 +00002726 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002727 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002728 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002729
Chris Lattner9530ddc2005-05-13 05:09:11 +00002730 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002731}
2732
Jeff Cohen00b168892005-07-27 06:12:32 +00002733/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002734void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2735 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002736 // Nothing to splice it into?
2737 if (OutChain == 0) return;
2738
2739 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2740 //OutChain->dump();
2741
2742 // Form a token factor node merging the old inval and the new inval.
2743 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2744 OutChain->getOperand(0));
2745 // Change the node to refer to the new token.
2746 OutChain->setAdjCallChain(InToken);
2747}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002748
2749
Chris Lattner77e77a62005-01-21 06:05:23 +00002750// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2751// does not fit into a register, return the lo part and set the hi part to the
2752// by-reg argument. If it does fit into a single register, return the result
2753// and leave the Hi part unset.
2754SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2755 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002756 SDNode *OutChain;
2757 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2758 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002759 if (InChain.Val == 0)
2760 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002761
Chris Lattner77e77a62005-01-21 06:05:23 +00002762 TargetLowering::ArgListTy Args;
2763 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2764 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2765 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2766 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2767 }
2768 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002769
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002770 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002771 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002772 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002773 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2774 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002775
Chris Lattner99c25b82005-09-02 20:26:58 +00002776 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002777 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002778 default: assert(0 && "Unknown thing");
2779 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002780 Result = CallInfo.first;
2781 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002782 case Promote:
2783 assert(0 && "Cannot promote this yet!");
2784 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002785 ExpandOp(CallInfo.first, Result, Hi);
2786 CallInfo.second = LegalizeOp(CallInfo.second);
2787 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002788 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002789
2790 SpliceCallInto(CallInfo.second, OutChain);
2791 NeedsAnotherIteration = true;
2792 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002793}
2794
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002795
Chris Lattner77e77a62005-01-21 06:05:23 +00002796/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2797/// destination type is legal.
2798SDOperand SelectionDAGLegalize::
2799ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002800 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002801 assert(getTypeAction(Source.getValueType()) == Expand &&
2802 "This is not an expansion!");
2803 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2804
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002805 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002806 assert(Source.getValueType() == MVT::i64 &&
2807 "This only works for 64-bit -> FP");
2808 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2809 // incoming integer is set. To handle this, we dynamically test to see if
2810 // it is set, and, if so, add a fudge factor.
2811 SDOperand Lo, Hi;
2812 ExpandOp(Source, Lo, Hi);
2813
Chris Lattner66de05b2005-05-13 04:45:13 +00002814 // If this is unsigned, and not supported, first perform the conversion to
2815 // signed, then adjust the result if the sign bit is set.
2816 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2817 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2818
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002819 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2820 DAG.getConstant(0, Hi.getValueType()),
2821 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002822 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2823 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2824 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002825 uint64_t FF = 0x5f800000ULL;
2826 if (TLI.isLittleEndian()) FF <<= 32;
2827 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002828
Chris Lattner5839bf22005-08-26 17:15:30 +00002829 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002830 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2831 SDOperand FudgeInReg;
2832 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002833 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2834 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002835 else {
2836 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002837 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2838 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002839 }
Chris Lattner473a9902005-09-29 06:44:39 +00002840 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002841 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002842
Chris Lattnera88a2602005-05-14 05:33:54 +00002843 // Check to see if the target has a custom way to lower this. If so, use it.
2844 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2845 default: assert(0 && "This action not implemented for this operation!");
2846 case TargetLowering::Legal:
2847 case TargetLowering::Expand:
2848 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002849 case TargetLowering::Custom: {
2850 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2851 Source), DAG);
2852 if (NV.Val)
2853 return LegalizeOp(NV);
2854 break; // The target decided this was legal after all
2855 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002856 }
2857
Chris Lattner13689e22005-05-12 07:00:44 +00002858 // Expand the source, then glue it back together for the call. We must expand
2859 // the source in case it is shared (this pass of legalize must traverse it).
2860 SDOperand SrcLo, SrcHi;
2861 ExpandOp(Source, SrcLo, SrcHi);
2862 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2863
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002864 SDNode *OutChain = 0;
2865 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2866 DAG.getEntryNode());
2867 const char *FnName = 0;
2868 if (DestTy == MVT::f32)
2869 FnName = "__floatdisf";
2870 else {
2871 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2872 FnName = "__floatdidf";
2873 }
2874
Chris Lattner77e77a62005-01-21 06:05:23 +00002875 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2876
2877 TargetLowering::ArgListTy Args;
2878 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002879
Chris Lattner77e77a62005-01-21 06:05:23 +00002880 Args.push_back(std::make_pair(Source, ArgTy));
2881
2882 // We don't care about token chains for libcalls. We just use the entry
2883 // node as our input and ignore the output chain. This allows us to place
2884 // calls wherever we need them to satisfy data dependences.
2885 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002886
2887 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002888 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2889 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002890
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002891 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002892 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002893}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002894
Chris Lattnere34b3962005-01-19 04:19:40 +00002895
2896
Chris Lattner3e928bb2005-01-07 07:47:09 +00002897/// ExpandOp - Expand the specified SDOperand into its two component pieces
2898/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2899/// LegalizeNodes map is filled in for any results that are not expanded, the
2900/// ExpandedNodes map is filled in for any results that are expanded, and the
2901/// Lo/Hi values are returned.
2902void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2903 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002904 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002905 SDNode *Node = Op.Val;
2906 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2907 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2908 assert(MVT::isInteger(NVT) && NVT < VT &&
2909 "Cannot expand to FP value or to larger int value!");
2910
Chris Lattner6fdcb252005-09-02 20:32:45 +00002911 // See if we already expanded it.
2912 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2913 = ExpandedNodes.find(Op);
2914 if (I != ExpandedNodes.end()) {
2915 Lo = I->second.first;
2916 Hi = I->second.second;
2917 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002918 }
2919
Chris Lattner4e6c7462005-01-08 19:27:05 +00002920 // Expanding to multiple registers needs to perform an optimization step, and
2921 // is not careful to avoid operations the target does not support. Make sure
2922 // that all generated operations are legalized in the next iteration.
2923 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002924
Chris Lattner3e928bb2005-01-07 07:47:09 +00002925 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002926 case ISD::CopyFromReg:
2927 assert(0 && "CopyFromReg must be legal!");
2928 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002929 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2930 assert(0 && "Do not know how to expand this operator!");
2931 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002932 case ISD::UNDEF:
2933 Lo = DAG.getNode(ISD::UNDEF, NVT);
2934 Hi = DAG.getNode(ISD::UNDEF, NVT);
2935 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002936 case ISD::Constant: {
2937 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2938 Lo = DAG.getConstant(Cst, NVT);
2939 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2940 break;
2941 }
2942
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002943 case ISD::BUILD_PAIR:
2944 // Legalize both operands. FIXME: in the future we should handle the case
2945 // where the two elements are not legal.
2946 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2947 Lo = LegalizeOp(Node->getOperand(0));
2948 Hi = LegalizeOp(Node->getOperand(1));
2949 break;
2950
Chris Lattneredb1add2005-05-11 04:51:16 +00002951 case ISD::CTPOP:
2952 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002953 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2954 DAG.getNode(ISD::CTPOP, NVT, Lo),
2955 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002956 Hi = DAG.getConstant(0, NVT);
2957 break;
2958
Chris Lattner39a8f332005-05-12 19:05:01 +00002959 case ISD::CTLZ: {
2960 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002961 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002962 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2963 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002964 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2965 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002966 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2967 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2968
2969 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2970 Hi = DAG.getConstant(0, NVT);
2971 break;
2972 }
2973
2974 case ISD::CTTZ: {
2975 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002976 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002977 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2978 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002979 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2980 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002981 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2982 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2983
2984 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2985 Hi = DAG.getConstant(0, NVT);
2986 break;
2987 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002988
Chris Lattner3e928bb2005-01-07 07:47:09 +00002989 case ISD::LOAD: {
2990 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2991 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002992 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002993
2994 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002995 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002996 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2997 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00002998 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002999 //are from the same instruction?
3000 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003001
3002 // Build a factor node to remember that this load is independent of the
3003 // other one.
3004 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3005 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003006
Chris Lattner3e928bb2005-01-07 07:47:09 +00003007 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003008 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003009 if (!TLI.isLittleEndian())
3010 std::swap(Lo, Hi);
3011 break;
3012 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003013 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003014 case ISD::CALL: {
3015 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3016 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3017
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003018 bool Changed = false;
3019 std::vector<SDOperand> Ops;
3020 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3021 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3022 Changed |= Ops.back() != Node->getOperand(i);
3023 }
3024
Chris Lattner3e928bb2005-01-07 07:47:09 +00003025 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3026 "Can only expand a call once so far, not i64 -> i16!");
3027
3028 std::vector<MVT::ValueType> RetTyVTs;
3029 RetTyVTs.reserve(3);
3030 RetTyVTs.push_back(NVT);
3031 RetTyVTs.push_back(NVT);
3032 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003033 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3034 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003035 Lo = SDOperand(NC, 0);
3036 Hi = SDOperand(NC, 1);
3037
3038 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003039 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003040 break;
3041 }
3042 case ISD::AND:
3043 case ISD::OR:
3044 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3045 SDOperand LL, LH, RL, RH;
3046 ExpandOp(Node->getOperand(0), LL, LH);
3047 ExpandOp(Node->getOperand(1), RL, RH);
3048 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3049 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3050 break;
3051 }
3052 case ISD::SELECT: {
3053 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003054
3055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3056 case Expand: assert(0 && "It's impossible to expand bools");
3057 case Legal:
3058 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3059 break;
3060 case Promote:
3061 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3062 break;
3063 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003064 ExpandOp(Node->getOperand(1), LL, LH);
3065 ExpandOp(Node->getOperand(2), RL, RH);
3066 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3067 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3068 break;
3069 }
Nate Begeman9373a812005-08-10 20:51:12 +00003070 case ISD::SELECT_CC: {
3071 SDOperand TL, TH, FL, FH;
3072 ExpandOp(Node->getOperand(2), TL, TH);
3073 ExpandOp(Node->getOperand(3), FL, FH);
3074 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3075 Node->getOperand(1), TL, FL, Node->getOperand(4));
3076 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3077 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003078 Lo = LegalizeOp(Lo);
3079 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003080 break;
3081 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003082 case ISD::ANY_EXTEND: {
3083 SDOperand In;
3084 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3085 case Expand: assert(0 && "expand-expand not implemented yet!");
3086 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3087 case Promote:
3088 In = PromoteOp(Node->getOperand(0));
3089 break;
3090 }
3091
3092 // The low part is any extension of the input (which degenerates to a copy).
3093 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3094 // The high part is undefined.
3095 Hi = DAG.getNode(ISD::UNDEF, NVT);
3096 break;
3097 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003098 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003099 SDOperand In;
3100 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3101 case Expand: assert(0 && "expand-expand not implemented yet!");
3102 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3103 case Promote:
3104 In = PromoteOp(Node->getOperand(0));
3105 // Emit the appropriate sign_extend_inreg to get the value we want.
3106 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003107 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003108 break;
3109 }
3110
Chris Lattner3e928bb2005-01-07 07:47:09 +00003111 // The low part is just a sign extension of the input (which degenerates to
3112 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003113 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003114
Chris Lattner3e928bb2005-01-07 07:47:09 +00003115 // The high part is obtained by SRA'ing all but one of the bits of the lo
3116 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003117 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003118 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3119 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003120 break;
3121 }
Chris Lattner06098e02005-04-03 23:41:52 +00003122 case ISD::ZERO_EXTEND: {
3123 SDOperand In;
3124 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3125 case Expand: assert(0 && "expand-expand not implemented yet!");
3126 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3127 case Promote:
3128 In = PromoteOp(Node->getOperand(0));
3129 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003130 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003131 break;
3132 }
3133
Chris Lattner3e928bb2005-01-07 07:47:09 +00003134 // The low part is just a zero extension of the input (which degenerates to
3135 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003136 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003137
Chris Lattner3e928bb2005-01-07 07:47:09 +00003138 // The high part is just a zero.
3139 Hi = DAG.getConstant(0, NVT);
3140 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003141 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003142 // These operators cannot be expanded directly, emit them as calls to
3143 // library functions.
3144 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003145 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003146 SDOperand Op;
3147 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3148 case Expand: assert(0 && "cannot expand FP!");
3149 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3150 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3151 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003152
Chris Lattnerf20d1832005-07-30 01:40:57 +00003153 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3154
Chris Lattner80a3e942005-07-29 00:33:32 +00003155 // Now that the custom expander is done, expand the result, which is still
3156 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003157 if (Op.Val) {
3158 ExpandOp(Op, Lo, Hi);
3159 break;
3160 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003161 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003162
Chris Lattner4e6c7462005-01-08 19:27:05 +00003163 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003164 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003165 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003166 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003167 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003168
Chris Lattner4e6c7462005-01-08 19:27:05 +00003169 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003170 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3171 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3172 LegalizeOp(Node->getOperand(0)));
3173 // Now that the custom expander is done, expand the result, which is still
3174 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003175 Op = TLI.LowerOperation(Op, DAG);
3176 if (Op.Val) {
3177 ExpandOp(Op, Lo, Hi);
3178 break;
3179 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003180 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003181
Chris Lattner4e6c7462005-01-08 19:27:05 +00003182 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003183 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003184 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003185 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003186 break;
3187
Chris Lattnere34b3962005-01-19 04:19:40 +00003188 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003189 // If the target wants custom lowering, do so.
3190 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3191 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3192 LegalizeOp(Node->getOperand(1)));
3193 Op = TLI.LowerOperation(Op, DAG);
3194 if (Op.Val) {
3195 // Now that the custom expander is done, expand the result, which is
3196 // still VT.
3197 ExpandOp(Op, Lo, Hi);
3198 break;
3199 }
3200 }
3201
Chris Lattnere34b3962005-01-19 04:19:40 +00003202 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003203 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003204 break;
Chris Lattner47599822005-04-02 03:38:53 +00003205
3206 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003207 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003208 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3209 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003210 break;
3211 }
3212
Chris Lattnere34b3962005-01-19 04:19:40 +00003213 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003214 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003215 break;
3216
3217 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003218 // If the target wants custom lowering, do so.
3219 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3220 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3221 LegalizeOp(Node->getOperand(1)));
3222 Op = TLI.LowerOperation(Op, DAG);
3223 if (Op.Val) {
3224 // Now that the custom expander is done, expand the result, which is
3225 // still VT.
3226 ExpandOp(Op, Lo, Hi);
3227 break;
3228 }
3229 }
3230
Chris Lattnere34b3962005-01-19 04:19:40 +00003231 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003232 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003233 break;
Chris Lattner47599822005-04-02 03:38:53 +00003234
3235 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003236 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003237 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3238 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003239 break;
3240 }
3241
Chris Lattnere34b3962005-01-19 04:19:40 +00003242 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003243 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003244 break;
3245 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003246 // If the target wants custom lowering, do so.
3247 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3248 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3249 LegalizeOp(Node->getOperand(1)));
3250 Op = TLI.LowerOperation(Op, DAG);
3251 if (Op.Val) {
3252 // Now that the custom expander is done, expand the result, which is
3253 // still VT.
3254 ExpandOp(Op, Lo, Hi);
3255 break;
3256 }
3257 }
3258
Chris Lattnere34b3962005-01-19 04:19:40 +00003259 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003260 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003261 break;
Chris Lattner47599822005-04-02 03:38:53 +00003262
3263 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003264 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003265 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3266 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003267 break;
3268 }
3269
Chris Lattnere34b3962005-01-19 04:19:40 +00003270 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003271 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003272 break;
3273
Misha Brukmanedf128a2005-04-21 22:36:52 +00003274 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003275 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3276 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003277 break;
3278 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003279 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3280 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003281 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003282 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003283 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003284 SDOperand LL, LH, RL, RH;
3285 ExpandOp(Node->getOperand(0), LL, LH);
3286 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003287 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3288 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3289 // extended the sign bit of the low half through the upper half, and if so
3290 // emit a MULHS instead of the alternate sequence that is valid for any
3291 // i64 x i64 multiply.
3292 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3293 // is RH an extension of the sign bit of RL?
3294 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3295 RH.getOperand(1).getOpcode() == ISD::Constant &&
3296 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3297 // is LH an extension of the sign bit of LL?
3298 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3299 LH.getOperand(1).getOpcode() == ISD::Constant &&
3300 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3301 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3302 } else {
3303 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3304 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3305 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3306 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3307 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3308 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003309 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3310 } else {
3311 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3312 }
3313 break;
3314 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003315 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3316 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3317 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3318 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003319 }
3320
3321 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003322 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3323 std::make_pair(Lo, Hi))).second;
3324 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003325}
3326
3327
3328// SelectionDAG::Legalize - This is the entry point for the file.
3329//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003330void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003331 /// run - This is the main entry point to this class.
3332 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003333 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003334}
3335