blob: d07c4df1247fb8d0186639876d3e381ffafc8c9b [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000018#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Constants.h"
24#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000025#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
30/// hacks on it until the target machine can handle it. This involves
31/// eliminating value sizes the machine cannot handle (promoting small sizes to
32/// large sizes or splitting up large values into small values) as well as
33/// eliminating operations the machine cannot handle.
34///
35/// This code also does a small amount of optimization and recognition of idioms
36/// as part of its processing. For example, if a target does not support a
37/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
38/// will attempt merge setcc and brc instructions into brcc's.
39///
40namespace {
41class SelectionDAGLegalize {
42 TargetLowering &TLI;
43 SelectionDAG &DAG;
44
45 /// LegalizeAction - This enum indicates what action we should take for each
46 /// value type the can occur in the program.
47 enum LegalizeAction {
48 Legal, // The target natively supports this value type.
49 Promote, // This should be promoted to the next larger type.
50 Expand, // This integer type should be broken into smaller pieces.
51 };
52
Chris Lattnerdc750592005-01-07 07:47:09 +000053 /// ValueTypeActions - This is a bitvector that contains two bits for each
54 /// value type, where the two bits correspond to the LegalizeAction enum.
55 /// This can be queried with "getTypeAction(VT)".
56 unsigned ValueTypeActions;
57
58 /// NeedsAnotherIteration - This is set when we expand a large integer
59 /// operation into smaller integer operations, but the smaller operations are
60 /// not set. This occurs only rarely in practice, for targets that don't have
61 /// 32-bit or larger integer registers.
62 bool NeedsAnotherIteration;
63
64 /// LegalizedNodes - For nodes that are of legal width, and that have more
65 /// than one use, this map indicates what regularized operand to use. This
66 /// allows us to avoid legalizing the same thing more than once.
67 std::map<SDOperand, SDOperand> LegalizedNodes;
68
Chris Lattner1f2c9d82005-01-15 05:21:40 +000069 /// PromotedNodes - For nodes that are below legal width, and that have more
70 /// than one use, this map indicates what promoted value to use. This allows
71 /// us to avoid promoting the same thing more than once.
72 std::map<SDOperand, SDOperand> PromotedNodes;
73
Chris Lattnerdc750592005-01-07 07:47:09 +000074 /// ExpandedNodes - For nodes that need to be expanded, and which have more
75 /// than one use, this map indicates which which operands are the expanded
76 /// version of the input. This allows us to avoid expanding the same node
77 /// more than once.
78 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
79
Chris Lattnerea4ca942005-01-07 22:28:47 +000080 void AddLegalizedOperand(SDOperand From, SDOperand To) {
81 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
82 assert(isNew && "Got into the map somehow?");
83 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000084 void AddPromotedOperand(SDOperand From, SDOperand To) {
85 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
86 assert(isNew && "Got into the map somehow?");
87 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000088
Chris Lattnerdc750592005-01-07 07:47:09 +000089public:
90
Chris Lattner4add7e32005-01-23 04:42:50 +000091 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000092
93 /// Run - While there is still lowering to do, perform a pass over the DAG.
94 /// Most regularization can be done in a single pass, but targets that require
95 /// large values to be split into registers multiple times (e.g. i64 -> 4x
96 /// i16) require iteration for these values (the first iteration will demote
97 /// to i32, the second will demote to i16).
98 void Run() {
99 do {
100 NeedsAnotherIteration = false;
101 LegalizeDAG();
102 } while (NeedsAnotherIteration);
103 }
104
105 /// getTypeAction - Return how we should legalize values of this type, either
106 /// it is already legal or we need to expand it into multiple registers of
107 /// smaller integer type, or we need to promote it to a larger type.
108 LegalizeAction getTypeAction(MVT::ValueType VT) const {
109 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
110 }
111
112 /// isTypeLegal - Return true if this type is legal on this target.
113 ///
114 bool isTypeLegal(MVT::ValueType VT) const {
115 return getTypeAction(VT) == Legal;
116 }
117
118private:
119 void LegalizeDAG();
120
121 SDOperand LegalizeOp(SDOperand O);
122 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000123 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124
Chris Lattneraac464e2005-01-21 06:05:23 +0000125 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
126 SDOperand &Hi);
127 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
128 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000129
Jim Laskeyf2516a92005-08-17 00:39:29 +0000130 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
131 SDOperand LegalOp,
132 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000133 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
134 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000135 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
136 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000137
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000138 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
139 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000140 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
141 SDOperand &Lo, SDOperand &Hi);
142 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000144
Chris Lattnera5bf1032005-05-12 04:49:08 +0000145 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
146
Chris Lattnerdc750592005-01-07 07:47:09 +0000147 SDOperand getIntPtrConstant(uint64_t Val) {
148 return DAG.getConstant(Val, TLI.getPointerTy());
149 }
150};
151}
152
153
Chris Lattner4add7e32005-01-23 04:42:50 +0000154SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
155 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
156 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000157 assert(MVT::LAST_VALUETYPE <= 16 &&
158 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000159}
160
Jim Laskeyf2516a92005-08-17 00:39:29 +0000161/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
162/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000163/// we expand it. At this point, we know that the result and operand types are
164/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000165SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
166 SDOperand Op0,
167 MVT::ValueType DestVT) {
168 if (Op0.getValueType() == MVT::i32) {
169 // simple 32-bit [signed|unsigned] integer to float/double expansion
170
171 // get the stack frame index of a 8 byte buffer
172 MachineFunction &MF = DAG.getMachineFunction();
173 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
174 // get address of 8 byte buffer
175 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
176 // word offset constant for Hi/Lo address computation
177 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
178 // set up Hi and Lo (into buffer) address based on endian
179 SDOperand Hi, Lo;
180 if (TLI.isLittleEndian()) {
181 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
182 Lo = StackSlot;
183 } else {
184 Hi = StackSlot;
185 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
186 }
187 // if signed map to unsigned space
188 SDOperand Op0Mapped;
189 if (isSigned) {
190 // constant used to invert sign bit (signed to unsigned mapping)
191 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
192 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
193 } else {
194 Op0Mapped = Op0;
195 }
196 // store the lo of the constructed double - based on integer input
197 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
198 Op0Mapped, Lo, DAG.getSrcValue(NULL));
199 // initial hi portion of constructed double
200 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
201 // store the hi of the constructed double - biased exponent
202 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
203 InitialHi, Hi, DAG.getSrcValue(NULL));
204 // load the constructed double
205 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
206 DAG.getSrcValue(NULL));
207 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000208 SDOperand Bias = DAG.getConstantFP(isSigned ?
209 BitsToDouble(0x4330000080000000ULL)
210 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000211 MVT::f64);
212 // subtract the bias
213 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
214 // final result
215 SDOperand Result;
216 // handle final rounding
217 if (DestVT == MVT::f64) {
218 // do nothing
219 Result = Sub;
220 } else {
221 // if f32 then cast to f32
222 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
223 }
224 NeedsAnotherIteration = true;
225 return Result;
226 }
227 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000228 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000229
Chris Lattnerd47675e2005-08-09 20:20:18 +0000230 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
231 DAG.getConstant(0, Op0.getValueType()),
232 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000233 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
234 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
235 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Jim Laskeyf2516a92005-08-17 00:39:29 +0000237 // If the sign bit of the integer is set, the large number will be treated
238 // as a negative number. To counteract this, the dynamic code adds an
239 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000240 uint64_t FF;
241 switch (Op0.getValueType()) {
242 default: assert(0 && "Unsupported integer type!");
243 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
244 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
245 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
246 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
247 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000248 if (TLI.isLittleEndian()) FF <<= 32;
249 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000250
Chris Lattnere3e847b2005-07-16 00:19:57 +0000251 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
252 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
253 TLI.getPointerTy());
254 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
255 SDOperand FudgeInReg;
256 if (DestVT == MVT::f32)
257 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
258 DAG.getSrcValue(NULL));
259 else {
260 assert(DestVT == MVT::f64 && "Unexpected conversion");
261 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
262 DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL), MVT::f32));
264 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000265
Chris Lattnere3e847b2005-07-16 00:19:57 +0000266 NeedsAnotherIteration = true;
267 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
268}
269
Chris Lattner19732782005-08-16 18:17:10 +0000270/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000271/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000272/// we promote it. At this point, we know that the result and operand types are
273/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
274/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000275SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
276 MVT::ValueType DestVT,
277 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000278 // First step, figure out the appropriate *INT_TO_FP operation to use.
279 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000280
Chris Lattnere3e847b2005-07-16 00:19:57 +0000281 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000282
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // Scan for the appropriate larger type to use.
284 while (1) {
285 NewInTy = (MVT::ValueType)(NewInTy+1);
286 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // If the target supports SINT_TO_FP of this type, use it.
289 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
290 default: break;
291 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000292 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000293 break; // Can't use this datatype.
294 // FALL THROUGH.
295 case TargetLowering::Custom:
296 OpToUse = ISD::SINT_TO_FP;
297 break;
298 }
299 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000300 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000301
Chris Lattnere3e847b2005-07-16 00:19:57 +0000302 // If the target supports UINT_TO_FP of this type, use it.
303 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
304 default: break;
305 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000306 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-07-16 00:19:57 +0000307 break; // Can't use this datatype.
308 // FALL THROUGH.
309 case TargetLowering::Custom:
310 OpToUse = ISD::UINT_TO_FP;
311 break;
312 }
313 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000314
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315 // Otherwise, try a larger type.
316 }
317
318 // Make sure to legalize any nodes we create here in the next pass.
319 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000320
Chris Lattnere3e847b2005-07-16 00:19:57 +0000321 // Okay, we found the operation and type to use. Zero extend our input to the
322 // desired type then run the operation on it.
323 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000324 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
325 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326}
327
Chris Lattner44fe26f2005-07-29 00:11:56 +0000328/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
329/// FP_TO_*INT operation of the specified operand when the target requests that
330/// we promote it. At this point, we know that the result and operand types are
331/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
332/// operation that returns a larger result.
333SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
334 MVT::ValueType DestVT,
335 bool isSigned) {
336 // First step, figure out the appropriate FP_TO*INT operation to use.
337 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000338
Chris Lattner44fe26f2005-07-29 00:11:56 +0000339 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000340
Chris Lattner44fe26f2005-07-29 00:11:56 +0000341 // Scan for the appropriate larger type to use.
342 while (1) {
343 NewOutTy = (MVT::ValueType)(NewOutTy+1);
344 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // If the target supports FP_TO_SINT returning this type, use it.
347 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
348 default: break;
349 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000350 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000351 break; // Can't use this datatype.
352 // FALL THROUGH.
353 case TargetLowering::Custom:
354 OpToUse = ISD::FP_TO_SINT;
355 break;
356 }
357 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000358
Chris Lattner44fe26f2005-07-29 00:11:56 +0000359 // If the target supports FP_TO_UINT of this type, use it.
360 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
361 default: break;
362 case TargetLowering::Legal:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000363 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-07-29 00:11:56 +0000364 break; // Can't use this datatype.
365 // FALL THROUGH.
366 case TargetLowering::Custom:
367 OpToUse = ISD::FP_TO_UINT;
368 break;
369 }
370 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000371
Chris Lattner44fe26f2005-07-29 00:11:56 +0000372 // Otherwise, try a larger type.
373 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000374
Chris Lattner44fe26f2005-07-29 00:11:56 +0000375 // Make sure to legalize any nodes we create here in the next pass.
376 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000377
Chris Lattner44fe26f2005-07-29 00:11:56 +0000378 // Okay, we found the operation and type to use. Truncate the result of the
379 // extended FP_TO_*INT operation to the desired size.
380 return DAG.getNode(ISD::TRUNCATE, DestVT,
381 DAG.getNode(OpToUse, NewOutTy, LegalOp));
382}
383
384
Chris Lattnerdc750592005-01-07 07:47:09 +0000385void SelectionDAGLegalize::LegalizeDAG() {
386 SDOperand OldRoot = DAG.getRoot();
387 SDOperand NewRoot = LegalizeOp(OldRoot);
388 DAG.setRoot(NewRoot);
389
390 ExpandedNodes.clear();
391 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000392 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000393
394 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000395 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000396}
397
398SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000399 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000400 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000401 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000402
Chris Lattnerdc750592005-01-07 07:47:09 +0000403 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000404 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000405 if (Node->getNumValues() > 1) {
406 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
407 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000408 case Legal: break; // Nothing to do.
409 case Expand: {
410 SDOperand T1, T2;
411 ExpandOp(Op.getValue(i), T1, T2);
412 assert(LegalizedNodes.count(Op) &&
413 "Expansion didn't add legal operands!");
414 return LegalizedNodes[Op];
415 }
416 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000417 PromoteOp(Op.getValue(i));
418 assert(LegalizedNodes.count(Op) &&
419 "Expansion didn't add legal operands!");
420 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000421 }
422 }
423
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000424 // Note that LegalizeOp may be reentered even from single-use nodes, which
425 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000426 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
427 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000428
Nate Begemane5b86d72005-08-10 20:51:12 +0000429 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000430
431 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000432
433 switch (Node->getOpcode()) {
434 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000435 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
436 // If this is a target node, legalize it by legalizing the operands then
437 // passing it through.
438 std::vector<SDOperand> Ops;
439 bool Changed = false;
440 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
441 Ops.push_back(LegalizeOp(Node->getOperand(i)));
442 Changed = Changed || Node->getOperand(i) != Ops.back();
443 }
444 if (Changed)
445 if (Node->getNumValues() == 1)
446 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
447 else {
448 std::vector<MVT::ValueType> VTs(Node->value_begin(),
449 Node->value_end());
450 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
451 }
452
453 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
454 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
455 return Result.getValue(Op.ResNo);
456 }
457 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000458 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
459 assert(0 && "Do not know how to legalize this operator!");
460 abort();
461 case ISD::EntryToken:
462 case ISD::FrameIndex:
463 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000464 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000465 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000466 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000467 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000468 case ISD::CopyFromReg:
469 Tmp1 = LegalizeOp(Node->getOperand(0));
470 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000471 Result = DAG.getCopyFromReg(Tmp1,
472 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
473 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000474 else
475 Result = Op.getValue(0);
476
477 // Since CopyFromReg produces two values, make sure to remember that we
478 // legalized both of them.
479 AddLegalizedOperand(Op.getValue(0), Result);
480 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
481 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000482 case ISD::ImplicitDef:
483 Tmp1 = LegalizeOp(Node->getOperand(0));
484 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000485 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
486 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000487 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000488 case ISD::UNDEF: {
489 MVT::ValueType VT = Op.getValueType();
490 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000491 default: assert(0 && "This action is not supported yet!");
492 case TargetLowering::Expand:
493 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000494 if (MVT::isInteger(VT))
495 Result = DAG.getConstant(0, VT);
496 else if (MVT::isFloatingPoint(VT))
497 Result = DAG.getConstantFP(0, VT);
498 else
499 assert(0 && "Unknown value type!");
500 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000501 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000502 break;
503 }
504 break;
505 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000506 case ISD::Constant:
507 // We know we don't need to expand constants here, constants only have one
508 // value and we check that it is fine above.
509
510 // FIXME: Maybe we should handle things like targets that don't support full
511 // 32-bit immediates?
512 break;
513 case ISD::ConstantFP: {
514 // Spill FP immediates to the constant pool if the target cannot directly
515 // codegen them. Targets often have some immediate values that can be
516 // efficiently generated into an FP register without a load. We explicitly
517 // leave these constants as ConstantFP nodes for the target to deal with.
518
519 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
520
521 // Check to see if this FP immediate is already legal.
522 bool isLegal = false;
523 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
524 E = TLI.legal_fpimm_end(); I != E; ++I)
525 if (CFP->isExactlyValue(*I)) {
526 isLegal = true;
527 break;
528 }
529
530 if (!isLegal) {
531 // Otherwise we need to spill the constant to memory.
532 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
533
534 bool Extend = false;
535
536 // If a FP immediate is precise when represented as a float, we put it
537 // into the constant pool as a float, even if it's is statically typed
538 // as a double.
539 MVT::ValueType VT = CFP->getValueType(0);
540 bool isDouble = VT == MVT::f64;
541 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
542 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000543 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
544 // Only do this if the target has a native EXTLOAD instruction from
545 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000546 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000547 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
548 VT = MVT::f32;
549 Extend = true;
550 }
Misha Brukman835702a2005-04-21 22:36:52 +0000551
Chris Lattnerdc750592005-01-07 07:47:09 +0000552 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
553 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000554 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000555 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
556 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000557 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000558 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
559 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000560 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000561 }
562 break;
563 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000564 case ISD::TokenFactor: {
565 std::vector<SDOperand> Ops;
566 bool Changed = false;
567 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000568 SDOperand Op = Node->getOperand(i);
569 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000570 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000571 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
572 Changed = true;
573 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
574 Ops.push_back(LegalizeOp(Op.getOperand(j)));
575 } else {
576 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
577 Changed |= Ops[i] != Op;
578 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000579 }
580 if (Changed)
581 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
582 break;
583 }
584
Chris Lattner2dce7032005-05-12 23:24:06 +0000585 case ISD::CALLSEQ_START:
586 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000587 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000588 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000589 Tmp2 = Node->getOperand(0);
590 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000591 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000592
593 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
594 // this will cause the maps used to memoize results to get confused.
595 // Create and add a dummy use, just to increase its use count. This will
596 // be removed at the end of legalize when dead nodes are removed.
597 if (Tmp2.Val->hasOneUse())
598 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
599 DAG.getConstant(0, MVT::i32));
600 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000601 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000602 // nodes are treated specially and are mutated in place. This makes the dag
603 // legalization process more efficient and also makes libcall insertion
604 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000605 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000606 case ISD::DYNAMIC_STACKALLOC:
607 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
608 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
609 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
610 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000611 Tmp3 != Node->getOperand(2)) {
612 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
613 std::vector<SDOperand> Ops;
614 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
615 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
616 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000617 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000618
619 // Since this op produces two values, make sure to remember that we
620 // legalized both of them.
621 AddLegalizedOperand(SDOperand(Node, 0), Result);
622 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
623 return Result.getValue(Op.ResNo);
624
Chris Lattnerd0feb642005-05-13 18:43:43 +0000625 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000626 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000627 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
628 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000629
630 bool Changed = false;
631 std::vector<SDOperand> Ops;
632 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
633 Ops.push_back(LegalizeOp(Node->getOperand(i)));
634 Changed |= Ops.back() != Node->getOperand(i);
635 }
636
637 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000638 std::vector<MVT::ValueType> RetTyVTs;
639 RetTyVTs.reserve(Node->getNumValues());
640 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000641 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000642 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
643 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000644 } else {
645 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000646 }
Chris Lattner9242c502005-01-09 19:43:23 +0000647 // Since calls produce multiple values, make sure to remember that we
648 // legalized all of them.
649 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
650 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
651 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000652 }
Chris Lattner68a12142005-01-07 22:12:08 +0000653 case ISD::BR:
654 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
655 if (Tmp1 != Node->getOperand(0))
656 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
657 break;
658
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000659 case ISD::BRCOND:
660 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000661
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000662 switch (getTypeAction(Node->getOperand(1).getValueType())) {
663 case Expand: assert(0 && "It's impossible to expand bools");
664 case Legal:
665 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
666 break;
667 case Promote:
668 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
669 break;
670 }
Nate Begeman371e4952005-08-16 19:49:35 +0000671
672 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
673 default: assert(0 && "This action is not supported yet!");
674 case TargetLowering::Expand:
675 // Expand brcond's setcc into its constituent parts and create a BR_CC
676 // Node.
677 if (Tmp2.getOpcode() == ISD::SETCC) {
678 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
679 Tmp2.getOperand(0), Tmp2.getOperand(1),
680 Node->getOperand(2));
681 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000682 // Make sure the condition is either zero or one. It may have been
683 // promoted from something else.
684 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
685
Nate Begeman371e4952005-08-16 19:49:35 +0000686 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
687 DAG.getCondCode(ISD::SETNE), Tmp2,
688 DAG.getConstant(0, Tmp2.getValueType()),
689 Node->getOperand(2));
690 }
691 break;
692 case TargetLowering::Legal:
693 // Basic block destination (Op#2) is always legal.
694 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
695 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
696 Node->getOperand(2));
697 break;
698 }
699 break;
700 case ISD::BR_CC:
701 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
702
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000703 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000704 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
705 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
706 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
707 Tmp3 != Node->getOperand(3)) {
708 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
709 Tmp2, Tmp3, Node->getOperand(4));
710 }
711 break;
712 } else {
713 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
714 Node->getOperand(2), // LHS
715 Node->getOperand(3), // RHS
716 Node->getOperand(1)));
717 // If we get a SETCC back from legalizing the SETCC node we just
718 // created, then use its LHS, RHS, and CC directly in creating a new
719 // node. Otherwise, select between the true and false value based on
720 // comparing the result of the legalized with zero.
721 if (Tmp2.getOpcode() == ISD::SETCC) {
722 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
723 Tmp2.getOperand(0), Tmp2.getOperand(1),
724 Node->getOperand(4));
725 } else {
726 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
727 DAG.getCondCode(ISD::SETNE),
728 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
729 Node->getOperand(4));
730 }
731 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000732 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000733 case ISD::BRCONDTWOWAY:
734 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
735 switch (getTypeAction(Node->getOperand(1).getValueType())) {
736 case Expand: assert(0 && "It's impossible to expand bools");
737 case Legal:
738 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
739 break;
740 case Promote:
741 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
742 break;
743 }
744 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
745 // pair.
746 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
747 case TargetLowering::Promote:
748 default: assert(0 && "This action is not supported yet!");
749 case TargetLowering::Legal:
750 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
751 std::vector<SDOperand> Ops;
752 Ops.push_back(Tmp1);
753 Ops.push_back(Tmp2);
754 Ops.push_back(Node->getOperand(2));
755 Ops.push_back(Node->getOperand(3));
756 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
757 }
758 break;
759 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000760 // If BRTWOWAY_CC is legal for this target, then simply expand this node
761 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
762 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000763 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000764 if (Tmp2.getOpcode() == ISD::SETCC) {
765 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
766 Tmp2.getOperand(0), Tmp2.getOperand(1),
767 Node->getOperand(2), Node->getOperand(3));
768 } else {
769 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
770 DAG.getConstant(0, Tmp2.getValueType()),
771 Node->getOperand(2), Node->getOperand(3));
772 }
773 } else {
774 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000775 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000776 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
777 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000778 break;
779 }
780 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000781 case ISD::BRTWOWAY_CC:
782 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000783 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000784 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
785 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
786 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
787 Tmp3 != Node->getOperand(3)) {
788 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
789 Node->getOperand(4), Node->getOperand(5));
790 }
791 break;
792 } else {
793 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
794 Node->getOperand(2), // LHS
795 Node->getOperand(3), // RHS
796 Node->getOperand(1)));
797 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
798 // pair.
799 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
800 default: assert(0 && "This action is not supported yet!");
801 case TargetLowering::Legal:
802 // If we get a SETCC back from legalizing the SETCC node we just
803 // created, then use its LHS, RHS, and CC directly in creating a new
804 // node. Otherwise, select between the true and false value based on
805 // comparing the result of the legalized with zero.
806 if (Tmp2.getOpcode() == ISD::SETCC) {
807 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
808 Tmp2.getOperand(0), Tmp2.getOperand(1),
809 Node->getOperand(4), Node->getOperand(5));
810 } else {
811 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
812 DAG.getConstant(0, Tmp2.getValueType()),
813 Node->getOperand(4), Node->getOperand(5));
814 }
815 break;
816 case TargetLowering::Expand:
817 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
818 Node->getOperand(4));
819 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
820 break;
821 }
822 }
823 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000824 case ISD::LOAD:
825 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
826 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000827
Chris Lattnerdc750592005-01-07 07:47:09 +0000828 if (Tmp1 != Node->getOperand(0) ||
829 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000830 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
831 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000832 else
833 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000834
Chris Lattnerea4ca942005-01-07 22:28:47 +0000835 // Since loads produce two values, make sure to remember that we legalized
836 // both of them.
837 AddLegalizedOperand(SDOperand(Node, 0), Result);
838 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
839 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000840
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000841 case ISD::EXTLOAD:
842 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000843 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000844 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
845 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000846
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000847 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000848 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000849 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000850 case TargetLowering::Promote:
851 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000852 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
853 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000854 // Since loads produce two values, make sure to remember that we legalized
855 // both of them.
856 AddLegalizedOperand(SDOperand(Node, 0), Result);
857 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
858 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000859
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000860 case TargetLowering::Legal:
861 if (Tmp1 != Node->getOperand(0) ||
862 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000863 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
864 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000865 else
866 Result = SDOperand(Node, 0);
867
868 // Since loads produce two values, make sure to remember that we legalized
869 // both of them.
870 AddLegalizedOperand(SDOperand(Node, 0), Result);
871 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
872 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000873 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000874 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
875 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
876 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000877 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000878 if (Op.ResNo)
879 return Load.getValue(1);
880 return Result;
881 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000882 assert(Node->getOpcode() != ISD::EXTLOAD &&
883 "EXTLOAD should always be supported!");
884 // Turn the unsupported load into an EXTLOAD followed by an explicit
885 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000886 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
887 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000888 SDOperand ValRes;
889 if (Node->getOpcode() == ISD::SEXTLOAD)
890 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000891 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000892 else
893 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000894 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
895 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
896 if (Op.ResNo)
897 return Result.getValue(1);
898 return ValRes;
899 }
900 assert(0 && "Unreachable");
901 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000902 case ISD::EXTRACT_ELEMENT:
903 // Get both the low and high parts.
904 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
905 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
906 Result = Tmp2; // 1 -> Hi
907 else
908 Result = Tmp1; // 0 -> Lo
909 break;
910
911 case ISD::CopyToReg:
912 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000913
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000914 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +0000915 "Register type must be legal!");
916 // Legalize the incoming value (must be legal).
917 Tmp2 = LegalizeOp(Node->getOperand(2));
918 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
919 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
920 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000921 break;
922
923 case ISD::RET:
924 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
925 switch (Node->getNumOperands()) {
926 case 2: // ret val
927 switch (getTypeAction(Node->getOperand(1).getValueType())) {
928 case Legal:
929 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000930 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000931 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
932 break;
933 case Expand: {
934 SDOperand Lo, Hi;
935 ExpandOp(Node->getOperand(1), Lo, Hi);
936 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000937 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000938 }
939 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000940 Tmp2 = PromoteOp(Node->getOperand(1));
941 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
942 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000943 }
944 break;
945 case 1: // ret void
946 if (Tmp1 != Node->getOperand(0))
947 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
948 break;
949 default: { // ret <values>
950 std::vector<SDOperand> NewValues;
951 NewValues.push_back(Tmp1);
952 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
953 switch (getTypeAction(Node->getOperand(i).getValueType())) {
954 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000955 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000956 break;
957 case Expand: {
958 SDOperand Lo, Hi;
959 ExpandOp(Node->getOperand(i), Lo, Hi);
960 NewValues.push_back(Lo);
961 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000962 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000963 }
964 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000965 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000966 }
967 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
968 break;
969 }
970 }
971 break;
972 case ISD::STORE:
973 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
974 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
975
Chris Lattnere69daaf2005-01-08 06:25:56 +0000976 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000977 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000978 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000979 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000980 DAG.getConstant(FloatToBits(CFP->getValue()),
981 MVT::i32),
982 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000983 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000984 } else {
985 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000986 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000987 DAG.getConstant(DoubleToBits(CFP->getValue()),
988 MVT::i64),
989 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000990 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000991 }
Chris Lattnera4743132005-02-22 07:23:39 +0000992 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000993 }
994
Chris Lattnerdc750592005-01-07 07:47:09 +0000995 switch (getTypeAction(Node->getOperand(1).getValueType())) {
996 case Legal: {
997 SDOperand Val = LegalizeOp(Node->getOperand(1));
998 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
999 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001000 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1001 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001002 break;
1003 }
1004 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001005 // Truncate the value and store the result.
1006 Tmp3 = PromoteOp(Node->getOperand(1));
1007 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001008 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001009 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001010 break;
1011
Chris Lattnerdc750592005-01-07 07:47:09 +00001012 case Expand:
1013 SDOperand Lo, Hi;
1014 ExpandOp(Node->getOperand(1), Lo, Hi);
1015
1016 if (!TLI.isLittleEndian())
1017 std::swap(Lo, Hi);
1018
Chris Lattner55e9cde2005-05-11 04:51:16 +00001019 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1020 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001021 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001022 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1023 getIntPtrConstant(IncrementSize));
1024 assert(isTypeLegal(Tmp2.getValueType()) &&
1025 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001026 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001027 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1028 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001029 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1030 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001031 }
1032 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001033 case ISD::PCMARKER:
1034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001035 if (Tmp1 != Node->getOperand(0))
1036 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001037 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001038 case ISD::TRUNCSTORE:
1039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1040 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1041
1042 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1043 case Legal:
1044 Tmp2 = LegalizeOp(Node->getOperand(1));
1045 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1046 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +00001047 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001048 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001049 break;
1050 case Promote:
1051 case Expand:
1052 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1053 }
1054 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001055 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001056 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1057 case Expand: assert(0 && "It's impossible to expand bools");
1058 case Legal:
1059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1060 break;
1061 case Promote:
1062 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1063 break;
1064 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001065 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001066 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001067
Nate Begeman987121a2005-08-23 04:29:48 +00001068 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001069 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001070 case TargetLowering::Expand:
1071 if (Tmp1.getOpcode() == ISD::SETCC) {
1072 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1073 Tmp2, Tmp3,
1074 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1075 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001076 // Make sure the condition is either zero or one. It may have been
1077 // promoted from something else.
1078 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001079 Result = DAG.getSelectCC(Tmp1,
1080 DAG.getConstant(0, Tmp1.getValueType()),
1081 Tmp2, Tmp3, ISD::SETNE);
1082 }
1083 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001084 case TargetLowering::Legal:
1085 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1086 Tmp3 != Node->getOperand(2))
1087 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1088 Tmp1, Tmp2, Tmp3);
1089 break;
1090 case TargetLowering::Promote: {
1091 MVT::ValueType NVT =
1092 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1093 unsigned ExtOp, TruncOp;
1094 if (MVT::isInteger(Tmp2.getValueType())) {
1095 ExtOp = ISD::ZERO_EXTEND;
1096 TruncOp = ISD::TRUNCATE;
1097 } else {
1098 ExtOp = ISD::FP_EXTEND;
1099 TruncOp = ISD::FP_ROUND;
1100 }
1101 // Promote each of the values to the new type.
1102 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1103 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1104 // Perform the larger operation, then round down.
1105 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1106 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1107 break;
1108 }
1109 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001110 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001111 case ISD::SELECT_CC:
1112 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1113 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1114
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001115 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Nate Begemane5b86d72005-08-10 20:51:12 +00001116 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1117 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1118 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1119 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1120 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1121 Tmp3, Tmp4, Node->getOperand(4));
1122 }
1123 break;
1124 } else {
1125 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1126 Node->getOperand(0), // LHS
1127 Node->getOperand(1), // RHS
1128 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001129 // If we get a SETCC back from legalizing the SETCC node we just
1130 // created, then use its LHS, RHS, and CC directly in creating a new
1131 // node. Otherwise, select between the true and false value based on
1132 // comparing the result of the legalized with zero.
1133 if (Tmp1.getOpcode() == ISD::SETCC) {
1134 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1135 Tmp1.getOperand(0), Tmp1.getOperand(1),
1136 Tmp3, Tmp4, Tmp1.getOperand(2));
1137 } else {
1138 Result = DAG.getSelectCC(Tmp1,
1139 DAG.getConstant(0, Tmp1.getValueType()),
1140 Tmp3, Tmp4, ISD::SETNE);
1141 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001142 }
1143 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001144 case ISD::SETCC:
1145 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1146 case Legal:
1147 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1148 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001149 break;
1150 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001151 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1152 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1153
1154 // If this is an FP compare, the operands have already been extended.
1155 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1156 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001157 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001158
1159 // Otherwise, we have to insert explicit sign or zero extends. Note
1160 // that we could insert sign extends for ALL conditions, but zero extend
1161 // is cheaper on many machines (an AND instead of two shifts), so prefer
1162 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001163 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001164 default: assert(0 && "Unknown integer comparison!");
1165 case ISD::SETEQ:
1166 case ISD::SETNE:
1167 case ISD::SETUGE:
1168 case ISD::SETUGT:
1169 case ISD::SETULE:
1170 case ISD::SETULT:
1171 // ALL of these operations will work if we either sign or zero extend
1172 // the operands (including the unsigned comparisons!). Zero extend is
1173 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001174 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1175 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001176 break;
1177 case ISD::SETGE:
1178 case ISD::SETGT:
1179 case ISD::SETLT:
1180 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001181 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1182 DAG.getValueType(VT));
1183 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1184 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001185 break;
1186 }
Chris Lattner4d978642005-01-15 22:16:26 +00001187 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001188 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001189 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001190 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1191 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1192 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001193 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001194 case ISD::SETEQ:
1195 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001196 if (RHSLo == RHSHi)
1197 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1198 if (RHSCST->isAllOnesValue()) {
1199 // Comparison to -1.
1200 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001201 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001202 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001203 }
1204
Chris Lattnerdc750592005-01-07 07:47:09 +00001205 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1206 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1207 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001208 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001209 break;
1210 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001211 // If this is a comparison of the sign bit, just look at the top part.
1212 // X > -1, x < 0
1213 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001214 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001215 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001216 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001217 (CST->isAllOnesValue()))) { // X > -1
1218 Tmp1 = LHSHi;
1219 Tmp2 = RHSHi;
1220 break;
1221 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001222
Chris Lattnerdc750592005-01-07 07:47:09 +00001223 // FIXME: This generated code sucks.
1224 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001225 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 default: assert(0 && "Unknown integer setcc!");
1227 case ISD::SETLT:
1228 case ISD::SETULT: LowCC = ISD::SETULT; break;
1229 case ISD::SETGT:
1230 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1231 case ISD::SETLE:
1232 case ISD::SETULE: LowCC = ISD::SETULE; break;
1233 case ISD::SETGE:
1234 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1235 }
Misha Brukman835702a2005-04-21 22:36:52 +00001236
Chris Lattnerdc750592005-01-07 07:47:09 +00001237 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1238 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1239 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1240
1241 // NOTE: on targets without efficient SELECT of bools, we can always use
1242 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001243 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1244 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1245 Node->getOperand(2));
1246 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001247 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1248 Result, Tmp1, Tmp2));
1249 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001250 }
1251 }
Nate Begeman987121a2005-08-23 04:29:48 +00001252
1253 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1254 default:
1255 assert(0 && "Cannot handle this action for SETCC yet!");
1256 break;
1257 case TargetLowering::Legal:
1258 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1259 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1260 Node->getOperand(2));
1261 break;
1262 case TargetLowering::Expand:
1263 // Expand a setcc node into a select_cc of the same condition, lhs, and
1264 // rhs that selects between const 1 (true) and const 0 (false).
1265 MVT::ValueType VT = Node->getValueType(0);
1266 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1267 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1268 Node->getOperand(2));
1269 Result = LegalizeOp(Result);
1270 break;
1271 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001272 break;
1273
Chris Lattner85d70c62005-01-11 05:57:22 +00001274 case ISD::MEMSET:
1275 case ISD::MEMCPY:
1276 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001277 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001278 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1279
1280 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1281 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1282 case Expand: assert(0 && "Cannot expand a byte!");
1283 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001284 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001285 break;
1286 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001287 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001288 break;
1289 }
1290 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001291 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001292 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001293
1294 SDOperand Tmp4;
1295 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001296 case Expand: {
1297 // Length is too big, just take the lo-part of the length.
1298 SDOperand HiPart;
1299 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1300 break;
1301 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001302 case Legal:
1303 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001304 break;
1305 case Promote:
1306 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001307 break;
1308 }
1309
1310 SDOperand Tmp5;
1311 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1312 case Expand: assert(0 && "Cannot expand this yet!");
1313 case Legal:
1314 Tmp5 = LegalizeOp(Node->getOperand(4));
1315 break;
1316 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001317 Tmp5 = PromoteOp(Node->getOperand(4));
1318 break;
1319 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001320
1321 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1322 default: assert(0 && "This action not implemented for this operation!");
1323 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001324 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1325 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1326 Tmp5 != Node->getOperand(4)) {
1327 std::vector<SDOperand> Ops;
1328 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1329 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1330 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1331 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001332 break;
1333 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001334 // Otherwise, the target does not support this operation. Lower the
1335 // operation to an explicit libcall as appropriate.
1336 MVT::ValueType IntPtr = TLI.getPointerTy();
1337 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1338 std::vector<std::pair<SDOperand, const Type*> > Args;
1339
Reid Spencer6dced922005-01-12 14:53:45 +00001340 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001341 if (Node->getOpcode() == ISD::MEMSET) {
1342 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1343 // Extend the ubyte argument to be an int value for the call.
1344 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1345 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1346 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1347
1348 FnName = "memset";
1349 } else if (Node->getOpcode() == ISD::MEMCPY ||
1350 Node->getOpcode() == ISD::MEMMOVE) {
1351 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1352 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1353 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1354 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1355 } else {
1356 assert(0 && "Unknown op!");
1357 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001358
Chris Lattner85d70c62005-01-11 05:57:22 +00001359 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001360 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001361 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001362 Result = CallResult.second;
1363 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001364 break;
1365 }
1366 case TargetLowering::Custom:
1367 std::vector<SDOperand> Ops;
1368 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1369 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1370 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001371 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001372 Result = LegalizeOp(Result);
1373 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001374 }
1375 break;
1376 }
Chris Lattner5385db52005-05-09 20:23:03 +00001377
1378 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001379 Tmp1 = LegalizeOp(Node->getOperand(0));
1380 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001381
Chris Lattner86535992005-05-14 07:45:46 +00001382 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1383 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1384 std::vector<SDOperand> Ops;
1385 Ops.push_back(Tmp1);
1386 Ops.push_back(Tmp2);
1387 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1388 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001389 Result = SDOperand(Node, 0);
1390 // Since these produce two values, make sure to remember that we legalized
1391 // both of them.
1392 AddLegalizedOperand(SDOperand(Node, 0), Result);
1393 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1394 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001395 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001396 Tmp1 = LegalizeOp(Node->getOperand(0));
1397 Tmp2 = LegalizeOp(Node->getOperand(1));
1398 Tmp3 = LegalizeOp(Node->getOperand(2));
1399 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1400 Tmp3 != Node->getOperand(2))
1401 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1402 break;
1403
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001404 case ISD::READIO:
1405 Tmp1 = LegalizeOp(Node->getOperand(0));
1406 Tmp2 = LegalizeOp(Node->getOperand(1));
1407
1408 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1409 case TargetLowering::Custom:
1410 default: assert(0 && "This action not implemented for this operation!");
1411 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001412 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1413 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1414 std::vector<SDOperand> Ops;
1415 Ops.push_back(Tmp1);
1416 Ops.push_back(Tmp2);
1417 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1418 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001419 Result = SDOperand(Node, 0);
1420 break;
1421 case TargetLowering::Expand:
1422 // Replace this with a load from memory.
1423 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1424 Node->getOperand(1), DAG.getSrcValue(NULL));
1425 Result = LegalizeOp(Result);
1426 break;
1427 }
1428
1429 // Since these produce two values, make sure to remember that we legalized
1430 // both of them.
1431 AddLegalizedOperand(SDOperand(Node, 0), Result);
1432 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1433 return Result.getValue(Op.ResNo);
1434
1435 case ISD::WRITEIO:
1436 Tmp1 = LegalizeOp(Node->getOperand(0));
1437 Tmp2 = LegalizeOp(Node->getOperand(1));
1438 Tmp3 = LegalizeOp(Node->getOperand(2));
1439
1440 switch (TLI.getOperationAction(Node->getOpcode(),
1441 Node->getOperand(1).getValueType())) {
1442 case TargetLowering::Custom:
1443 default: assert(0 && "This action not implemented for this operation!");
1444 case TargetLowering::Legal:
1445 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1446 Tmp3 != Node->getOperand(2))
1447 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1448 break;
1449 case TargetLowering::Expand:
1450 // Replace this with a store to memory.
1451 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1452 Node->getOperand(1), Node->getOperand(2),
1453 DAG.getSrcValue(NULL));
1454 Result = LegalizeOp(Result);
1455 break;
1456 }
1457 break;
1458
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001459 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001460 case ISD::SUB_PARTS:
1461 case ISD::SHL_PARTS:
1462 case ISD::SRA_PARTS:
1463 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001464 std::vector<SDOperand> Ops;
1465 bool Changed = false;
1466 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1467 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1468 Changed |= Ops.back() != Node->getOperand(i);
1469 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001470 if (Changed) {
1471 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1472 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1473 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001474
1475 // Since these produce multiple values, make sure to remember that we
1476 // legalized all of them.
1477 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1478 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1479 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001480 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001481
1482 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001483 case ISD::ADD:
1484 case ISD::SUB:
1485 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001486 case ISD::MULHS:
1487 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001488 case ISD::UDIV:
1489 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001490 case ISD::AND:
1491 case ISD::OR:
1492 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001493 case ISD::SHL:
1494 case ISD::SRL:
1495 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001496 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001497 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1498 case Expand: assert(0 && "Not possible");
1499 case Legal:
1500 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1501 break;
1502 case Promote:
1503 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1504 break;
1505 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001506 if (Tmp1 != Node->getOperand(0) ||
1507 Tmp2 != Node->getOperand(1))
1508 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1509 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001510
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001511 case ISD::UREM:
1512 case ISD::SREM:
1513 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1514 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1515 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1516 case TargetLowering::Legal:
1517 if (Tmp1 != Node->getOperand(0) ||
1518 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001519 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001520 Tmp2);
1521 break;
1522 case TargetLowering::Promote:
1523 case TargetLowering::Custom:
1524 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001525 case TargetLowering::Expand:
1526 if (MVT::isInteger(Node->getValueType(0))) {
1527 MVT::ValueType VT = Node->getValueType(0);
1528 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1529 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1530 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1531 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1532 } else {
1533 // Floating point mod -> fmod libcall.
1534 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1535 SDOperand Dummy;
1536 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001537 }
1538 break;
1539 }
1540 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001541
Andrew Lenharth5e177822005-05-03 17:19:30 +00001542 case ISD::CTPOP:
1543 case ISD::CTTZ:
1544 case ISD::CTLZ:
1545 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1546 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1547 case TargetLowering::Legal:
1548 if (Tmp1 != Node->getOperand(0))
1549 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1550 break;
1551 case TargetLowering::Promote: {
1552 MVT::ValueType OVT = Tmp1.getValueType();
1553 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001554
1555 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001556 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1557 // Perform the larger operation, then subtract if needed.
1558 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1559 switch(Node->getOpcode())
1560 {
1561 case ISD::CTPOP:
1562 Result = Tmp1;
1563 break;
1564 case ISD::CTTZ:
1565 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001566 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1567 DAG.getConstant(getSizeInBits(NVT), NVT),
1568 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001569 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001570 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1571 break;
1572 case ISD::CTLZ:
1573 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001574 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1575 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001576 getSizeInBits(OVT), NVT));
1577 break;
1578 }
1579 break;
1580 }
1581 case TargetLowering::Custom:
1582 assert(0 && "Cannot custom handle this yet!");
1583 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001584 switch(Node->getOpcode())
1585 {
1586 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001587 static const uint64_t mask[6] = {
1588 0x5555555555555555ULL, 0x3333333333333333ULL,
1589 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1590 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1591 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001592 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001593 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1594 unsigned len = getSizeInBits(VT);
1595 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001596 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001597 Tmp2 = DAG.getConstant(mask[i], VT);
1598 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001599 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001600 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1601 DAG.getNode(ISD::AND, VT,
1602 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1603 Tmp2));
1604 }
1605 Result = Tmp1;
1606 break;
1607 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001608 case ISD::CTLZ: {
1609 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001610 x = x | (x >> 1);
1611 x = x | (x >> 2);
1612 ...
1613 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001614 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001615 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001616
Chris Lattner56add052005-05-11 18:35:21 +00001617 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1618 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001619 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1620 unsigned len = getSizeInBits(VT);
1621 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1622 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001623 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001624 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1625 }
1626 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001627 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001628 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001629 }
1630 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001631 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001632 // unless the target has ctlz but not ctpop, in which case we use:
1633 // { return 32 - nlz(~x & (x-1)); }
1634 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001635 MVT::ValueType VT = Tmp1.getValueType();
1636 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001637 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001638 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1639 DAG.getNode(ISD::SUB, VT, Tmp1,
1640 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001641 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001642 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1643 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001644 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001645 DAG.getConstant(getSizeInBits(VT), VT),
1646 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1647 } else {
1648 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1649 }
Chris Lattner72473242005-05-11 05:27:09 +00001650 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001651 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001652 default:
1653 assert(0 && "Cannot expand this yet!");
1654 break;
1655 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001656 break;
1657 }
1658 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001659
Chris Lattner13fe99c2005-04-02 05:00:07 +00001660 // Unary operators
1661 case ISD::FABS:
1662 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001663 case ISD::FSQRT:
1664 case ISD::FSIN:
1665 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001666 Tmp1 = LegalizeOp(Node->getOperand(0));
1667 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1668 case TargetLowering::Legal:
1669 if (Tmp1 != Node->getOperand(0))
1670 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1671 break;
1672 case TargetLowering::Promote:
1673 case TargetLowering::Custom:
1674 assert(0 && "Cannot promote/custom handle this yet!");
1675 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001676 switch(Node->getOpcode()) {
1677 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001678 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1679 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1680 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1681 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001682 break;
1683 }
1684 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001685 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1686 MVT::ValueType VT = Node->getValueType(0);
1687 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001688 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001689 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1690 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1691 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001692 break;
1693 }
1694 case ISD::FSQRT:
1695 case ISD::FSIN:
1696 case ISD::FCOS: {
1697 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001698 const char *FnName = 0;
1699 switch(Node->getOpcode()) {
1700 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1701 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1702 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1703 default: assert(0 && "Unreachable!");
1704 }
Nate Begeman77558da2005-08-04 21:43:28 +00001705 SDOperand Dummy;
1706 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001707 break;
1708 }
1709 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001710 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001711 }
1712 break;
1713 }
1714 break;
1715
1716 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001717 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001718 case ISD::UINT_TO_FP: {
1719 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001720 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1721 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001722 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001723 Node->getOperand(0).getValueType())) {
1724 default: assert(0 && "Unknown operation action!");
1725 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001726 Result = ExpandLegalINT_TO_FP(isSigned,
1727 LegalizeOp(Node->getOperand(0)),
1728 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001729 AddLegalizedOperand(Op, Result);
1730 return Result;
1731 case TargetLowering::Promote:
1732 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1733 Node->getValueType(0),
1734 isSigned);
1735 AddLegalizedOperand(Op, Result);
1736 return Result;
1737 case TargetLowering::Legal:
1738 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001739 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001740
Chris Lattnerdc750592005-01-07 07:47:09 +00001741 Tmp1 = LegalizeOp(Node->getOperand(0));
1742 if (Tmp1 != Node->getOperand(0))
1743 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1744 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001745 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001746 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1747 Node->getValueType(0), Node->getOperand(0));
1748 break;
1749 case Promote:
1750 if (isSigned) {
1751 Result = PromoteOp(Node->getOperand(0));
1752 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1753 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1754 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1755 } else {
1756 Result = PromoteOp(Node->getOperand(0));
1757 Result = DAG.getZeroExtendInReg(Result,
1758 Node->getOperand(0).getValueType());
1759 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001760 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001761 break;
1762 }
1763 break;
1764 }
1765 case ISD::TRUNCATE:
1766 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1767 case Legal:
1768 Tmp1 = LegalizeOp(Node->getOperand(0));
1769 if (Tmp1 != Node->getOperand(0))
1770 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1771 break;
1772 case Expand:
1773 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1774
1775 // Since the result is legal, we should just be able to truncate the low
1776 // part of the source.
1777 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1778 break;
1779 case Promote:
1780 Result = PromoteOp(Node->getOperand(0));
1781 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1782 break;
1783 }
1784 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001785
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001786 case ISD::FP_TO_SINT:
1787 case ISD::FP_TO_UINT:
1788 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1789 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001790 Tmp1 = LegalizeOp(Node->getOperand(0));
1791
Chris Lattner44fe26f2005-07-29 00:11:56 +00001792 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1793 default: assert(0 && "Unknown operation action!");
1794 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00001795 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1796 SDOperand True, False;
1797 MVT::ValueType VT = Node->getOperand(0).getValueType();
1798 MVT::ValueType NVT = Node->getValueType(0);
1799 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1800 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1801 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1802 Node->getOperand(0), Tmp2, ISD::SETLT);
1803 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1804 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1805 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1806 Tmp2));
1807 False = DAG.getNode(ISD::XOR, NVT, False,
1808 DAG.getConstant(1ULL << ShiftAmt, NVT));
1809 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00001810 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00001811 } else {
1812 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1813 }
1814 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001815 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001816 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001817 Node->getOpcode() == ISD::FP_TO_SINT);
1818 AddLegalizedOperand(Op, Result);
1819 return Result;
1820 case TargetLowering::Legal:
1821 break;
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001822 case TargetLowering::Custom:
1823 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1824 Result = TLI.LowerOperation(Result, DAG);
1825 AddLegalizedOperand(Op, Result);
1826 NeedsAnotherIteration = true;
1827 return Result;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001828 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001829
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001830 if (Tmp1 != Node->getOperand(0))
1831 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1832 break;
1833 case Expand:
1834 assert(0 && "Shouldn't need to expand other operators here!");
1835 case Promote:
1836 Result = PromoteOp(Node->getOperand(0));
1837 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1838 break;
1839 }
1840 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001841
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001842 case ISD::ZERO_EXTEND:
1843 case ISD::SIGN_EXTEND:
1844 case ISD::FP_EXTEND:
1845 case ISD::FP_ROUND:
1846 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1847 case Legal:
1848 Tmp1 = LegalizeOp(Node->getOperand(0));
1849 if (Tmp1 != Node->getOperand(0))
1850 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1851 break;
1852 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001853 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001854
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001855 case Promote:
1856 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001857 case ISD::ZERO_EXTEND:
1858 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001859 // NOTE: Any extend would work here...
1860 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001861 Result = DAG.getZeroExtendInReg(Result,
1862 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001863 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001864 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001865 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001866 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001867 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001868 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001869 Result,
1870 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001871 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001872 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001873 Result = PromoteOp(Node->getOperand(0));
1874 if (Result.getValueType() != Op.getValueType())
1875 // Dynamically dead while we have only 2 FP types.
1876 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1877 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001878 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001879 Result = PromoteOp(Node->getOperand(0));
1880 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1881 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001882 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001883 }
1884 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001885 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001886 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001887 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001888 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001889
1890 // If this operation is not supported, convert it to a shl/shr or load/store
1891 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001892 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1893 default: assert(0 && "This action not supported for this op yet!");
1894 case TargetLowering::Legal:
1895 if (Tmp1 != Node->getOperand(0))
1896 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001897 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001898 break;
1899 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001900 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001901 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001902 // NOTE: we could fall back on load/store here too for targets without
1903 // SAR. However, it is doubtful that any exist.
1904 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1905 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001906 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001907 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1908 Node->getOperand(0), ShiftCst);
1909 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1910 Result, ShiftCst);
1911 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1912 // The only way we can lower this is to turn it into a STORETRUNC,
1913 // EXTLOAD pair, targetting a temporary location (a stack slot).
1914
1915 // NOTE: there is a choice here between constantly creating new stack
1916 // slots and always reusing the same one. We currently always create
1917 // new ones, as reuse may inhibit scheduling.
1918 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1919 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1920 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1921 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001922 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001923 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1924 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1925 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001926 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001927 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001928 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1929 Result, StackSlot, DAG.getSrcValue(NULL),
1930 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001931 } else {
1932 assert(0 && "Unknown op");
1933 }
1934 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001935 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001936 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001937 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001938 }
Chris Lattner99222f72005-01-15 07:15:18 +00001939 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001940
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001941 // Note that LegalizeOp may be reentered even from single-use nodes, which
1942 // means that we always must cache transformed nodes.
1943 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001944 return Result;
1945}
1946
Chris Lattner4d978642005-01-15 22:16:26 +00001947/// PromoteOp - Given an operation that produces a value in an invalid type,
1948/// promote it to compute the value into a larger type. The produced value will
1949/// have the correct bits for the low portion of the register, but no guarantee
1950/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001951SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1952 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001953 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001954 assert(getTypeAction(VT) == Promote &&
1955 "Caller should expand or legalize operands that are not promotable!");
1956 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1957 "Cannot promote to smaller type!");
1958
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001959 SDOperand Tmp1, Tmp2, Tmp3;
1960
1961 SDOperand Result;
1962 SDNode *Node = Op.Val;
1963
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001964 if (!Node->hasOneUse()) {
1965 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1966 if (I != PromotedNodes.end()) return I->second;
1967 } else {
1968 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1969 }
1970
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001971 // Promotion needs an optimization step to clean up after it, and is not
1972 // careful to avoid operations the target does not support. Make sure that
1973 // all generated operations are legalized in the next iteration.
1974 NeedsAnotherIteration = true;
1975
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001976 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00001977 case ISD::CopyFromReg:
1978 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001979 default:
1980 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1981 assert(0 && "Do not know how to promote this operator!");
1982 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001983 case ISD::UNDEF:
1984 Result = DAG.getNode(ISD::UNDEF, NVT);
1985 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001986 case ISD::Constant:
1987 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1988 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1989 break;
1990 case ISD::ConstantFP:
1991 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1992 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1993 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001994
Chris Lattner2cb338d2005-01-18 02:59:52 +00001995 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001996 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00001997 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
1998 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00001999 Result = LegalizeOp(Result);
2000 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002001
2002 case ISD::TRUNCATE:
2003 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2004 case Legal:
2005 Result = LegalizeOp(Node->getOperand(0));
2006 assert(Result.getValueType() >= NVT &&
2007 "This truncation doesn't make sense!");
2008 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2009 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2010 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002011 case Promote:
2012 // The truncation is not required, because we don't guarantee anything
2013 // about high bits anyway.
2014 Result = PromoteOp(Node->getOperand(0));
2015 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002016 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002017 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2018 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002019 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002020 }
2021 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002022 case ISD::SIGN_EXTEND:
2023 case ISD::ZERO_EXTEND:
2024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2025 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2026 case Legal:
2027 // Input is legal? Just do extend all the way to the larger type.
2028 Result = LegalizeOp(Node->getOperand(0));
2029 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2030 break;
2031 case Promote:
2032 // Promote the reg if it's smaller.
2033 Result = PromoteOp(Node->getOperand(0));
2034 // The high bits are not guaranteed to be anything. Insert an extend.
2035 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002036 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002037 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002038 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002039 Result = DAG.getZeroExtendInReg(Result,
2040 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002041 break;
2042 }
2043 break;
2044
2045 case ISD::FP_EXTEND:
2046 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2047 case ISD::FP_ROUND:
2048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2049 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2050 case Promote: assert(0 && "Unreachable with 2 FP types!");
2051 case Legal:
2052 // Input is legal? Do an FP_ROUND_INREG.
2053 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002054 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2055 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002056 break;
2057 }
2058 break;
2059
2060 case ISD::SINT_TO_FP:
2061 case ISD::UINT_TO_FP:
2062 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2063 case Legal:
2064 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002065 // No extra round required here.
2066 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002067 break;
2068
2069 case Promote:
2070 Result = PromoteOp(Node->getOperand(0));
2071 if (Node->getOpcode() == ISD::SINT_TO_FP)
2072 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002073 Result,
2074 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002075 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002076 Result = DAG.getZeroExtendInReg(Result,
2077 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002078 // No extra round required here.
2079 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002080 break;
2081 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002082 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2083 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002084 // Round if we cannot tolerate excess precision.
2085 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002086 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2087 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002088 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002089 }
Chris Lattner4d978642005-01-15 22:16:26 +00002090 break;
2091
2092 case ISD::FP_TO_SINT:
2093 case ISD::FP_TO_UINT:
2094 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2095 case Legal:
2096 Tmp1 = LegalizeOp(Node->getOperand(0));
2097 break;
2098 case Promote:
2099 // The input result is prerounded, so we don't have to do anything
2100 // special.
2101 Tmp1 = PromoteOp(Node->getOperand(0));
2102 break;
2103 case Expand:
2104 assert(0 && "not implemented");
2105 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002106 // If we're promoting a UINT to a larger size, check to see if the new node
2107 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2108 // we can use that instead. This allows us to generate better code for
2109 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2110 // legal, such as PowerPC.
2111 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002112 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2113 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begeman36853ee2005-08-14 01:20:53 +00002114 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2115 } else {
2116 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2117 }
Chris Lattner4d978642005-01-15 22:16:26 +00002118 break;
2119
Chris Lattner13fe99c2005-04-02 05:00:07 +00002120 case ISD::FABS:
2121 case ISD::FNEG:
2122 Tmp1 = PromoteOp(Node->getOperand(0));
2123 assert(Tmp1.getValueType() == NVT);
2124 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2125 // NOTE: we do not have to do any extra rounding here for
2126 // NoExcessFPPrecision, because we know the input will have the appropriate
2127 // precision, and these operations don't modify precision at all.
2128 break;
2129
Chris Lattner9d6fa982005-04-28 21:44:33 +00002130 case ISD::FSQRT:
2131 case ISD::FSIN:
2132 case ISD::FCOS:
2133 Tmp1 = PromoteOp(Node->getOperand(0));
2134 assert(Tmp1.getValueType() == NVT);
2135 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2136 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002137 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2138 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002139 break;
2140
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002141 case ISD::AND:
2142 case ISD::OR:
2143 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002144 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002145 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002146 case ISD::MUL:
2147 // The input may have strange things in the top bits of the registers, but
2148 // these operations don't care. They may have wierd bits going out, but
2149 // that too is okay if they are integer operations.
2150 Tmp1 = PromoteOp(Node->getOperand(0));
2151 Tmp2 = PromoteOp(Node->getOperand(1));
2152 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2153 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2154
2155 // However, if this is a floating point operation, they will give excess
2156 // precision that we may not be able to tolerate. If we DO allow excess
2157 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002158 // FIXME: Why would we need to round FP ops more than integer ones?
2159 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002160 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002161 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2162 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002163 break;
2164
Chris Lattner4d978642005-01-15 22:16:26 +00002165 case ISD::SDIV:
2166 case ISD::SREM:
2167 // These operators require that their input be sign extended.
2168 Tmp1 = PromoteOp(Node->getOperand(0));
2169 Tmp2 = PromoteOp(Node->getOperand(1));
2170 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002171 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2172 DAG.getValueType(VT));
2173 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2174 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002175 }
2176 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2177
2178 // Perform FP_ROUND: this is probably overly pessimistic.
2179 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002180 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2181 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002182 break;
2183
2184 case ISD::UDIV:
2185 case ISD::UREM:
2186 // These operators require that their input be zero extended.
2187 Tmp1 = PromoteOp(Node->getOperand(0));
2188 Tmp2 = PromoteOp(Node->getOperand(1));
2189 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002190 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2191 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002192 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2193 break;
2194
2195 case ISD::SHL:
2196 Tmp1 = PromoteOp(Node->getOperand(0));
2197 Tmp2 = LegalizeOp(Node->getOperand(1));
2198 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2199 break;
2200 case ISD::SRA:
2201 // The input value must be properly sign extended.
2202 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002203 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2204 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002205 Tmp2 = LegalizeOp(Node->getOperand(1));
2206 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2207 break;
2208 case ISD::SRL:
2209 // The input value must be properly zero extended.
2210 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002211 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002212 Tmp2 = LegalizeOp(Node->getOperand(1));
2213 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2214 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002215 case ISD::LOAD:
2216 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2217 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00002218 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00002219 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002220 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2221 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00002222 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002223 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2224 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002225
2226 // Remember that we legalized the chain.
2227 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2228 break;
2229 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002230 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2231 case Expand: assert(0 && "It's impossible to expand bools");
2232 case Legal:
2233 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2234 break;
2235 case Promote:
2236 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2237 break;
2238 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002239 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2240 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2241 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2242 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002243 case ISD::SELECT_CC:
2244 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2245 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2246 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2247 Node->getOperand(1), Tmp2, Tmp3,
2248 Node->getOperand(4));
2249 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002250 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002251 case ISD::CALL: {
2252 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2253 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2254
Chris Lattner3d95c142005-01-19 20:24:35 +00002255 std::vector<SDOperand> Ops;
2256 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2257 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2258
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002259 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2260 "Can only promote single result calls");
2261 std::vector<MVT::ValueType> RetTyVTs;
2262 RetTyVTs.reserve(2);
2263 RetTyVTs.push_back(NVT);
2264 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002265 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2266 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002267 Result = SDOperand(NC, 0);
2268
2269 // Insert the new chain mapping.
2270 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2271 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002272 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002273 case ISD::CTPOP:
2274 case ISD::CTTZ:
2275 case ISD::CTLZ:
2276 Tmp1 = Node->getOperand(0);
2277 //Zero extend the argument
2278 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2279 // Perform the larger operation, then subtract if needed.
2280 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2281 switch(Node->getOpcode())
2282 {
2283 case ISD::CTPOP:
2284 Result = Tmp1;
2285 break;
2286 case ISD::CTTZ:
2287 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002288 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002289 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002290 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002291 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2292 break;
2293 case ISD::CTLZ:
2294 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002295 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2296 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002297 getSizeInBits(VT), NVT));
2298 break;
2299 }
2300 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002301 }
2302
2303 assert(Result.Val && "Didn't set a result!");
2304 AddPromotedOperand(Op, Result);
2305 return Result;
2306}
Chris Lattnerdc750592005-01-07 07:47:09 +00002307
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002308/// ExpandAddSub - Find a clever way to expand this add operation into
2309/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002310void SelectionDAGLegalize::
2311ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2312 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002313 // Expand the subcomponents.
2314 SDOperand LHSL, LHSH, RHSL, RHSH;
2315 ExpandOp(LHS, LHSL, LHSH);
2316 ExpandOp(RHS, RHSL, RHSH);
2317
Chris Lattner8ffd0042005-04-11 20:29:59 +00002318 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002319 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2320 if (LHSL.getValueType() == MVT::i32) {
2321 SDOperand LowEl;
2322 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2323 if (C->getValue() == 0)
2324 LowEl = RHSL;
2325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2326 if (C->getValue() == 0)
2327 LowEl = LHSL;
2328 if (LowEl.Val) {
2329 // Turn this into an add/sub of the high part only.
2330 SDOperand HiEl =
2331 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2332 LowEl.getValueType(), LHSH, RHSH);
2333 Lo = LowEl;
2334 Hi = HiEl;
2335 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002336 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002337 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002338
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002339 std::vector<SDOperand> Ops;
2340 Ops.push_back(LHSL);
2341 Ops.push_back(LHSH);
2342 Ops.push_back(RHSL);
2343 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002344
2345 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2346 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002347 Hi = Lo.getValue(1);
2348}
2349
Chris Lattner4157c412005-04-02 04:00:59 +00002350void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2351 SDOperand Op, SDOperand Amt,
2352 SDOperand &Lo, SDOperand &Hi) {
2353 // Expand the subcomponents.
2354 SDOperand LHSL, LHSH;
2355 ExpandOp(Op, LHSL, LHSH);
2356
2357 std::vector<SDOperand> Ops;
2358 Ops.push_back(LHSL);
2359 Ops.push_back(LHSH);
2360 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002361 std::vector<MVT::ValueType> VTs;
2362 VTs.push_back(LHSL.getValueType());
2363 VTs.push_back(LHSH.getValueType());
2364 VTs.push_back(Amt.getValueType());
2365 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002366 Hi = Lo.getValue(1);
2367}
2368
2369
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002370/// ExpandShift - Try to find a clever way to expand this shift operation out to
2371/// smaller elements. If we can't find a way that is more efficient than a
2372/// libcall on this target, return false. Otherwise, return true with the
2373/// low-parts expanded into Lo and Hi.
2374bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2375 SDOperand &Lo, SDOperand &Hi) {
2376 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2377 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002378
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002379 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002380 SDOperand ShAmt = LegalizeOp(Amt);
2381 MVT::ValueType ShTy = ShAmt.getValueType();
2382 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2383 unsigned NVTBits = MVT::getSizeInBits(NVT);
2384
2385 // Handle the case when Amt is an immediate. Other cases are currently broken
2386 // and are disabled.
2387 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2388 unsigned Cst = CN->getValue();
2389 // Expand the incoming operand to be shifted, so that we have its parts
2390 SDOperand InL, InH;
2391 ExpandOp(Op, InL, InH);
2392 switch(Opc) {
2393 case ISD::SHL:
2394 if (Cst > VTBits) {
2395 Lo = DAG.getConstant(0, NVT);
2396 Hi = DAG.getConstant(0, NVT);
2397 } else if (Cst > NVTBits) {
2398 Lo = DAG.getConstant(0, NVT);
2399 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002400 } else if (Cst == NVTBits) {
2401 Lo = DAG.getConstant(0, NVT);
2402 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002403 } else {
2404 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2405 Hi = DAG.getNode(ISD::OR, NVT,
2406 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2407 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2408 }
2409 return true;
2410 case ISD::SRL:
2411 if (Cst > VTBits) {
2412 Lo = DAG.getConstant(0, NVT);
2413 Hi = DAG.getConstant(0, NVT);
2414 } else if (Cst > NVTBits) {
2415 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2416 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002417 } else if (Cst == NVTBits) {
2418 Lo = InH;
2419 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002420 } else {
2421 Lo = DAG.getNode(ISD::OR, NVT,
2422 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2423 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2424 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2425 }
2426 return true;
2427 case ISD::SRA:
2428 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002429 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002430 DAG.getConstant(NVTBits-1, ShTy));
2431 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002432 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002433 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002434 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002435 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002436 } else if (Cst == NVTBits) {
2437 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002438 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002439 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002440 } else {
2441 Lo = DAG.getNode(ISD::OR, NVT,
2442 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2443 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2444 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2445 }
2446 return true;
2447 }
2448 }
2449 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2450 // so disable it for now. Currently targets are handling this via SHL_PARTS
2451 // and friends.
2452 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002453
2454 // If we have an efficient select operation (or if the selects will all fold
2455 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002456 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002457 return false;
2458
2459 SDOperand InL, InH;
2460 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002461 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2462 DAG.getConstant(NVTBits, ShTy), ShAmt);
2463
Chris Lattner4d25c042005-01-20 20:29:23 +00002464 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002465 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2466 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002467
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002468 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2469 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2470 DAG.getConstant(NVTBits-1, ShTy));
2471 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2472 DAG.getConstant(NVTBits-1, ShTy));
2473 }
2474
2475 if (Opc == ISD::SHL) {
2476 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2477 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2478 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002479 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002480
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002481 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2482 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2483 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002484 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002485 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2486 DAG.getConstant(32, ShTy),
2487 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002488 DAG.getConstant(0, NVT),
2489 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002490 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002491 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002492 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002493 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002494
2495 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002496 if (Opc == ISD::SRA)
2497 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2498 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002499 else
2500 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002501 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002502 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002503 }
2504 return true;
2505}
Chris Lattneraac464e2005-01-21 06:05:23 +00002506
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002507/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2508/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002509/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002510static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002511 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002512
Chris Lattner2dce7032005-05-12 23:24:06 +00002513 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002514 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002515 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002516 Found = Node;
2517 return;
2518 }
2519
2520 // Otherwise, scan the operands of Node to see if any of them is a call.
2521 assert(Node->getNumOperands() != 0 &&
2522 "All leaves should have depth equal to the entry node!");
2523 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002524 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002525
2526 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002527 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002528 Found);
2529}
2530
2531
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002532/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2533/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002534/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002535static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2536 std::set<SDNode*> &Visited) {
2537 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2538 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002539
Chris Lattner2dce7032005-05-12 23:24:06 +00002540 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002541 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002542 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002543 Found = Node;
2544 return;
2545 }
2546
2547 // Otherwise, scan the operands of Node to see if any of them is a call.
2548 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2549 if (UI == E) return;
2550 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002551 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002552
2553 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002554 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002555}
2556
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002557/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002558/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002559static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002560 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002561 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002562 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002563 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002564
2565 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002566 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002567
Chris Lattner4add7e32005-01-23 04:42:50 +00002568 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002569 if (TheChain.getValueType() != MVT::Other)
2570 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002571 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002572
2573 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002574 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002575
Chris Lattner4add7e32005-01-23 04:42:50 +00002576 // Make sure to only follow users of our token chain.
2577 SDNode *User = *UI;
2578 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2579 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002580 if (SDNode *Result = FindCallSeqEnd(User))
2581 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002582 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002583 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002584}
2585
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002586/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002587/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002588static SDNode *FindCallSeqStart(SDNode *Node) {
2589 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002590 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002591
2592 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2593 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002594 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002595}
2596
2597
Chris Lattner4add7e32005-01-23 04:42:50 +00002598/// FindInputOutputChains - If we are replacing an operation with a call we need
2599/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002600/// properly serialize the calls in the block. The returned operand is the
2601/// input chain value for the new call (e.g. the entry node or the previous
2602/// call), and OutChain is set to be the chain node to update to point to the
2603/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002604static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2605 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002606 SDNode *LatestCallSeqStart = Entry.Val;
2607 SDNode *LatestCallSeqEnd = 0;
2608 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2609 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002610
Chris Lattner2dce7032005-05-12 23:24:06 +00002611 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002612 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002613 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2614 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002615 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2616 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002617 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002618 LatestCallSeqEnd = Entry.Val;
2619 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002620
Chris Lattner06bbeb62005-05-11 19:02:11 +00002621 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002622 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002623 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002624 std::set<SDNode*> Visited;
2625 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002626
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002627 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002628 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002629 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002630
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002631 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002632}
2633
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002634/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002635void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2636 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002637 // Nothing to splice it into?
2638 if (OutChain == 0) return;
2639
2640 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2641 //OutChain->dump();
2642
2643 // Form a token factor node merging the old inval and the new inval.
2644 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2645 OutChain->getOperand(0));
2646 // Change the node to refer to the new token.
2647 OutChain->setAdjCallChain(InToken);
2648}
Chris Lattner4add7e32005-01-23 04:42:50 +00002649
2650
Chris Lattneraac464e2005-01-21 06:05:23 +00002651// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2652// does not fit into a register, return the lo part and set the hi part to the
2653// by-reg argument. If it does fit into a single register, return the result
2654// and leave the Hi part unset.
2655SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2656 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002657 SDNode *OutChain;
2658 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2659 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002660 if (InChain.Val == 0)
2661 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002662
Chris Lattneraac464e2005-01-21 06:05:23 +00002663 TargetLowering::ArgListTy Args;
2664 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2665 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2666 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2667 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2668 }
2669 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002670
Chris Lattner06bbeb62005-05-11 19:02:11 +00002671 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002672 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002673 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002674 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2675 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002676 SpliceCallInto(CallInfo.second, OutChain);
2677
2678 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002679
2680 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002681 default: assert(0 && "Unknown thing");
2682 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002683 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002684 case Promote:
2685 assert(0 && "Cannot promote this yet!");
2686 case Expand:
2687 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002688 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002689 return Lo;
2690 }
2691}
2692
Chris Lattner4add7e32005-01-23 04:42:50 +00002693
Chris Lattneraac464e2005-01-21 06:05:23 +00002694/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2695/// destination type is legal.
2696SDOperand SelectionDAGLegalize::
2697ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002698 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00002699 assert(getTypeAction(Source.getValueType()) == Expand &&
2700 "This is not an expansion!");
2701 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2702
Chris Lattner06bbeb62005-05-11 19:02:11 +00002703 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002704 assert(Source.getValueType() == MVT::i64 &&
2705 "This only works for 64-bit -> FP");
2706 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2707 // incoming integer is set. To handle this, we dynamically test to see if
2708 // it is set, and, if so, add a fudge factor.
2709 SDOperand Lo, Hi;
2710 ExpandOp(Source, Lo, Hi);
2711
Chris Lattner2a4f7312005-05-13 04:45:13 +00002712 // If this is unsigned, and not supported, first perform the conversion to
2713 // signed, then adjust the result if the sign bit is set.
2714 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2715 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2716
Chris Lattnerd47675e2005-08-09 20:20:18 +00002717 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2718 DAG.getConstant(0, Hi.getValueType()),
2719 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002720 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2721 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2722 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002723 uint64_t FF = 0x5f800000ULL;
2724 if (TLI.isLittleEndian()) FF <<= 32;
2725 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002726
2727 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2728 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2729 TLI.getPointerTy());
2730 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2731 SDOperand FudgeInReg;
2732 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002733 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2734 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002735 else {
2736 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002737 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2738 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002739 }
2740 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002741 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002742
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002743 // Check to see if the target has a custom way to lower this. If so, use it.
2744 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2745 default: assert(0 && "This action not implemented for this operation!");
2746 case TargetLowering::Legal:
2747 case TargetLowering::Expand:
2748 break; // This case is handled below.
2749 case TargetLowering::Custom:
2750 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002751 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002752 }
2753
Chris Lattner153587e2005-05-12 07:00:44 +00002754 // Expand the source, then glue it back together for the call. We must expand
2755 // the source in case it is shared (this pass of legalize must traverse it).
2756 SDOperand SrcLo, SrcHi;
2757 ExpandOp(Source, SrcLo, SrcHi);
2758 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2759
Chris Lattner06bbeb62005-05-11 19:02:11 +00002760 SDNode *OutChain = 0;
2761 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2762 DAG.getEntryNode());
2763 const char *FnName = 0;
2764 if (DestTy == MVT::f32)
2765 FnName = "__floatdisf";
2766 else {
2767 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2768 FnName = "__floatdidf";
2769 }
2770
Chris Lattneraac464e2005-01-21 06:05:23 +00002771 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2772
2773 TargetLowering::ArgListTy Args;
2774 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002775
Chris Lattneraac464e2005-01-21 06:05:23 +00002776 Args.push_back(std::make_pair(Source, ArgTy));
2777
2778 // We don't care about token chains for libcalls. We just use the entry
2779 // node as our input and ignore the output chain. This allows us to place
2780 // calls wherever we need them to satisfy data dependences.
2781 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002782
2783 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002784 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2785 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002786
Chris Lattnera5bf1032005-05-12 04:49:08 +00002787 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002788 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002789}
Misha Brukman835702a2005-04-21 22:36:52 +00002790
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002791
2792
Chris Lattnerdc750592005-01-07 07:47:09 +00002793/// ExpandOp - Expand the specified SDOperand into its two component pieces
2794/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2795/// LegalizeNodes map is filled in for any results that are not expanded, the
2796/// ExpandedNodes map is filled in for any results that are expanded, and the
2797/// Lo/Hi values are returned.
2798void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2799 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002800 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002801 SDNode *Node = Op.Val;
2802 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2803 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2804 assert(MVT::isInteger(NVT) && NVT < VT &&
2805 "Cannot expand to FP value or to larger int value!");
2806
2807 // If there is more than one use of this, see if we already expanded it.
2808 // There is no use remembering values that only have a single use, as the map
2809 // entries will never be reused.
2810 if (!Node->hasOneUse()) {
2811 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2812 = ExpandedNodes.find(Op);
2813 if (I != ExpandedNodes.end()) {
2814 Lo = I->second.first;
2815 Hi = I->second.second;
2816 return;
2817 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002818 } else {
2819 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002820 }
2821
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002822 // Expanding to multiple registers needs to perform an optimization step, and
2823 // is not careful to avoid operations the target does not support. Make sure
2824 // that all generated operations are legalized in the next iteration.
2825 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002826
Chris Lattnerdc750592005-01-07 07:47:09 +00002827 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002828 case ISD::CopyFromReg:
2829 assert(0 && "CopyFromReg must be legal!");
2830 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00002831 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2832 assert(0 && "Do not know how to expand this operator!");
2833 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002834 case ISD::UNDEF:
2835 Lo = DAG.getNode(ISD::UNDEF, NVT);
2836 Hi = DAG.getNode(ISD::UNDEF, NVT);
2837 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002838 case ISD::Constant: {
2839 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2840 Lo = DAG.getConstant(Cst, NVT);
2841 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2842 break;
2843 }
2844
Chris Lattner32e08b72005-03-28 22:03:13 +00002845 case ISD::BUILD_PAIR:
2846 // Legalize both operands. FIXME: in the future we should handle the case
2847 // where the two elements are not legal.
2848 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2849 Lo = LegalizeOp(Node->getOperand(0));
2850 Hi = LegalizeOp(Node->getOperand(1));
2851 break;
2852
Chris Lattner55e9cde2005-05-11 04:51:16 +00002853 case ISD::CTPOP:
2854 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002855 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2856 DAG.getNode(ISD::CTPOP, NVT, Lo),
2857 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002858 Hi = DAG.getConstant(0, NVT);
2859 break;
2860
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002861 case ISD::CTLZ: {
2862 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002863 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002864 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2865 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002866 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2867 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002868 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2869 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2870
2871 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2872 Hi = DAG.getConstant(0, NVT);
2873 break;
2874 }
2875
2876 case ISD::CTTZ: {
2877 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002878 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002879 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2880 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002881 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2882 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002883 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2884 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2885
2886 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2887 Hi = DAG.getConstant(0, NVT);
2888 break;
2889 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002890
Chris Lattnerdc750592005-01-07 07:47:09 +00002891 case ISD::LOAD: {
2892 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2893 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002894 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002895
2896 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002897 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002898 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2899 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002900 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002901 //are from the same instruction?
2902 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002903
2904 // Build a factor node to remember that this load is independent of the
2905 // other one.
2906 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2907 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002908
Chris Lattnerdc750592005-01-07 07:47:09 +00002909 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002910 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002911 if (!TLI.isLittleEndian())
2912 std::swap(Lo, Hi);
2913 break;
2914 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002915 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002916 case ISD::CALL: {
2917 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2918 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2919
Chris Lattner3d95c142005-01-19 20:24:35 +00002920 bool Changed = false;
2921 std::vector<SDOperand> Ops;
2922 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2923 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2924 Changed |= Ops.back() != Node->getOperand(i);
2925 }
2926
Chris Lattnerdc750592005-01-07 07:47:09 +00002927 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2928 "Can only expand a call once so far, not i64 -> i16!");
2929
2930 std::vector<MVT::ValueType> RetTyVTs;
2931 RetTyVTs.reserve(3);
2932 RetTyVTs.push_back(NVT);
2933 RetTyVTs.push_back(NVT);
2934 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002935 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2936 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002937 Lo = SDOperand(NC, 0);
2938 Hi = SDOperand(NC, 1);
2939
2940 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002941 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002942 break;
2943 }
2944 case ISD::AND:
2945 case ISD::OR:
2946 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2947 SDOperand LL, LH, RL, RH;
2948 ExpandOp(Node->getOperand(0), LL, LH);
2949 ExpandOp(Node->getOperand(1), RL, RH);
2950 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2951 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2952 break;
2953 }
2954 case ISD::SELECT: {
2955 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002956
2957 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2958 case Expand: assert(0 && "It's impossible to expand bools");
2959 case Legal:
2960 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2961 break;
2962 case Promote:
2963 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2964 break;
2965 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002966 ExpandOp(Node->getOperand(1), LL, LH);
2967 ExpandOp(Node->getOperand(2), RL, RH);
2968 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2969 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2970 break;
2971 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002972 case ISD::SELECT_CC: {
2973 SDOperand TL, TH, FL, FH;
2974 ExpandOp(Node->getOperand(2), TL, TH);
2975 ExpandOp(Node->getOperand(3), FL, FH);
2976 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2977 Node->getOperand(1), TL, FL, Node->getOperand(4));
2978 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2979 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00002980 Lo = LegalizeOp(Lo);
2981 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00002982 break;
2983 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002984 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002985 SDOperand In;
2986 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2987 case Expand: assert(0 && "expand-expand not implemented yet!");
2988 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2989 case Promote:
2990 In = PromoteOp(Node->getOperand(0));
2991 // Emit the appropriate sign_extend_inreg to get the value we want.
2992 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002993 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002994 break;
2995 }
2996
Chris Lattnerdc750592005-01-07 07:47:09 +00002997 // The low part is just a sign extension of the input (which degenerates to
2998 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002999 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003000
Chris Lattnerdc750592005-01-07 07:47:09 +00003001 // The high part is obtained by SRA'ing all but one of the bits of the lo
3002 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003003 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003004 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3005 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003006 break;
3007 }
Chris Lattner47844892005-04-03 23:41:52 +00003008 case ISD::ZERO_EXTEND: {
3009 SDOperand In;
3010 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3011 case Expand: assert(0 && "expand-expand not implemented yet!");
3012 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3013 case Promote:
3014 In = PromoteOp(Node->getOperand(0));
3015 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003016 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003017 break;
3018 }
3019
Chris Lattnerdc750592005-01-07 07:47:09 +00003020 // The low part is just a zero extension of the input (which degenerates to
3021 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003022 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003023
Chris Lattnerdc750592005-01-07 07:47:09 +00003024 // The high part is just a zero.
3025 Hi = DAG.getConstant(0, NVT);
3026 break;
Chris Lattner47844892005-04-03 23:41:52 +00003027 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003028 // These operators cannot be expanded directly, emit them as calls to
3029 // library functions.
3030 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003031 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003032 SDOperand Op;
3033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3034 case Expand: assert(0 && "cannot expand FP!");
3035 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3036 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3037 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003038
Chris Lattner941d84a2005-07-30 01:40:57 +00003039 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3040
Chris Lattnerfe68d752005-07-29 00:33:32 +00003041 // Now that the custom expander is done, expand the result, which is still
3042 // VT.
Chris Lattner941d84a2005-07-30 01:40:57 +00003043 ExpandOp(Op, Lo, Hi);
Chris Lattnerfe68d752005-07-29 00:33:32 +00003044 break;
3045 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003046
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003047 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003048 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003049 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003050 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003051 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003052
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003053 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003054 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3055 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3056 LegalizeOp(Node->getOperand(0)));
3057 // Now that the custom expander is done, expand the result, which is still
3058 // VT.
3059 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
3060 break;
3061 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003062
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003063 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003064 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003065 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003066 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003067 break;
3068
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003069 case ISD::SHL:
3070 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003071 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003072 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003073
3074 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003075 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003076 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3077 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003078 break;
3079 }
3080
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003081 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003082 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003083 break;
3084
3085 case ISD::SRA:
3086 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003087 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003088 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003089
3090 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003091 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003092 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3093 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003094 break;
3095 }
3096
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003097 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003098 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003099 break;
3100 case ISD::SRL:
3101 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003102 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003103 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003104
3105 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003106 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003107 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3108 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003109 break;
3110 }
3111
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003112 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003113 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003114 break;
3115
Misha Brukman835702a2005-04-21 22:36:52 +00003116 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003117 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3118 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003119 break;
3120 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003121 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3122 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003123 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003124 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003125 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003126 SDOperand LL, LH, RL, RH;
3127 ExpandOp(Node->getOperand(0), LL, LH);
3128 ExpandOp(Node->getOperand(1), RL, RH);
3129 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3130 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3131 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3132 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3133 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3134 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3135 } else {
3136 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3137 }
3138 break;
3139 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003140 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3141 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3142 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3143 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003144 }
3145
3146 // Remember in a map if the values will be reused later.
3147 if (!Node->hasOneUse()) {
3148 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3149 std::make_pair(Lo, Hi))).second;
3150 assert(isNew && "Value already expanded?!?");
3151 }
3152}
3153
3154
3155// SelectionDAG::Legalize - This is the entry point for the file.
3156//
Chris Lattner4add7e32005-01-23 04:42:50 +00003157void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003158 /// run - This is the main entry point to this class.
3159 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003160 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003161}
3162