blob: 2c01982df2bf8569e6fbaeaf8d53f9188d4c9509 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000018#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Constants.h"
24#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000025#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
30/// hacks on it until the target machine can handle it. This involves
31/// eliminating value sizes the machine cannot handle (promoting small sizes to
32/// large sizes or splitting up large values into small values) as well as
33/// eliminating operations the machine cannot handle.
34///
35/// This code also does a small amount of optimization and recognition of idioms
36/// as part of its processing. For example, if a target does not support a
37/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
38/// will attempt merge setcc and brc instructions into brcc's.
39///
40namespace {
41class SelectionDAGLegalize {
42 TargetLowering &TLI;
43 SelectionDAG &DAG;
44
45 /// LegalizeAction - This enum indicates what action we should take for each
46 /// value type the can occur in the program.
47 enum LegalizeAction {
48 Legal, // The target natively supports this value type.
49 Promote, // This should be promoted to the next larger type.
50 Expand, // This integer type should be broken into smaller pieces.
51 };
52
Chris Lattnerdc750592005-01-07 07:47:09 +000053 /// ValueTypeActions - This is a bitvector that contains two bits for each
54 /// value type, where the two bits correspond to the LegalizeAction enum.
55 /// This can be queried with "getTypeAction(VT)".
56 unsigned ValueTypeActions;
57
58 /// NeedsAnotherIteration - This is set when we expand a large integer
59 /// operation into smaller integer operations, but the smaller operations are
60 /// not set. This occurs only rarely in practice, for targets that don't have
61 /// 32-bit or larger integer registers.
62 bool NeedsAnotherIteration;
63
64 /// LegalizedNodes - For nodes that are of legal width, and that have more
65 /// than one use, this map indicates what regularized operand to use. This
66 /// allows us to avoid legalizing the same thing more than once.
67 std::map<SDOperand, SDOperand> LegalizedNodes;
68
Chris Lattner1f2c9d82005-01-15 05:21:40 +000069 /// PromotedNodes - For nodes that are below legal width, and that have more
70 /// than one use, this map indicates what promoted value to use. This allows
71 /// us to avoid promoting the same thing more than once.
72 std::map<SDOperand, SDOperand> PromotedNodes;
73
Chris Lattnerdc750592005-01-07 07:47:09 +000074 /// ExpandedNodes - For nodes that need to be expanded, and which have more
75 /// than one use, this map indicates which which operands are the expanded
76 /// version of the input. This allows us to avoid expanding the same node
77 /// more than once.
78 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
79
Chris Lattnerea4ca942005-01-07 22:28:47 +000080 void AddLegalizedOperand(SDOperand From, SDOperand To) {
81 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
82 assert(isNew && "Got into the map somehow?");
83 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000084 void AddPromotedOperand(SDOperand From, SDOperand To) {
85 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
86 assert(isNew && "Got into the map somehow?");
87 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000088
Chris Lattnerdc750592005-01-07 07:47:09 +000089public:
90
Chris Lattner4add7e32005-01-23 04:42:50 +000091 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000092
93 /// Run - While there is still lowering to do, perform a pass over the DAG.
94 /// Most regularization can be done in a single pass, but targets that require
95 /// large values to be split into registers multiple times (e.g. i64 -> 4x
96 /// i16) require iteration for these values (the first iteration will demote
97 /// to i32, the second will demote to i16).
98 void Run() {
99 do {
100 NeedsAnotherIteration = false;
101 LegalizeDAG();
102 } while (NeedsAnotherIteration);
103 }
104
105 /// getTypeAction - Return how we should legalize values of this type, either
106 /// it is already legal or we need to expand it into multiple registers of
107 /// smaller integer type, or we need to promote it to a larger type.
108 LegalizeAction getTypeAction(MVT::ValueType VT) const {
109 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
110 }
111
112 /// isTypeLegal - Return true if this type is legal on this target.
113 ///
114 bool isTypeLegal(MVT::ValueType VT) const {
115 return getTypeAction(VT) == Legal;
116 }
117
118private:
119 void LegalizeDAG();
120
121 SDOperand LegalizeOp(SDOperand O);
122 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000123 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124
Chris Lattneraac464e2005-01-21 06:05:23 +0000125 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
126 SDOperand &Hi);
127 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
128 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000129
Jim Laskeyf2516a92005-08-17 00:39:29 +0000130 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
131 SDOperand LegalOp,
132 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000133 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
134 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000135 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
136 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000137
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000138 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
139 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000140 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
141 SDOperand &Lo, SDOperand &Hi);
142 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000144
Chris Lattnera5bf1032005-05-12 04:49:08 +0000145 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
146
Chris Lattnerdc750592005-01-07 07:47:09 +0000147 SDOperand getIntPtrConstant(uint64_t Val) {
148 return DAG.getConstant(Val, TLI.getPointerTy());
149 }
150};
151}
152
153
Chris Lattner4add7e32005-01-23 04:42:50 +0000154SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
155 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
156 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000157 assert(MVT::LAST_VALUETYPE <= 16 &&
158 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000159}
160
Jim Laskeyf2516a92005-08-17 00:39:29 +0000161/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
162/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000163/// we expand it. At this point, we know that the result and operand types are
164/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000165SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
166 SDOperand Op0,
167 MVT::ValueType DestVT) {
168 if (Op0.getValueType() == MVT::i32) {
169 // simple 32-bit [signed|unsigned] integer to float/double expansion
170
171 // get the stack frame index of a 8 byte buffer
172 MachineFunction &MF = DAG.getMachineFunction();
173 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
174 // get address of 8 byte buffer
175 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
176 // word offset constant for Hi/Lo address computation
177 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
178 // set up Hi and Lo (into buffer) address based on endian
179 SDOperand Hi, Lo;
180 if (TLI.isLittleEndian()) {
181 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
182 Lo = StackSlot;
183 } else {
184 Hi = StackSlot;
185 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
186 }
187 // if signed map to unsigned space
188 SDOperand Op0Mapped;
189 if (isSigned) {
190 // constant used to invert sign bit (signed to unsigned mapping)
191 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
192 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
193 } else {
194 Op0Mapped = Op0;
195 }
196 // store the lo of the constructed double - based on integer input
197 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
198 Op0Mapped, Lo, DAG.getSrcValue(NULL));
199 // initial hi portion of constructed double
200 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
201 // store the hi of the constructed double - biased exponent
202 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
203 InitialHi, Hi, DAG.getSrcValue(NULL));
204 // load the constructed double
205 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
206 DAG.getSrcValue(NULL));
207 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000208 SDOperand Bias = DAG.getConstantFP(isSigned ?
209 BitsToDouble(0x4330000080000000ULL)
210 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000211 MVT::f64);
212 // subtract the bias
213 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
214 // final result
215 SDOperand Result;
216 // handle final rounding
217 if (DestVT == MVT::f64) {
218 // do nothing
219 Result = Sub;
220 } else {
221 // if f32 then cast to f32
222 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
223 }
224 NeedsAnotherIteration = true;
225 return Result;
226 }
227 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000228 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000229
Chris Lattnerd47675e2005-08-09 20:20:18 +0000230 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
231 DAG.getConstant(0, Op0.getValueType()),
232 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000233 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
234 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
235 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Jim Laskeyf2516a92005-08-17 00:39:29 +0000237 // If the sign bit of the integer is set, the large number will be treated
238 // as a negative number. To counteract this, the dynamic code adds an
239 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000240 uint64_t FF;
241 switch (Op0.getValueType()) {
242 default: assert(0 && "Unsupported integer type!");
243 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
244 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
245 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
246 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
247 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000248 if (TLI.isLittleEndian()) FF <<= 32;
249 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000250
Chris Lattnere3e847b2005-07-16 00:19:57 +0000251 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
252 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
253 TLI.getPointerTy());
254 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
255 SDOperand FudgeInReg;
256 if (DestVT == MVT::f32)
257 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
258 DAG.getSrcValue(NULL));
259 else {
260 assert(DestVT == MVT::f64 && "Unexpected conversion");
261 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
262 DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL), MVT::f32));
264 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000265
Chris Lattnere3e847b2005-07-16 00:19:57 +0000266 NeedsAnotherIteration = true;
267 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
268}
269
Chris Lattner19732782005-08-16 18:17:10 +0000270/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000271/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000272/// we promote it. At this point, we know that the result and operand types are
273/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
274/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000275SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
276 MVT::ValueType DestVT,
277 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000278 // First step, figure out the appropriate *INT_TO_FP operation to use.
279 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000280
Chris Lattnere3e847b2005-07-16 00:19:57 +0000281 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000282
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // Scan for the appropriate larger type to use.
284 while (1) {
285 NewInTy = (MVT::ValueType)(NewInTy+1);
286 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // If the target supports SINT_TO_FP of this type, use it.
289 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
290 default: break;
291 case TargetLowering::Legal:
292 if (!TLI.hasNativeSupportFor(NewInTy))
293 break; // Can't use this datatype.
294 // FALL THROUGH.
295 case TargetLowering::Custom:
296 OpToUse = ISD::SINT_TO_FP;
297 break;
298 }
299 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000300 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000301
Chris Lattnere3e847b2005-07-16 00:19:57 +0000302 // If the target supports UINT_TO_FP of this type, use it.
303 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
304 default: break;
305 case TargetLowering::Legal:
306 if (!TLI.hasNativeSupportFor(NewInTy))
307 break; // Can't use this datatype.
308 // FALL THROUGH.
309 case TargetLowering::Custom:
310 OpToUse = ISD::UINT_TO_FP;
311 break;
312 }
313 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000314
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315 // Otherwise, try a larger type.
316 }
317
318 // Make sure to legalize any nodes we create here in the next pass.
319 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000320
Chris Lattnere3e847b2005-07-16 00:19:57 +0000321 // Okay, we found the operation and type to use. Zero extend our input to the
322 // desired type then run the operation on it.
323 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000324 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
325 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326}
327
Chris Lattner44fe26f2005-07-29 00:11:56 +0000328/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
329/// FP_TO_*INT operation of the specified operand when the target requests that
330/// we promote it. At this point, we know that the result and operand types are
331/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
332/// operation that returns a larger result.
333SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
334 MVT::ValueType DestVT,
335 bool isSigned) {
336 // First step, figure out the appropriate FP_TO*INT operation to use.
337 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000338
Chris Lattner44fe26f2005-07-29 00:11:56 +0000339 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000340
Chris Lattner44fe26f2005-07-29 00:11:56 +0000341 // Scan for the appropriate larger type to use.
342 while (1) {
343 NewOutTy = (MVT::ValueType)(NewOutTy+1);
344 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // If the target supports FP_TO_SINT returning this type, use it.
347 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
348 default: break;
349 case TargetLowering::Legal:
350 if (!TLI.hasNativeSupportFor(NewOutTy))
351 break; // Can't use this datatype.
352 // FALL THROUGH.
353 case TargetLowering::Custom:
354 OpToUse = ISD::FP_TO_SINT;
355 break;
356 }
357 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000358
Chris Lattner44fe26f2005-07-29 00:11:56 +0000359 // If the target supports FP_TO_UINT of this type, use it.
360 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
361 default: break;
362 case TargetLowering::Legal:
363 if (!TLI.hasNativeSupportFor(NewOutTy))
364 break; // Can't use this datatype.
365 // FALL THROUGH.
366 case TargetLowering::Custom:
367 OpToUse = ISD::FP_TO_UINT;
368 break;
369 }
370 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000371
Chris Lattner44fe26f2005-07-29 00:11:56 +0000372 // Otherwise, try a larger type.
373 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000374
Chris Lattner44fe26f2005-07-29 00:11:56 +0000375 // Make sure to legalize any nodes we create here in the next pass.
376 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000377
Chris Lattner44fe26f2005-07-29 00:11:56 +0000378 // Okay, we found the operation and type to use. Truncate the result of the
379 // extended FP_TO_*INT operation to the desired size.
380 return DAG.getNode(ISD::TRUNCATE, DestVT,
381 DAG.getNode(OpToUse, NewOutTy, LegalOp));
382}
383
384
Chris Lattnerdc750592005-01-07 07:47:09 +0000385void SelectionDAGLegalize::LegalizeDAG() {
386 SDOperand OldRoot = DAG.getRoot();
387 SDOperand NewRoot = LegalizeOp(OldRoot);
388 DAG.setRoot(NewRoot);
389
390 ExpandedNodes.clear();
391 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000392 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000393
394 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000395 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000396}
397
398SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000399 assert(getTypeAction(Op.getValueType()) == Legal &&
400 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000401 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000402
Chris Lattnerdc750592005-01-07 07:47:09 +0000403 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000404 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000405 if (Node->getNumValues() > 1) {
406 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
407 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000408 case Legal: break; // Nothing to do.
409 case Expand: {
410 SDOperand T1, T2;
411 ExpandOp(Op.getValue(i), T1, T2);
412 assert(LegalizedNodes.count(Op) &&
413 "Expansion didn't add legal operands!");
414 return LegalizedNodes[Op];
415 }
416 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000417 PromoteOp(Op.getValue(i));
418 assert(LegalizedNodes.count(Op) &&
419 "Expansion didn't add legal operands!");
420 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000421 }
422 }
423
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000424 // Note that LegalizeOp may be reentered even from single-use nodes, which
425 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000426 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
427 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000428
Nate Begemane5b86d72005-08-10 20:51:12 +0000429 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000430
431 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000432
433 switch (Node->getOpcode()) {
434 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000435 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
436 // If this is a target node, legalize it by legalizing the operands then
437 // passing it through.
438 std::vector<SDOperand> Ops;
439 bool Changed = false;
440 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
441 Ops.push_back(LegalizeOp(Node->getOperand(i)));
442 Changed = Changed || Node->getOperand(i) != Ops.back();
443 }
444 if (Changed)
445 if (Node->getNumValues() == 1)
446 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
447 else {
448 std::vector<MVT::ValueType> VTs(Node->value_begin(),
449 Node->value_end());
450 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
451 }
452
453 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
454 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
455 return Result.getValue(Op.ResNo);
456 }
457 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000458 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
459 assert(0 && "Do not know how to legalize this operator!");
460 abort();
461 case ISD::EntryToken:
462 case ISD::FrameIndex:
463 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000464 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000465 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000466 assert(getTypeAction(Node->getValueType(0)) == Legal &&
467 "This must be legal!");
468 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000469 case ISD::CopyFromReg:
470 Tmp1 = LegalizeOp(Node->getOperand(0));
471 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000472 Result = DAG.getCopyFromReg(Tmp1,
473 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
474 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000475 else
476 Result = Op.getValue(0);
477
478 // Since CopyFromReg produces two values, make sure to remember that we
479 // legalized both of them.
480 AddLegalizedOperand(Op.getValue(0), Result);
481 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
482 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000483 case ISD::ImplicitDef:
484 Tmp1 = LegalizeOp(Node->getOperand(0));
485 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000486 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
487 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000488 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000489 case ISD::UNDEF: {
490 MVT::ValueType VT = Op.getValueType();
491 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000492 default: assert(0 && "This action is not supported yet!");
493 case TargetLowering::Expand:
494 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000495 if (MVT::isInteger(VT))
496 Result = DAG.getConstant(0, VT);
497 else if (MVT::isFloatingPoint(VT))
498 Result = DAG.getConstantFP(0, VT);
499 else
500 assert(0 && "Unknown value type!");
501 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000502 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000503 break;
504 }
505 break;
506 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000507 case ISD::Constant:
508 // We know we don't need to expand constants here, constants only have one
509 // value and we check that it is fine above.
510
511 // FIXME: Maybe we should handle things like targets that don't support full
512 // 32-bit immediates?
513 break;
514 case ISD::ConstantFP: {
515 // Spill FP immediates to the constant pool if the target cannot directly
516 // codegen them. Targets often have some immediate values that can be
517 // efficiently generated into an FP register without a load. We explicitly
518 // leave these constants as ConstantFP nodes for the target to deal with.
519
520 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
521
522 // Check to see if this FP immediate is already legal.
523 bool isLegal = false;
524 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
525 E = TLI.legal_fpimm_end(); I != E; ++I)
526 if (CFP->isExactlyValue(*I)) {
527 isLegal = true;
528 break;
529 }
530
531 if (!isLegal) {
532 // Otherwise we need to spill the constant to memory.
533 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
534
535 bool Extend = false;
536
537 // If a FP immediate is precise when represented as a float, we put it
538 // into the constant pool as a float, even if it's is statically typed
539 // as a double.
540 MVT::ValueType VT = CFP->getValueType(0);
541 bool isDouble = VT == MVT::f64;
542 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
543 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000544 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
545 // Only do this if the target has a native EXTLOAD instruction from
546 // f32.
547 TLI.getOperationAction(ISD::EXTLOAD,
548 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000549 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
550 VT = MVT::f32;
551 Extend = true;
552 }
Misha Brukman835702a2005-04-21 22:36:52 +0000553
Chris Lattnerdc750592005-01-07 07:47:09 +0000554 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
555 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000556 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000557 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
558 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000559 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000560 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
561 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000562 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000563 }
564 break;
565 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000566 case ISD::TokenFactor: {
567 std::vector<SDOperand> Ops;
568 bool Changed = false;
569 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000570 SDOperand Op = Node->getOperand(i);
571 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000572 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000573 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
574 Changed = true;
575 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
576 Ops.push_back(LegalizeOp(Op.getOperand(j)));
577 } else {
578 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
579 Changed |= Ops[i] != Op;
580 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000581 }
582 if (Changed)
583 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
584 break;
585 }
586
Chris Lattner2dce7032005-05-12 23:24:06 +0000587 case ISD::CALLSEQ_START:
588 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000590 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000591 Tmp2 = Node->getOperand(0);
592 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000593 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000594
595 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
596 // this will cause the maps used to memoize results to get confused.
597 // Create and add a dummy use, just to increase its use count. This will
598 // be removed at the end of legalize when dead nodes are removed.
599 if (Tmp2.Val->hasOneUse())
600 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
601 DAG.getConstant(0, MVT::i32));
602 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000603 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000604 // nodes are treated specially and are mutated in place. This makes the dag
605 // legalization process more efficient and also makes libcall insertion
606 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000607 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000608 case ISD::DYNAMIC_STACKALLOC:
609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
610 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
611 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
612 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000613 Tmp3 != Node->getOperand(2)) {
614 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
615 std::vector<SDOperand> Ops;
616 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
617 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
618 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000619 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000620
621 // Since this op produces two values, make sure to remember that we
622 // legalized both of them.
623 AddLegalizedOperand(SDOperand(Node, 0), Result);
624 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
625 return Result.getValue(Op.ResNo);
626
Chris Lattnerd0feb642005-05-13 18:43:43 +0000627 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000628 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000631
632 bool Changed = false;
633 std::vector<SDOperand> Ops;
634 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
635 Ops.push_back(LegalizeOp(Node->getOperand(i)));
636 Changed |= Ops.back() != Node->getOperand(i);
637 }
638
639 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000640 std::vector<MVT::ValueType> RetTyVTs;
641 RetTyVTs.reserve(Node->getNumValues());
642 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000643 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000644 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
645 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000646 } else {
647 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000648 }
Chris Lattner9242c502005-01-09 19:43:23 +0000649 // Since calls produce multiple values, make sure to remember that we
650 // legalized all of them.
651 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
652 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
653 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000654 }
Chris Lattner68a12142005-01-07 22:12:08 +0000655 case ISD::BR:
656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
657 if (Tmp1 != Node->getOperand(0))
658 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
659 break;
660
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000661 case ISD::BRCOND:
662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000663
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000664 switch (getTypeAction(Node->getOperand(1).getValueType())) {
665 case Expand: assert(0 && "It's impossible to expand bools");
666 case Legal:
667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
668 break;
669 case Promote:
670 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
671 break;
672 }
Nate Begeman371e4952005-08-16 19:49:35 +0000673
674 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
675 default: assert(0 && "This action is not supported yet!");
676 case TargetLowering::Expand:
677 // Expand brcond's setcc into its constituent parts and create a BR_CC
678 // Node.
679 if (Tmp2.getOpcode() == ISD::SETCC) {
680 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
681 Tmp2.getOperand(0), Tmp2.getOperand(1),
682 Node->getOperand(2));
683 } else {
684 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
685 DAG.getCondCode(ISD::SETNE), Tmp2,
686 DAG.getConstant(0, Tmp2.getValueType()),
687 Node->getOperand(2));
688 }
689 break;
690 case TargetLowering::Legal:
691 // Basic block destination (Op#2) is always legal.
692 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
693 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
694 Node->getOperand(2));
695 break;
696 }
697 break;
698 case ISD::BR_CC:
699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
700
701 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
702 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
703 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
704 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
705 Tmp3 != Node->getOperand(3)) {
706 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
707 Tmp2, Tmp3, Node->getOperand(4));
708 }
709 break;
710 } else {
711 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
712 Node->getOperand(2), // LHS
713 Node->getOperand(3), // RHS
714 Node->getOperand(1)));
715 // If we get a SETCC back from legalizing the SETCC node we just
716 // created, then use its LHS, RHS, and CC directly in creating a new
717 // node. Otherwise, select between the true and false value based on
718 // comparing the result of the legalized with zero.
719 if (Tmp2.getOpcode() == ISD::SETCC) {
720 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
721 Tmp2.getOperand(0), Tmp2.getOperand(1),
722 Node->getOperand(4));
723 } else {
724 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
725 DAG.getCondCode(ISD::SETNE),
726 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
727 Node->getOperand(4));
728 }
729 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000730 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000731 case ISD::BRCONDTWOWAY:
732 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
733 switch (getTypeAction(Node->getOperand(1).getValueType())) {
734 case Expand: assert(0 && "It's impossible to expand bools");
735 case Legal:
736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
737 break;
738 case Promote:
739 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
740 break;
741 }
742 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
743 // pair.
744 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
745 case TargetLowering::Promote:
746 default: assert(0 && "This action is not supported yet!");
747 case TargetLowering::Legal:
748 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
749 std::vector<SDOperand> Ops;
750 Ops.push_back(Tmp1);
751 Ops.push_back(Tmp2);
752 Ops.push_back(Node->getOperand(2));
753 Ops.push_back(Node->getOperand(3));
754 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
755 }
756 break;
757 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000758 // If BRTWOWAY_CC is legal for this target, then simply expand this node
759 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
760 // BRCOND/BR pair.
761 if (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other) ==
762 TargetLowering::Legal) {
763 if (Tmp2.getOpcode() == ISD::SETCC) {
764 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
765 Tmp2.getOperand(0), Tmp2.getOperand(1),
766 Node->getOperand(2), Node->getOperand(3));
767 } else {
768 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
769 DAG.getConstant(0, Tmp2.getValueType()),
770 Node->getOperand(2), Node->getOperand(3));
771 }
772 } else {
773 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000774 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000775 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
776 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000777 break;
778 }
779 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000780 case ISD::BRTWOWAY_CC:
781 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
782 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
783 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
784 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
785 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
786 Tmp3 != Node->getOperand(3)) {
787 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
788 Node->getOperand(4), Node->getOperand(5));
789 }
790 break;
791 } else {
792 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
793 Node->getOperand(2), // LHS
794 Node->getOperand(3), // RHS
795 Node->getOperand(1)));
796 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
797 // pair.
798 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
799 default: assert(0 && "This action is not supported yet!");
800 case TargetLowering::Legal:
801 // If we get a SETCC back from legalizing the SETCC node we just
802 // created, then use its LHS, RHS, and CC directly in creating a new
803 // node. Otherwise, select between the true and false value based on
804 // comparing the result of the legalized with zero.
805 if (Tmp2.getOpcode() == ISD::SETCC) {
806 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
807 Tmp2.getOperand(0), Tmp2.getOperand(1),
808 Node->getOperand(4), Node->getOperand(5));
809 } else {
810 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
811 DAG.getConstant(0, Tmp2.getValueType()),
812 Node->getOperand(4), Node->getOperand(5));
813 }
814 break;
815 case TargetLowering::Expand:
816 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
817 Node->getOperand(4));
818 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
819 break;
820 }
821 }
822 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000823 case ISD::LOAD:
824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
825 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000826
Chris Lattnerdc750592005-01-07 07:47:09 +0000827 if (Tmp1 != Node->getOperand(0) ||
828 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000829 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
830 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000831 else
832 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000833
Chris Lattnerea4ca942005-01-07 22:28:47 +0000834 // Since loads produce two values, make sure to remember that we legalized
835 // both of them.
836 AddLegalizedOperand(SDOperand(Node, 0), Result);
837 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
838 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000839
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000840 case ISD::EXTLOAD:
841 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000842 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000843 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
844 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000845
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000846 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000847 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000848 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000849 case TargetLowering::Promote:
850 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000851 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
852 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000853 // Since loads produce two values, make sure to remember that we legalized
854 // both of them.
855 AddLegalizedOperand(SDOperand(Node, 0), Result);
856 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
857 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000858
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000859 case TargetLowering::Legal:
860 if (Tmp1 != Node->getOperand(0) ||
861 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000862 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
863 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000864 else
865 Result = SDOperand(Node, 0);
866
867 // Since loads produce two values, make sure to remember that we legalized
868 // both of them.
869 AddLegalizedOperand(SDOperand(Node, 0), Result);
870 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
871 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000872 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000873 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
874 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
875 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000876 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000877 if (Op.ResNo)
878 return Load.getValue(1);
879 return Result;
880 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000881 assert(Node->getOpcode() != ISD::EXTLOAD &&
882 "EXTLOAD should always be supported!");
883 // Turn the unsupported load into an EXTLOAD followed by an explicit
884 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000885 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
886 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000887 SDOperand ValRes;
888 if (Node->getOpcode() == ISD::SEXTLOAD)
889 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000890 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000891 else
892 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000893 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
894 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
895 if (Op.ResNo)
896 return Result.getValue(1);
897 return ValRes;
898 }
899 assert(0 && "Unreachable");
900 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000901 case ISD::EXTRACT_ELEMENT:
902 // Get both the low and high parts.
903 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
904 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
905 Result = Tmp2; // 1 -> Hi
906 else
907 Result = Tmp1; // 0 -> Lo
908 break;
909
910 case ISD::CopyToReg:
911 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000912
Chris Lattner33182322005-08-16 21:55:35 +0000913 assert(getTypeAction(Node->getOperand(2).getValueType()) == Legal &&
914 "Register type must be legal!");
915 // Legalize the incoming value (must be legal).
916 Tmp2 = LegalizeOp(Node->getOperand(2));
917 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
918 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
919 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000920 break;
921
922 case ISD::RET:
923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
924 switch (Node->getNumOperands()) {
925 case 2: // ret val
926 switch (getTypeAction(Node->getOperand(1).getValueType())) {
927 case Legal:
928 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000929 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000930 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
931 break;
932 case Expand: {
933 SDOperand Lo, Hi;
934 ExpandOp(Node->getOperand(1), Lo, Hi);
935 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000936 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000937 }
938 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000939 Tmp2 = PromoteOp(Node->getOperand(1));
940 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
941 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000942 }
943 break;
944 case 1: // ret void
945 if (Tmp1 != Node->getOperand(0))
946 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
947 break;
948 default: { // ret <values>
949 std::vector<SDOperand> NewValues;
950 NewValues.push_back(Tmp1);
951 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
952 switch (getTypeAction(Node->getOperand(i).getValueType())) {
953 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000954 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000955 break;
956 case Expand: {
957 SDOperand Lo, Hi;
958 ExpandOp(Node->getOperand(i), Lo, Hi);
959 NewValues.push_back(Lo);
960 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000961 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000962 }
963 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000964 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000965 }
966 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
967 break;
968 }
969 }
970 break;
971 case ISD::STORE:
972 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
973 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
974
Chris Lattnere69daaf2005-01-08 06:25:56 +0000975 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000976 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000977 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000978 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000979 DAG.getConstant(FloatToBits(CFP->getValue()),
980 MVT::i32),
981 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000982 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000983 } else {
984 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000985 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000986 DAG.getConstant(DoubleToBits(CFP->getValue()),
987 MVT::i64),
988 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000989 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000990 }
Chris Lattnera4743132005-02-22 07:23:39 +0000991 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000992 }
993
Chris Lattnerdc750592005-01-07 07:47:09 +0000994 switch (getTypeAction(Node->getOperand(1).getValueType())) {
995 case Legal: {
996 SDOperand Val = LegalizeOp(Node->getOperand(1));
997 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
998 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000999 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1000 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001001 break;
1002 }
1003 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001004 // Truncate the value and store the result.
1005 Tmp3 = PromoteOp(Node->getOperand(1));
1006 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001007 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001008 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001009 break;
1010
Chris Lattnerdc750592005-01-07 07:47:09 +00001011 case Expand:
1012 SDOperand Lo, Hi;
1013 ExpandOp(Node->getOperand(1), Lo, Hi);
1014
1015 if (!TLI.isLittleEndian())
1016 std::swap(Lo, Hi);
1017
Chris Lattner55e9cde2005-05-11 04:51:16 +00001018 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1019 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001020 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001021 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1022 getIntPtrConstant(IncrementSize));
1023 assert(isTypeLegal(Tmp2.getValueType()) &&
1024 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001025 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001026 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1027 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001028 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1029 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001030 }
1031 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001032 case ISD::PCMARKER:
1033 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001034 if (Tmp1 != Node->getOperand(0))
1035 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001036 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001037 case ISD::TRUNCSTORE:
1038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1039 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1040
1041 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1042 case Legal:
1043 Tmp2 = LegalizeOp(Node->getOperand(1));
1044 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1045 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +00001046 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001047 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001048 break;
1049 case Promote:
1050 case Expand:
1051 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1052 }
1053 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001054 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001055 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1056 case Expand: assert(0 && "It's impossible to expand bools");
1057 case Legal:
1058 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1059 break;
1060 case Promote:
1061 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1062 break;
1063 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001064 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001065 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001066
1067 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
1068 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001069 case TargetLowering::Expand:
1070 if (Tmp1.getOpcode() == ISD::SETCC) {
1071 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1072 Tmp2, Tmp3,
1073 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1074 } else {
1075 Result = DAG.getSelectCC(Tmp1,
1076 DAG.getConstant(0, Tmp1.getValueType()),
1077 Tmp2, Tmp3, ISD::SETNE);
1078 }
1079 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001080 case TargetLowering::Legal:
1081 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1082 Tmp3 != Node->getOperand(2))
1083 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1084 Tmp1, Tmp2, Tmp3);
1085 break;
1086 case TargetLowering::Promote: {
1087 MVT::ValueType NVT =
1088 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1089 unsigned ExtOp, TruncOp;
1090 if (MVT::isInteger(Tmp2.getValueType())) {
1091 ExtOp = ISD::ZERO_EXTEND;
1092 TruncOp = ISD::TRUNCATE;
1093 } else {
1094 ExtOp = ISD::FP_EXTEND;
1095 TruncOp = ISD::FP_ROUND;
1096 }
1097 // Promote each of the values to the new type.
1098 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1099 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1100 // Perform the larger operation, then round down.
1101 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1102 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1103 break;
1104 }
1105 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001106 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001107 case ISD::SELECT_CC:
1108 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1109 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1110
1111 if (getTypeAction(Node->getOperand(0).getValueType()) == Legal) {
1112 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1113 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1114 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1115 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1116 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1117 Tmp3, Tmp4, Node->getOperand(4));
1118 }
1119 break;
1120 } else {
1121 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1122 Node->getOperand(0), // LHS
1123 Node->getOperand(1), // RHS
1124 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001125 // If we get a SETCC back from legalizing the SETCC node we just
1126 // created, then use its LHS, RHS, and CC directly in creating a new
1127 // node. Otherwise, select between the true and false value based on
1128 // comparing the result of the legalized with zero.
1129 if (Tmp1.getOpcode() == ISD::SETCC) {
1130 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1131 Tmp1.getOperand(0), Tmp1.getOperand(1),
1132 Tmp3, Tmp4, Tmp1.getOperand(2));
1133 } else {
1134 Result = DAG.getSelectCC(Tmp1,
1135 DAG.getConstant(0, Tmp1.getValueType()),
1136 Tmp3, Tmp4, ISD::SETNE);
1137 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001138 }
1139 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001140 case ISD::SETCC:
1141 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1142 case Legal:
1143 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1144 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1145 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001146 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1147 Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001148 break;
1149 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001150 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1151 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1152
1153 // If this is an FP compare, the operands have already been extended.
1154 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1155 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001156 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001157
1158 // Otherwise, we have to insert explicit sign or zero extends. Note
1159 // that we could insert sign extends for ALL conditions, but zero extend
1160 // is cheaper on many machines (an AND instead of two shifts), so prefer
1161 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001162 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001163 default: assert(0 && "Unknown integer comparison!");
1164 case ISD::SETEQ:
1165 case ISD::SETNE:
1166 case ISD::SETUGE:
1167 case ISD::SETUGT:
1168 case ISD::SETULE:
1169 case ISD::SETULT:
1170 // ALL of these operations will work if we either sign or zero extend
1171 // the operands (including the unsigned comparisons!). Zero extend is
1172 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001173 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1174 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001175 break;
1176 case ISD::SETGE:
1177 case ISD::SETGT:
1178 case ISD::SETLT:
1179 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001180 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1181 DAG.getValueType(VT));
1182 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1183 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001184 break;
1185 }
1186
1187 }
Chris Lattnerd47675e2005-08-09 20:20:18 +00001188 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1189 Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001190 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001191 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001192 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1193 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1194 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001195 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001196 case ISD::SETEQ:
1197 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001198 if (RHSLo == RHSHi)
1199 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1200 if (RHSCST->isAllOnesValue()) {
1201 // Comparison to -1.
1202 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001203 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1204 RHSLo, Node->getOperand(2));
Misha Brukman835702a2005-04-21 22:36:52 +00001205 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001206 }
1207
Chris Lattnerdc750592005-01-07 07:47:09 +00001208 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1209 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1210 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001211 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1212 DAG.getConstant(0, Tmp1.getValueType()),
1213 Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001214 break;
1215 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001216 // If this is a comparison of the sign bit, just look at the top part.
1217 // X > -1, x < 0
1218 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001219 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001220 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001221 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001222 (CST->isAllOnesValue()))) // X > -1
Chris Lattnerd47675e2005-08-09 20:20:18 +00001223 return DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1224 Node->getOperand(2));
Chris Lattneraedcabe2005-04-12 02:19:10 +00001225
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 // FIXME: This generated code sucks.
1227 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001228 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001229 default: assert(0 && "Unknown integer setcc!");
1230 case ISD::SETLT:
1231 case ISD::SETULT: LowCC = ISD::SETULT; break;
1232 case ISD::SETGT:
1233 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1234 case ISD::SETLE:
1235 case ISD::SETULE: LowCC = ISD::SETULE; break;
1236 case ISD::SETGE:
1237 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1238 }
Misha Brukman835702a2005-04-21 22:36:52 +00001239
Chris Lattnerdc750592005-01-07 07:47:09 +00001240 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1241 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1242 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1243
1244 // NOTE: on targets without efficient SELECT of bools, we can always use
1245 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001246 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1247 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1248 Node->getOperand(2));
1249 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001250 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1251 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001252 break;
1253 }
1254 }
1255 break;
1256
Chris Lattner85d70c62005-01-11 05:57:22 +00001257 case ISD::MEMSET:
1258 case ISD::MEMCPY:
1259 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001260 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001261 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1262
1263 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1264 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1265 case Expand: assert(0 && "Cannot expand a byte!");
1266 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001267 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001268 break;
1269 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001270 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001271 break;
1272 }
1273 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001274 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001275 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001276
1277 SDOperand Tmp4;
1278 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001279 case Expand: {
1280 // Length is too big, just take the lo-part of the length.
1281 SDOperand HiPart;
1282 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1283 break;
1284 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001285 case Legal:
1286 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001287 break;
1288 case Promote:
1289 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001290 break;
1291 }
1292
1293 SDOperand Tmp5;
1294 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1295 case Expand: assert(0 && "Cannot expand this yet!");
1296 case Legal:
1297 Tmp5 = LegalizeOp(Node->getOperand(4));
1298 break;
1299 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001300 Tmp5 = PromoteOp(Node->getOperand(4));
1301 break;
1302 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001303
1304 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1305 default: assert(0 && "This action not implemented for this operation!");
1306 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001307 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1308 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1309 Tmp5 != Node->getOperand(4)) {
1310 std::vector<SDOperand> Ops;
1311 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1312 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1313 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1314 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001315 break;
1316 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001317 // Otherwise, the target does not support this operation. Lower the
1318 // operation to an explicit libcall as appropriate.
1319 MVT::ValueType IntPtr = TLI.getPointerTy();
1320 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1321 std::vector<std::pair<SDOperand, const Type*> > Args;
1322
Reid Spencer6dced922005-01-12 14:53:45 +00001323 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001324 if (Node->getOpcode() == ISD::MEMSET) {
1325 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1326 // Extend the ubyte argument to be an int value for the call.
1327 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1328 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1329 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1330
1331 FnName = "memset";
1332 } else if (Node->getOpcode() == ISD::MEMCPY ||
1333 Node->getOpcode() == ISD::MEMMOVE) {
1334 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1335 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1336 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1337 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1338 } else {
1339 assert(0 && "Unknown op!");
1340 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001341
Chris Lattner85d70c62005-01-11 05:57:22 +00001342 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001343 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001344 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001345 Result = CallResult.second;
1346 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001347 break;
1348 }
1349 case TargetLowering::Custom:
1350 std::vector<SDOperand> Ops;
1351 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1352 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1353 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001354 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001355 Result = LegalizeOp(Result);
1356 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001357 }
1358 break;
1359 }
Chris Lattner5385db52005-05-09 20:23:03 +00001360
1361 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001362 Tmp1 = LegalizeOp(Node->getOperand(0));
1363 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001364
Chris Lattner86535992005-05-14 07:45:46 +00001365 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1366 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1367 std::vector<SDOperand> Ops;
1368 Ops.push_back(Tmp1);
1369 Ops.push_back(Tmp2);
1370 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1371 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001372 Result = SDOperand(Node, 0);
1373 // Since these produce two values, make sure to remember that we legalized
1374 // both of them.
1375 AddLegalizedOperand(SDOperand(Node, 0), Result);
1376 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1377 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001378 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001379 Tmp1 = LegalizeOp(Node->getOperand(0));
1380 Tmp2 = LegalizeOp(Node->getOperand(1));
1381 Tmp3 = LegalizeOp(Node->getOperand(2));
1382 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1383 Tmp3 != Node->getOperand(2))
1384 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1385 break;
1386
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001387 case ISD::READIO:
1388 Tmp1 = LegalizeOp(Node->getOperand(0));
1389 Tmp2 = LegalizeOp(Node->getOperand(1));
1390
1391 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1392 case TargetLowering::Custom:
1393 default: assert(0 && "This action not implemented for this operation!");
1394 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001395 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1396 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1397 std::vector<SDOperand> Ops;
1398 Ops.push_back(Tmp1);
1399 Ops.push_back(Tmp2);
1400 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1401 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001402 Result = SDOperand(Node, 0);
1403 break;
1404 case TargetLowering::Expand:
1405 // Replace this with a load from memory.
1406 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1407 Node->getOperand(1), DAG.getSrcValue(NULL));
1408 Result = LegalizeOp(Result);
1409 break;
1410 }
1411
1412 // Since these produce two values, make sure to remember that we legalized
1413 // both of them.
1414 AddLegalizedOperand(SDOperand(Node, 0), Result);
1415 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1416 return Result.getValue(Op.ResNo);
1417
1418 case ISD::WRITEIO:
1419 Tmp1 = LegalizeOp(Node->getOperand(0));
1420 Tmp2 = LegalizeOp(Node->getOperand(1));
1421 Tmp3 = LegalizeOp(Node->getOperand(2));
1422
1423 switch (TLI.getOperationAction(Node->getOpcode(),
1424 Node->getOperand(1).getValueType())) {
1425 case TargetLowering::Custom:
1426 default: assert(0 && "This action not implemented for this operation!");
1427 case TargetLowering::Legal:
1428 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1429 Tmp3 != Node->getOperand(2))
1430 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1431 break;
1432 case TargetLowering::Expand:
1433 // Replace this with a store to memory.
1434 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1435 Node->getOperand(1), Node->getOperand(2),
1436 DAG.getSrcValue(NULL));
1437 Result = LegalizeOp(Result);
1438 break;
1439 }
1440 break;
1441
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001442 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001443 case ISD::SUB_PARTS:
1444 case ISD::SHL_PARTS:
1445 case ISD::SRA_PARTS:
1446 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001447 std::vector<SDOperand> Ops;
1448 bool Changed = false;
1449 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1450 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1451 Changed |= Ops.back() != Node->getOperand(i);
1452 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001453 if (Changed) {
1454 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1455 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1456 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001457
1458 // Since these produce multiple values, make sure to remember that we
1459 // legalized all of them.
1460 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1461 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1462 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001463 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001464
1465 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001466 case ISD::ADD:
1467 case ISD::SUB:
1468 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001469 case ISD::MULHS:
1470 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001471 case ISD::UDIV:
1472 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001473 case ISD::AND:
1474 case ISD::OR:
1475 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001476 case ISD::SHL:
1477 case ISD::SRL:
1478 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001479 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001480 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1481 case Expand: assert(0 && "Not possible");
1482 case Legal:
1483 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1484 break;
1485 case Promote:
1486 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1487 break;
1488 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001489 if (Tmp1 != Node->getOperand(0) ||
1490 Tmp2 != Node->getOperand(1))
1491 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1492 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001493
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001494 case ISD::UREM:
1495 case ISD::SREM:
1496 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1497 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1498 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1499 case TargetLowering::Legal:
1500 if (Tmp1 != Node->getOperand(0) ||
1501 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001502 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001503 Tmp2);
1504 break;
1505 case TargetLowering::Promote:
1506 case TargetLowering::Custom:
1507 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001508 case TargetLowering::Expand:
1509 if (MVT::isInteger(Node->getValueType(0))) {
1510 MVT::ValueType VT = Node->getValueType(0);
1511 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1512 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1513 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1514 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1515 } else {
1516 // Floating point mod -> fmod libcall.
1517 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1518 SDOperand Dummy;
1519 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001520 }
1521 break;
1522 }
1523 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001524
Andrew Lenharth5e177822005-05-03 17:19:30 +00001525 case ISD::CTPOP:
1526 case ISD::CTTZ:
1527 case ISD::CTLZ:
1528 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1529 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1530 case TargetLowering::Legal:
1531 if (Tmp1 != Node->getOperand(0))
1532 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1533 break;
1534 case TargetLowering::Promote: {
1535 MVT::ValueType OVT = Tmp1.getValueType();
1536 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001537
1538 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001539 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1540 // Perform the larger operation, then subtract if needed.
1541 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1542 switch(Node->getOpcode())
1543 {
1544 case ISD::CTPOP:
1545 Result = Tmp1;
1546 break;
1547 case ISD::CTTZ:
1548 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001549 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1550 DAG.getConstant(getSizeInBits(NVT), NVT),
1551 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001552 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001553 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1554 break;
1555 case ISD::CTLZ:
1556 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001557 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1558 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001559 getSizeInBits(OVT), NVT));
1560 break;
1561 }
1562 break;
1563 }
1564 case TargetLowering::Custom:
1565 assert(0 && "Cannot custom handle this yet!");
1566 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001567 switch(Node->getOpcode())
1568 {
1569 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001570 static const uint64_t mask[6] = {
1571 0x5555555555555555ULL, 0x3333333333333333ULL,
1572 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1573 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1574 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001575 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001576 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1577 unsigned len = getSizeInBits(VT);
1578 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001579 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001580 Tmp2 = DAG.getConstant(mask[i], VT);
1581 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001582 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001583 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1584 DAG.getNode(ISD::AND, VT,
1585 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1586 Tmp2));
1587 }
1588 Result = Tmp1;
1589 break;
1590 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001591 case ISD::CTLZ: {
1592 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001593 x = x | (x >> 1);
1594 x = x | (x >> 2);
1595 ...
1596 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001597 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001598 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001599
Chris Lattner56add052005-05-11 18:35:21 +00001600 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1601 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001602 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1603 unsigned len = getSizeInBits(VT);
1604 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1605 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001606 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001607 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1608 }
1609 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001610 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001611 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001612 }
1613 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001614 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001615 // unless the target has ctlz but not ctpop, in which case we use:
1616 // { return 32 - nlz(~x & (x-1)); }
1617 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001618 MVT::ValueType VT = Tmp1.getValueType();
1619 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001620 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001621 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1622 DAG.getNode(ISD::SUB, VT, Tmp1,
1623 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001624 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1625 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1626 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001627 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001628 DAG.getConstant(getSizeInBits(VT), VT),
1629 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1630 } else {
1631 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1632 }
Chris Lattner72473242005-05-11 05:27:09 +00001633 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001634 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001635 default:
1636 assert(0 && "Cannot expand this yet!");
1637 break;
1638 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001639 break;
1640 }
1641 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001642
Chris Lattner13fe99c2005-04-02 05:00:07 +00001643 // Unary operators
1644 case ISD::FABS:
1645 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001646 case ISD::FSQRT:
1647 case ISD::FSIN:
1648 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001649 Tmp1 = LegalizeOp(Node->getOperand(0));
1650 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1651 case TargetLowering::Legal:
1652 if (Tmp1 != Node->getOperand(0))
1653 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1654 break;
1655 case TargetLowering::Promote:
1656 case TargetLowering::Custom:
1657 assert(0 && "Cannot promote/custom handle this yet!");
1658 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001659 switch(Node->getOpcode()) {
1660 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001661 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1662 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1663 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1664 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001665 break;
1666 }
1667 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001668 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1669 MVT::ValueType VT = Node->getValueType(0);
1670 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001671 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001672 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1673 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1674 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001675 break;
1676 }
1677 case ISD::FSQRT:
1678 case ISD::FSIN:
1679 case ISD::FCOS: {
1680 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001681 const char *FnName = 0;
1682 switch(Node->getOpcode()) {
1683 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1684 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1685 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1686 default: assert(0 && "Unreachable!");
1687 }
Nate Begeman77558da2005-08-04 21:43:28 +00001688 SDOperand Dummy;
1689 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001690 break;
1691 }
1692 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001693 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001694 }
1695 break;
1696 }
1697 break;
1698
1699 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001700 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001701 case ISD::UINT_TO_FP: {
1702 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001703 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1704 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001705 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001706 Node->getOperand(0).getValueType())) {
1707 default: assert(0 && "Unknown operation action!");
1708 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001709 Result = ExpandLegalINT_TO_FP(isSigned,
1710 LegalizeOp(Node->getOperand(0)),
1711 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001712 AddLegalizedOperand(Op, Result);
1713 return Result;
1714 case TargetLowering::Promote:
1715 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1716 Node->getValueType(0),
1717 isSigned);
1718 AddLegalizedOperand(Op, Result);
1719 return Result;
1720 case TargetLowering::Legal:
1721 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001722 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001723
Chris Lattnerdc750592005-01-07 07:47:09 +00001724 Tmp1 = LegalizeOp(Node->getOperand(0));
1725 if (Tmp1 != Node->getOperand(0))
1726 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1727 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001728 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001729 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1730 Node->getValueType(0), Node->getOperand(0));
1731 break;
1732 case Promote:
1733 if (isSigned) {
1734 Result = PromoteOp(Node->getOperand(0));
1735 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1736 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1737 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1738 } else {
1739 Result = PromoteOp(Node->getOperand(0));
1740 Result = DAG.getZeroExtendInReg(Result,
1741 Node->getOperand(0).getValueType());
1742 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001743 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001744 break;
1745 }
1746 break;
1747 }
1748 case ISD::TRUNCATE:
1749 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1750 case Legal:
1751 Tmp1 = LegalizeOp(Node->getOperand(0));
1752 if (Tmp1 != Node->getOperand(0))
1753 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1754 break;
1755 case Expand:
1756 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1757
1758 // Since the result is legal, we should just be able to truncate the low
1759 // part of the source.
1760 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1761 break;
1762 case Promote:
1763 Result = PromoteOp(Node->getOperand(0));
1764 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1765 break;
1766 }
1767 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001768
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001769 case ISD::FP_TO_SINT:
1770 case ISD::FP_TO_UINT:
1771 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1772 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001773 Tmp1 = LegalizeOp(Node->getOperand(0));
1774
Chris Lattner44fe26f2005-07-29 00:11:56 +00001775 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1776 default: assert(0 && "Unknown operation action!");
1777 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00001778 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1779 SDOperand True, False;
1780 MVT::ValueType VT = Node->getOperand(0).getValueType();
1781 MVT::ValueType NVT = Node->getValueType(0);
1782 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1783 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1784 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1785 Node->getOperand(0), Tmp2, ISD::SETLT);
1786 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1787 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1788 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1789 Tmp2));
1790 False = DAG.getNode(ISD::XOR, NVT, False,
1791 DAG.getConstant(1ULL << ShiftAmt, NVT));
1792 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00001793 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00001794 } else {
1795 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1796 }
1797 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001798 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001799 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001800 Node->getOpcode() == ISD::FP_TO_SINT);
1801 AddLegalizedOperand(Op, Result);
1802 return Result;
1803 case TargetLowering::Legal:
1804 break;
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001805 case TargetLowering::Custom:
1806 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1807 Result = TLI.LowerOperation(Result, DAG);
1808 AddLegalizedOperand(Op, Result);
1809 NeedsAnotherIteration = true;
1810 return Result;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001811 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001812
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001813 if (Tmp1 != Node->getOperand(0))
1814 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1815 break;
1816 case Expand:
1817 assert(0 && "Shouldn't need to expand other operators here!");
1818 case Promote:
1819 Result = PromoteOp(Node->getOperand(0));
1820 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1821 break;
1822 }
1823 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001824
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001825 case ISD::ZERO_EXTEND:
1826 case ISD::SIGN_EXTEND:
1827 case ISD::FP_EXTEND:
1828 case ISD::FP_ROUND:
1829 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1830 case Legal:
1831 Tmp1 = LegalizeOp(Node->getOperand(0));
1832 if (Tmp1 != Node->getOperand(0))
1833 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1834 break;
1835 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001836 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001837
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001838 case Promote:
1839 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001840 case ISD::ZERO_EXTEND:
1841 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001842 // NOTE: Any extend would work here...
1843 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001844 Result = DAG.getZeroExtendInReg(Result,
1845 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001846 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001847 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001848 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001849 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001850 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001851 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001852 Result,
1853 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001854 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001855 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001856 Result = PromoteOp(Node->getOperand(0));
1857 if (Result.getValueType() != Op.getValueType())
1858 // Dynamically dead while we have only 2 FP types.
1859 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1860 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001861 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001862 Result = PromoteOp(Node->getOperand(0));
1863 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1864 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001865 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001866 }
1867 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001868 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001869 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001870 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001871 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001872
1873 // If this operation is not supported, convert it to a shl/shr or load/store
1874 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001875 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1876 default: assert(0 && "This action not supported for this op yet!");
1877 case TargetLowering::Legal:
1878 if (Tmp1 != Node->getOperand(0))
1879 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001880 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001881 break;
1882 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001883 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001884 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001885 // NOTE: we could fall back on load/store here too for targets without
1886 // SAR. However, it is doubtful that any exist.
1887 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1888 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001889 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001890 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1891 Node->getOperand(0), ShiftCst);
1892 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1893 Result, ShiftCst);
1894 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1895 // The only way we can lower this is to turn it into a STORETRUNC,
1896 // EXTLOAD pair, targetting a temporary location (a stack slot).
1897
1898 // NOTE: there is a choice here between constantly creating new stack
1899 // slots and always reusing the same one. We currently always create
1900 // new ones, as reuse may inhibit scheduling.
1901 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1902 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1903 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1904 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001905 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001906 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1907 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1908 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001909 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001910 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001911 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1912 Result, StackSlot, DAG.getSrcValue(NULL),
1913 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001914 } else {
1915 assert(0 && "Unknown op");
1916 }
1917 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001918 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001919 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001920 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001921 }
Chris Lattner99222f72005-01-15 07:15:18 +00001922 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001923
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001924 // Note that LegalizeOp may be reentered even from single-use nodes, which
1925 // means that we always must cache transformed nodes.
1926 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001927 return Result;
1928}
1929
Chris Lattner4d978642005-01-15 22:16:26 +00001930/// PromoteOp - Given an operation that produces a value in an invalid type,
1931/// promote it to compute the value into a larger type. The produced value will
1932/// have the correct bits for the low portion of the register, but no guarantee
1933/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001934SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1935 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001936 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001937 assert(getTypeAction(VT) == Promote &&
1938 "Caller should expand or legalize operands that are not promotable!");
1939 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1940 "Cannot promote to smaller type!");
1941
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001942 SDOperand Tmp1, Tmp2, Tmp3;
1943
1944 SDOperand Result;
1945 SDNode *Node = Op.Val;
1946
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001947 if (!Node->hasOneUse()) {
1948 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1949 if (I != PromotedNodes.end()) return I->second;
1950 } else {
1951 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1952 }
1953
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001954 // Promotion needs an optimization step to clean up after it, and is not
1955 // careful to avoid operations the target does not support. Make sure that
1956 // all generated operations are legalized in the next iteration.
1957 NeedsAnotherIteration = true;
1958
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001959 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00001960 case ISD::CopyFromReg:
1961 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001962 default:
1963 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1964 assert(0 && "Do not know how to promote this operator!");
1965 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001966 case ISD::UNDEF:
1967 Result = DAG.getNode(ISD::UNDEF, NVT);
1968 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001969 case ISD::Constant:
1970 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1971 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1972 break;
1973 case ISD::ConstantFP:
1974 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1975 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1976 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001977
Chris Lattner2cb338d2005-01-18 02:59:52 +00001978 case ISD::SETCC:
1979 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1980 "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00001981 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
1982 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00001983 Result = LegalizeOp(Result);
1984 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001985
1986 case ISD::TRUNCATE:
1987 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1988 case Legal:
1989 Result = LegalizeOp(Node->getOperand(0));
1990 assert(Result.getValueType() >= NVT &&
1991 "This truncation doesn't make sense!");
1992 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1993 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1994 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001995 case Promote:
1996 // The truncation is not required, because we don't guarantee anything
1997 // about high bits anyway.
1998 Result = PromoteOp(Node->getOperand(0));
1999 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002000 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002001 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2002 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002003 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002004 }
2005 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002006 case ISD::SIGN_EXTEND:
2007 case ISD::ZERO_EXTEND:
2008 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2009 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2010 case Legal:
2011 // Input is legal? Just do extend all the way to the larger type.
2012 Result = LegalizeOp(Node->getOperand(0));
2013 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2014 break;
2015 case Promote:
2016 // Promote the reg if it's smaller.
2017 Result = PromoteOp(Node->getOperand(0));
2018 // The high bits are not guaranteed to be anything. Insert an extend.
2019 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002020 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002021 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002022 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002023 Result = DAG.getZeroExtendInReg(Result,
2024 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002025 break;
2026 }
2027 break;
2028
2029 case ISD::FP_EXTEND:
2030 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2031 case ISD::FP_ROUND:
2032 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2033 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2034 case Promote: assert(0 && "Unreachable with 2 FP types!");
2035 case Legal:
2036 // Input is legal? Do an FP_ROUND_INREG.
2037 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002038 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2039 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002040 break;
2041 }
2042 break;
2043
2044 case ISD::SINT_TO_FP:
2045 case ISD::UINT_TO_FP:
2046 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2047 case Legal:
2048 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002049 // No extra round required here.
2050 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002051 break;
2052
2053 case Promote:
2054 Result = PromoteOp(Node->getOperand(0));
2055 if (Node->getOpcode() == ISD::SINT_TO_FP)
2056 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002057 Result,
2058 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002059 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002060 Result = DAG.getZeroExtendInReg(Result,
2061 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002062 // No extra round required here.
2063 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002064 break;
2065 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002066 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2067 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002068 // Round if we cannot tolerate excess precision.
2069 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002070 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2071 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002072 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002073 }
Chris Lattner4d978642005-01-15 22:16:26 +00002074 break;
2075
2076 case ISD::FP_TO_SINT:
2077 case ISD::FP_TO_UINT:
2078 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2079 case Legal:
2080 Tmp1 = LegalizeOp(Node->getOperand(0));
2081 break;
2082 case Promote:
2083 // The input result is prerounded, so we don't have to do anything
2084 // special.
2085 Tmp1 = PromoteOp(Node->getOperand(0));
2086 break;
2087 case Expand:
2088 assert(0 && "not implemented");
2089 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002090 // If we're promoting a UINT to a larger size, check to see if the new node
2091 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2092 // we can use that instead. This allows us to generate better code for
2093 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2094 // legal, such as PowerPC.
2095 if (Node->getOpcode() == ISD::FP_TO_UINT &&
2096 TargetLowering::Legal != TLI.getOperationAction(ISD::FP_TO_UINT, NVT) &&
2097 TargetLowering::Legal == TLI.getOperationAction(ISD::FP_TO_SINT, NVT)) {
2098 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2099 } else {
2100 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2101 }
Chris Lattner4d978642005-01-15 22:16:26 +00002102 break;
2103
Chris Lattner13fe99c2005-04-02 05:00:07 +00002104 case ISD::FABS:
2105 case ISD::FNEG:
2106 Tmp1 = PromoteOp(Node->getOperand(0));
2107 assert(Tmp1.getValueType() == NVT);
2108 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2109 // NOTE: we do not have to do any extra rounding here for
2110 // NoExcessFPPrecision, because we know the input will have the appropriate
2111 // precision, and these operations don't modify precision at all.
2112 break;
2113
Chris Lattner9d6fa982005-04-28 21:44:33 +00002114 case ISD::FSQRT:
2115 case ISD::FSIN:
2116 case ISD::FCOS:
2117 Tmp1 = PromoteOp(Node->getOperand(0));
2118 assert(Tmp1.getValueType() == NVT);
2119 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2120 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002121 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2122 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002123 break;
2124
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002125 case ISD::AND:
2126 case ISD::OR:
2127 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002128 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002129 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002130 case ISD::MUL:
2131 // The input may have strange things in the top bits of the registers, but
2132 // these operations don't care. They may have wierd bits going out, but
2133 // that too is okay if they are integer operations.
2134 Tmp1 = PromoteOp(Node->getOperand(0));
2135 Tmp2 = PromoteOp(Node->getOperand(1));
2136 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2137 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2138
2139 // However, if this is a floating point operation, they will give excess
2140 // precision that we may not be able to tolerate. If we DO allow excess
2141 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002142 // FIXME: Why would we need to round FP ops more than integer ones?
2143 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002144 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002145 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2146 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002147 break;
2148
Chris Lattner4d978642005-01-15 22:16:26 +00002149 case ISD::SDIV:
2150 case ISD::SREM:
2151 // These operators require that their input be sign extended.
2152 Tmp1 = PromoteOp(Node->getOperand(0));
2153 Tmp2 = PromoteOp(Node->getOperand(1));
2154 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002155 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2156 DAG.getValueType(VT));
2157 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2158 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002159 }
2160 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2161
2162 // Perform FP_ROUND: this is probably overly pessimistic.
2163 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002164 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2165 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002166 break;
2167
2168 case ISD::UDIV:
2169 case ISD::UREM:
2170 // These operators require that their input be zero extended.
2171 Tmp1 = PromoteOp(Node->getOperand(0));
2172 Tmp2 = PromoteOp(Node->getOperand(1));
2173 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002174 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2175 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002176 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2177 break;
2178
2179 case ISD::SHL:
2180 Tmp1 = PromoteOp(Node->getOperand(0));
2181 Tmp2 = LegalizeOp(Node->getOperand(1));
2182 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2183 break;
2184 case ISD::SRA:
2185 // The input value must be properly sign extended.
2186 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002187 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2188 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002189 Tmp2 = LegalizeOp(Node->getOperand(1));
2190 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2191 break;
2192 case ISD::SRL:
2193 // The input value must be properly zero extended.
2194 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002195 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002196 Tmp2 = LegalizeOp(Node->getOperand(1));
2197 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2198 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002199 case ISD::LOAD:
2200 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2201 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00002202 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00002203 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002204 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2205 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00002206 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002207 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2208 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002209
2210 // Remember that we legalized the chain.
2211 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2212 break;
2213 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002214 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2215 case Expand: assert(0 && "It's impossible to expand bools");
2216 case Legal:
2217 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2218 break;
2219 case Promote:
2220 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2221 break;
2222 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002223 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2224 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2225 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2226 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002227 case ISD::SELECT_CC:
2228 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2229 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2230 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2231 Node->getOperand(1), Tmp2, Tmp3,
2232 Node->getOperand(4));
2233 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002234 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002235 case ISD::CALL: {
2236 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2237 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2238
Chris Lattner3d95c142005-01-19 20:24:35 +00002239 std::vector<SDOperand> Ops;
2240 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2241 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2242
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002243 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2244 "Can only promote single result calls");
2245 std::vector<MVT::ValueType> RetTyVTs;
2246 RetTyVTs.reserve(2);
2247 RetTyVTs.push_back(NVT);
2248 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002249 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2250 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002251 Result = SDOperand(NC, 0);
2252
2253 // Insert the new chain mapping.
2254 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2255 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002256 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002257 case ISD::CTPOP:
2258 case ISD::CTTZ:
2259 case ISD::CTLZ:
2260 Tmp1 = Node->getOperand(0);
2261 //Zero extend the argument
2262 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2263 // Perform the larger operation, then subtract if needed.
2264 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2265 switch(Node->getOpcode())
2266 {
2267 case ISD::CTPOP:
2268 Result = Tmp1;
2269 break;
2270 case ISD::CTTZ:
2271 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002272 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002273 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002274 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002275 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2276 break;
2277 case ISD::CTLZ:
2278 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002279 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2280 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002281 getSizeInBits(VT), NVT));
2282 break;
2283 }
2284 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002285 }
2286
2287 assert(Result.Val && "Didn't set a result!");
2288 AddPromotedOperand(Op, Result);
2289 return Result;
2290}
Chris Lattnerdc750592005-01-07 07:47:09 +00002291
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002292/// ExpandAddSub - Find a clever way to expand this add operation into
2293/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002294void SelectionDAGLegalize::
2295ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2296 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002297 // Expand the subcomponents.
2298 SDOperand LHSL, LHSH, RHSL, RHSH;
2299 ExpandOp(LHS, LHSL, LHSH);
2300 ExpandOp(RHS, RHSL, RHSH);
2301
Chris Lattner8ffd0042005-04-11 20:29:59 +00002302 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002303 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2304 if (LHSL.getValueType() == MVT::i32) {
2305 SDOperand LowEl;
2306 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2307 if (C->getValue() == 0)
2308 LowEl = RHSL;
2309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2310 if (C->getValue() == 0)
2311 LowEl = LHSL;
2312 if (LowEl.Val) {
2313 // Turn this into an add/sub of the high part only.
2314 SDOperand HiEl =
2315 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2316 LowEl.getValueType(), LHSH, RHSH);
2317 Lo = LowEl;
2318 Hi = HiEl;
2319 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002320 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002321 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002322
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002323 std::vector<SDOperand> Ops;
2324 Ops.push_back(LHSL);
2325 Ops.push_back(LHSH);
2326 Ops.push_back(RHSL);
2327 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002328
2329 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2330 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002331 Hi = Lo.getValue(1);
2332}
2333
Chris Lattner4157c412005-04-02 04:00:59 +00002334void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2335 SDOperand Op, SDOperand Amt,
2336 SDOperand &Lo, SDOperand &Hi) {
2337 // Expand the subcomponents.
2338 SDOperand LHSL, LHSH;
2339 ExpandOp(Op, LHSL, LHSH);
2340
2341 std::vector<SDOperand> Ops;
2342 Ops.push_back(LHSL);
2343 Ops.push_back(LHSH);
2344 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002345 std::vector<MVT::ValueType> VTs;
2346 VTs.push_back(LHSL.getValueType());
2347 VTs.push_back(LHSH.getValueType());
2348 VTs.push_back(Amt.getValueType());
2349 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002350 Hi = Lo.getValue(1);
2351}
2352
2353
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002354/// ExpandShift - Try to find a clever way to expand this shift operation out to
2355/// smaller elements. If we can't find a way that is more efficient than a
2356/// libcall on this target, return false. Otherwise, return true with the
2357/// low-parts expanded into Lo and Hi.
2358bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2359 SDOperand &Lo, SDOperand &Hi) {
2360 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2361 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002362
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002363 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002364 SDOperand ShAmt = LegalizeOp(Amt);
2365 MVT::ValueType ShTy = ShAmt.getValueType();
2366 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2367 unsigned NVTBits = MVT::getSizeInBits(NVT);
2368
2369 // Handle the case when Amt is an immediate. Other cases are currently broken
2370 // and are disabled.
2371 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2372 unsigned Cst = CN->getValue();
2373 // Expand the incoming operand to be shifted, so that we have its parts
2374 SDOperand InL, InH;
2375 ExpandOp(Op, InL, InH);
2376 switch(Opc) {
2377 case ISD::SHL:
2378 if (Cst > VTBits) {
2379 Lo = DAG.getConstant(0, NVT);
2380 Hi = DAG.getConstant(0, NVT);
2381 } else if (Cst > NVTBits) {
2382 Lo = DAG.getConstant(0, NVT);
2383 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002384 } else if (Cst == NVTBits) {
2385 Lo = DAG.getConstant(0, NVT);
2386 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002387 } else {
2388 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2389 Hi = DAG.getNode(ISD::OR, NVT,
2390 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2391 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2392 }
2393 return true;
2394 case ISD::SRL:
2395 if (Cst > VTBits) {
2396 Lo = DAG.getConstant(0, NVT);
2397 Hi = DAG.getConstant(0, NVT);
2398 } else if (Cst > NVTBits) {
2399 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2400 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002401 } else if (Cst == NVTBits) {
2402 Lo = InH;
2403 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002404 } else {
2405 Lo = DAG.getNode(ISD::OR, NVT,
2406 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2407 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2408 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2409 }
2410 return true;
2411 case ISD::SRA:
2412 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002413 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002414 DAG.getConstant(NVTBits-1, ShTy));
2415 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002416 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002417 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002418 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002419 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002420 } else if (Cst == NVTBits) {
2421 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002422 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002423 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002424 } else {
2425 Lo = DAG.getNode(ISD::OR, NVT,
2426 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2427 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2428 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2429 }
2430 return true;
2431 }
2432 }
2433 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2434 // so disable it for now. Currently targets are handling this via SHL_PARTS
2435 // and friends.
2436 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002437
2438 // If we have an efficient select operation (or if the selects will all fold
2439 // away), lower to some complex code, otherwise just emit the libcall.
2440 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2441 !isa<ConstantSDNode>(Amt))
2442 return false;
2443
2444 SDOperand InL, InH;
2445 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002446 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2447 DAG.getConstant(NVTBits, ShTy), ShAmt);
2448
Chris Lattner4d25c042005-01-20 20:29:23 +00002449 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002450 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2451 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002452
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002453 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2454 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2455 DAG.getConstant(NVTBits-1, ShTy));
2456 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2457 DAG.getConstant(NVTBits-1, ShTy));
2458 }
2459
2460 if (Opc == ISD::SHL) {
2461 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2462 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2463 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002464 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002465
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002466 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2467 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2468 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002469 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002470 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2471 DAG.getConstant(32, ShTy),
2472 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002473 DAG.getConstant(0, NVT),
2474 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002475 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002476 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002477 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002478 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002479
2480 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002481 if (Opc == ISD::SRA)
2482 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2483 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002484 else
2485 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002486 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002487 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002488 }
2489 return true;
2490}
Chris Lattneraac464e2005-01-21 06:05:23 +00002491
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002492/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2493/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002494/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002495static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002496 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002497
Chris Lattner2dce7032005-05-12 23:24:06 +00002498 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002499 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002500 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002501 Found = Node;
2502 return;
2503 }
2504
2505 // Otherwise, scan the operands of Node to see if any of them is a call.
2506 assert(Node->getNumOperands() != 0 &&
2507 "All leaves should have depth equal to the entry node!");
2508 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002509 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002510
2511 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002512 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002513 Found);
2514}
2515
2516
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002517/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2518/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002519/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002520static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2521 std::set<SDNode*> &Visited) {
2522 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2523 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002524
Chris Lattner2dce7032005-05-12 23:24:06 +00002525 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002526 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002527 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002528 Found = Node;
2529 return;
2530 }
2531
2532 // Otherwise, scan the operands of Node to see if any of them is a call.
2533 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2534 if (UI == E) return;
2535 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002536 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002537
2538 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002539 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002540}
2541
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002542/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002543/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002544static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002545 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002546 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002547 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002548 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002549
2550 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002551 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002552
Chris Lattner4add7e32005-01-23 04:42:50 +00002553 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002554 if (TheChain.getValueType() != MVT::Other)
2555 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002556 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002557
2558 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002559 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002560
Chris Lattner4add7e32005-01-23 04:42:50 +00002561 // Make sure to only follow users of our token chain.
2562 SDNode *User = *UI;
2563 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2564 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002565 if (SDNode *Result = FindCallSeqEnd(User))
2566 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002567 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002568 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002569}
2570
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002571/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002572/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002573static SDNode *FindCallSeqStart(SDNode *Node) {
2574 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002575 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002576
2577 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2578 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002579 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002580}
2581
2582
Chris Lattner4add7e32005-01-23 04:42:50 +00002583/// FindInputOutputChains - If we are replacing an operation with a call we need
2584/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002585/// properly serialize the calls in the block. The returned operand is the
2586/// input chain value for the new call (e.g. the entry node or the previous
2587/// call), and OutChain is set to be the chain node to update to point to the
2588/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002589static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2590 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002591 SDNode *LatestCallSeqStart = Entry.Val;
2592 SDNode *LatestCallSeqEnd = 0;
2593 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2594 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002595
Chris Lattner2dce7032005-05-12 23:24:06 +00002596 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002597 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002598 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2599 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002600 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2601 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002602 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002603 LatestCallSeqEnd = Entry.Val;
2604 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002605
Chris Lattner06bbeb62005-05-11 19:02:11 +00002606 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002607 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002608 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002609 std::set<SDNode*> Visited;
2610 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002611
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002612 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002613 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002614 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002615
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002616 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002617}
2618
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002619/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002620void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2621 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002622 // Nothing to splice it into?
2623 if (OutChain == 0) return;
2624
2625 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2626 //OutChain->dump();
2627
2628 // Form a token factor node merging the old inval and the new inval.
2629 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2630 OutChain->getOperand(0));
2631 // Change the node to refer to the new token.
2632 OutChain->setAdjCallChain(InToken);
2633}
Chris Lattner4add7e32005-01-23 04:42:50 +00002634
2635
Chris Lattneraac464e2005-01-21 06:05:23 +00002636// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2637// does not fit into a register, return the lo part and set the hi part to the
2638// by-reg argument. If it does fit into a single register, return the result
2639// and leave the Hi part unset.
2640SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2641 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002642 SDNode *OutChain;
2643 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2644 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002645 if (InChain.Val == 0)
2646 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002647
Chris Lattneraac464e2005-01-21 06:05:23 +00002648 TargetLowering::ArgListTy Args;
2649 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2650 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2651 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2652 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2653 }
2654 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002655
Chris Lattner06bbeb62005-05-11 19:02:11 +00002656 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002657 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002658 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002659 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2660 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002661 SpliceCallInto(CallInfo.second, OutChain);
2662
2663 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002664
2665 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002666 default: assert(0 && "Unknown thing");
2667 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002668 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002669 case Promote:
2670 assert(0 && "Cannot promote this yet!");
2671 case Expand:
2672 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002673 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002674 return Lo;
2675 }
2676}
2677
Chris Lattner4add7e32005-01-23 04:42:50 +00002678
Chris Lattneraac464e2005-01-21 06:05:23 +00002679/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2680/// destination type is legal.
2681SDOperand SelectionDAGLegalize::
2682ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2683 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2684 assert(getTypeAction(Source.getValueType()) == Expand &&
2685 "This is not an expansion!");
2686 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2687
Chris Lattner06bbeb62005-05-11 19:02:11 +00002688 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002689 assert(Source.getValueType() == MVT::i64 &&
2690 "This only works for 64-bit -> FP");
2691 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2692 // incoming integer is set. To handle this, we dynamically test to see if
2693 // it is set, and, if so, add a fudge factor.
2694 SDOperand Lo, Hi;
2695 ExpandOp(Source, Lo, Hi);
2696
Chris Lattner2a4f7312005-05-13 04:45:13 +00002697 // If this is unsigned, and not supported, first perform the conversion to
2698 // signed, then adjust the result if the sign bit is set.
2699 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2700 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2701
Chris Lattnerd47675e2005-08-09 20:20:18 +00002702 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2703 DAG.getConstant(0, Hi.getValueType()),
2704 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002705 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2706 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2707 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002708 uint64_t FF = 0x5f800000ULL;
2709 if (TLI.isLittleEndian()) FF <<= 32;
2710 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002711
2712 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2713 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2714 TLI.getPointerTy());
2715 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2716 SDOperand FudgeInReg;
2717 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002718 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2719 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002720 else {
2721 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002722 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2723 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002724 }
2725 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002726 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002727
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002728 // Check to see if the target has a custom way to lower this. If so, use it.
2729 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2730 default: assert(0 && "This action not implemented for this operation!");
2731 case TargetLowering::Legal:
2732 case TargetLowering::Expand:
2733 break; // This case is handled below.
2734 case TargetLowering::Custom:
2735 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002736 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002737 }
2738
Chris Lattner153587e2005-05-12 07:00:44 +00002739 // Expand the source, then glue it back together for the call. We must expand
2740 // the source in case it is shared (this pass of legalize must traverse it).
2741 SDOperand SrcLo, SrcHi;
2742 ExpandOp(Source, SrcLo, SrcHi);
2743 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2744
Chris Lattner06bbeb62005-05-11 19:02:11 +00002745 SDNode *OutChain = 0;
2746 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2747 DAG.getEntryNode());
2748 const char *FnName = 0;
2749 if (DestTy == MVT::f32)
2750 FnName = "__floatdisf";
2751 else {
2752 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2753 FnName = "__floatdidf";
2754 }
2755
Chris Lattneraac464e2005-01-21 06:05:23 +00002756 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2757
2758 TargetLowering::ArgListTy Args;
2759 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002760
Chris Lattneraac464e2005-01-21 06:05:23 +00002761 Args.push_back(std::make_pair(Source, ArgTy));
2762
2763 // We don't care about token chains for libcalls. We just use the entry
2764 // node as our input and ignore the output chain. This allows us to place
2765 // calls wherever we need them to satisfy data dependences.
2766 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002767
2768 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002769 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2770 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002771
Chris Lattnera5bf1032005-05-12 04:49:08 +00002772 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002773 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002774}
Misha Brukman835702a2005-04-21 22:36:52 +00002775
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002776
2777
Chris Lattnerdc750592005-01-07 07:47:09 +00002778/// ExpandOp - Expand the specified SDOperand into its two component pieces
2779/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2780/// LegalizeNodes map is filled in for any results that are not expanded, the
2781/// ExpandedNodes map is filled in for any results that are expanded, and the
2782/// Lo/Hi values are returned.
2783void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2784 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002785 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002786 SDNode *Node = Op.Val;
2787 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2788 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2789 assert(MVT::isInteger(NVT) && NVT < VT &&
2790 "Cannot expand to FP value or to larger int value!");
2791
2792 // If there is more than one use of this, see if we already expanded it.
2793 // There is no use remembering values that only have a single use, as the map
2794 // entries will never be reused.
2795 if (!Node->hasOneUse()) {
2796 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2797 = ExpandedNodes.find(Op);
2798 if (I != ExpandedNodes.end()) {
2799 Lo = I->second.first;
2800 Hi = I->second.second;
2801 return;
2802 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002803 } else {
2804 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002805 }
2806
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002807 // Expanding to multiple registers needs to perform an optimization step, and
2808 // is not careful to avoid operations the target does not support. Make sure
2809 // that all generated operations are legalized in the next iteration.
2810 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002811
Chris Lattnerdc750592005-01-07 07:47:09 +00002812 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002813 case ISD::CopyFromReg:
2814 assert(0 && "CopyFromReg must be legal!");
2815 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00002816 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2817 assert(0 && "Do not know how to expand this operator!");
2818 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002819 case ISD::UNDEF:
2820 Lo = DAG.getNode(ISD::UNDEF, NVT);
2821 Hi = DAG.getNode(ISD::UNDEF, NVT);
2822 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002823 case ISD::Constant: {
2824 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2825 Lo = DAG.getConstant(Cst, NVT);
2826 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2827 break;
2828 }
2829
Chris Lattner32e08b72005-03-28 22:03:13 +00002830 case ISD::BUILD_PAIR:
2831 // Legalize both operands. FIXME: in the future we should handle the case
2832 // where the two elements are not legal.
2833 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2834 Lo = LegalizeOp(Node->getOperand(0));
2835 Hi = LegalizeOp(Node->getOperand(1));
2836 break;
2837
Chris Lattner55e9cde2005-05-11 04:51:16 +00002838 case ISD::CTPOP:
2839 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002840 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2841 DAG.getNode(ISD::CTPOP, NVT, Lo),
2842 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002843 Hi = DAG.getConstant(0, NVT);
2844 break;
2845
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002846 case ISD::CTLZ: {
2847 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002848 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002849 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2850 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002851 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2852 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002853 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2854 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2855
2856 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2857 Hi = DAG.getConstant(0, NVT);
2858 break;
2859 }
2860
2861 case ISD::CTTZ: {
2862 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002863 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002864 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2865 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002866 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2867 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002868 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2869 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2870
2871 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2872 Hi = DAG.getConstant(0, NVT);
2873 break;
2874 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002875
Chris Lattnerdc750592005-01-07 07:47:09 +00002876 case ISD::LOAD: {
2877 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2878 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002879 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002880
2881 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002882 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002883 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2884 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002885 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002886 //are from the same instruction?
2887 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002888
2889 // Build a factor node to remember that this load is independent of the
2890 // other one.
2891 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2892 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002893
Chris Lattnerdc750592005-01-07 07:47:09 +00002894 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002895 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002896 if (!TLI.isLittleEndian())
2897 std::swap(Lo, Hi);
2898 break;
2899 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002900 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002901 case ISD::CALL: {
2902 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2903 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2904
Chris Lattner3d95c142005-01-19 20:24:35 +00002905 bool Changed = false;
2906 std::vector<SDOperand> Ops;
2907 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2908 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2909 Changed |= Ops.back() != Node->getOperand(i);
2910 }
2911
Chris Lattnerdc750592005-01-07 07:47:09 +00002912 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2913 "Can only expand a call once so far, not i64 -> i16!");
2914
2915 std::vector<MVT::ValueType> RetTyVTs;
2916 RetTyVTs.reserve(3);
2917 RetTyVTs.push_back(NVT);
2918 RetTyVTs.push_back(NVT);
2919 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002920 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2921 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002922 Lo = SDOperand(NC, 0);
2923 Hi = SDOperand(NC, 1);
2924
2925 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002926 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002927 break;
2928 }
2929 case ISD::AND:
2930 case ISD::OR:
2931 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2932 SDOperand LL, LH, RL, RH;
2933 ExpandOp(Node->getOperand(0), LL, LH);
2934 ExpandOp(Node->getOperand(1), RL, RH);
2935 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2936 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2937 break;
2938 }
2939 case ISD::SELECT: {
2940 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002941
2942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2943 case Expand: assert(0 && "It's impossible to expand bools");
2944 case Legal:
2945 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2946 break;
2947 case Promote:
2948 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2949 break;
2950 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002951 ExpandOp(Node->getOperand(1), LL, LH);
2952 ExpandOp(Node->getOperand(2), RL, RH);
2953 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2954 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2955 break;
2956 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002957 case ISD::SELECT_CC: {
2958 SDOperand TL, TH, FL, FH;
2959 ExpandOp(Node->getOperand(2), TL, TH);
2960 ExpandOp(Node->getOperand(3), FL, FH);
2961 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2962 Node->getOperand(1), TL, FL, Node->getOperand(4));
2963 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2964 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00002965 Lo = LegalizeOp(Lo);
2966 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00002967 break;
2968 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002969 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002970 SDOperand In;
2971 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2972 case Expand: assert(0 && "expand-expand not implemented yet!");
2973 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2974 case Promote:
2975 In = PromoteOp(Node->getOperand(0));
2976 // Emit the appropriate sign_extend_inreg to get the value we want.
2977 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002978 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002979 break;
2980 }
2981
Chris Lattnerdc750592005-01-07 07:47:09 +00002982 // The low part is just a sign extension of the input (which degenerates to
2983 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002984 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002985
Chris Lattnerdc750592005-01-07 07:47:09 +00002986 // The high part is obtained by SRA'ing all but one of the bits of the lo
2987 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002988 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002989 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2990 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002991 break;
2992 }
Chris Lattner47844892005-04-03 23:41:52 +00002993 case ISD::ZERO_EXTEND: {
2994 SDOperand In;
2995 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2996 case Expand: assert(0 && "expand-expand not implemented yet!");
2997 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2998 case Promote:
2999 In = PromoteOp(Node->getOperand(0));
3000 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003001 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003002 break;
3003 }
3004
Chris Lattnerdc750592005-01-07 07:47:09 +00003005 // The low part is just a zero extension of the input (which degenerates to
3006 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003007 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003008
Chris Lattnerdc750592005-01-07 07:47:09 +00003009 // The high part is just a zero.
3010 Hi = DAG.getConstant(0, NVT);
3011 break;
Chris Lattner47844892005-04-03 23:41:52 +00003012 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003013 // These operators cannot be expanded directly, emit them as calls to
3014 // library functions.
3015 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003016 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003017 SDOperand Op;
3018 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3019 case Expand: assert(0 && "cannot expand FP!");
3020 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3021 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3022 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003023
Chris Lattner941d84a2005-07-30 01:40:57 +00003024 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3025
Chris Lattnerfe68d752005-07-29 00:33:32 +00003026 // Now that the custom expander is done, expand the result, which is still
3027 // VT.
Chris Lattner941d84a2005-07-30 01:40:57 +00003028 ExpandOp(Op, Lo, Hi);
Chris Lattnerfe68d752005-07-29 00:33:32 +00003029 break;
3030 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003031
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003032 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003033 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003034 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003035 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003036 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003037
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003038 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003039 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3040 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3041 LegalizeOp(Node->getOperand(0)));
3042 // Now that the custom expander is done, expand the result, which is still
3043 // VT.
3044 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
3045 break;
3046 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003047
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003048 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003049 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003050 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003051 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003052 break;
3053
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003054 case ISD::SHL:
3055 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003056 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003057 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003058
3059 // If this target supports SHL_PARTS, use it.
3060 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003061 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3062 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003063 break;
3064 }
3065
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003066 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003067 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003068 break;
3069
3070 case ISD::SRA:
3071 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003072 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003073 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003074
3075 // If this target supports SRA_PARTS, use it.
3076 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003077 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3078 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003079 break;
3080 }
3081
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003082 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003083 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003084 break;
3085 case ISD::SRL:
3086 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003087 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003088 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003089
3090 // If this target supports SRL_PARTS, use it.
3091 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003092 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3093 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003094 break;
3095 }
3096
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003097 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003098 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003099 break;
3100
Misha Brukman835702a2005-04-21 22:36:52 +00003101 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003102 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3103 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003104 break;
3105 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003106 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3107 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003108 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003109 case ISD::MUL: {
3110 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
3111 SDOperand LL, LH, RL, RH;
3112 ExpandOp(Node->getOperand(0), LL, LH);
3113 ExpandOp(Node->getOperand(1), RL, RH);
3114 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3115 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3116 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3117 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3118 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3119 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3120 } else {
3121 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3122 }
3123 break;
3124 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003125 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3126 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3127 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3128 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003129 }
3130
3131 // Remember in a map if the values will be reused later.
3132 if (!Node->hasOneUse()) {
3133 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3134 std::make_pair(Lo, Hi))).second;
3135 assert(isNew && "Value already expanded?!?");
3136 }
3137}
3138
3139
3140// SelectionDAG::Legalize - This is the entry point for the file.
3141//
Chris Lattner4add7e32005-01-23 04:42:50 +00003142void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003143 /// run - This is the main entry point to this class.
3144 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003145 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003146}
3147