blob: ab070301b08725d9eb547c9adcf2ecca902a904f [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000018#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
24#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000025#include <set>
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-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 Lattner03c85462005-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 Lattner3e928bb2005-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 Lattner8afc48e2005-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 Lattner03c85462005-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 Lattner8afc48e2005-01-07 22:28:47 +000088
Chris Lattner3e928bb2005-01-07 07:47:09 +000089public:
90
Chris Lattner9c32d3b2005-01-23 04:42:50 +000091 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-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 Lattner03c85462005-01-15 05:21:40 +0000123 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124
Chris Lattner77e77a62005-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 Lattnercad063f2005-07-16 00:19:57 +0000129
Jim Laskey6269ed12005-08-17 00:39:29 +0000130 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
131 SDOperand LegalOp,
132 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000133 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
134 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000135 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
136 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000137
Chris Lattnere34b3962005-01-19 04:19:40 +0000138 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
139 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-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 Lattner47599822005-04-02 03:38:53 +0000143 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000144
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000145 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
146
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147 SDOperand getIntPtrConstant(uint64_t Val) {
148 return DAG.getConstant(Val, TLI.getPointerTy());
149 }
150};
151}
152
153
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000154SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
155 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
156 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000157 assert(MVT::LAST_VALUETYPE <= 16 &&
158 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000159}
160
Jim Laskey6269ed12005-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 Lattnercad063f2005-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 Laskey6269ed12005-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 Laskey02659d22005-08-17 17:42:52 +0000208 SDOperand Bias = DAG.getConstantFP(isSigned ?
209 BitsToDouble(0x4330000080000000ULL)
210 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-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 Lattnercad063f2005-07-16 00:19:57 +0000228 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000229
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000230 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
231 DAG.getConstant(0, Op0.getValueType()),
232 ISD::SETLT);
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000236
Jim Laskey6269ed12005-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 Lattnerf4d32722005-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 Lattnercad063f2005-07-16 00:19:57 +0000248 if (TLI.isLittleEndian()) FF <<= 32;
249 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000250
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000265
Chris Lattnercad063f2005-07-16 00:19:57 +0000266 NeedsAnotherIteration = true;
267 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
268}
269
Chris Lattner149c58c2005-08-16 18:17:10 +0000270/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000271/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-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 Begeman5a8441e2005-07-16 02:02:34 +0000275SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
276 MVT::ValueType DestVT,
277 bool isSigned) {
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000280
Chris Lattnercad063f2005-07-16 00:19:57 +0000281 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000282
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000287
Chris Lattnercad063f2005-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 Begeman5a8441e2005-07-16 02:02:34 +0000300 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000301
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000314
Chris Lattnercad063f2005-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 Cohen00b168892005-07-27 06:12:32 +0000320
Chris Lattnercad063f2005-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 Begeman5a8441e2005-07-16 02:02:34 +0000324 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
325 NewInTy, LegalOp));
Chris Lattnercad063f2005-07-16 00:19:57 +0000326}
327
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000338
Chris Lattner1618beb2005-07-29 00:11:56 +0000339 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000340
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000345
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000358
Chris Lattner1618beb2005-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 Cohend29b6aa2005-07-30 18:33:25 +0000371
Chris Lattner1618beb2005-07-29 00:11:56 +0000372 // Otherwise, try a larger type.
373 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000374
Chris Lattner1618beb2005-07-29 00:11:56 +0000375 // Make sure to legalize any nodes we create here in the next pass.
376 NeedsAnotherIteration = true;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000377
Chris Lattner1618beb2005-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 Lattner3e928bb2005-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 Lattner71c42a02005-01-16 01:11:45 +0000392 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000393
394 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000395 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000396}
397
398SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000399 assert(getTypeAction(Op.getValueType()) == Legal &&
400 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000401 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000402
Chris Lattner3e928bb2005-01-07 07:47:09 +0000403 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000404 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-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 Lattner3e928bb2005-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 Lattner03c85462005-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 Lattner3e928bb2005-01-07 07:47:09 +0000421 }
422 }
423
Chris Lattner45982da2005-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 Lattnere1bd8222005-01-11 05:57:22 +0000426 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
427 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000428
Nate Begeman9373a812005-08-10 20:51:12 +0000429 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000430
431 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000432
433 switch (Node->getOpcode()) {
434 default:
Chris Lattnerd73cc5d2005-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 Lattner3e928bb2005-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 Lattner03c0cf82005-01-07 21:45:56 +0000464 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000465 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466 assert(getTypeAction(Node->getValueType(0)) == Legal &&
467 "This must be legal!");
468 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000469 case ISD::CopyFromReg:
470 Tmp1 = LegalizeOp(Node->getOperand(0));
471 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000472 Result = DAG.getCopyFromReg(Tmp1,
473 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
474 Node->getValueType(0));
Chris Lattner13c184d2005-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 Lattner18c2f132005-01-13 20:50:02 +0000483 case ISD::ImplicitDef:
484 Tmp1 = LegalizeOp(Node->getOperand(0));
485 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000486 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
487 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000488 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000489 case ISD::UNDEF: {
490 MVT::ValueType VT = Op.getValueType();
491 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000492 default: assert(0 && "This action is not supported yet!");
493 case TargetLowering::Expand:
494 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-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 Begemanea19cd52005-04-02 00:41:14 +0000502 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000503 break;
504 }
505 break;
506 }
Chris Lattner3e928bb2005-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 Lattner99939d32005-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 Lattner3e928bb2005-01-07 07:47:09 +0000549 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
550 VT = MVT::f32;
551 Extend = true;
552 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000553
Chris Lattner3e928bb2005-01-07 07:47:09 +0000554 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
555 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000556 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000557 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
558 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000559 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000560 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
561 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000562 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000563 }
564 break;
565 }
Chris Lattnera385e9b2005-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 Lattner1e81b9e2005-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 Lattnere131e5b2005-05-12 06:04:14 +0000572 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-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 Lattnera385e9b2005-01-13 17:59:25 +0000581 }
582 if (Changed)
583 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
584 break;
585 }
586
Chris Lattner16cd04d2005-05-12 23:24:06 +0000587 case ISD::CALLSEQ_START:
588 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000590 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000591 Tmp2 = Node->getOperand(0);
592 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000593 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-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 Lattner16cd04d2005-05-12 23:24:06 +0000603 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-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 Lattner3e928bb2005-01-07 07:47:09 +0000607 break;
Chris Lattnerfa404e82005-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 Lattneradf6c2a2005-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 Lattner513e52e2005-01-09 19:07:54 +0000619 Result = Op.getValue(0);
Chris Lattnerfa404e82005-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 Lattnerd71c0412005-05-13 18:43:43 +0000627 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000628 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-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 Lattner3e928bb2005-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 Lattnerebda9422005-01-07 21:34:13 +0000643 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000644 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
645 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000646 } else {
647 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000648 }
Chris Lattner38d6be52005-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 Lattner3d9dffc2005-01-19 20:24:35 +0000654 }
Chris Lattnerc7af1792005-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 Lattnerc18ae4c2005-01-07 08:19:42 +0000661 case ISD::BRCOND:
662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000663
Chris Lattner47e92232005-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 Begeman7cbd5252005-08-16 19:49:35 +0000673
674 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
675 default: assert(0 && "This action is not supported yet!");
676 case TargetLowering::Expand:
677 // Expand brcond's setcc into its constituent parts and create a BR_CC
678 // Node.
679 if (Tmp2.getOpcode() == ISD::SETCC) {
680 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
681 Tmp2.getOperand(0), Tmp2.getOperand(1),
682 Node->getOperand(2));
683 } else {
684 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
685 DAG.getCondCode(ISD::SETNE), Tmp2,
686 DAG.getConstant(0, Tmp2.getValueType()),
687 Node->getOperand(2));
688 }
689 break;
690 case TargetLowering::Legal:
691 // Basic block destination (Op#2) is always legal.
692 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
693 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
694 Node->getOperand(2));
695 break;
696 }
697 break;
698 case ISD::BR_CC:
699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
700
701 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
702 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
703 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
704 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
705 Tmp3 != Node->getOperand(3)) {
706 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
707 Tmp2, Tmp3, Node->getOperand(4));
708 }
709 break;
710 } else {
711 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
712 Node->getOperand(2), // LHS
713 Node->getOperand(3), // RHS
714 Node->getOperand(1)));
715 // If we get a SETCC back from legalizing the SETCC node we just
716 // created, then use its LHS, RHS, and CC directly in creating a new
717 // node. Otherwise, select between the true and false value based on
718 // comparing the result of the legalized with zero.
719 if (Tmp2.getOpcode() == ISD::SETCC) {
720 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
721 Tmp2.getOperand(0), Tmp2.getOperand(1),
722 Node->getOperand(4));
723 } else {
724 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
725 DAG.getCondCode(ISD::SETNE),
726 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
727 Node->getOperand(4));
728 }
729 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000730 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000731 case ISD::BRCONDTWOWAY:
732 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
733 switch (getTypeAction(Node->getOperand(1).getValueType())) {
734 case Expand: assert(0 && "It's impossible to expand bools");
735 case Legal:
736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
737 break;
738 case Promote:
739 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
740 break;
741 }
742 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
743 // pair.
744 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
745 case TargetLowering::Promote:
746 default: assert(0 && "This action is not supported yet!");
747 case TargetLowering::Legal:
748 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
749 std::vector<SDOperand> Ops;
750 Ops.push_back(Tmp1);
751 Ops.push_back(Tmp2);
752 Ops.push_back(Node->getOperand(2));
753 Ops.push_back(Node->getOperand(3));
754 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
755 }
756 break;
757 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000758 // If BRTWOWAY_CC is legal for this target, then simply expand this node
759 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
760 // BRCOND/BR pair.
761 if (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other) ==
762 TargetLowering::Legal) {
763 if (Tmp2.getOpcode() == ISD::SETCC) {
764 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
765 Tmp2.getOperand(0), Tmp2.getOperand(1),
766 Node->getOperand(2), Node->getOperand(3));
767 } else {
768 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
769 DAG.getConstant(0, Tmp2.getValueType()),
770 Node->getOperand(2), Node->getOperand(3));
771 }
772 } else {
773 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000774 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000775 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
776 }
Chris Lattner411e8882005-04-09 03:30:19 +0000777 break;
778 }
779 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000780 case ISD::BRTWOWAY_CC:
781 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
782 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
783 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
784 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
785 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
786 Tmp3 != Node->getOperand(3)) {
787 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
788 Node->getOperand(4), Node->getOperand(5));
789 }
790 break;
791 } else {
792 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
793 Node->getOperand(2), // LHS
794 Node->getOperand(3), // RHS
795 Node->getOperand(1)));
796 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
797 // pair.
798 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
799 default: assert(0 && "This action is not supported yet!");
800 case TargetLowering::Legal:
801 // If we get a SETCC back from legalizing the SETCC node we just
802 // created, then use its LHS, RHS, and CC directly in creating a new
803 // node. Otherwise, select between the true and false value based on
804 // comparing the result of the legalized with zero.
805 if (Tmp2.getOpcode() == ISD::SETCC) {
806 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
807 Tmp2.getOperand(0), Tmp2.getOperand(1),
808 Node->getOperand(4), Node->getOperand(5));
809 } else {
810 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
811 DAG.getConstant(0, Tmp2.getValueType()),
812 Node->getOperand(4), Node->getOperand(5));
813 }
814 break;
815 case TargetLowering::Expand:
816 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
817 Node->getOperand(4));
818 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
819 break;
820 }
821 }
822 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000823 case ISD::LOAD:
824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
825 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000826
Chris Lattner3e928bb2005-01-07 07:47:09 +0000827 if (Tmp1 != Node->getOperand(0) ||
828 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000829 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
830 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000831 else
832 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000833
Chris Lattner8afc48e2005-01-07 22:28:47 +0000834 // Since loads produce two values, make sure to remember that we legalized
835 // both of them.
836 AddLegalizedOperand(SDOperand(Node, 0), Result);
837 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
838 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000839
Chris Lattner0f69b292005-01-15 06:18:18 +0000840 case ISD::EXTLOAD:
841 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000842 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000843 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
844 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000845
Chris Lattner5f056bf2005-07-10 01:55:33 +0000846 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000847 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000848 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000849 case TargetLowering::Promote:
850 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000851 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
852 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000853 // Since loads produce two values, make sure to remember that we legalized
854 // both of them.
855 AddLegalizedOperand(SDOperand(Node, 0), Result);
856 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
857 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000858
Chris Lattner01ff7212005-04-10 22:54:25 +0000859 case TargetLowering::Legal:
860 if (Tmp1 != Node->getOperand(0) ||
861 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000862 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
863 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000864 else
865 Result = SDOperand(Node, 0);
866
867 // Since loads produce two values, make sure to remember that we legalized
868 // both of them.
869 AddLegalizedOperand(SDOperand(Node, 0), Result);
870 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
871 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000872 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000873 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
874 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
875 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000876 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000877 if (Op.ResNo)
878 return Load.getValue(1);
879 return Result;
880 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000881 assert(Node->getOpcode() != ISD::EXTLOAD &&
882 "EXTLOAD should always be supported!");
883 // Turn the unsupported load into an EXTLOAD followed by an explicit
884 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000885 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
886 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000887 SDOperand ValRes;
888 if (Node->getOpcode() == ISD::SEXTLOAD)
889 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000890 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000891 else
892 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000893 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
894 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
895 if (Op.ResNo)
896 return Result.getValue(1);
897 return ValRes;
898 }
899 assert(0 && "Unreachable");
900 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000901 case ISD::EXTRACT_ELEMENT:
902 // Get both the low and high parts.
903 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
904 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
905 Result = Tmp2; // 1 -> Hi
906 else
907 Result = Tmp1; // 0 -> Lo
908 break;
909
910 case ISD::CopyToReg:
911 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000912
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000913 assert(getTypeAction(Node->getOperand(2).getValueType()) == Legal &&
914 "Register type must be legal!");
915 // Legalize the incoming value (must be legal).
916 Tmp2 = LegalizeOp(Node->getOperand(2));
917 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
918 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
919 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000920 break;
921
922 case ISD::RET:
923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
924 switch (Node->getNumOperands()) {
925 case 2: // ret val
926 switch (getTypeAction(Node->getOperand(1).getValueType())) {
927 case Legal:
928 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000929 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000930 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
931 break;
932 case Expand: {
933 SDOperand Lo, Hi;
934 ExpandOp(Node->getOperand(1), Lo, Hi);
935 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000936 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000937 }
938 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000939 Tmp2 = PromoteOp(Node->getOperand(1));
940 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
941 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000942 }
943 break;
944 case 1: // ret void
945 if (Tmp1 != Node->getOperand(0))
946 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
947 break;
948 default: { // ret <values>
949 std::vector<SDOperand> NewValues;
950 NewValues.push_back(Tmp1);
951 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
952 switch (getTypeAction(Node->getOperand(i).getValueType())) {
953 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000954 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000955 break;
956 case Expand: {
957 SDOperand Lo, Hi;
958 ExpandOp(Node->getOperand(i), Lo, Hi);
959 NewValues.push_back(Lo);
960 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000961 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000962 }
963 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000964 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000965 }
966 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
967 break;
968 }
969 }
970 break;
971 case ISD::STORE:
972 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
973 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
974
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000975 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000976 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000977 if (CFP->getValueType(0) == MVT::f32) {
978 union {
979 unsigned I;
980 float F;
981 } V;
982 V.F = CFP->getValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000983 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000984 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000985 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000986 } else {
987 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
988 union {
989 uint64_t I;
990 double F;
991 } V;
992 V.F = CFP->getValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000993 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000994 DAG.getConstant(V.I, MVT::i64), Tmp2,
995 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000996 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000997 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000998 }
999
Chris Lattner3e928bb2005-01-07 07:47:09 +00001000 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1001 case Legal: {
1002 SDOperand Val = LegalizeOp(Node->getOperand(1));
1003 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1004 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001005 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1006 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001007 break;
1008 }
1009 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001010 // Truncate the value and store the result.
1011 Tmp3 = PromoteOp(Node->getOperand(1));
1012 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001013 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001014 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001015 break;
1016
Chris Lattner3e928bb2005-01-07 07:47:09 +00001017 case Expand:
1018 SDOperand Lo, Hi;
1019 ExpandOp(Node->getOperand(1), Lo, Hi);
1020
1021 if (!TLI.isLittleEndian())
1022 std::swap(Lo, Hi);
1023
Chris Lattneredb1add2005-05-11 04:51:16 +00001024 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1025 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001026 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001027 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1028 getIntPtrConstant(IncrementSize));
1029 assert(isTypeLegal(Tmp2.getValueType()) &&
1030 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001031 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001032 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1033 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001034 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1035 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001036 }
1037 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001038 case ISD::PCMARKER:
1039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001040 if (Tmp1 != Node->getOperand(0))
1041 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001042 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001043 case ISD::TRUNCSTORE:
1044 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1045 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1046
1047 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1048 case Legal:
1049 Tmp2 = LegalizeOp(Node->getOperand(1));
1050 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1051 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +00001052 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001053 Node->getOperand(3), Node->getOperand(4));
Chris Lattner0f69b292005-01-15 06:18:18 +00001054 break;
1055 case Promote:
1056 case Expand:
1057 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1058 }
1059 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001060 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001061 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1062 case Expand: assert(0 && "It's impossible to expand bools");
1063 case Legal:
1064 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1065 break;
1066 case Promote:
1067 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1068 break;
1069 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001070 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001071 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001072
1073 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
1074 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001075 case TargetLowering::Expand:
1076 if (Tmp1.getOpcode() == ISD::SETCC) {
1077 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1078 Tmp2, Tmp3,
1079 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1080 } else {
1081 Result = DAG.getSelectCC(Tmp1,
1082 DAG.getConstant(0, Tmp1.getValueType()),
1083 Tmp2, Tmp3, ISD::SETNE);
1084 }
1085 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001086 case TargetLowering::Legal:
1087 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1088 Tmp3 != Node->getOperand(2))
1089 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1090 Tmp1, Tmp2, Tmp3);
1091 break;
1092 case TargetLowering::Promote: {
1093 MVT::ValueType NVT =
1094 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1095 unsigned ExtOp, TruncOp;
1096 if (MVT::isInteger(Tmp2.getValueType())) {
1097 ExtOp = ISD::ZERO_EXTEND;
1098 TruncOp = ISD::TRUNCATE;
1099 } else {
1100 ExtOp = ISD::FP_EXTEND;
1101 TruncOp = ISD::FP_ROUND;
1102 }
1103 // Promote each of the values to the new type.
1104 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1105 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1106 // Perform the larger operation, then round down.
1107 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1108 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1109 break;
1110 }
1111 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001112 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001113 case ISD::SELECT_CC:
1114 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1115 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1116
1117 if (getTypeAction(Node->getOperand(0).getValueType()) == Legal) {
1118 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1119 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1120 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1121 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1122 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1123 Tmp3, Tmp4, Node->getOperand(4));
1124 }
1125 break;
1126 } else {
1127 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1128 Node->getOperand(0), // LHS
1129 Node->getOperand(1), // RHS
1130 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001131 // If we get a SETCC back from legalizing the SETCC node we just
1132 // created, then use its LHS, RHS, and CC directly in creating a new
1133 // node. Otherwise, select between the true and false value based on
1134 // comparing the result of the legalized with zero.
1135 if (Tmp1.getOpcode() == ISD::SETCC) {
1136 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1137 Tmp1.getOperand(0), Tmp1.getOperand(1),
1138 Tmp3, Tmp4, Tmp1.getOperand(2));
1139 } else {
1140 Result = DAG.getSelectCC(Tmp1,
1141 DAG.getConstant(0, Tmp1.getValueType()),
1142 Tmp3, Tmp4, ISD::SETNE);
1143 }
Nate Begeman9373a812005-08-10 20:51:12 +00001144 }
1145 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001146 case ISD::SETCC:
1147 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1148 case Legal:
1149 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1150 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1151 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001152 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1153 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001154 break;
1155 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001156 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1157 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1158
1159 // If this is an FP compare, the operands have already been extended.
1160 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1161 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001162 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001163
1164 // Otherwise, we have to insert explicit sign or zero extends. Note
1165 // that we could insert sign extends for ALL conditions, but zero extend
1166 // is cheaper on many machines (an AND instead of two shifts), so prefer
1167 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001168 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001169 default: assert(0 && "Unknown integer comparison!");
1170 case ISD::SETEQ:
1171 case ISD::SETNE:
1172 case ISD::SETUGE:
1173 case ISD::SETUGT:
1174 case ISD::SETULE:
1175 case ISD::SETULT:
1176 // ALL of these operations will work if we either sign or zero extend
1177 // the operands (including the unsigned comparisons!). Zero extend is
1178 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001179 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1180 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001181 break;
1182 case ISD::SETGE:
1183 case ISD::SETGT:
1184 case ISD::SETLT:
1185 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001186 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1187 DAG.getValueType(VT));
1188 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1189 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001190 break;
1191 }
1192
1193 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001194 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1195 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001196 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001197 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001198 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1199 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1200 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001201 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001202 case ISD::SETEQ:
1203 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001204 if (RHSLo == RHSHi)
1205 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1206 if (RHSCST->isAllOnesValue()) {
1207 // Comparison to -1.
1208 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001209 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1210 RHSLo, Node->getOperand(2));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001211 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001212 }
1213
Chris Lattner3e928bb2005-01-07 07:47:09 +00001214 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1215 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1216 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001217 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1218 DAG.getConstant(0, Tmp1.getValueType()),
1219 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001220 break;
1221 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001222 // If this is a comparison of the sign bit, just look at the top part.
1223 // X > -1, x < 0
1224 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001225 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001226 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001227 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001228 (CST->isAllOnesValue()))) // X > -1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001229 return DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1230 Node->getOperand(2));
Chris Lattner5b95ed62005-04-12 02:19:10 +00001231
Chris Lattner3e928bb2005-01-07 07:47:09 +00001232 // FIXME: This generated code sucks.
1233 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001234 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001235 default: assert(0 && "Unknown integer setcc!");
1236 case ISD::SETLT:
1237 case ISD::SETULT: LowCC = ISD::SETULT; break;
1238 case ISD::SETGT:
1239 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1240 case ISD::SETLE:
1241 case ISD::SETULE: LowCC = ISD::SETULE; break;
1242 case ISD::SETGE:
1243 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1244 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001245
Chris Lattner3e928bb2005-01-07 07:47:09 +00001246 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1247 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1248 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1249
1250 // NOTE: on targets without efficient SELECT of bools, we can always use
1251 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001252 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1253 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1254 Node->getOperand(2));
1255 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001256 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1257 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001258 break;
1259 }
1260 }
1261 break;
1262
Chris Lattnere1bd8222005-01-11 05:57:22 +00001263 case ISD::MEMSET:
1264 case ISD::MEMCPY:
1265 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001266 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001267 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1268
1269 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1270 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1271 case Expand: assert(0 && "Cannot expand a byte!");
1272 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001273 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001274 break;
1275 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001276 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001277 break;
1278 }
1279 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001280 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001281 }
Chris Lattner272455b2005-02-02 03:44:41 +00001282
1283 SDOperand Tmp4;
1284 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001285 case Expand: {
1286 // Length is too big, just take the lo-part of the length.
1287 SDOperand HiPart;
1288 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1289 break;
1290 }
Chris Lattnere5605212005-01-28 22:29:18 +00001291 case Legal:
1292 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001293 break;
1294 case Promote:
1295 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001296 break;
1297 }
1298
1299 SDOperand Tmp5;
1300 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1301 case Expand: assert(0 && "Cannot expand this yet!");
1302 case Legal:
1303 Tmp5 = LegalizeOp(Node->getOperand(4));
1304 break;
1305 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001306 Tmp5 = PromoteOp(Node->getOperand(4));
1307 break;
1308 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001309
1310 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1311 default: assert(0 && "This action not implemented for this operation!");
1312 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001313 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1314 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1315 Tmp5 != Node->getOperand(4)) {
1316 std::vector<SDOperand> Ops;
1317 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1318 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1319 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1320 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001321 break;
1322 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001323 // Otherwise, the target does not support this operation. Lower the
1324 // operation to an explicit libcall as appropriate.
1325 MVT::ValueType IntPtr = TLI.getPointerTy();
1326 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1327 std::vector<std::pair<SDOperand, const Type*> > Args;
1328
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001329 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001330 if (Node->getOpcode() == ISD::MEMSET) {
1331 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1332 // Extend the ubyte argument to be an int value for the call.
1333 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1334 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1335 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1336
1337 FnName = "memset";
1338 } else if (Node->getOpcode() == ISD::MEMCPY ||
1339 Node->getOpcode() == ISD::MEMMOVE) {
1340 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1341 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1342 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1343 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1344 } else {
1345 assert(0 && "Unknown op!");
1346 }
Chris Lattner45982da2005-05-12 16:53:42 +00001347
Chris Lattnere1bd8222005-01-11 05:57:22 +00001348 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001349 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001350 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001351 Result = CallResult.second;
1352 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001353 break;
1354 }
1355 case TargetLowering::Custom:
1356 std::vector<SDOperand> Ops;
1357 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1358 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1359 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner50381b62005-05-14 05:50:48 +00001360 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001361 Result = LegalizeOp(Result);
1362 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001363 }
1364 break;
1365 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001366
1367 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001368 Tmp1 = LegalizeOp(Node->getOperand(0));
1369 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001370
Chris Lattner3e011362005-05-14 07:45:46 +00001371 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1372 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1373 std::vector<SDOperand> Ops;
1374 Ops.push_back(Tmp1);
1375 Ops.push_back(Tmp2);
1376 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1377 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001378 Result = SDOperand(Node, 0);
1379 // Since these produce two values, make sure to remember that we legalized
1380 // both of them.
1381 AddLegalizedOperand(SDOperand(Node, 0), Result);
1382 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1383 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001384 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001385 Tmp1 = LegalizeOp(Node->getOperand(0));
1386 Tmp2 = LegalizeOp(Node->getOperand(1));
1387 Tmp3 = LegalizeOp(Node->getOperand(2));
1388 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1389 Tmp3 != Node->getOperand(2))
1390 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1391 break;
1392
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001393 case ISD::READIO:
1394 Tmp1 = LegalizeOp(Node->getOperand(0));
1395 Tmp2 = LegalizeOp(Node->getOperand(1));
1396
1397 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1398 case TargetLowering::Custom:
1399 default: assert(0 && "This action not implemented for this operation!");
1400 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001401 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1402 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1403 std::vector<SDOperand> Ops;
1404 Ops.push_back(Tmp1);
1405 Ops.push_back(Tmp2);
1406 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1407 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001408 Result = SDOperand(Node, 0);
1409 break;
1410 case TargetLowering::Expand:
1411 // Replace this with a load from memory.
1412 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1413 Node->getOperand(1), DAG.getSrcValue(NULL));
1414 Result = LegalizeOp(Result);
1415 break;
1416 }
1417
1418 // Since these produce two values, make sure to remember that we legalized
1419 // both of them.
1420 AddLegalizedOperand(SDOperand(Node, 0), Result);
1421 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1422 return Result.getValue(Op.ResNo);
1423
1424 case ISD::WRITEIO:
1425 Tmp1 = LegalizeOp(Node->getOperand(0));
1426 Tmp2 = LegalizeOp(Node->getOperand(1));
1427 Tmp3 = LegalizeOp(Node->getOperand(2));
1428
1429 switch (TLI.getOperationAction(Node->getOpcode(),
1430 Node->getOperand(1).getValueType())) {
1431 case TargetLowering::Custom:
1432 default: assert(0 && "This action not implemented for this operation!");
1433 case TargetLowering::Legal:
1434 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1435 Tmp3 != Node->getOperand(2))
1436 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1437 break;
1438 case TargetLowering::Expand:
1439 // Replace this with a store to memory.
1440 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1441 Node->getOperand(1), Node->getOperand(2),
1442 DAG.getSrcValue(NULL));
1443 Result = LegalizeOp(Result);
1444 break;
1445 }
1446 break;
1447
Chris Lattner84f67882005-01-20 18:52:28 +00001448 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001449 case ISD::SUB_PARTS:
1450 case ISD::SHL_PARTS:
1451 case ISD::SRA_PARTS:
1452 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001453 std::vector<SDOperand> Ops;
1454 bool Changed = false;
1455 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1456 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1457 Changed |= Ops.back() != Node->getOperand(i);
1458 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001459 if (Changed) {
1460 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1461 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1462 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001463
1464 // Since these produce multiple values, make sure to remember that we
1465 // legalized all of them.
1466 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1467 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1468 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001469 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001470
1471 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001472 case ISD::ADD:
1473 case ISD::SUB:
1474 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001475 case ISD::MULHS:
1476 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001477 case ISD::UDIV:
1478 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001479 case ISD::AND:
1480 case ISD::OR:
1481 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001482 case ISD::SHL:
1483 case ISD::SRL:
1484 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001485 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001486 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1487 case Expand: assert(0 && "Not possible");
1488 case Legal:
1489 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1490 break;
1491 case Promote:
1492 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1493 break;
1494 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001495 if (Tmp1 != Node->getOperand(0) ||
1496 Tmp2 != Node->getOperand(1))
1497 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1498 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001499
Nate Begemanc105e192005-04-06 00:23:54 +00001500 case ISD::UREM:
1501 case ISD::SREM:
1502 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1503 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1504 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1505 case TargetLowering::Legal:
1506 if (Tmp1 != Node->getOperand(0) ||
1507 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001508 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001509 Tmp2);
1510 break;
1511 case TargetLowering::Promote:
1512 case TargetLowering::Custom:
1513 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001514 case TargetLowering::Expand:
1515 if (MVT::isInteger(Node->getValueType(0))) {
1516 MVT::ValueType VT = Node->getValueType(0);
1517 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1518 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1519 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1520 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1521 } else {
1522 // Floating point mod -> fmod libcall.
1523 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1524 SDOperand Dummy;
1525 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001526 }
1527 break;
1528 }
1529 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001530
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001531 case ISD::CTPOP:
1532 case ISD::CTTZ:
1533 case ISD::CTLZ:
1534 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1535 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1536 case TargetLowering::Legal:
1537 if (Tmp1 != Node->getOperand(0))
1538 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1539 break;
1540 case TargetLowering::Promote: {
1541 MVT::ValueType OVT = Tmp1.getValueType();
1542 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001543
1544 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001545 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1546 // Perform the larger operation, then subtract if needed.
1547 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1548 switch(Node->getOpcode())
1549 {
1550 case ISD::CTPOP:
1551 Result = Tmp1;
1552 break;
1553 case ISD::CTTZ:
1554 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001555 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1556 DAG.getConstant(getSizeInBits(NVT), NVT),
1557 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001558 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001559 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1560 break;
1561 case ISD::CTLZ:
1562 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001563 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1564 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001565 getSizeInBits(OVT), NVT));
1566 break;
1567 }
1568 break;
1569 }
1570 case TargetLowering::Custom:
1571 assert(0 && "Cannot custom handle this yet!");
1572 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001573 switch(Node->getOpcode())
1574 {
1575 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001576 static const uint64_t mask[6] = {
1577 0x5555555555555555ULL, 0x3333333333333333ULL,
1578 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1579 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1580 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001581 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001582 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1583 unsigned len = getSizeInBits(VT);
1584 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001585 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001586 Tmp2 = DAG.getConstant(mask[i], VT);
1587 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001588 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001589 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1590 DAG.getNode(ISD::AND, VT,
1591 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1592 Tmp2));
1593 }
1594 Result = Tmp1;
1595 break;
1596 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001597 case ISD::CTLZ: {
1598 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001599 x = x | (x >> 1);
1600 x = x | (x >> 2);
1601 ...
1602 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001603 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001604 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001605
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001606 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1607 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001608 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1609 unsigned len = getSizeInBits(VT);
1610 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1611 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001612 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001613 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1614 }
1615 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001616 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001617 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001618 }
1619 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001620 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001621 // unless the target has ctlz but not ctpop, in which case we use:
1622 // { return 32 - nlz(~x & (x-1)); }
1623 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001624 MVT::ValueType VT = Tmp1.getValueType();
1625 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001626 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001627 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1628 DAG.getNode(ISD::SUB, VT, Tmp1,
1629 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001630 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1631 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1632 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001633 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001634 DAG.getConstant(getSizeInBits(VT), VT),
1635 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1636 } else {
1637 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1638 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001639 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001640 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001641 default:
1642 assert(0 && "Cannot expand this yet!");
1643 break;
1644 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001645 break;
1646 }
1647 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001648
Chris Lattner2c8086f2005-04-02 05:00:07 +00001649 // Unary operators
1650 case ISD::FABS:
1651 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001652 case ISD::FSQRT:
1653 case ISD::FSIN:
1654 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001655 Tmp1 = LegalizeOp(Node->getOperand(0));
1656 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1657 case TargetLowering::Legal:
1658 if (Tmp1 != Node->getOperand(0))
1659 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1660 break;
1661 case TargetLowering::Promote:
1662 case TargetLowering::Custom:
1663 assert(0 && "Cannot promote/custom handle this yet!");
1664 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001665 switch(Node->getOpcode()) {
1666 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001667 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1668 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1669 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1670 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001671 break;
1672 }
1673 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001674 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1675 MVT::ValueType VT = Node->getValueType(0);
1676 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001677 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001678 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1679 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1680 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001681 break;
1682 }
1683 case ISD::FSQRT:
1684 case ISD::FSIN:
1685 case ISD::FCOS: {
1686 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001687 const char *FnName = 0;
1688 switch(Node->getOpcode()) {
1689 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1690 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1691 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1692 default: assert(0 && "Unreachable!");
1693 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001694 SDOperand Dummy;
1695 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001696 break;
1697 }
1698 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001699 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001700 }
1701 break;
1702 }
1703 break;
1704
1705 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001706 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001707 case ISD::UINT_TO_FP: {
1708 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001709 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1710 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001711 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001712 Node->getOperand(0).getValueType())) {
1713 default: assert(0 && "Unknown operation action!");
1714 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001715 Result = ExpandLegalINT_TO_FP(isSigned,
1716 LegalizeOp(Node->getOperand(0)),
1717 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001718 AddLegalizedOperand(Op, Result);
1719 return Result;
1720 case TargetLowering::Promote:
1721 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1722 Node->getValueType(0),
1723 isSigned);
1724 AddLegalizedOperand(Op, Result);
1725 return Result;
1726 case TargetLowering::Legal:
1727 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001728 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001729
Chris Lattner3e928bb2005-01-07 07:47:09 +00001730 Tmp1 = LegalizeOp(Node->getOperand(0));
1731 if (Tmp1 != Node->getOperand(0))
1732 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1733 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001734 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001735 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1736 Node->getValueType(0), Node->getOperand(0));
1737 break;
1738 case Promote:
1739 if (isSigned) {
1740 Result = PromoteOp(Node->getOperand(0));
1741 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1742 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1743 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1744 } else {
1745 Result = PromoteOp(Node->getOperand(0));
1746 Result = DAG.getZeroExtendInReg(Result,
1747 Node->getOperand(0).getValueType());
1748 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001749 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001750 break;
1751 }
1752 break;
1753 }
1754 case ISD::TRUNCATE:
1755 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1756 case Legal:
1757 Tmp1 = LegalizeOp(Node->getOperand(0));
1758 if (Tmp1 != Node->getOperand(0))
1759 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1760 break;
1761 case Expand:
1762 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1763
1764 // Since the result is legal, we should just be able to truncate the low
1765 // part of the source.
1766 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1767 break;
1768 case Promote:
1769 Result = PromoteOp(Node->getOperand(0));
1770 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1771 break;
1772 }
1773 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001774
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001775 case ISD::FP_TO_SINT:
1776 case ISD::FP_TO_UINT:
1777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1778 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001779 Tmp1 = LegalizeOp(Node->getOperand(0));
1780
Chris Lattner1618beb2005-07-29 00:11:56 +00001781 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1782 default: assert(0 && "Unknown operation action!");
1783 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001784 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1785 SDOperand True, False;
1786 MVT::ValueType VT = Node->getOperand(0).getValueType();
1787 MVT::ValueType NVT = Node->getValueType(0);
1788 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1789 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1790 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1791 Node->getOperand(0), Tmp2, ISD::SETLT);
1792 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1793 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1794 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1795 Tmp2));
1796 False = DAG.getNode(ISD::XOR, NVT, False,
1797 DAG.getConstant(1ULL << ShiftAmt, NVT));
1798 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001799 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001800 } else {
1801 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1802 }
1803 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001804 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001805 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001806 Node->getOpcode() == ISD::FP_TO_SINT);
1807 AddLegalizedOperand(Op, Result);
1808 return Result;
1809 case TargetLowering::Legal:
1810 break;
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001811 case TargetLowering::Custom:
1812 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1813 Result = TLI.LowerOperation(Result, DAG);
1814 AddLegalizedOperand(Op, Result);
1815 NeedsAnotherIteration = true;
1816 return Result;
Chris Lattner1618beb2005-07-29 00:11:56 +00001817 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001818
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001819 if (Tmp1 != Node->getOperand(0))
1820 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1821 break;
1822 case Expand:
1823 assert(0 && "Shouldn't need to expand other operators here!");
1824 case Promote:
1825 Result = PromoteOp(Node->getOperand(0));
1826 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1827 break;
1828 }
1829 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001830
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001831 case ISD::ZERO_EXTEND:
1832 case ISD::SIGN_EXTEND:
1833 case ISD::FP_EXTEND:
1834 case ISD::FP_ROUND:
1835 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1836 case Legal:
1837 Tmp1 = LegalizeOp(Node->getOperand(0));
1838 if (Tmp1 != Node->getOperand(0))
1839 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1840 break;
1841 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001842 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001843
Chris Lattner03c85462005-01-15 05:21:40 +00001844 case Promote:
1845 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001846 case ISD::ZERO_EXTEND:
1847 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001848 // NOTE: Any extend would work here...
1849 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001850 Result = DAG.getZeroExtendInReg(Result,
1851 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001852 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001853 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001854 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001855 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001856 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001857 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001858 Result,
1859 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001860 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001861 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001862 Result = PromoteOp(Node->getOperand(0));
1863 if (Result.getValueType() != Op.getValueType())
1864 // Dynamically dead while we have only 2 FP types.
1865 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1866 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001867 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001868 Result = PromoteOp(Node->getOperand(0));
1869 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1870 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001871 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001872 }
1873 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001874 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001875 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001876 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001877 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001878
1879 // If this operation is not supported, convert it to a shl/shr or load/store
1880 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001881 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1882 default: assert(0 && "This action not supported for this op yet!");
1883 case TargetLowering::Legal:
1884 if (Tmp1 != Node->getOperand(0))
1885 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001886 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001887 break;
1888 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001889 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001890 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001891 // NOTE: we could fall back on load/store here too for targets without
1892 // SAR. However, it is doubtful that any exist.
1893 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1894 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001895 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001896 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1897 Node->getOperand(0), ShiftCst);
1898 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1899 Result, ShiftCst);
1900 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1901 // The only way we can lower this is to turn it into a STORETRUNC,
1902 // EXTLOAD pair, targetting a temporary location (a stack slot).
1903
1904 // NOTE: there is a choice here between constantly creating new stack
1905 // slots and always reusing the same one. We currently always create
1906 // new ones, as reuse may inhibit scheduling.
1907 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1908 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1909 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1910 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001911 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001912 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1913 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1914 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001915 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001916 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00001917 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1918 Result, StackSlot, DAG.getSrcValue(NULL),
1919 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001920 } else {
1921 assert(0 && "Unknown op");
1922 }
1923 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001924 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001925 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001926 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001927 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001928 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001929
Chris Lattner45982da2005-05-12 16:53:42 +00001930 // Note that LegalizeOp may be reentered even from single-use nodes, which
1931 // means that we always must cache transformed nodes.
1932 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001933 return Result;
1934}
1935
Chris Lattner8b6fa222005-01-15 22:16:26 +00001936/// PromoteOp - Given an operation that produces a value in an invalid type,
1937/// promote it to compute the value into a larger type. The produced value will
1938/// have the correct bits for the low portion of the register, but no guarantee
1939/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001940SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1941 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001942 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001943 assert(getTypeAction(VT) == Promote &&
1944 "Caller should expand or legalize operands that are not promotable!");
1945 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1946 "Cannot promote to smaller type!");
1947
Chris Lattner03c85462005-01-15 05:21:40 +00001948 SDOperand Tmp1, Tmp2, Tmp3;
1949
1950 SDOperand Result;
1951 SDNode *Node = Op.Val;
1952
Chris Lattner45982da2005-05-12 16:53:42 +00001953 if (!Node->hasOneUse()) {
1954 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1955 if (I != PromotedNodes.end()) return I->second;
1956 } else {
1957 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1958 }
1959
Chris Lattner0f69b292005-01-15 06:18:18 +00001960 // Promotion needs an optimization step to clean up after it, and is not
1961 // careful to avoid operations the target does not support. Make sure that
1962 // all generated operations are legalized in the next iteration.
1963 NeedsAnotherIteration = true;
1964
Chris Lattner03c85462005-01-15 05:21:40 +00001965 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001966 case ISD::CopyFromReg:
1967 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00001968 default:
1969 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1970 assert(0 && "Do not know how to promote this operator!");
1971 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001972 case ISD::UNDEF:
1973 Result = DAG.getNode(ISD::UNDEF, NVT);
1974 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001975 case ISD::Constant:
1976 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1977 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1978 break;
1979 case ISD::ConstantFP:
1980 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1981 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1982 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001983
Chris Lattner82fbfb62005-01-18 02:59:52 +00001984 case ISD::SETCC:
1985 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1986 "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001987 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
1988 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00001989 Result = LegalizeOp(Result);
1990 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001991
1992 case ISD::TRUNCATE:
1993 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1994 case Legal:
1995 Result = LegalizeOp(Node->getOperand(0));
1996 assert(Result.getValueType() >= NVT &&
1997 "This truncation doesn't make sense!");
1998 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1999 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2000 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002001 case Promote:
2002 // The truncation is not required, because we don't guarantee anything
2003 // about high bits anyway.
2004 Result = PromoteOp(Node->getOperand(0));
2005 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002006 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002007 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2008 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002009 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002010 }
2011 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002012 case ISD::SIGN_EXTEND:
2013 case ISD::ZERO_EXTEND:
2014 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2015 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2016 case Legal:
2017 // Input is legal? Just do extend all the way to the larger type.
2018 Result = LegalizeOp(Node->getOperand(0));
2019 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2020 break;
2021 case Promote:
2022 // Promote the reg if it's smaller.
2023 Result = PromoteOp(Node->getOperand(0));
2024 // The high bits are not guaranteed to be anything. Insert an extend.
2025 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002026 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002027 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002028 else
Chris Lattner23993562005-04-13 02:38:47 +00002029 Result = DAG.getZeroExtendInReg(Result,
2030 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002031 break;
2032 }
2033 break;
2034
2035 case ISD::FP_EXTEND:
2036 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2037 case ISD::FP_ROUND:
2038 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2039 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2040 case Promote: assert(0 && "Unreachable with 2 FP types!");
2041 case Legal:
2042 // Input is legal? Do an FP_ROUND_INREG.
2043 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002044 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2045 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002046 break;
2047 }
2048 break;
2049
2050 case ISD::SINT_TO_FP:
2051 case ISD::UINT_TO_FP:
2052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2053 case Legal:
2054 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002055 // No extra round required here.
2056 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002057 break;
2058
2059 case Promote:
2060 Result = PromoteOp(Node->getOperand(0));
2061 if (Node->getOpcode() == ISD::SINT_TO_FP)
2062 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002063 Result,
2064 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002065 else
Chris Lattner23993562005-04-13 02:38:47 +00002066 Result = DAG.getZeroExtendInReg(Result,
2067 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002068 // No extra round required here.
2069 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002070 break;
2071 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002072 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2073 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002074 // Round if we cannot tolerate excess precision.
2075 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002076 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2077 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002078 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002079 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002080 break;
2081
2082 case ISD::FP_TO_SINT:
2083 case ISD::FP_TO_UINT:
2084 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2085 case Legal:
2086 Tmp1 = LegalizeOp(Node->getOperand(0));
2087 break;
2088 case Promote:
2089 // The input result is prerounded, so we don't have to do anything
2090 // special.
2091 Tmp1 = PromoteOp(Node->getOperand(0));
2092 break;
2093 case Expand:
2094 assert(0 && "not implemented");
2095 }
Nate Begemand2558e32005-08-14 01:20:53 +00002096 // If we're promoting a UINT to a larger size, check to see if the new node
2097 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2098 // we can use that instead. This allows us to generate better code for
2099 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2100 // legal, such as PowerPC.
2101 if (Node->getOpcode() == ISD::FP_TO_UINT &&
2102 TargetLowering::Legal != TLI.getOperationAction(ISD::FP_TO_UINT, NVT) &&
2103 TargetLowering::Legal == TLI.getOperationAction(ISD::FP_TO_SINT, NVT)) {
2104 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2105 } else {
2106 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2107 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002108 break;
2109
Chris Lattner2c8086f2005-04-02 05:00:07 +00002110 case ISD::FABS:
2111 case ISD::FNEG:
2112 Tmp1 = PromoteOp(Node->getOperand(0));
2113 assert(Tmp1.getValueType() == NVT);
2114 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2115 // NOTE: we do not have to do any extra rounding here for
2116 // NoExcessFPPrecision, because we know the input will have the appropriate
2117 // precision, and these operations don't modify precision at all.
2118 break;
2119
Chris Lattnerda6ba872005-04-28 21:44:33 +00002120 case ISD::FSQRT:
2121 case ISD::FSIN:
2122 case ISD::FCOS:
2123 Tmp1 = PromoteOp(Node->getOperand(0));
2124 assert(Tmp1.getValueType() == NVT);
2125 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2126 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002127 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2128 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002129 break;
2130
Chris Lattner03c85462005-01-15 05:21:40 +00002131 case ISD::AND:
2132 case ISD::OR:
2133 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002134 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002135 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002136 case ISD::MUL:
2137 // The input may have strange things in the top bits of the registers, but
2138 // these operations don't care. They may have wierd bits going out, but
2139 // that too is okay if they are integer operations.
2140 Tmp1 = PromoteOp(Node->getOperand(0));
2141 Tmp2 = PromoteOp(Node->getOperand(1));
2142 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2143 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2144
2145 // However, if this is a floating point operation, they will give excess
2146 // precision that we may not be able to tolerate. If we DO allow excess
2147 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002148 // FIXME: Why would we need to round FP ops more than integer ones?
2149 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00002150 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002151 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2152 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002153 break;
2154
Chris Lattner8b6fa222005-01-15 22:16:26 +00002155 case ISD::SDIV:
2156 case ISD::SREM:
2157 // These operators require that their input be sign extended.
2158 Tmp1 = PromoteOp(Node->getOperand(0));
2159 Tmp2 = PromoteOp(Node->getOperand(1));
2160 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002161 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2162 DAG.getValueType(VT));
2163 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2164 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002165 }
2166 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2167
2168 // Perform FP_ROUND: this is probably overly pessimistic.
2169 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002170 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2171 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002172 break;
2173
2174 case ISD::UDIV:
2175 case ISD::UREM:
2176 // These operators require that their input be zero extended.
2177 Tmp1 = PromoteOp(Node->getOperand(0));
2178 Tmp2 = PromoteOp(Node->getOperand(1));
2179 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002180 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2181 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002182 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2183 break;
2184
2185 case ISD::SHL:
2186 Tmp1 = PromoteOp(Node->getOperand(0));
2187 Tmp2 = LegalizeOp(Node->getOperand(1));
2188 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2189 break;
2190 case ISD::SRA:
2191 // The input value must be properly sign extended.
2192 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002193 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2194 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002195 Tmp2 = LegalizeOp(Node->getOperand(1));
2196 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2197 break;
2198 case ISD::SRL:
2199 // The input value must be properly zero extended.
2200 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002201 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002202 Tmp2 = LegalizeOp(Node->getOperand(1));
2203 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2204 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002205 case ISD::LOAD:
2206 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2207 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002208 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002209 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002210 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2211 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002212 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002213 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2214 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002215
2216 // Remember that we legalized the chain.
2217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2218 break;
2219 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002220 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2221 case Expand: assert(0 && "It's impossible to expand bools");
2222 case Legal:
2223 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2224 break;
2225 case Promote:
2226 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2227 break;
2228 }
Chris Lattner03c85462005-01-15 05:21:40 +00002229 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2230 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2231 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2232 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002233 case ISD::SELECT_CC:
2234 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2235 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2236 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2237 Node->getOperand(1), Tmp2, Tmp3,
2238 Node->getOperand(4));
2239 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002240 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002241 case ISD::CALL: {
2242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2243 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2244
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002245 std::vector<SDOperand> Ops;
2246 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2247 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2248
Chris Lattner8ac532c2005-01-16 19:46:48 +00002249 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2250 "Can only promote single result calls");
2251 std::vector<MVT::ValueType> RetTyVTs;
2252 RetTyVTs.reserve(2);
2253 RetTyVTs.push_back(NVT);
2254 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002255 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2256 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002257 Result = SDOperand(NC, 0);
2258
2259 // Insert the new chain mapping.
2260 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2261 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002262 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002263 case ISD::CTPOP:
2264 case ISD::CTTZ:
2265 case ISD::CTLZ:
2266 Tmp1 = Node->getOperand(0);
2267 //Zero extend the argument
2268 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2269 // Perform the larger operation, then subtract if needed.
2270 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2271 switch(Node->getOpcode())
2272 {
2273 case ISD::CTPOP:
2274 Result = Tmp1;
2275 break;
2276 case ISD::CTTZ:
2277 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002278 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002279 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002280 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002281 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2282 break;
2283 case ISD::CTLZ:
2284 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002285 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2286 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002287 getSizeInBits(VT), NVT));
2288 break;
2289 }
2290 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002291 }
2292
2293 assert(Result.Val && "Didn't set a result!");
2294 AddPromotedOperand(Op, Result);
2295 return Result;
2296}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002297
Chris Lattner84f67882005-01-20 18:52:28 +00002298/// ExpandAddSub - Find a clever way to expand this add operation into
2299/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002300void SelectionDAGLegalize::
2301ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2302 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002303 // Expand the subcomponents.
2304 SDOperand LHSL, LHSH, RHSL, RHSH;
2305 ExpandOp(LHS, LHSL, LHSH);
2306 ExpandOp(RHS, RHSL, RHSH);
2307
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002308 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002309 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2310 if (LHSL.getValueType() == MVT::i32) {
2311 SDOperand LowEl;
2312 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2313 if (C->getValue() == 0)
2314 LowEl = RHSL;
2315 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2316 if (C->getValue() == 0)
2317 LowEl = LHSL;
2318 if (LowEl.Val) {
2319 // Turn this into an add/sub of the high part only.
2320 SDOperand HiEl =
2321 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2322 LowEl.getValueType(), LHSH, RHSH);
2323 Lo = LowEl;
2324 Hi = HiEl;
2325 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002326 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002327 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002328
Chris Lattner84f67882005-01-20 18:52:28 +00002329 std::vector<SDOperand> Ops;
2330 Ops.push_back(LHSL);
2331 Ops.push_back(LHSH);
2332 Ops.push_back(RHSL);
2333 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002334
2335 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2336 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002337 Hi = Lo.getValue(1);
2338}
2339
Chris Lattner5b359c62005-04-02 04:00:59 +00002340void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2341 SDOperand Op, SDOperand Amt,
2342 SDOperand &Lo, SDOperand &Hi) {
2343 // Expand the subcomponents.
2344 SDOperand LHSL, LHSH;
2345 ExpandOp(Op, LHSL, LHSH);
2346
2347 std::vector<SDOperand> Ops;
2348 Ops.push_back(LHSL);
2349 Ops.push_back(LHSH);
2350 Ops.push_back(Amt);
Chris Lattnere89083a2005-05-14 07:25:05 +00002351 std::vector<MVT::ValueType> VTs;
2352 VTs.push_back(LHSL.getValueType());
2353 VTs.push_back(LHSH.getValueType());
2354 VTs.push_back(Amt.getValueType());
2355 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002356 Hi = Lo.getValue(1);
2357}
2358
2359
Chris Lattnere34b3962005-01-19 04:19:40 +00002360/// ExpandShift - Try to find a clever way to expand this shift operation out to
2361/// smaller elements. If we can't find a way that is more efficient than a
2362/// libcall on this target, return false. Otherwise, return true with the
2363/// low-parts expanded into Lo and Hi.
2364bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2365 SDOperand &Lo, SDOperand &Hi) {
2366 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2367 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002368
Chris Lattnere34b3962005-01-19 04:19:40 +00002369 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002370 SDOperand ShAmt = LegalizeOp(Amt);
2371 MVT::ValueType ShTy = ShAmt.getValueType();
2372 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2373 unsigned NVTBits = MVT::getSizeInBits(NVT);
2374
2375 // Handle the case when Amt is an immediate. Other cases are currently broken
2376 // and are disabled.
2377 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2378 unsigned Cst = CN->getValue();
2379 // Expand the incoming operand to be shifted, so that we have its parts
2380 SDOperand InL, InH;
2381 ExpandOp(Op, InL, InH);
2382 switch(Opc) {
2383 case ISD::SHL:
2384 if (Cst > VTBits) {
2385 Lo = DAG.getConstant(0, NVT);
2386 Hi = DAG.getConstant(0, NVT);
2387 } else if (Cst > NVTBits) {
2388 Lo = DAG.getConstant(0, NVT);
2389 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002390 } else if (Cst == NVTBits) {
2391 Lo = DAG.getConstant(0, NVT);
2392 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002393 } else {
2394 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2395 Hi = DAG.getNode(ISD::OR, NVT,
2396 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2397 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2398 }
2399 return true;
2400 case ISD::SRL:
2401 if (Cst > VTBits) {
2402 Lo = DAG.getConstant(0, NVT);
2403 Hi = DAG.getConstant(0, NVT);
2404 } else if (Cst > NVTBits) {
2405 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2406 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002407 } else if (Cst == NVTBits) {
2408 Lo = InH;
2409 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002410 } else {
2411 Lo = DAG.getNode(ISD::OR, NVT,
2412 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2413 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2414 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2415 }
2416 return true;
2417 case ISD::SRA:
2418 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002419 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002420 DAG.getConstant(NVTBits-1, ShTy));
2421 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002422 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002423 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002424 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002425 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002426 } else if (Cst == NVTBits) {
2427 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002428 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002429 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002430 } else {
2431 Lo = DAG.getNode(ISD::OR, NVT,
2432 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2433 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2434 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2435 }
2436 return true;
2437 }
2438 }
2439 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2440 // so disable it for now. Currently targets are handling this via SHL_PARTS
2441 // and friends.
2442 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002443
2444 // If we have an efficient select operation (or if the selects will all fold
2445 // away), lower to some complex code, otherwise just emit the libcall.
2446 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2447 !isa<ConstantSDNode>(Amt))
2448 return false;
2449
2450 SDOperand InL, InH;
2451 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002452 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2453 DAG.getConstant(NVTBits, ShTy), ShAmt);
2454
Chris Lattnere5544f82005-01-20 20:29:23 +00002455 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002456 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2457 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002458
Chris Lattnere34b3962005-01-19 04:19:40 +00002459 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2460 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2461 DAG.getConstant(NVTBits-1, ShTy));
2462 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2463 DAG.getConstant(NVTBits-1, ShTy));
2464 }
2465
2466 if (Opc == ISD::SHL) {
2467 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2468 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2469 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002470 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002471
Chris Lattnere34b3962005-01-19 04:19:40 +00002472 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2473 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2474 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002475 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002476 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2477 DAG.getConstant(32, ShTy),
2478 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002479 DAG.getConstant(0, NVT),
2480 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002481 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002482 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002483 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002484 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002485
2486 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002487 if (Opc == ISD::SRA)
2488 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2489 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002490 else
2491 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002492 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002493 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002494 }
2495 return true;
2496}
Chris Lattner77e77a62005-01-21 06:05:23 +00002497
Chris Lattner9530ddc2005-05-13 05:09:11 +00002498/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2499/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002500/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002501static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002502 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002503
Chris Lattner16cd04d2005-05-12 23:24:06 +00002504 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002505 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002506 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002507 Found = Node;
2508 return;
2509 }
2510
2511 // Otherwise, scan the operands of Node to see if any of them is a call.
2512 assert(Node->getNumOperands() != 0 &&
2513 "All leaves should have depth equal to the entry node!");
2514 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002515 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002516
2517 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002518 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002519 Found);
2520}
2521
2522
Chris Lattner9530ddc2005-05-13 05:09:11 +00002523/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2524/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002525/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002526static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2527 std::set<SDNode*> &Visited) {
2528 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2529 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002530
Chris Lattner16cd04d2005-05-12 23:24:06 +00002531 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002532 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002533 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002534 Found = Node;
2535 return;
2536 }
2537
2538 // Otherwise, scan the operands of Node to see if any of them is a call.
2539 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2540 if (UI == E) return;
2541 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002542 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002543
2544 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002545 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002546}
2547
Chris Lattner9530ddc2005-05-13 05:09:11 +00002548/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002549/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002550static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002551 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002552 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002553 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002554 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002555
2556 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002557 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002558
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002559 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002560 if (TheChain.getValueType() != MVT::Other)
2561 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002562 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002563
2564 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002565 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002566
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002567 // Make sure to only follow users of our token chain.
2568 SDNode *User = *UI;
2569 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2570 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002571 if (SDNode *Result = FindCallSeqEnd(User))
2572 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002573 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002574 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002575}
2576
Chris Lattner9530ddc2005-05-13 05:09:11 +00002577/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002578/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002579static SDNode *FindCallSeqStart(SDNode *Node) {
2580 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002581 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002582
2583 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2584 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002585 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002586}
2587
2588
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002589/// FindInputOutputChains - If we are replacing an operation with a call we need
2590/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002591/// properly serialize the calls in the block. The returned operand is the
2592/// input chain value for the new call (e.g. the entry node or the previous
2593/// call), and OutChain is set to be the chain node to update to point to the
2594/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002595static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2596 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002597 SDNode *LatestCallSeqStart = Entry.Val;
2598 SDNode *LatestCallSeqEnd = 0;
2599 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2600 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002601
Chris Lattner16cd04d2005-05-12 23:24:06 +00002602 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002603 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002604 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2605 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002606 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2607 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002608 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002609 LatestCallSeqEnd = Entry.Val;
2610 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002611
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002612 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002613 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002614 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002615 std::set<SDNode*> Visited;
2616 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002617
Chris Lattner9530ddc2005-05-13 05:09:11 +00002618 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002619 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002620 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002621
Chris Lattner9530ddc2005-05-13 05:09:11 +00002622 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002623}
2624
Jeff Cohen00b168892005-07-27 06:12:32 +00002625/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002626void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2627 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002628 // Nothing to splice it into?
2629 if (OutChain == 0) return;
2630
2631 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2632 //OutChain->dump();
2633
2634 // Form a token factor node merging the old inval and the new inval.
2635 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2636 OutChain->getOperand(0));
2637 // Change the node to refer to the new token.
2638 OutChain->setAdjCallChain(InToken);
2639}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002640
2641
Chris Lattner77e77a62005-01-21 06:05:23 +00002642// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2643// does not fit into a register, return the lo part and set the hi part to the
2644// by-reg argument. If it does fit into a single register, return the result
2645// and leave the Hi part unset.
2646SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2647 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002648 SDNode *OutChain;
2649 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2650 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002651 if (InChain.Val == 0)
2652 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002653
Chris Lattner77e77a62005-01-21 06:05:23 +00002654 TargetLowering::ArgListTy Args;
2655 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2656 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2657 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2658 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2659 }
2660 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002661
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002662 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002663 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002664 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002665 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2666 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002667 SpliceCallInto(CallInfo.second, OutChain);
2668
2669 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002670
2671 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002672 default: assert(0 && "Unknown thing");
2673 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002674 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002675 case Promote:
2676 assert(0 && "Cannot promote this yet!");
2677 case Expand:
2678 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002679 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002680 return Lo;
2681 }
2682}
2683
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002684
Chris Lattner77e77a62005-01-21 06:05:23 +00002685/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2686/// destination type is legal.
2687SDOperand SelectionDAGLegalize::
2688ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2689 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2690 assert(getTypeAction(Source.getValueType()) == Expand &&
2691 "This is not an expansion!");
2692 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2693
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002694 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002695 assert(Source.getValueType() == MVT::i64 &&
2696 "This only works for 64-bit -> FP");
2697 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2698 // incoming integer is set. To handle this, we dynamically test to see if
2699 // it is set, and, if so, add a fudge factor.
2700 SDOperand Lo, Hi;
2701 ExpandOp(Source, Lo, Hi);
2702
Chris Lattner66de05b2005-05-13 04:45:13 +00002703 // If this is unsigned, and not supported, first perform the conversion to
2704 // signed, then adjust the result if the sign bit is set.
2705 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2706 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2707
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002708 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2709 DAG.getConstant(0, Hi.getValueType()),
2710 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002711 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2712 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2713 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002714 uint64_t FF = 0x5f800000ULL;
2715 if (TLI.isLittleEndian()) FF <<= 32;
2716 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002717
2718 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2719 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2720 TLI.getPointerTy());
2721 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2722 SDOperand FudgeInReg;
2723 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002724 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2725 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002726 else {
2727 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002728 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2729 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002730 }
2731 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002732 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002733
Chris Lattnera88a2602005-05-14 05:33:54 +00002734 // Check to see if the target has a custom way to lower this. If so, use it.
2735 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2736 default: assert(0 && "This action not implemented for this operation!");
2737 case TargetLowering::Legal:
2738 case TargetLowering::Expand:
2739 break; // This case is handled below.
2740 case TargetLowering::Custom:
2741 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner50381b62005-05-14 05:50:48 +00002742 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnera88a2602005-05-14 05:33:54 +00002743 }
2744
Chris Lattner13689e22005-05-12 07:00:44 +00002745 // Expand the source, then glue it back together for the call. We must expand
2746 // the source in case it is shared (this pass of legalize must traverse it).
2747 SDOperand SrcLo, SrcHi;
2748 ExpandOp(Source, SrcLo, SrcHi);
2749 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2750
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002751 SDNode *OutChain = 0;
2752 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2753 DAG.getEntryNode());
2754 const char *FnName = 0;
2755 if (DestTy == MVT::f32)
2756 FnName = "__floatdisf";
2757 else {
2758 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2759 FnName = "__floatdidf";
2760 }
2761
Chris Lattner77e77a62005-01-21 06:05:23 +00002762 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2763
2764 TargetLowering::ArgListTy Args;
2765 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002766
Chris Lattner77e77a62005-01-21 06:05:23 +00002767 Args.push_back(std::make_pair(Source, ArgTy));
2768
2769 // We don't care about token chains for libcalls. We just use the entry
2770 // node as our input and ignore the output chain. This allows us to place
2771 // calls wherever we need them to satisfy data dependences.
2772 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002773
2774 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002775 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2776 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002777
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002778 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002779 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002780}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002781
Chris Lattnere34b3962005-01-19 04:19:40 +00002782
2783
Chris Lattner3e928bb2005-01-07 07:47:09 +00002784/// ExpandOp - Expand the specified SDOperand into its two component pieces
2785/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2786/// LegalizeNodes map is filled in for any results that are not expanded, the
2787/// ExpandedNodes map is filled in for any results that are expanded, and the
2788/// Lo/Hi values are returned.
2789void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2790 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002791 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002792 SDNode *Node = Op.Val;
2793 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2794 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2795 assert(MVT::isInteger(NVT) && NVT < VT &&
2796 "Cannot expand to FP value or to larger int value!");
2797
2798 // If there is more than one use of this, see if we already expanded it.
2799 // There is no use remembering values that only have a single use, as the map
2800 // entries will never be reused.
2801 if (!Node->hasOneUse()) {
2802 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2803 = ExpandedNodes.find(Op);
2804 if (I != ExpandedNodes.end()) {
2805 Lo = I->second.first;
2806 Hi = I->second.second;
2807 return;
2808 }
Chris Lattner45982da2005-05-12 16:53:42 +00002809 } else {
2810 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002811 }
2812
Chris Lattner4e6c7462005-01-08 19:27:05 +00002813 // Expanding to multiple registers needs to perform an optimization step, and
2814 // is not careful to avoid operations the target does not support. Make sure
2815 // that all generated operations are legalized in the next iteration.
2816 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002817
Chris Lattner3e928bb2005-01-07 07:47:09 +00002818 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002819 case ISD::CopyFromReg:
2820 assert(0 && "CopyFromReg must be legal!");
2821 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002822 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2823 assert(0 && "Do not know how to expand this operator!");
2824 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002825 case ISD::UNDEF:
2826 Lo = DAG.getNode(ISD::UNDEF, NVT);
2827 Hi = DAG.getNode(ISD::UNDEF, NVT);
2828 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002829 case ISD::Constant: {
2830 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2831 Lo = DAG.getConstant(Cst, NVT);
2832 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2833 break;
2834 }
2835
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002836 case ISD::BUILD_PAIR:
2837 // Legalize both operands. FIXME: in the future we should handle the case
2838 // where the two elements are not legal.
2839 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2840 Lo = LegalizeOp(Node->getOperand(0));
2841 Hi = LegalizeOp(Node->getOperand(1));
2842 break;
2843
Chris Lattneredb1add2005-05-11 04:51:16 +00002844 case ISD::CTPOP:
2845 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002846 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2847 DAG.getNode(ISD::CTPOP, NVT, Lo),
2848 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002849 Hi = DAG.getConstant(0, NVT);
2850 break;
2851
Chris Lattner39a8f332005-05-12 19:05:01 +00002852 case ISD::CTLZ: {
2853 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002854 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002855 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2856 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002857 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2858 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002859 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2860 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2861
2862 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2863 Hi = DAG.getConstant(0, NVT);
2864 break;
2865 }
2866
2867 case ISD::CTTZ: {
2868 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002869 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002870 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2871 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002872 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2873 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002874 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2875 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2876
2877 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2878 Hi = DAG.getConstant(0, NVT);
2879 break;
2880 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002881
Chris Lattner3e928bb2005-01-07 07:47:09 +00002882 case ISD::LOAD: {
2883 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2884 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002885 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002886
2887 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002888 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002889 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2890 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00002891 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002892 //are from the same instruction?
2893 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002894
2895 // Build a factor node to remember that this load is independent of the
2896 // other one.
2897 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2898 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002899
Chris Lattner3e928bb2005-01-07 07:47:09 +00002900 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002901 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002902 if (!TLI.isLittleEndian())
2903 std::swap(Lo, Hi);
2904 break;
2905 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002906 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002907 case ISD::CALL: {
2908 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2909 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2910
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002911 bool Changed = false;
2912 std::vector<SDOperand> Ops;
2913 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2914 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2915 Changed |= Ops.back() != Node->getOperand(i);
2916 }
2917
Chris Lattner3e928bb2005-01-07 07:47:09 +00002918 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2919 "Can only expand a call once so far, not i64 -> i16!");
2920
2921 std::vector<MVT::ValueType> RetTyVTs;
2922 RetTyVTs.reserve(3);
2923 RetTyVTs.push_back(NVT);
2924 RetTyVTs.push_back(NVT);
2925 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002926 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2927 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002928 Lo = SDOperand(NC, 0);
2929 Hi = SDOperand(NC, 1);
2930
2931 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002932 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002933 break;
2934 }
2935 case ISD::AND:
2936 case ISD::OR:
2937 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2938 SDOperand LL, LH, RL, RH;
2939 ExpandOp(Node->getOperand(0), LL, LH);
2940 ExpandOp(Node->getOperand(1), RL, RH);
2941 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2942 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2943 break;
2944 }
2945 case ISD::SELECT: {
2946 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002947
2948 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2949 case Expand: assert(0 && "It's impossible to expand bools");
2950 case Legal:
2951 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2952 break;
2953 case Promote:
2954 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2955 break;
2956 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002957 ExpandOp(Node->getOperand(1), LL, LH);
2958 ExpandOp(Node->getOperand(2), RL, RH);
2959 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2960 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2961 break;
2962 }
Nate Begeman9373a812005-08-10 20:51:12 +00002963 case ISD::SELECT_CC: {
2964 SDOperand TL, TH, FL, FH;
2965 ExpandOp(Node->getOperand(2), TL, TH);
2966 ExpandOp(Node->getOperand(3), FL, FH);
2967 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2968 Node->getOperand(1), TL, FL, Node->getOperand(4));
2969 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2970 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00002971 Lo = LegalizeOp(Lo);
2972 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00002973 break;
2974 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002975 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002976 SDOperand In;
2977 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2978 case Expand: assert(0 && "expand-expand not implemented yet!");
2979 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2980 case Promote:
2981 In = PromoteOp(Node->getOperand(0));
2982 // Emit the appropriate sign_extend_inreg to get the value we want.
2983 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00002984 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00002985 break;
2986 }
2987
Chris Lattner3e928bb2005-01-07 07:47:09 +00002988 // The low part is just a sign extension of the input (which degenerates to
2989 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002990 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002991
Chris Lattner3e928bb2005-01-07 07:47:09 +00002992 // The high part is obtained by SRA'ing all but one of the bits of the lo
2993 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002994 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002995 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2996 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002997 break;
2998 }
Chris Lattner06098e02005-04-03 23:41:52 +00002999 case ISD::ZERO_EXTEND: {
3000 SDOperand In;
3001 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3002 case Expand: assert(0 && "expand-expand not implemented yet!");
3003 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3004 case Promote:
3005 In = PromoteOp(Node->getOperand(0));
3006 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003007 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003008 break;
3009 }
3010
Chris Lattner3e928bb2005-01-07 07:47:09 +00003011 // The low part is just a zero extension of the input (which degenerates to
3012 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003013 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003014
Chris Lattner3e928bb2005-01-07 07:47:09 +00003015 // The high part is just a zero.
3016 Hi = DAG.getConstant(0, NVT);
3017 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003018 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003019 // These operators cannot be expanded directly, emit them as calls to
3020 // library functions.
3021 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003022 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003023 SDOperand Op;
3024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3025 case Expand: assert(0 && "cannot expand FP!");
3026 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3027 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3028 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003029
Chris Lattnerf20d1832005-07-30 01:40:57 +00003030 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3031
Chris Lattner80a3e942005-07-29 00:33:32 +00003032 // Now that the custom expander is done, expand the result, which is still
3033 // VT.
Chris Lattnerf20d1832005-07-30 01:40:57 +00003034 ExpandOp(Op, Lo, Hi);
Chris Lattner80a3e942005-07-29 00:33:32 +00003035 break;
3036 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003037
Chris Lattner4e6c7462005-01-08 19:27:05 +00003038 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003039 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003040 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003041 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003042 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003043
Chris Lattner4e6c7462005-01-08 19:27:05 +00003044 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003045 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3046 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3047 LegalizeOp(Node->getOperand(0)));
3048 // Now that the custom expander is done, expand the result, which is still
3049 // VT.
3050 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
3051 break;
3052 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003053
Chris Lattner4e6c7462005-01-08 19:27:05 +00003054 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003055 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003056 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003057 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003058 break;
3059
Chris Lattnere34b3962005-01-19 04:19:40 +00003060 case ISD::SHL:
3061 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003062 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003063 break;
Chris Lattner47599822005-04-02 03:38:53 +00003064
3065 // If this target supports SHL_PARTS, use it.
3066 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003067 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3068 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003069 break;
3070 }
3071
Chris Lattnere34b3962005-01-19 04:19:40 +00003072 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003073 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003074 break;
3075
3076 case ISD::SRA:
3077 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003078 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003079 break;
Chris Lattner47599822005-04-02 03:38:53 +00003080
3081 // If this target supports SRA_PARTS, use it.
3082 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003083 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3084 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003085 break;
3086 }
3087
Chris Lattnere34b3962005-01-19 04:19:40 +00003088 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003089 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003090 break;
3091 case ISD::SRL:
3092 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003093 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003094 break;
Chris Lattner47599822005-04-02 03:38:53 +00003095
3096 // If this target supports SRL_PARTS, use it.
3097 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003098 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3099 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003100 break;
3101 }
3102
Chris Lattnere34b3962005-01-19 04:19:40 +00003103 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003104 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003105 break;
3106
Misha Brukmanedf128a2005-04-21 22:36:52 +00003107 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003108 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3109 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003110 break;
3111 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003112 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3113 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003114 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003115 case ISD::MUL: {
3116 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
3117 SDOperand LL, LH, RL, RH;
3118 ExpandOp(Node->getOperand(0), LL, LH);
3119 ExpandOp(Node->getOperand(1), RL, RH);
3120 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3121 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3122 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3123 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3124 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3125 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3126 } else {
3127 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3128 }
3129 break;
3130 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003131 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3132 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3133 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3134 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003135 }
3136
3137 // Remember in a map if the values will be reused later.
3138 if (!Node->hasOneUse()) {
3139 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3140 std::make_pair(Lo, Hi))).second;
3141 assert(isNew && "Value already expanded?!?");
3142 }
3143}
3144
3145
3146// SelectionDAG::Legalize - This is the entry point for the file.
3147//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003148void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003149 /// run - This is the main entry point to this class.
3150 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003151 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003152}
3153