blob: 3b686b95c708bd363e64fd4391f5516ac52e982a [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 {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000684 // Make sure the condition is either zero or one. It may have been
685 // promoted from something else.
686 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
687
Nate Begeman371e4952005-08-16 19:49:35 +0000688 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
689 DAG.getCondCode(ISD::SETNE), Tmp2,
690 DAG.getConstant(0, Tmp2.getValueType()),
691 Node->getOperand(2));
692 }
693 break;
694 case TargetLowering::Legal:
695 // Basic block destination (Op#2) is always legal.
696 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
697 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
698 Node->getOperand(2));
699 break;
700 }
701 break;
702 case ISD::BR_CC:
703 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
704
705 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
706 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
707 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
708 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
709 Tmp3 != Node->getOperand(3)) {
710 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
711 Tmp2, Tmp3, Node->getOperand(4));
712 }
713 break;
714 } else {
715 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
716 Node->getOperand(2), // LHS
717 Node->getOperand(3), // RHS
718 Node->getOperand(1)));
719 // If we get a SETCC back from legalizing the SETCC node we just
720 // created, then use its LHS, RHS, and CC directly in creating a new
721 // node. Otherwise, select between the true and false value based on
722 // comparing the result of the legalized with zero.
723 if (Tmp2.getOpcode() == ISD::SETCC) {
724 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
725 Tmp2.getOperand(0), Tmp2.getOperand(1),
726 Node->getOperand(4));
727 } else {
728 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
729 DAG.getCondCode(ISD::SETNE),
730 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
731 Node->getOperand(4));
732 }
733 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000734 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000735 case ISD::BRCONDTWOWAY:
736 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
737 switch (getTypeAction(Node->getOperand(1).getValueType())) {
738 case Expand: assert(0 && "It's impossible to expand bools");
739 case Legal:
740 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
741 break;
742 case Promote:
743 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
744 break;
745 }
746 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
747 // pair.
748 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
749 case TargetLowering::Promote:
750 default: assert(0 && "This action is not supported yet!");
751 case TargetLowering::Legal:
752 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
753 std::vector<SDOperand> Ops;
754 Ops.push_back(Tmp1);
755 Ops.push_back(Tmp2);
756 Ops.push_back(Node->getOperand(2));
757 Ops.push_back(Node->getOperand(3));
758 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
759 }
760 break;
761 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000762 // If BRTWOWAY_CC is legal for this target, then simply expand this node
763 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
764 // BRCOND/BR pair.
765 if (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other) ==
766 TargetLowering::Legal) {
767 if (Tmp2.getOpcode() == ISD::SETCC) {
768 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
769 Tmp2.getOperand(0), Tmp2.getOperand(1),
770 Node->getOperand(2), Node->getOperand(3));
771 } else {
772 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
773 DAG.getConstant(0, Tmp2.getValueType()),
774 Node->getOperand(2), Node->getOperand(3));
775 }
776 } else {
777 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000778 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000779 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
780 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000781 break;
782 }
783 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000784 case ISD::BRTWOWAY_CC:
785 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
786 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
787 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
788 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
789 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
790 Tmp3 != Node->getOperand(3)) {
791 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
792 Node->getOperand(4), Node->getOperand(5));
793 }
794 break;
795 } else {
796 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
797 Node->getOperand(2), // LHS
798 Node->getOperand(3), // RHS
799 Node->getOperand(1)));
800 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
801 // pair.
802 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
803 default: assert(0 && "This action is not supported yet!");
804 case TargetLowering::Legal:
805 // If we get a SETCC back from legalizing the SETCC node we just
806 // created, then use its LHS, RHS, and CC directly in creating a new
807 // node. Otherwise, select between the true and false value based on
808 // comparing the result of the legalized with zero.
809 if (Tmp2.getOpcode() == ISD::SETCC) {
810 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
811 Tmp2.getOperand(0), Tmp2.getOperand(1),
812 Node->getOperand(4), Node->getOperand(5));
813 } else {
814 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
815 DAG.getConstant(0, Tmp2.getValueType()),
816 Node->getOperand(4), Node->getOperand(5));
817 }
818 break;
819 case TargetLowering::Expand:
820 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
821 Node->getOperand(4));
822 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
823 break;
824 }
825 }
826 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000827 case ISD::LOAD:
828 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
829 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000830
Chris Lattnerdc750592005-01-07 07:47:09 +0000831 if (Tmp1 != Node->getOperand(0) ||
832 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000833 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
834 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000835 else
836 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000837
Chris Lattnerea4ca942005-01-07 22:28:47 +0000838 // Since loads produce two values, make sure to remember that we legalized
839 // both of them.
840 AddLegalizedOperand(SDOperand(Node, 0), Result);
841 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
842 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000843
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000844 case ISD::EXTLOAD:
845 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000846 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
848 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000849
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000850 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000851 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000852 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000853 case TargetLowering::Promote:
854 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000855 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
856 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000857 // Since loads produce two values, make sure to remember that we legalized
858 // both of them.
859 AddLegalizedOperand(SDOperand(Node, 0), Result);
860 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
861 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000862
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000863 case TargetLowering::Legal:
864 if (Tmp1 != Node->getOperand(0) ||
865 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000866 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
867 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000868 else
869 Result = SDOperand(Node, 0);
870
871 // Since loads produce two values, make sure to remember that we legalized
872 // both of them.
873 AddLegalizedOperand(SDOperand(Node, 0), Result);
874 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
875 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000876 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000877 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
878 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
879 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000880 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000881 if (Op.ResNo)
882 return Load.getValue(1);
883 return Result;
884 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000885 assert(Node->getOpcode() != ISD::EXTLOAD &&
886 "EXTLOAD should always be supported!");
887 // Turn the unsupported load into an EXTLOAD followed by an explicit
888 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000889 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
890 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000891 SDOperand ValRes;
892 if (Node->getOpcode() == ISD::SEXTLOAD)
893 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000894 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000895 else
896 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000897 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
898 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
899 if (Op.ResNo)
900 return Result.getValue(1);
901 return ValRes;
902 }
903 assert(0 && "Unreachable");
904 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000905 case ISD::EXTRACT_ELEMENT:
906 // Get both the low and high parts.
907 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
908 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
909 Result = Tmp2; // 1 -> Hi
910 else
911 Result = Tmp1; // 0 -> Lo
912 break;
913
914 case ISD::CopyToReg:
915 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000916
Chris Lattner33182322005-08-16 21:55:35 +0000917 assert(getTypeAction(Node->getOperand(2).getValueType()) == Legal &&
918 "Register type must be legal!");
919 // Legalize the incoming value (must be legal).
920 Tmp2 = LegalizeOp(Node->getOperand(2));
921 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
922 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
923 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000924 break;
925
926 case ISD::RET:
927 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
928 switch (Node->getNumOperands()) {
929 case 2: // ret val
930 switch (getTypeAction(Node->getOperand(1).getValueType())) {
931 case Legal:
932 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000933 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000934 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
935 break;
936 case Expand: {
937 SDOperand Lo, Hi;
938 ExpandOp(Node->getOperand(1), Lo, Hi);
939 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000940 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000941 }
942 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000943 Tmp2 = PromoteOp(Node->getOperand(1));
944 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
945 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000946 }
947 break;
948 case 1: // ret void
949 if (Tmp1 != Node->getOperand(0))
950 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
951 break;
952 default: { // ret <values>
953 std::vector<SDOperand> NewValues;
954 NewValues.push_back(Tmp1);
955 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
956 switch (getTypeAction(Node->getOperand(i).getValueType())) {
957 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000958 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000959 break;
960 case Expand: {
961 SDOperand Lo, Hi;
962 ExpandOp(Node->getOperand(i), Lo, Hi);
963 NewValues.push_back(Lo);
964 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000965 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000966 }
967 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000968 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000969 }
970 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
971 break;
972 }
973 }
974 break;
975 case ISD::STORE:
976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
977 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
978
Chris Lattnere69daaf2005-01-08 06:25:56 +0000979 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000980 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000981 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000982 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000983 DAG.getConstant(FloatToBits(CFP->getValue()),
984 MVT::i32),
985 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000986 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000987 } else {
988 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000989 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000990 DAG.getConstant(DoubleToBits(CFP->getValue()),
991 MVT::i64),
992 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000993 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000994 }
Chris Lattnera4743132005-02-22 07:23:39 +0000995 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000996 }
997
Chris Lattnerdc750592005-01-07 07:47:09 +0000998 switch (getTypeAction(Node->getOperand(1).getValueType())) {
999 case Legal: {
1000 SDOperand Val = LegalizeOp(Node->getOperand(1));
1001 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1002 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001003 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1004 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001005 break;
1006 }
1007 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001008 // Truncate the value and store the result.
1009 Tmp3 = PromoteOp(Node->getOperand(1));
1010 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001011 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001012 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001013 break;
1014
Chris Lattnerdc750592005-01-07 07:47:09 +00001015 case Expand:
1016 SDOperand Lo, Hi;
1017 ExpandOp(Node->getOperand(1), Lo, Hi);
1018
1019 if (!TLI.isLittleEndian())
1020 std::swap(Lo, Hi);
1021
Chris Lattner55e9cde2005-05-11 04:51:16 +00001022 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1023 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001024 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001025 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1026 getIntPtrConstant(IncrementSize));
1027 assert(isTypeLegal(Tmp2.getValueType()) &&
1028 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001029 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001030 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1031 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001032 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1033 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001034 }
1035 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001036 case ISD::PCMARKER:
1037 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001038 if (Tmp1 != Node->getOperand(0))
1039 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001040 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001041 case ISD::TRUNCSTORE:
1042 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1043 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1044
1045 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1046 case Legal:
1047 Tmp2 = LegalizeOp(Node->getOperand(1));
1048 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1049 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +00001050 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001051 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001052 break;
1053 case Promote:
1054 case Expand:
1055 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1056 }
1057 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001058 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1060 case Expand: assert(0 && "It's impossible to expand bools");
1061 case Legal:
1062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1063 break;
1064 case Promote:
1065 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1066 break;
1067 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001068 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001069 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001070
1071 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
1072 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001073 case TargetLowering::Expand:
1074 if (Tmp1.getOpcode() == ISD::SETCC) {
1075 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1076 Tmp2, Tmp3,
1077 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1078 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001079 // Make sure the condition is either zero or one. It may have been
1080 // promoted from something else.
1081 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001082 Result = DAG.getSelectCC(Tmp1,
1083 DAG.getConstant(0, Tmp1.getValueType()),
1084 Tmp2, Tmp3, ISD::SETNE);
1085 }
1086 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001087 case TargetLowering::Legal:
1088 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1089 Tmp3 != Node->getOperand(2))
1090 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1091 Tmp1, Tmp2, Tmp3);
1092 break;
1093 case TargetLowering::Promote: {
1094 MVT::ValueType NVT =
1095 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1096 unsigned ExtOp, TruncOp;
1097 if (MVT::isInteger(Tmp2.getValueType())) {
1098 ExtOp = ISD::ZERO_EXTEND;
1099 TruncOp = ISD::TRUNCATE;
1100 } else {
1101 ExtOp = ISD::FP_EXTEND;
1102 TruncOp = ISD::FP_ROUND;
1103 }
1104 // Promote each of the values to the new type.
1105 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1106 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1107 // Perform the larger operation, then round down.
1108 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1109 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1110 break;
1111 }
1112 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001113 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001114 case ISD::SELECT_CC:
1115 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1116 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1117
1118 if (getTypeAction(Node->getOperand(0).getValueType()) == Legal) {
1119 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1120 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1121 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1122 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1123 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1124 Tmp3, Tmp4, Node->getOperand(4));
1125 }
1126 break;
1127 } else {
1128 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1129 Node->getOperand(0), // LHS
1130 Node->getOperand(1), // RHS
1131 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001132 // If we get a SETCC back from legalizing the SETCC node we just
1133 // created, then use its LHS, RHS, and CC directly in creating a new
1134 // node. Otherwise, select between the true and false value based on
1135 // comparing the result of the legalized with zero.
1136 if (Tmp1.getOpcode() == ISD::SETCC) {
1137 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1138 Tmp1.getOperand(0), Tmp1.getOperand(1),
1139 Tmp3, Tmp4, Tmp1.getOperand(2));
1140 } else {
1141 Result = DAG.getSelectCC(Tmp1,
1142 DAG.getConstant(0, Tmp1.getValueType()),
1143 Tmp3, Tmp4, ISD::SETNE);
1144 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001145 }
1146 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001147 case ISD::SETCC:
1148 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1149 case Legal:
1150 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1151 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1152 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001153 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1154 Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001155 break;
1156 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001157 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1158 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1159
1160 // If this is an FP compare, the operands have already been extended.
1161 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1162 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001163 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001164
1165 // Otherwise, we have to insert explicit sign or zero extends. Note
1166 // that we could insert sign extends for ALL conditions, but zero extend
1167 // is cheaper on many machines (an AND instead of two shifts), so prefer
1168 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001169 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001170 default: assert(0 && "Unknown integer comparison!");
1171 case ISD::SETEQ:
1172 case ISD::SETNE:
1173 case ISD::SETUGE:
1174 case ISD::SETUGT:
1175 case ISD::SETULE:
1176 case ISD::SETULT:
1177 // ALL of these operations will work if we either sign or zero extend
1178 // the operands (including the unsigned comparisons!). Zero extend is
1179 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001180 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1181 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001182 break;
1183 case ISD::SETGE:
1184 case ISD::SETGT:
1185 case ISD::SETLT:
1186 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001187 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1188 DAG.getValueType(VT));
1189 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1190 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001191 break;
1192 }
1193
1194 }
Chris Lattnerd47675e2005-08-09 20:20:18 +00001195 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1196 Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001197 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001198 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001199 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1200 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1201 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001202 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001203 case ISD::SETEQ:
1204 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001205 if (RHSLo == RHSHi)
1206 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1207 if (RHSCST->isAllOnesValue()) {
1208 // Comparison to -1.
1209 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001210 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1211 RHSLo, Node->getOperand(2));
Misha Brukman835702a2005-04-21 22:36:52 +00001212 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001213 }
1214
Chris Lattnerdc750592005-01-07 07:47:09 +00001215 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1216 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1217 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001218 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1219 DAG.getConstant(0, Tmp1.getValueType()),
1220 Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001221 break;
1222 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001223 // If this is a comparison of the sign bit, just look at the top part.
1224 // X > -1, x < 0
1225 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001226 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001227 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001228 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001229 (CST->isAllOnesValue()))) // X > -1
Chris Lattnerd47675e2005-08-09 20:20:18 +00001230 return DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1231 Node->getOperand(2));
Chris Lattneraedcabe2005-04-12 02:19:10 +00001232
Chris Lattnerdc750592005-01-07 07:47:09 +00001233 // FIXME: This generated code sucks.
1234 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001235 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001236 default: assert(0 && "Unknown integer setcc!");
1237 case ISD::SETLT:
1238 case ISD::SETULT: LowCC = ISD::SETULT; break;
1239 case ISD::SETGT:
1240 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1241 case ISD::SETLE:
1242 case ISD::SETULE: LowCC = ISD::SETULE; break;
1243 case ISD::SETGE:
1244 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1245 }
Misha Brukman835702a2005-04-21 22:36:52 +00001246
Chris Lattnerdc750592005-01-07 07:47:09 +00001247 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1248 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1249 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1250
1251 // NOTE: on targets without efficient SELECT of bools, we can always use
1252 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001253 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1254 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1255 Node->getOperand(2));
1256 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001257 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1258 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001259 break;
1260 }
1261 }
1262 break;
1263
Chris Lattner85d70c62005-01-11 05:57:22 +00001264 case ISD::MEMSET:
1265 case ISD::MEMCPY:
1266 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001267 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001268 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1269
1270 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1271 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1272 case Expand: assert(0 && "Cannot expand a byte!");
1273 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001274 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001275 break;
1276 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001277 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001278 break;
1279 }
1280 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001281 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001282 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001283
1284 SDOperand Tmp4;
1285 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001286 case Expand: {
1287 // Length is too big, just take the lo-part of the length.
1288 SDOperand HiPart;
1289 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1290 break;
1291 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001292 case Legal:
1293 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001294 break;
1295 case Promote:
1296 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001297 break;
1298 }
1299
1300 SDOperand Tmp5;
1301 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1302 case Expand: assert(0 && "Cannot expand this yet!");
1303 case Legal:
1304 Tmp5 = LegalizeOp(Node->getOperand(4));
1305 break;
1306 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001307 Tmp5 = PromoteOp(Node->getOperand(4));
1308 break;
1309 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001310
1311 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1312 default: assert(0 && "This action not implemented for this operation!");
1313 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001314 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1315 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1316 Tmp5 != Node->getOperand(4)) {
1317 std::vector<SDOperand> Ops;
1318 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1319 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1320 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1321 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001322 break;
1323 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001324 // Otherwise, the target does not support this operation. Lower the
1325 // operation to an explicit libcall as appropriate.
1326 MVT::ValueType IntPtr = TLI.getPointerTy();
1327 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1328 std::vector<std::pair<SDOperand, const Type*> > Args;
1329
Reid Spencer6dced922005-01-12 14:53:45 +00001330 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001331 if (Node->getOpcode() == ISD::MEMSET) {
1332 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1333 // Extend the ubyte argument to be an int value for the call.
1334 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1335 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1336 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1337
1338 FnName = "memset";
1339 } else if (Node->getOpcode() == ISD::MEMCPY ||
1340 Node->getOpcode() == ISD::MEMMOVE) {
1341 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1342 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1343 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1344 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1345 } else {
1346 assert(0 && "Unknown op!");
1347 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001348
Chris Lattner85d70c62005-01-11 05:57:22 +00001349 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001350 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001351 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001352 Result = CallResult.second;
1353 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001354 break;
1355 }
1356 case TargetLowering::Custom:
1357 std::vector<SDOperand> Ops;
1358 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1359 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1360 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001361 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001362 Result = LegalizeOp(Result);
1363 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001364 }
1365 break;
1366 }
Chris Lattner5385db52005-05-09 20:23:03 +00001367
1368 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001369 Tmp1 = LegalizeOp(Node->getOperand(0));
1370 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001371
Chris Lattner86535992005-05-14 07:45:46 +00001372 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1373 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1374 std::vector<SDOperand> Ops;
1375 Ops.push_back(Tmp1);
1376 Ops.push_back(Tmp2);
1377 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1378 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001379 Result = SDOperand(Node, 0);
1380 // Since these produce two values, make sure to remember that we legalized
1381 // both of them.
1382 AddLegalizedOperand(SDOperand(Node, 0), Result);
1383 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1384 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001385 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001386 Tmp1 = LegalizeOp(Node->getOperand(0));
1387 Tmp2 = LegalizeOp(Node->getOperand(1));
1388 Tmp3 = LegalizeOp(Node->getOperand(2));
1389 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1390 Tmp3 != Node->getOperand(2))
1391 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1392 break;
1393
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001394 case ISD::READIO:
1395 Tmp1 = LegalizeOp(Node->getOperand(0));
1396 Tmp2 = LegalizeOp(Node->getOperand(1));
1397
1398 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1399 case TargetLowering::Custom:
1400 default: assert(0 && "This action not implemented for this operation!");
1401 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001402 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1403 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1404 std::vector<SDOperand> Ops;
1405 Ops.push_back(Tmp1);
1406 Ops.push_back(Tmp2);
1407 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1408 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001409 Result = SDOperand(Node, 0);
1410 break;
1411 case TargetLowering::Expand:
1412 // Replace this with a load from memory.
1413 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1414 Node->getOperand(1), DAG.getSrcValue(NULL));
1415 Result = LegalizeOp(Result);
1416 break;
1417 }
1418
1419 // Since these produce two values, make sure to remember that we legalized
1420 // both of them.
1421 AddLegalizedOperand(SDOperand(Node, 0), Result);
1422 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1423 return Result.getValue(Op.ResNo);
1424
1425 case ISD::WRITEIO:
1426 Tmp1 = LegalizeOp(Node->getOperand(0));
1427 Tmp2 = LegalizeOp(Node->getOperand(1));
1428 Tmp3 = LegalizeOp(Node->getOperand(2));
1429
1430 switch (TLI.getOperationAction(Node->getOpcode(),
1431 Node->getOperand(1).getValueType())) {
1432 case TargetLowering::Custom:
1433 default: assert(0 && "This action not implemented for this operation!");
1434 case TargetLowering::Legal:
1435 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1436 Tmp3 != Node->getOperand(2))
1437 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1438 break;
1439 case TargetLowering::Expand:
1440 // Replace this with a store to memory.
1441 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1442 Node->getOperand(1), Node->getOperand(2),
1443 DAG.getSrcValue(NULL));
1444 Result = LegalizeOp(Result);
1445 break;
1446 }
1447 break;
1448
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001449 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001450 case ISD::SUB_PARTS:
1451 case ISD::SHL_PARTS:
1452 case ISD::SRA_PARTS:
1453 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001454 std::vector<SDOperand> Ops;
1455 bool Changed = false;
1456 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1457 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1458 Changed |= Ops.back() != Node->getOperand(i);
1459 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001460 if (Changed) {
1461 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1462 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1463 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001464
1465 // Since these produce multiple values, make sure to remember that we
1466 // legalized all of them.
1467 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1468 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1469 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001470 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001471
1472 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001473 case ISD::ADD:
1474 case ISD::SUB:
1475 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001476 case ISD::MULHS:
1477 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001478 case ISD::UDIV:
1479 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001480 case ISD::AND:
1481 case ISD::OR:
1482 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001483 case ISD::SHL:
1484 case ISD::SRL:
1485 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001486 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001487 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1488 case Expand: assert(0 && "Not possible");
1489 case Legal:
1490 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1491 break;
1492 case Promote:
1493 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1494 break;
1495 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001496 if (Tmp1 != Node->getOperand(0) ||
1497 Tmp2 != Node->getOperand(1))
1498 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1499 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001500
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001501 case ISD::UREM:
1502 case ISD::SREM:
1503 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1504 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1505 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1506 case TargetLowering::Legal:
1507 if (Tmp1 != Node->getOperand(0) ||
1508 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001509 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001510 Tmp2);
1511 break;
1512 case TargetLowering::Promote:
1513 case TargetLowering::Custom:
1514 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001515 case TargetLowering::Expand:
1516 if (MVT::isInteger(Node->getValueType(0))) {
1517 MVT::ValueType VT = Node->getValueType(0);
1518 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1519 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1520 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1521 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1522 } else {
1523 // Floating point mod -> fmod libcall.
1524 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1525 SDOperand Dummy;
1526 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001527 }
1528 break;
1529 }
1530 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001531
Andrew Lenharth5e177822005-05-03 17:19:30 +00001532 case ISD::CTPOP:
1533 case ISD::CTTZ:
1534 case ISD::CTLZ:
1535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1536 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1537 case TargetLowering::Legal:
1538 if (Tmp1 != Node->getOperand(0))
1539 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1540 break;
1541 case TargetLowering::Promote: {
1542 MVT::ValueType OVT = Tmp1.getValueType();
1543 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001544
1545 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001546 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1547 // Perform the larger operation, then subtract if needed.
1548 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1549 switch(Node->getOpcode())
1550 {
1551 case ISD::CTPOP:
1552 Result = Tmp1;
1553 break;
1554 case ISD::CTTZ:
1555 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001556 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1557 DAG.getConstant(getSizeInBits(NVT), NVT),
1558 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001559 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001560 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1561 break;
1562 case ISD::CTLZ:
1563 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001564 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1565 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001566 getSizeInBits(OVT), NVT));
1567 break;
1568 }
1569 break;
1570 }
1571 case TargetLowering::Custom:
1572 assert(0 && "Cannot custom handle this yet!");
1573 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001574 switch(Node->getOpcode())
1575 {
1576 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001577 static const uint64_t mask[6] = {
1578 0x5555555555555555ULL, 0x3333333333333333ULL,
1579 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1580 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1581 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001582 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001583 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1584 unsigned len = getSizeInBits(VT);
1585 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001586 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001587 Tmp2 = DAG.getConstant(mask[i], VT);
1588 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001589 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001590 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1591 DAG.getNode(ISD::AND, VT,
1592 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1593 Tmp2));
1594 }
1595 Result = Tmp1;
1596 break;
1597 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001598 case ISD::CTLZ: {
1599 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001600 x = x | (x >> 1);
1601 x = x | (x >> 2);
1602 ...
1603 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001604 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001605 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001606
Chris Lattner56add052005-05-11 18:35:21 +00001607 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1608 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001609 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1610 unsigned len = getSizeInBits(VT);
1611 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1612 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001613 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001614 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1615 }
1616 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001617 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001618 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001619 }
1620 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001621 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001622 // unless the target has ctlz but not ctpop, in which case we use:
1623 // { return 32 - nlz(~x & (x-1)); }
1624 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001625 MVT::ValueType VT = Tmp1.getValueType();
1626 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001627 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001628 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1629 DAG.getNode(ISD::SUB, VT, Tmp1,
1630 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001631 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1632 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1633 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001634 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001635 DAG.getConstant(getSizeInBits(VT), VT),
1636 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1637 } else {
1638 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1639 }
Chris Lattner72473242005-05-11 05:27:09 +00001640 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001641 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001642 default:
1643 assert(0 && "Cannot expand this yet!");
1644 break;
1645 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001646 break;
1647 }
1648 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001649
Chris Lattner13fe99c2005-04-02 05:00:07 +00001650 // Unary operators
1651 case ISD::FABS:
1652 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001653 case ISD::FSQRT:
1654 case ISD::FSIN:
1655 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001656 Tmp1 = LegalizeOp(Node->getOperand(0));
1657 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1658 case TargetLowering::Legal:
1659 if (Tmp1 != Node->getOperand(0))
1660 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1661 break;
1662 case TargetLowering::Promote:
1663 case TargetLowering::Custom:
1664 assert(0 && "Cannot promote/custom handle this yet!");
1665 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001666 switch(Node->getOpcode()) {
1667 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001668 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1669 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1670 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1671 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001672 break;
1673 }
1674 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001675 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1676 MVT::ValueType VT = Node->getValueType(0);
1677 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001678 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001679 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1680 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1681 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001682 break;
1683 }
1684 case ISD::FSQRT:
1685 case ISD::FSIN:
1686 case ISD::FCOS: {
1687 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001688 const char *FnName = 0;
1689 switch(Node->getOpcode()) {
1690 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1691 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1692 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1693 default: assert(0 && "Unreachable!");
1694 }
Nate Begeman77558da2005-08-04 21:43:28 +00001695 SDOperand Dummy;
1696 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001697 break;
1698 }
1699 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001700 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001701 }
1702 break;
1703 }
1704 break;
1705
1706 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001707 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001708 case ISD::UINT_TO_FP: {
1709 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001710 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1711 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001712 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001713 Node->getOperand(0).getValueType())) {
1714 default: assert(0 && "Unknown operation action!");
1715 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001716 Result = ExpandLegalINT_TO_FP(isSigned,
1717 LegalizeOp(Node->getOperand(0)),
1718 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001719 AddLegalizedOperand(Op, Result);
1720 return Result;
1721 case TargetLowering::Promote:
1722 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1723 Node->getValueType(0),
1724 isSigned);
1725 AddLegalizedOperand(Op, Result);
1726 return Result;
1727 case TargetLowering::Legal:
1728 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001729 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001730
Chris Lattnerdc750592005-01-07 07:47:09 +00001731 Tmp1 = LegalizeOp(Node->getOperand(0));
1732 if (Tmp1 != Node->getOperand(0))
1733 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1734 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001735 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001736 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1737 Node->getValueType(0), Node->getOperand(0));
1738 break;
1739 case Promote:
1740 if (isSigned) {
1741 Result = PromoteOp(Node->getOperand(0));
1742 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1743 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1744 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1745 } else {
1746 Result = PromoteOp(Node->getOperand(0));
1747 Result = DAG.getZeroExtendInReg(Result,
1748 Node->getOperand(0).getValueType());
1749 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001750 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001751 break;
1752 }
1753 break;
1754 }
1755 case ISD::TRUNCATE:
1756 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1757 case Legal:
1758 Tmp1 = LegalizeOp(Node->getOperand(0));
1759 if (Tmp1 != Node->getOperand(0))
1760 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1761 break;
1762 case Expand:
1763 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1764
1765 // Since the result is legal, we should just be able to truncate the low
1766 // part of the source.
1767 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1768 break;
1769 case Promote:
1770 Result = PromoteOp(Node->getOperand(0));
1771 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1772 break;
1773 }
1774 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001775
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001776 case ISD::FP_TO_SINT:
1777 case ISD::FP_TO_UINT:
1778 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1779 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001780 Tmp1 = LegalizeOp(Node->getOperand(0));
1781
Chris Lattner44fe26f2005-07-29 00:11:56 +00001782 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1783 default: assert(0 && "Unknown operation action!");
1784 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00001785 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1786 SDOperand True, False;
1787 MVT::ValueType VT = Node->getOperand(0).getValueType();
1788 MVT::ValueType NVT = Node->getValueType(0);
1789 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1790 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1791 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1792 Node->getOperand(0), Tmp2, ISD::SETLT);
1793 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1794 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1795 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1796 Tmp2));
1797 False = DAG.getNode(ISD::XOR, NVT, False,
1798 DAG.getConstant(1ULL << ShiftAmt, NVT));
1799 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00001800 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00001801 } else {
1802 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1803 }
1804 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001805 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001806 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001807 Node->getOpcode() == ISD::FP_TO_SINT);
1808 AddLegalizedOperand(Op, Result);
1809 return Result;
1810 case TargetLowering::Legal:
1811 break;
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001812 case TargetLowering::Custom:
1813 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1814 Result = TLI.LowerOperation(Result, DAG);
1815 AddLegalizedOperand(Op, Result);
1816 NeedsAnotherIteration = true;
1817 return Result;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001818 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001819
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001820 if (Tmp1 != Node->getOperand(0))
1821 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1822 break;
1823 case Expand:
1824 assert(0 && "Shouldn't need to expand other operators here!");
1825 case Promote:
1826 Result = PromoteOp(Node->getOperand(0));
1827 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1828 break;
1829 }
1830 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001831
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001832 case ISD::ZERO_EXTEND:
1833 case ISD::SIGN_EXTEND:
1834 case ISD::FP_EXTEND:
1835 case ISD::FP_ROUND:
1836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1837 case Legal:
1838 Tmp1 = LegalizeOp(Node->getOperand(0));
1839 if (Tmp1 != Node->getOperand(0))
1840 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1841 break;
1842 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001843 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001844
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001845 case Promote:
1846 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001847 case ISD::ZERO_EXTEND:
1848 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001849 // NOTE: Any extend would work here...
1850 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001851 Result = DAG.getZeroExtendInReg(Result,
1852 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001853 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001854 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001855 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001856 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001857 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001858 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001859 Result,
1860 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001861 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001862 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001863 Result = PromoteOp(Node->getOperand(0));
1864 if (Result.getValueType() != Op.getValueType())
1865 // Dynamically dead while we have only 2 FP types.
1866 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1867 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001868 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001869 Result = PromoteOp(Node->getOperand(0));
1870 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1871 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001872 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001873 }
1874 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001875 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001876 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001877 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001878 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001879
1880 // If this operation is not supported, convert it to a shl/shr or load/store
1881 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001882 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1883 default: assert(0 && "This action not supported for this op yet!");
1884 case TargetLowering::Legal:
1885 if (Tmp1 != Node->getOperand(0))
1886 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001887 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001888 break;
1889 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001890 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001891 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001892 // NOTE: we could fall back on load/store here too for targets without
1893 // SAR. However, it is doubtful that any exist.
1894 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1895 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001896 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001897 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1898 Node->getOperand(0), ShiftCst);
1899 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1900 Result, ShiftCst);
1901 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1902 // The only way we can lower this is to turn it into a STORETRUNC,
1903 // EXTLOAD pair, targetting a temporary location (a stack slot).
1904
1905 // NOTE: there is a choice here between constantly creating new stack
1906 // slots and always reusing the same one. We currently always create
1907 // new ones, as reuse may inhibit scheduling.
1908 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1909 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1910 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1911 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001912 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001913 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1914 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1915 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001916 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001917 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001918 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1919 Result, StackSlot, DAG.getSrcValue(NULL),
1920 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001921 } else {
1922 assert(0 && "Unknown op");
1923 }
1924 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001925 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001926 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001927 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001928 }
Chris Lattner99222f72005-01-15 07:15:18 +00001929 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001930
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001931 // Note that LegalizeOp may be reentered even from single-use nodes, which
1932 // means that we always must cache transformed nodes.
1933 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001934 return Result;
1935}
1936
Chris Lattner4d978642005-01-15 22:16:26 +00001937/// PromoteOp - Given an operation that produces a value in an invalid type,
1938/// promote it to compute the value into a larger type. The produced value will
1939/// have the correct bits for the low portion of the register, but no guarantee
1940/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001941SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1942 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001943 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001944 assert(getTypeAction(VT) == Promote &&
1945 "Caller should expand or legalize operands that are not promotable!");
1946 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1947 "Cannot promote to smaller type!");
1948
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001949 SDOperand Tmp1, Tmp2, Tmp3;
1950
1951 SDOperand Result;
1952 SDNode *Node = Op.Val;
1953
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001954 if (!Node->hasOneUse()) {
1955 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1956 if (I != PromotedNodes.end()) return I->second;
1957 } else {
1958 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1959 }
1960
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001961 // Promotion needs an optimization step to clean up after it, and is not
1962 // careful to avoid operations the target does not support. Make sure that
1963 // all generated operations are legalized in the next iteration.
1964 NeedsAnotherIteration = true;
1965
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001966 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00001967 case ISD::CopyFromReg:
1968 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001969 default:
1970 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1971 assert(0 && "Do not know how to promote this operator!");
1972 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001973 case ISD::UNDEF:
1974 Result = DAG.getNode(ISD::UNDEF, NVT);
1975 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001976 case ISD::Constant:
1977 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1978 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1979 break;
1980 case ISD::ConstantFP:
1981 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1982 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1983 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001984
Chris Lattner2cb338d2005-01-18 02:59:52 +00001985 case ISD::SETCC:
1986 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1987 "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00001988 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
1989 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00001990 Result = LegalizeOp(Result);
1991 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001992
1993 case ISD::TRUNCATE:
1994 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1995 case Legal:
1996 Result = LegalizeOp(Node->getOperand(0));
1997 assert(Result.getValueType() >= NVT &&
1998 "This truncation doesn't make sense!");
1999 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2000 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2001 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002002 case Promote:
2003 // The truncation is not required, because we don't guarantee anything
2004 // about high bits anyway.
2005 Result = PromoteOp(Node->getOperand(0));
2006 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002007 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002008 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2009 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002010 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002011 }
2012 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002013 case ISD::SIGN_EXTEND:
2014 case ISD::ZERO_EXTEND:
2015 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2016 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2017 case Legal:
2018 // Input is legal? Just do extend all the way to the larger type.
2019 Result = LegalizeOp(Node->getOperand(0));
2020 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2021 break;
2022 case Promote:
2023 // Promote the reg if it's smaller.
2024 Result = PromoteOp(Node->getOperand(0));
2025 // The high bits are not guaranteed to be anything. Insert an extend.
2026 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002027 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002028 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002029 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002030 Result = DAG.getZeroExtendInReg(Result,
2031 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002032 break;
2033 }
2034 break;
2035
2036 case ISD::FP_EXTEND:
2037 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2038 case ISD::FP_ROUND:
2039 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2040 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2041 case Promote: assert(0 && "Unreachable with 2 FP types!");
2042 case Legal:
2043 // Input is legal? Do an FP_ROUND_INREG.
2044 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002045 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2046 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002047 break;
2048 }
2049 break;
2050
2051 case ISD::SINT_TO_FP:
2052 case ISD::UINT_TO_FP:
2053 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2054 case Legal:
2055 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002056 // No extra round required here.
2057 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002058 break;
2059
2060 case Promote:
2061 Result = PromoteOp(Node->getOperand(0));
2062 if (Node->getOpcode() == ISD::SINT_TO_FP)
2063 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002064 Result,
2065 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002066 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002067 Result = DAG.getZeroExtendInReg(Result,
2068 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002069 // No extra round required here.
2070 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002071 break;
2072 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002073 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2074 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002075 // Round if we cannot tolerate excess precision.
2076 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002077 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2078 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002079 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002080 }
Chris Lattner4d978642005-01-15 22:16:26 +00002081 break;
2082
2083 case ISD::FP_TO_SINT:
2084 case ISD::FP_TO_UINT:
2085 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2086 case Legal:
2087 Tmp1 = LegalizeOp(Node->getOperand(0));
2088 break;
2089 case Promote:
2090 // The input result is prerounded, so we don't have to do anything
2091 // special.
2092 Tmp1 = PromoteOp(Node->getOperand(0));
2093 break;
2094 case Expand:
2095 assert(0 && "not implemented");
2096 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002097 // If we're promoting a UINT to a larger size, check to see if the new node
2098 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2099 // we can use that instead. This allows us to generate better code for
2100 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2101 // legal, such as PowerPC.
2102 if (Node->getOpcode() == ISD::FP_TO_UINT &&
2103 TargetLowering::Legal != TLI.getOperationAction(ISD::FP_TO_UINT, NVT) &&
2104 TargetLowering::Legal == TLI.getOperationAction(ISD::FP_TO_SINT, NVT)) {
2105 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2106 } else {
2107 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2108 }
Chris Lattner4d978642005-01-15 22:16:26 +00002109 break;
2110
Chris Lattner13fe99c2005-04-02 05:00:07 +00002111 case ISD::FABS:
2112 case ISD::FNEG:
2113 Tmp1 = PromoteOp(Node->getOperand(0));
2114 assert(Tmp1.getValueType() == NVT);
2115 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2116 // NOTE: we do not have to do any extra rounding here for
2117 // NoExcessFPPrecision, because we know the input will have the appropriate
2118 // precision, and these operations don't modify precision at all.
2119 break;
2120
Chris Lattner9d6fa982005-04-28 21:44:33 +00002121 case ISD::FSQRT:
2122 case ISD::FSIN:
2123 case ISD::FCOS:
2124 Tmp1 = PromoteOp(Node->getOperand(0));
2125 assert(Tmp1.getValueType() == NVT);
2126 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2127 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002128 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2129 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002130 break;
2131
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002132 case ISD::AND:
2133 case ISD::OR:
2134 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002135 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002136 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002137 case ISD::MUL:
2138 // The input may have strange things in the top bits of the registers, but
2139 // these operations don't care. They may have wierd bits going out, but
2140 // that too is okay if they are integer operations.
2141 Tmp1 = PromoteOp(Node->getOperand(0));
2142 Tmp2 = PromoteOp(Node->getOperand(1));
2143 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2144 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2145
2146 // However, if this is a floating point operation, they will give excess
2147 // precision that we may not be able to tolerate. If we DO allow excess
2148 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002149 // FIXME: Why would we need to round FP ops more than integer ones?
2150 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002151 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002152 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2153 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002154 break;
2155
Chris Lattner4d978642005-01-15 22:16:26 +00002156 case ISD::SDIV:
2157 case ISD::SREM:
2158 // These operators require that their input be sign extended.
2159 Tmp1 = PromoteOp(Node->getOperand(0));
2160 Tmp2 = PromoteOp(Node->getOperand(1));
2161 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002162 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2163 DAG.getValueType(VT));
2164 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2165 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002166 }
2167 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2168
2169 // Perform FP_ROUND: this is probably overly pessimistic.
2170 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002171 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2172 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002173 break;
2174
2175 case ISD::UDIV:
2176 case ISD::UREM:
2177 // These operators require that their input be zero extended.
2178 Tmp1 = PromoteOp(Node->getOperand(0));
2179 Tmp2 = PromoteOp(Node->getOperand(1));
2180 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002181 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2182 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002183 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2184 break;
2185
2186 case ISD::SHL:
2187 Tmp1 = PromoteOp(Node->getOperand(0));
2188 Tmp2 = LegalizeOp(Node->getOperand(1));
2189 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2190 break;
2191 case ISD::SRA:
2192 // The input value must be properly sign extended.
2193 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002194 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2195 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002196 Tmp2 = LegalizeOp(Node->getOperand(1));
2197 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2198 break;
2199 case ISD::SRL:
2200 // The input value must be properly zero extended.
2201 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002202 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002203 Tmp2 = LegalizeOp(Node->getOperand(1));
2204 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2205 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002206 case ISD::LOAD:
2207 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2208 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00002209 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00002210 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002211 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2212 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00002213 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002214 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2215 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002216
2217 // Remember that we legalized the chain.
2218 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2219 break;
2220 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002221 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2222 case Expand: assert(0 && "It's impossible to expand bools");
2223 case Legal:
2224 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2225 break;
2226 case Promote:
2227 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2228 break;
2229 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002230 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2231 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2232 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2233 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002234 case ISD::SELECT_CC:
2235 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2236 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2237 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2238 Node->getOperand(1), Tmp2, Tmp3,
2239 Node->getOperand(4));
2240 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002241 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002242 case ISD::CALL: {
2243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2244 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2245
Chris Lattner3d95c142005-01-19 20:24:35 +00002246 std::vector<SDOperand> Ops;
2247 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2248 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2249
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002250 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2251 "Can only promote single result calls");
2252 std::vector<MVT::ValueType> RetTyVTs;
2253 RetTyVTs.reserve(2);
2254 RetTyVTs.push_back(NVT);
2255 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002256 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2257 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002258 Result = SDOperand(NC, 0);
2259
2260 // Insert the new chain mapping.
2261 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2262 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002263 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002264 case ISD::CTPOP:
2265 case ISD::CTTZ:
2266 case ISD::CTLZ:
2267 Tmp1 = Node->getOperand(0);
2268 //Zero extend the argument
2269 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2270 // Perform the larger operation, then subtract if needed.
2271 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2272 switch(Node->getOpcode())
2273 {
2274 case ISD::CTPOP:
2275 Result = Tmp1;
2276 break;
2277 case ISD::CTTZ:
2278 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002279 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002280 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002281 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002282 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2283 break;
2284 case ISD::CTLZ:
2285 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002286 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2287 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002288 getSizeInBits(VT), NVT));
2289 break;
2290 }
2291 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002292 }
2293
2294 assert(Result.Val && "Didn't set a result!");
2295 AddPromotedOperand(Op, Result);
2296 return Result;
2297}
Chris Lattnerdc750592005-01-07 07:47:09 +00002298
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002299/// ExpandAddSub - Find a clever way to expand this add operation into
2300/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002301void SelectionDAGLegalize::
2302ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2303 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002304 // Expand the subcomponents.
2305 SDOperand LHSL, LHSH, RHSL, RHSH;
2306 ExpandOp(LHS, LHSL, LHSH);
2307 ExpandOp(RHS, RHSL, RHSH);
2308
Chris Lattner8ffd0042005-04-11 20:29:59 +00002309 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002310 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2311 if (LHSL.getValueType() == MVT::i32) {
2312 SDOperand LowEl;
2313 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2314 if (C->getValue() == 0)
2315 LowEl = RHSL;
2316 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2317 if (C->getValue() == 0)
2318 LowEl = LHSL;
2319 if (LowEl.Val) {
2320 // Turn this into an add/sub of the high part only.
2321 SDOperand HiEl =
2322 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2323 LowEl.getValueType(), LHSH, RHSH);
2324 Lo = LowEl;
2325 Hi = HiEl;
2326 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002327 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002328 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002329
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002330 std::vector<SDOperand> Ops;
2331 Ops.push_back(LHSL);
2332 Ops.push_back(LHSH);
2333 Ops.push_back(RHSL);
2334 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002335
2336 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2337 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002338 Hi = Lo.getValue(1);
2339}
2340
Chris Lattner4157c412005-04-02 04:00:59 +00002341void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2342 SDOperand Op, SDOperand Amt,
2343 SDOperand &Lo, SDOperand &Hi) {
2344 // Expand the subcomponents.
2345 SDOperand LHSL, LHSH;
2346 ExpandOp(Op, LHSL, LHSH);
2347
2348 std::vector<SDOperand> Ops;
2349 Ops.push_back(LHSL);
2350 Ops.push_back(LHSH);
2351 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002352 std::vector<MVT::ValueType> VTs;
2353 VTs.push_back(LHSL.getValueType());
2354 VTs.push_back(LHSH.getValueType());
2355 VTs.push_back(Amt.getValueType());
2356 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002357 Hi = Lo.getValue(1);
2358}
2359
2360
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002361/// ExpandShift - Try to find a clever way to expand this shift operation out to
2362/// smaller elements. If we can't find a way that is more efficient than a
2363/// libcall on this target, return false. Otherwise, return true with the
2364/// low-parts expanded into Lo and Hi.
2365bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2366 SDOperand &Lo, SDOperand &Hi) {
2367 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2368 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002369
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002370 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002371 SDOperand ShAmt = LegalizeOp(Amt);
2372 MVT::ValueType ShTy = ShAmt.getValueType();
2373 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2374 unsigned NVTBits = MVT::getSizeInBits(NVT);
2375
2376 // Handle the case when Amt is an immediate. Other cases are currently broken
2377 // and are disabled.
2378 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2379 unsigned Cst = CN->getValue();
2380 // Expand the incoming operand to be shifted, so that we have its parts
2381 SDOperand InL, InH;
2382 ExpandOp(Op, InL, InH);
2383 switch(Opc) {
2384 case ISD::SHL:
2385 if (Cst > VTBits) {
2386 Lo = DAG.getConstant(0, NVT);
2387 Hi = DAG.getConstant(0, NVT);
2388 } else if (Cst > NVTBits) {
2389 Lo = DAG.getConstant(0, NVT);
2390 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002391 } else if (Cst == NVTBits) {
2392 Lo = DAG.getConstant(0, NVT);
2393 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002394 } else {
2395 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2396 Hi = DAG.getNode(ISD::OR, NVT,
2397 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2398 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2399 }
2400 return true;
2401 case ISD::SRL:
2402 if (Cst > VTBits) {
2403 Lo = DAG.getConstant(0, NVT);
2404 Hi = DAG.getConstant(0, NVT);
2405 } else if (Cst > NVTBits) {
2406 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2407 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002408 } else if (Cst == NVTBits) {
2409 Lo = InH;
2410 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002411 } else {
2412 Lo = DAG.getNode(ISD::OR, NVT,
2413 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2414 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2415 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2416 }
2417 return true;
2418 case ISD::SRA:
2419 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002420 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002421 DAG.getConstant(NVTBits-1, ShTy));
2422 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002423 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002424 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002425 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002426 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002427 } else if (Cst == NVTBits) {
2428 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002429 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002430 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002431 } else {
2432 Lo = DAG.getNode(ISD::OR, NVT,
2433 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2434 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2435 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2436 }
2437 return true;
2438 }
2439 }
2440 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2441 // so disable it for now. Currently targets are handling this via SHL_PARTS
2442 // and friends.
2443 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002444
2445 // If we have an efficient select operation (or if the selects will all fold
2446 // away), lower to some complex code, otherwise just emit the libcall.
2447 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2448 !isa<ConstantSDNode>(Amt))
2449 return false;
2450
2451 SDOperand InL, InH;
2452 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002453 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2454 DAG.getConstant(NVTBits, ShTy), ShAmt);
2455
Chris Lattner4d25c042005-01-20 20:29:23 +00002456 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002457 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2458 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002459
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002460 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2461 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2462 DAG.getConstant(NVTBits-1, ShTy));
2463 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2464 DAG.getConstant(NVTBits-1, ShTy));
2465 }
2466
2467 if (Opc == ISD::SHL) {
2468 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2469 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2470 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002471 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002472
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002473 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2474 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2475 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002476 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002477 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2478 DAG.getConstant(32, ShTy),
2479 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002480 DAG.getConstant(0, NVT),
2481 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002482 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002483 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002484 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002485 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002486
2487 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002488 if (Opc == ISD::SRA)
2489 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2490 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002491 else
2492 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002493 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002494 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002495 }
2496 return true;
2497}
Chris Lattneraac464e2005-01-21 06:05:23 +00002498
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002499/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2500/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002501/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002502static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002503 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002504
Chris Lattner2dce7032005-05-12 23:24:06 +00002505 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002506 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002507 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002508 Found = Node;
2509 return;
2510 }
2511
2512 // Otherwise, scan the operands of Node to see if any of them is a call.
2513 assert(Node->getNumOperands() != 0 &&
2514 "All leaves should have depth equal to the entry node!");
2515 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002516 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002517
2518 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002519 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002520 Found);
2521}
2522
2523
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002524/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2525/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002526/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002527static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2528 std::set<SDNode*> &Visited) {
2529 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2530 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002531
Chris Lattner2dce7032005-05-12 23:24:06 +00002532 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002533 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002534 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002535 Found = Node;
2536 return;
2537 }
2538
2539 // Otherwise, scan the operands of Node to see if any of them is a call.
2540 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2541 if (UI == E) return;
2542 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002543 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002544
2545 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002546 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002547}
2548
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002549/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002550/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002551static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002552 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002553 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002554 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002555 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002556
2557 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002558 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002559
Chris Lattner4add7e32005-01-23 04:42:50 +00002560 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002561 if (TheChain.getValueType() != MVT::Other)
2562 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002563 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002564
2565 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002566 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002567
Chris Lattner4add7e32005-01-23 04:42:50 +00002568 // Make sure to only follow users of our token chain.
2569 SDNode *User = *UI;
2570 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2571 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002572 if (SDNode *Result = FindCallSeqEnd(User))
2573 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002574 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002575 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002576}
2577
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002578/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002579/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002580static SDNode *FindCallSeqStart(SDNode *Node) {
2581 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002582 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002583
2584 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2585 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002586 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002587}
2588
2589
Chris Lattner4add7e32005-01-23 04:42:50 +00002590/// FindInputOutputChains - If we are replacing an operation with a call we need
2591/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002592/// properly serialize the calls in the block. The returned operand is the
2593/// input chain value for the new call (e.g. the entry node or the previous
2594/// call), and OutChain is set to be the chain node to update to point to the
2595/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002596static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2597 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002598 SDNode *LatestCallSeqStart = Entry.Val;
2599 SDNode *LatestCallSeqEnd = 0;
2600 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2601 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002602
Chris Lattner2dce7032005-05-12 23:24:06 +00002603 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002604 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002605 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2606 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002607 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2608 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002609 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002610 LatestCallSeqEnd = Entry.Val;
2611 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002612
Chris Lattner06bbeb62005-05-11 19:02:11 +00002613 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002614 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002615 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002616 std::set<SDNode*> Visited;
2617 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002618
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002619 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002620 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002621 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002622
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002623 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002624}
2625
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002626/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002627void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2628 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002629 // Nothing to splice it into?
2630 if (OutChain == 0) return;
2631
2632 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2633 //OutChain->dump();
2634
2635 // Form a token factor node merging the old inval and the new inval.
2636 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2637 OutChain->getOperand(0));
2638 // Change the node to refer to the new token.
2639 OutChain->setAdjCallChain(InToken);
2640}
Chris Lattner4add7e32005-01-23 04:42:50 +00002641
2642
Chris Lattneraac464e2005-01-21 06:05:23 +00002643// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2644// does not fit into a register, return the lo part and set the hi part to the
2645// by-reg argument. If it does fit into a single register, return the result
2646// and leave the Hi part unset.
2647SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2648 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002649 SDNode *OutChain;
2650 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2651 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002652 if (InChain.Val == 0)
2653 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002654
Chris Lattneraac464e2005-01-21 06:05:23 +00002655 TargetLowering::ArgListTy Args;
2656 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2657 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2658 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2659 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2660 }
2661 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002662
Chris Lattner06bbeb62005-05-11 19:02:11 +00002663 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002664 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002665 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002666 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2667 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002668 SpliceCallInto(CallInfo.second, OutChain);
2669
2670 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002671
2672 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002673 default: assert(0 && "Unknown thing");
2674 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002675 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002676 case Promote:
2677 assert(0 && "Cannot promote this yet!");
2678 case Expand:
2679 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002680 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002681 return Lo;
2682 }
2683}
2684
Chris Lattner4add7e32005-01-23 04:42:50 +00002685
Chris Lattneraac464e2005-01-21 06:05:23 +00002686/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2687/// destination type is legal.
2688SDOperand SelectionDAGLegalize::
2689ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2690 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2691 assert(getTypeAction(Source.getValueType()) == Expand &&
2692 "This is not an expansion!");
2693 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2694
Chris Lattner06bbeb62005-05-11 19:02:11 +00002695 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002696 assert(Source.getValueType() == MVT::i64 &&
2697 "This only works for 64-bit -> FP");
2698 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2699 // incoming integer is set. To handle this, we dynamically test to see if
2700 // it is set, and, if so, add a fudge factor.
2701 SDOperand Lo, Hi;
2702 ExpandOp(Source, Lo, Hi);
2703
Chris Lattner2a4f7312005-05-13 04:45:13 +00002704 // If this is unsigned, and not supported, first perform the conversion to
2705 // signed, then adjust the result if the sign bit is set.
2706 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2707 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2708
Chris Lattnerd47675e2005-08-09 20:20:18 +00002709 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2710 DAG.getConstant(0, Hi.getValueType()),
2711 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002712 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2713 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2714 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002715 uint64_t FF = 0x5f800000ULL;
2716 if (TLI.isLittleEndian()) FF <<= 32;
2717 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002718
2719 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2720 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2721 TLI.getPointerTy());
2722 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2723 SDOperand FudgeInReg;
2724 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002725 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2726 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002727 else {
2728 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002729 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2730 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002731 }
2732 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002733 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002734
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002735 // Check to see if the target has a custom way to lower this. If so, use it.
2736 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2737 default: assert(0 && "This action not implemented for this operation!");
2738 case TargetLowering::Legal:
2739 case TargetLowering::Expand:
2740 break; // This case is handled below.
2741 case TargetLowering::Custom:
2742 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002743 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002744 }
2745
Chris Lattner153587e2005-05-12 07:00:44 +00002746 // Expand the source, then glue it back together for the call. We must expand
2747 // the source in case it is shared (this pass of legalize must traverse it).
2748 SDOperand SrcLo, SrcHi;
2749 ExpandOp(Source, SrcLo, SrcHi);
2750 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2751
Chris Lattner06bbeb62005-05-11 19:02:11 +00002752 SDNode *OutChain = 0;
2753 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2754 DAG.getEntryNode());
2755 const char *FnName = 0;
2756 if (DestTy == MVT::f32)
2757 FnName = "__floatdisf";
2758 else {
2759 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2760 FnName = "__floatdidf";
2761 }
2762
Chris Lattneraac464e2005-01-21 06:05:23 +00002763 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2764
2765 TargetLowering::ArgListTy Args;
2766 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002767
Chris Lattneraac464e2005-01-21 06:05:23 +00002768 Args.push_back(std::make_pair(Source, ArgTy));
2769
2770 // We don't care about token chains for libcalls. We just use the entry
2771 // node as our input and ignore the output chain. This allows us to place
2772 // calls wherever we need them to satisfy data dependences.
2773 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002774
2775 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002776 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2777 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002778
Chris Lattnera5bf1032005-05-12 04:49:08 +00002779 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002780 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002781}
Misha Brukman835702a2005-04-21 22:36:52 +00002782
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002783
2784
Chris Lattnerdc750592005-01-07 07:47:09 +00002785/// ExpandOp - Expand the specified SDOperand into its two component pieces
2786/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2787/// LegalizeNodes map is filled in for any results that are not expanded, the
2788/// ExpandedNodes map is filled in for any results that are expanded, and the
2789/// Lo/Hi values are returned.
2790void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2791 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002792 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002793 SDNode *Node = Op.Val;
2794 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2795 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2796 assert(MVT::isInteger(NVT) && NVT < VT &&
2797 "Cannot expand to FP value or to larger int value!");
2798
2799 // If there is more than one use of this, see if we already expanded it.
2800 // There is no use remembering values that only have a single use, as the map
2801 // entries will never be reused.
2802 if (!Node->hasOneUse()) {
2803 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2804 = ExpandedNodes.find(Op);
2805 if (I != ExpandedNodes.end()) {
2806 Lo = I->second.first;
2807 Hi = I->second.second;
2808 return;
2809 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002810 } else {
2811 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002812 }
2813
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002814 // Expanding to multiple registers needs to perform an optimization step, and
2815 // is not careful to avoid operations the target does not support. Make sure
2816 // that all generated operations are legalized in the next iteration.
2817 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002818
Chris Lattnerdc750592005-01-07 07:47:09 +00002819 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002820 case ISD::CopyFromReg:
2821 assert(0 && "CopyFromReg must be legal!");
2822 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00002823 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2824 assert(0 && "Do not know how to expand this operator!");
2825 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002826 case ISD::UNDEF:
2827 Lo = DAG.getNode(ISD::UNDEF, NVT);
2828 Hi = DAG.getNode(ISD::UNDEF, NVT);
2829 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002830 case ISD::Constant: {
2831 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2832 Lo = DAG.getConstant(Cst, NVT);
2833 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2834 break;
2835 }
2836
Chris Lattner32e08b72005-03-28 22:03:13 +00002837 case ISD::BUILD_PAIR:
2838 // Legalize both operands. FIXME: in the future we should handle the case
2839 // where the two elements are not legal.
2840 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2841 Lo = LegalizeOp(Node->getOperand(0));
2842 Hi = LegalizeOp(Node->getOperand(1));
2843 break;
2844
Chris Lattner55e9cde2005-05-11 04:51:16 +00002845 case ISD::CTPOP:
2846 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002847 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2848 DAG.getNode(ISD::CTPOP, NVT, Lo),
2849 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002850 Hi = DAG.getConstant(0, NVT);
2851 break;
2852
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002853 case ISD::CTLZ: {
2854 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002855 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002856 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2857 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002858 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2859 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002860 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2861 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2862
2863 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2864 Hi = DAG.getConstant(0, NVT);
2865 break;
2866 }
2867
2868 case ISD::CTTZ: {
2869 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002870 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002871 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2872 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002873 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2874 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002875 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2876 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2877
2878 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2879 Hi = DAG.getConstant(0, NVT);
2880 break;
2881 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002882
Chris Lattnerdc750592005-01-07 07:47:09 +00002883 case ISD::LOAD: {
2884 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2885 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002886 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002887
2888 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002889 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002890 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2891 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002892 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002893 //are from the same instruction?
2894 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002895
2896 // Build a factor node to remember that this load is independent of the
2897 // other one.
2898 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2899 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002900
Chris Lattnerdc750592005-01-07 07:47:09 +00002901 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002902 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002903 if (!TLI.isLittleEndian())
2904 std::swap(Lo, Hi);
2905 break;
2906 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002907 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002908 case ISD::CALL: {
2909 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2910 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2911
Chris Lattner3d95c142005-01-19 20:24:35 +00002912 bool Changed = false;
2913 std::vector<SDOperand> Ops;
2914 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2915 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2916 Changed |= Ops.back() != Node->getOperand(i);
2917 }
2918
Chris Lattnerdc750592005-01-07 07:47:09 +00002919 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2920 "Can only expand a call once so far, not i64 -> i16!");
2921
2922 std::vector<MVT::ValueType> RetTyVTs;
2923 RetTyVTs.reserve(3);
2924 RetTyVTs.push_back(NVT);
2925 RetTyVTs.push_back(NVT);
2926 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002927 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2928 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002929 Lo = SDOperand(NC, 0);
2930 Hi = SDOperand(NC, 1);
2931
2932 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002933 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002934 break;
2935 }
2936 case ISD::AND:
2937 case ISD::OR:
2938 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2939 SDOperand LL, LH, RL, RH;
2940 ExpandOp(Node->getOperand(0), LL, LH);
2941 ExpandOp(Node->getOperand(1), RL, RH);
2942 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2943 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2944 break;
2945 }
2946 case ISD::SELECT: {
2947 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002948
2949 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2950 case Expand: assert(0 && "It's impossible to expand bools");
2951 case Legal:
2952 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2953 break;
2954 case Promote:
2955 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2956 break;
2957 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002958 ExpandOp(Node->getOperand(1), LL, LH);
2959 ExpandOp(Node->getOperand(2), RL, RH);
2960 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2961 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2962 break;
2963 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002964 case ISD::SELECT_CC: {
2965 SDOperand TL, TH, FL, FH;
2966 ExpandOp(Node->getOperand(2), TL, TH);
2967 ExpandOp(Node->getOperand(3), FL, FH);
2968 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2969 Node->getOperand(1), TL, FL, Node->getOperand(4));
2970 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2971 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00002972 Lo = LegalizeOp(Lo);
2973 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00002974 break;
2975 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002976 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002977 SDOperand In;
2978 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2979 case Expand: assert(0 && "expand-expand not implemented yet!");
2980 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2981 case Promote:
2982 In = PromoteOp(Node->getOperand(0));
2983 // Emit the appropriate sign_extend_inreg to get the value we want.
2984 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002985 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002986 break;
2987 }
2988
Chris Lattnerdc750592005-01-07 07:47:09 +00002989 // The low part is just a sign extension of the input (which degenerates to
2990 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002991 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002992
Chris Lattnerdc750592005-01-07 07:47:09 +00002993 // The high part is obtained by SRA'ing all but one of the bits of the lo
2994 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002995 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002996 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2997 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002998 break;
2999 }
Chris Lattner47844892005-04-03 23:41:52 +00003000 case ISD::ZERO_EXTEND: {
3001 SDOperand In;
3002 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3003 case Expand: assert(0 && "expand-expand not implemented yet!");
3004 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3005 case Promote:
3006 In = PromoteOp(Node->getOperand(0));
3007 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003008 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003009 break;
3010 }
3011
Chris Lattnerdc750592005-01-07 07:47:09 +00003012 // The low part is just a zero extension of the input (which degenerates to
3013 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003014 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003015
Chris Lattnerdc750592005-01-07 07:47:09 +00003016 // The high part is just a zero.
3017 Hi = DAG.getConstant(0, NVT);
3018 break;
Chris Lattner47844892005-04-03 23:41:52 +00003019 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003020 // These operators cannot be expanded directly, emit them as calls to
3021 // library functions.
3022 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003023 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003024 SDOperand Op;
3025 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3026 case Expand: assert(0 && "cannot expand FP!");
3027 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3028 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3029 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003030
Chris Lattner941d84a2005-07-30 01:40:57 +00003031 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3032
Chris Lattnerfe68d752005-07-29 00:33:32 +00003033 // Now that the custom expander is done, expand the result, which is still
3034 // VT.
Chris Lattner941d84a2005-07-30 01:40:57 +00003035 ExpandOp(Op, Lo, Hi);
Chris Lattnerfe68d752005-07-29 00:33:32 +00003036 break;
3037 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003038
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003039 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003040 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003041 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003042 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003043 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003044
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003045 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003046 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3047 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3048 LegalizeOp(Node->getOperand(0)));
3049 // Now that the custom expander is done, expand the result, which is still
3050 // VT.
3051 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
3052 break;
3053 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003054
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003055 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003056 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003057 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003058 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003059 break;
3060
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003061 case ISD::SHL:
3062 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003063 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003064 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003065
3066 // If this target supports SHL_PARTS, use it.
3067 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003068 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3069 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003070 break;
3071 }
3072
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003073 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003074 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003075 break;
3076
3077 case ISD::SRA:
3078 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003079 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003080 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003081
3082 // If this target supports SRA_PARTS, use it.
3083 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003084 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3085 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003086 break;
3087 }
3088
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003089 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003090 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003091 break;
3092 case ISD::SRL:
3093 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003094 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003095 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003096
3097 // If this target supports SRL_PARTS, use it.
3098 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003099 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3100 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003101 break;
3102 }
3103
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003104 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003105 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003106 break;
3107
Misha Brukman835702a2005-04-21 22:36:52 +00003108 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003109 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3110 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003111 break;
3112 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003113 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3114 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003115 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003116 case ISD::MUL: {
3117 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
3118 SDOperand LL, LH, RL, RH;
3119 ExpandOp(Node->getOperand(0), LL, LH);
3120 ExpandOp(Node->getOperand(1), RL, RH);
3121 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3122 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3123 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3124 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3125 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3126 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3127 } else {
3128 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3129 }
3130 break;
3131 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003132 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3133 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3134 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3135 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003136 }
3137
3138 // Remember in a map if the values will be reused later.
3139 if (!Node->hasOneUse()) {
3140 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3141 std::make_pair(Lo, Hi))).second;
3142 assert(isNew && "Value already expanded?!?");
3143 }
3144}
3145
3146
3147// SelectionDAG::Legalize - This is the entry point for the file.
3148//
Chris Lattner4add7e32005-01-23 04:42:50 +00003149void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003150 /// run - This is the main entry point to this class.
3151 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003152 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003153}
3154