blob: 19a79749ebdff503d2f7bdaa324822dce8a001eb [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000018#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Constants.h"
24#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000025#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
30/// hacks on it until the target machine can handle it. This involves
31/// eliminating value sizes the machine cannot handle (promoting small sizes to
32/// large sizes or splitting up large values into small values) as well as
33/// eliminating operations the machine cannot handle.
34///
35/// This code also does a small amount of optimization and recognition of idioms
36/// as part of its processing. For example, if a target does not support a
37/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
38/// will attempt merge setcc and brc instructions into brcc's.
39///
40namespace {
41class SelectionDAGLegalize {
42 TargetLowering &TLI;
43 SelectionDAG &DAG;
44
45 /// LegalizeAction - This enum indicates what action we should take for each
46 /// value type the can occur in the program.
47 enum LegalizeAction {
48 Legal, // The target natively supports this value type.
49 Promote, // This should be promoted to the next larger type.
50 Expand, // This integer type should be broken into smaller pieces.
51 };
52
Chris Lattnerdc750592005-01-07 07:47:09 +000053 /// ValueTypeActions - This is a bitvector that contains two bits for each
54 /// value type, where the two bits correspond to the LegalizeAction enum.
55 /// This can be queried with "getTypeAction(VT)".
56 unsigned ValueTypeActions;
57
58 /// NeedsAnotherIteration - This is set when we expand a large integer
59 /// operation into smaller integer operations, but the smaller operations are
60 /// not set. This occurs only rarely in practice, for targets that don't have
61 /// 32-bit or larger integer registers.
62 bool NeedsAnotherIteration;
63
64 /// LegalizedNodes - For nodes that are of legal width, and that have more
65 /// than one use, this map indicates what regularized operand to use. This
66 /// allows us to avoid legalizing the same thing more than once.
67 std::map<SDOperand, SDOperand> LegalizedNodes;
68
Chris Lattner1f2c9d82005-01-15 05:21:40 +000069 /// PromotedNodes - For nodes that are below legal width, and that have more
70 /// than one use, this map indicates what promoted value to use. This allows
71 /// us to avoid promoting the same thing more than once.
72 std::map<SDOperand, SDOperand> PromotedNodes;
73
Chris Lattnerdc750592005-01-07 07:47:09 +000074 /// ExpandedNodes - For nodes that need to be expanded, and which have more
75 /// than one use, this map indicates which which operands are the expanded
76 /// version of the input. This allows us to avoid expanding the same node
77 /// more than once.
78 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
79
Chris Lattnerea4ca942005-01-07 22:28:47 +000080 void AddLegalizedOperand(SDOperand From, SDOperand To) {
81 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
82 assert(isNew && "Got into the map somehow?");
83 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000084 void AddPromotedOperand(SDOperand From, SDOperand To) {
85 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
86 assert(isNew && "Got into the map somehow?");
87 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000088
Chris Lattnerdc750592005-01-07 07:47:09 +000089public:
90
Chris Lattner4add7e32005-01-23 04:42:50 +000091 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000092
93 /// Run - While there is still lowering to do, perform a pass over the DAG.
94 /// Most regularization can be done in a single pass, but targets that require
95 /// large values to be split into registers multiple times (e.g. i64 -> 4x
96 /// i16) require iteration for these values (the first iteration will demote
97 /// to i32, the second will demote to i16).
98 void Run() {
99 do {
100 NeedsAnotherIteration = false;
101 LegalizeDAG();
102 } while (NeedsAnotherIteration);
103 }
104
105 /// getTypeAction - Return how we should legalize values of this type, either
106 /// it is already legal or we need to expand it into multiple registers of
107 /// smaller integer type, or we need to promote it to a larger type.
108 LegalizeAction getTypeAction(MVT::ValueType VT) const {
109 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
110 }
111
112 /// isTypeLegal - Return true if this type is legal on this target.
113 ///
114 bool isTypeLegal(MVT::ValueType VT) const {
115 return getTypeAction(VT) == Legal;
116 }
117
118private:
119 void LegalizeDAG();
120
121 SDOperand LegalizeOp(SDOperand O);
122 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000123 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124
Chris Lattneraac464e2005-01-21 06:05:23 +0000125 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
126 SDOperand &Hi);
127 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
128 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000129
Jim Laskeyf2516a92005-08-17 00:39:29 +0000130 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
131 SDOperand LegalOp,
132 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000133 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
134 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000135 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
136 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000137
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000138 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
139 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000140 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
141 SDOperand &Lo, SDOperand &Hi);
142 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000144
Chris Lattnera5bf1032005-05-12 04:49:08 +0000145 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
146
Chris Lattnerdc750592005-01-07 07:47:09 +0000147 SDOperand getIntPtrConstant(uint64_t Val) {
148 return DAG.getConstant(Val, TLI.getPointerTy());
149 }
150};
151}
152
153
Chris Lattner4add7e32005-01-23 04:42:50 +0000154SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
155 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
156 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000157 assert(MVT::LAST_VALUETYPE <= 16 &&
158 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000159}
160
Jim Laskeyf2516a92005-08-17 00:39:29 +0000161/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
162/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000163/// we expand it. At this point, we know that the result and operand types are
164/// legal for the target.
Jim Laskeyf2516a92005-08-17 00:39:29 +0000165SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
166 SDOperand Op0,
167 MVT::ValueType DestVT) {
168 if (Op0.getValueType() == MVT::i32) {
169 // simple 32-bit [signed|unsigned] integer to float/double expansion
170
171 // get the stack frame index of a 8 byte buffer
172 MachineFunction &MF = DAG.getMachineFunction();
173 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
174 // get address of 8 byte buffer
175 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
176 // word offset constant for Hi/Lo address computation
177 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
178 // set up Hi and Lo (into buffer) address based on endian
179 SDOperand Hi, Lo;
180 if (TLI.isLittleEndian()) {
181 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
182 Lo = StackSlot;
183 } else {
184 Hi = StackSlot;
185 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
186 }
187 // if signed map to unsigned space
188 SDOperand Op0Mapped;
189 if (isSigned) {
190 // constant used to invert sign bit (signed to unsigned mapping)
191 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
192 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
193 } else {
194 Op0Mapped = Op0;
195 }
196 // store the lo of the constructed double - based on integer input
197 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
198 Op0Mapped, Lo, DAG.getSrcValue(NULL));
199 // initial hi portion of constructed double
200 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
201 // store the hi of the constructed double - biased exponent
202 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
203 InitialHi, Hi, DAG.getSrcValue(NULL));
204 // load the constructed double
205 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
206 DAG.getSrcValue(NULL));
207 // FP constant to bias correct the final result
Jim Laskey686d6a12005-08-17 17:42:52 +0000208 SDOperand Bias = DAG.getConstantFP(isSigned ?
209 BitsToDouble(0x4330000080000000ULL)
210 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000211 MVT::f64);
212 // subtract the bias
213 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
214 // final result
215 SDOperand Result;
216 // handle final rounding
217 if (DestVT == MVT::f64) {
218 // do nothing
219 Result = Sub;
220 } else {
221 // if f32 then cast to f32
222 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
223 }
224 NeedsAnotherIteration = true;
225 return Result;
226 }
227 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnere3e847b2005-07-16 00:19:57 +0000228 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000229
Chris Lattnerd47675e2005-08-09 20:20:18 +0000230 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
231 DAG.getConstant(0, Op0.getValueType()),
232 ISD::SETLT);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000233 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
234 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
235 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Jim Laskeyf2516a92005-08-17 00:39:29 +0000237 // If the sign bit of the integer is set, the large number will be treated
238 // as a negative number. To counteract this, the dynamic code adds an
239 // offset depending on the data type.
Chris Lattnerb35912e2005-07-18 04:31:14 +0000240 uint64_t FF;
241 switch (Op0.getValueType()) {
242 default: assert(0 && "Unsupported integer type!");
243 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
244 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
245 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
246 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
247 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000248 if (TLI.isLittleEndian()) FF <<= 32;
249 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000250
Chris Lattnere3e847b2005-07-16 00:19:57 +0000251 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
252 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
253 TLI.getPointerTy());
254 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
255 SDOperand FudgeInReg;
256 if (DestVT == MVT::f32)
257 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
258 DAG.getSrcValue(NULL));
259 else {
260 assert(DestVT == MVT::f64 && "Unexpected conversion");
261 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
262 DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL), MVT::f32));
264 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000265
Chris Lattnere3e847b2005-07-16 00:19:57 +0000266 NeedsAnotherIteration = true;
267 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
268}
269
Chris Lattner19732782005-08-16 18:17:10 +0000270/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000271/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000272/// we promote it. At this point, we know that the result and operand types are
273/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
274/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000275SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
276 MVT::ValueType DestVT,
277 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000278 // First step, figure out the appropriate *INT_TO_FP operation to use.
279 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000280
Chris Lattnere3e847b2005-07-16 00:19:57 +0000281 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000282
Chris Lattnere3e847b2005-07-16 00:19:57 +0000283 // Scan for the appropriate larger type to use.
284 while (1) {
285 NewInTy = (MVT::ValueType)(NewInTy+1);
286 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-07-16 00:19:57 +0000288 // If the target supports SINT_TO_FP of this type, use it.
289 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
290 default: break;
291 case TargetLowering::Legal:
292 if (!TLI.hasNativeSupportFor(NewInTy))
293 break; // Can't use this datatype.
294 // FALL THROUGH.
295 case TargetLowering::Custom:
296 OpToUse = ISD::SINT_TO_FP;
297 break;
298 }
299 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000300 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000301
Chris Lattnere3e847b2005-07-16 00:19:57 +0000302 // If the target supports UINT_TO_FP of this type, use it.
303 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
304 default: break;
305 case TargetLowering::Legal:
306 if (!TLI.hasNativeSupportFor(NewInTy))
307 break; // Can't use this datatype.
308 // FALL THROUGH.
309 case TargetLowering::Custom:
310 OpToUse = ISD::UINT_TO_FP;
311 break;
312 }
313 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000314
Chris Lattnere3e847b2005-07-16 00:19:57 +0000315 // Otherwise, try a larger type.
316 }
317
318 // Make sure to legalize any nodes we create here in the next pass.
319 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000320
Chris Lattnere3e847b2005-07-16 00:19:57 +0000321 // Okay, we found the operation and type to use. Zero extend our input to the
322 // desired type then run the operation on it.
323 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000324 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
325 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000326}
327
Chris Lattner44fe26f2005-07-29 00:11:56 +0000328/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
329/// FP_TO_*INT operation of the specified operand when the target requests that
330/// we promote it. At this point, we know that the result and operand types are
331/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
332/// operation that returns a larger result.
333SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
334 MVT::ValueType DestVT,
335 bool isSigned) {
336 // First step, figure out the appropriate FP_TO*INT operation to use.
337 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000338
Chris Lattner44fe26f2005-07-29 00:11:56 +0000339 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000340
Chris Lattner44fe26f2005-07-29 00:11:56 +0000341 // Scan for the appropriate larger type to use.
342 while (1) {
343 NewOutTy = (MVT::ValueType)(NewOutTy+1);
344 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-07-29 00:11:56 +0000346 // If the target supports FP_TO_SINT returning this type, use it.
347 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
348 default: break;
349 case TargetLowering::Legal:
350 if (!TLI.hasNativeSupportFor(NewOutTy))
351 break; // Can't use this datatype.
352 // FALL THROUGH.
353 case TargetLowering::Custom:
354 OpToUse = ISD::FP_TO_SINT;
355 break;
356 }
357 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000358
Chris Lattner44fe26f2005-07-29 00:11:56 +0000359 // If the target supports FP_TO_UINT of this type, use it.
360 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
361 default: break;
362 case TargetLowering::Legal:
363 if (!TLI.hasNativeSupportFor(NewOutTy))
364 break; // Can't use this datatype.
365 // FALL THROUGH.
366 case TargetLowering::Custom:
367 OpToUse = ISD::FP_TO_UINT;
368 break;
369 }
370 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000371
Chris Lattner44fe26f2005-07-29 00:11:56 +0000372 // Otherwise, try a larger type.
373 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000374
Chris Lattner44fe26f2005-07-29 00:11:56 +0000375 // Make sure to legalize any nodes we create here in the next pass.
376 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000377
Chris Lattner44fe26f2005-07-29 00:11:56 +0000378 // Okay, we found the operation and type to use. Truncate the result of the
379 // extended FP_TO_*INT operation to the desired size.
380 return DAG.getNode(ISD::TRUNCATE, DestVT,
381 DAG.getNode(OpToUse, NewOutTy, LegalOp));
382}
383
384
Chris Lattnerdc750592005-01-07 07:47:09 +0000385void SelectionDAGLegalize::LegalizeDAG() {
386 SDOperand OldRoot = DAG.getRoot();
387 SDOperand NewRoot = LegalizeOp(OldRoot);
388 DAG.setRoot(NewRoot);
389
390 ExpandedNodes.clear();
391 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000392 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000393
394 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000395 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000396}
397
398SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000399 assert(getTypeAction(Op.getValueType()) == Legal &&
400 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000401 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000402
Chris Lattnerdc750592005-01-07 07:47:09 +0000403 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000404 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000405 if (Node->getNumValues() > 1) {
406 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
407 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000408 case Legal: break; // Nothing to do.
409 case Expand: {
410 SDOperand T1, T2;
411 ExpandOp(Op.getValue(i), T1, T2);
412 assert(LegalizedNodes.count(Op) &&
413 "Expansion didn't add legal operands!");
414 return LegalizedNodes[Op];
415 }
416 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000417 PromoteOp(Op.getValue(i));
418 assert(LegalizedNodes.count(Op) &&
419 "Expansion didn't add legal operands!");
420 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000421 }
422 }
423
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000424 // Note that LegalizeOp may be reentered even from single-use nodes, which
425 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000426 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
427 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000428
Nate Begemane5b86d72005-08-10 20:51:12 +0000429 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000430
431 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000432
433 switch (Node->getOpcode()) {
434 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000435 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
436 // If this is a target node, legalize it by legalizing the operands then
437 // passing it through.
438 std::vector<SDOperand> Ops;
439 bool Changed = false;
440 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
441 Ops.push_back(LegalizeOp(Node->getOperand(i)));
442 Changed = Changed || Node->getOperand(i) != Ops.back();
443 }
444 if (Changed)
445 if (Node->getNumValues() == 1)
446 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
447 else {
448 std::vector<MVT::ValueType> VTs(Node->value_begin(),
449 Node->value_end());
450 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
451 }
452
453 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
454 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
455 return Result.getValue(Op.ResNo);
456 }
457 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000458 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
459 assert(0 && "Do not know how to legalize this operator!");
460 abort();
461 case ISD::EntryToken:
462 case ISD::FrameIndex:
463 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000464 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000465 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000466 assert(getTypeAction(Node->getValueType(0)) == Legal &&
467 "This must be legal!");
468 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000469 case ISD::CopyFromReg:
470 Tmp1 = LegalizeOp(Node->getOperand(0));
471 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000472 Result = DAG.getCopyFromReg(Tmp1,
473 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
474 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000475 else
476 Result = Op.getValue(0);
477
478 // Since CopyFromReg produces two values, make sure to remember that we
479 // legalized both of them.
480 AddLegalizedOperand(Op.getValue(0), Result);
481 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
482 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000483 case ISD::ImplicitDef:
484 Tmp1 = LegalizeOp(Node->getOperand(0));
485 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000486 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
487 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000488 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000489 case ISD::UNDEF: {
490 MVT::ValueType VT = Op.getValueType();
491 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000492 default: assert(0 && "This action is not supported yet!");
493 case TargetLowering::Expand:
494 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000495 if (MVT::isInteger(VT))
496 Result = DAG.getConstant(0, VT);
497 else if (MVT::isFloatingPoint(VT))
498 Result = DAG.getConstantFP(0, VT);
499 else
500 assert(0 && "Unknown value type!");
501 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000502 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000503 break;
504 }
505 break;
506 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000507 case ISD::Constant:
508 // We know we don't need to expand constants here, constants only have one
509 // value and we check that it is fine above.
510
511 // FIXME: Maybe we should handle things like targets that don't support full
512 // 32-bit immediates?
513 break;
514 case ISD::ConstantFP: {
515 // Spill FP immediates to the constant pool if the target cannot directly
516 // codegen them. Targets often have some immediate values that can be
517 // efficiently generated into an FP register without a load. We explicitly
518 // leave these constants as ConstantFP nodes for the target to deal with.
519
520 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
521
522 // Check to see if this FP immediate is already legal.
523 bool isLegal = false;
524 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
525 E = TLI.legal_fpimm_end(); I != E; ++I)
526 if (CFP->isExactlyValue(*I)) {
527 isLegal = true;
528 break;
529 }
530
531 if (!isLegal) {
532 // Otherwise we need to spill the constant to memory.
533 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
534
535 bool Extend = false;
536
537 // If a FP immediate is precise when represented as a float, we put it
538 // into the constant pool as a float, even if it's is statically typed
539 // as a double.
540 MVT::ValueType VT = CFP->getValueType(0);
541 bool isDouble = VT == MVT::f64;
542 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
543 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000544 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
545 // Only do this if the target has a native EXTLOAD instruction from
546 // f32.
547 TLI.getOperationAction(ISD::EXTLOAD,
548 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000549 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
550 VT = MVT::f32;
551 Extend = true;
552 }
Misha Brukman835702a2005-04-21 22:36:52 +0000553
Chris Lattnerdc750592005-01-07 07:47:09 +0000554 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
555 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000556 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000557 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
558 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000559 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000560 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
561 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000562 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000563 }
564 break;
565 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000566 case ISD::TokenFactor: {
567 std::vector<SDOperand> Ops;
568 bool Changed = false;
569 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000570 SDOperand Op = Node->getOperand(i);
571 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000572 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000573 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
574 Changed = true;
575 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
576 Ops.push_back(LegalizeOp(Op.getOperand(j)));
577 } else {
578 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
579 Changed |= Ops[i] != Op;
580 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000581 }
582 if (Changed)
583 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
584 break;
585 }
586
Chris Lattner2dce7032005-05-12 23:24:06 +0000587 case ISD::CALLSEQ_START:
588 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000590 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000591 Tmp2 = Node->getOperand(0);
592 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000593 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000594
595 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
596 // this will cause the maps used to memoize results to get confused.
597 // Create and add a dummy use, just to increase its use count. This will
598 // be removed at the end of legalize when dead nodes are removed.
599 if (Tmp2.Val->hasOneUse())
600 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
601 DAG.getConstant(0, MVT::i32));
602 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000603 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000604 // nodes are treated specially and are mutated in place. This makes the dag
605 // legalization process more efficient and also makes libcall insertion
606 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000607 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000608 case ISD::DYNAMIC_STACKALLOC:
609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
610 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
611 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
612 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000613 Tmp3 != Node->getOperand(2)) {
614 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
615 std::vector<SDOperand> Ops;
616 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
617 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
618 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000619 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000620
621 // Since this op produces two values, make sure to remember that we
622 // legalized both of them.
623 AddLegalizedOperand(SDOperand(Node, 0), Result);
624 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
625 return Result.getValue(Op.ResNo);
626
Chris Lattnerd0feb642005-05-13 18:43:43 +0000627 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000628 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000631
632 bool Changed = false;
633 std::vector<SDOperand> Ops;
634 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
635 Ops.push_back(LegalizeOp(Node->getOperand(i)));
636 Changed |= Ops.back() != Node->getOperand(i);
637 }
638
639 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000640 std::vector<MVT::ValueType> RetTyVTs;
641 RetTyVTs.reserve(Node->getNumValues());
642 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000643 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000644 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
645 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000646 } else {
647 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000648 }
Chris Lattner9242c502005-01-09 19:43:23 +0000649 // Since calls produce multiple values, make sure to remember that we
650 // legalized all of them.
651 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
652 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
653 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000654 }
Chris Lattner68a12142005-01-07 22:12:08 +0000655 case ISD::BR:
656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
657 if (Tmp1 != Node->getOperand(0))
658 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
659 break;
660
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000661 case ISD::BRCOND:
662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000663
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000664 switch (getTypeAction(Node->getOperand(1).getValueType())) {
665 case Expand: assert(0 && "It's impossible to expand bools");
666 case Legal:
667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
668 break;
669 case Promote:
670 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
671 break;
672 }
Nate Begeman371e4952005-08-16 19:49:35 +0000673
674 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
675 default: assert(0 && "This action is not supported yet!");
676 case TargetLowering::Expand:
677 // Expand brcond's setcc into its constituent parts and create a BR_CC
678 // Node.
679 if (Tmp2.getOpcode() == ISD::SETCC) {
680 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
681 Tmp2.getOperand(0), Tmp2.getOperand(1),
682 Node->getOperand(2));
683 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000684 // Make sure the condition is either zero or one. It may have been
685 // promoted from something else.
686 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
687
Nate Begeman371e4952005-08-16 19:49:35 +0000688 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
689 DAG.getCondCode(ISD::SETNE), Tmp2,
690 DAG.getConstant(0, Tmp2.getValueType()),
691 Node->getOperand(2));
692 }
693 break;
694 case TargetLowering::Legal:
695 // Basic block destination (Op#2) is always legal.
696 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
697 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
698 Node->getOperand(2));
699 break;
700 }
701 break;
702 case ISD::BR_CC:
703 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
704
705 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
706 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
707 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
708 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
709 Tmp3 != Node->getOperand(3)) {
710 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
711 Tmp2, Tmp3, Node->getOperand(4));
712 }
713 break;
714 } else {
715 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
716 Node->getOperand(2), // LHS
717 Node->getOperand(3), // RHS
718 Node->getOperand(1)));
719 // If we get a SETCC back from legalizing the SETCC node we just
720 // created, then use its LHS, RHS, and CC directly in creating a new
721 // node. Otherwise, select between the true and false value based on
722 // comparing the result of the legalized with zero.
723 if (Tmp2.getOpcode() == ISD::SETCC) {
724 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
725 Tmp2.getOperand(0), Tmp2.getOperand(1),
726 Node->getOperand(4));
727 } else {
728 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
729 DAG.getCondCode(ISD::SETNE),
730 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
731 Node->getOperand(4));
732 }
733 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000734 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000735 case ISD::BRCONDTWOWAY:
736 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
737 switch (getTypeAction(Node->getOperand(1).getValueType())) {
738 case Expand: assert(0 && "It's impossible to expand bools");
739 case Legal:
740 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
741 break;
742 case Promote:
743 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
744 break;
745 }
746 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
747 // pair.
748 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
749 case TargetLowering::Promote:
750 default: assert(0 && "This action is not supported yet!");
751 case TargetLowering::Legal:
752 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
753 std::vector<SDOperand> Ops;
754 Ops.push_back(Tmp1);
755 Ops.push_back(Tmp2);
756 Ops.push_back(Node->getOperand(2));
757 Ops.push_back(Node->getOperand(3));
758 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
759 }
760 break;
761 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000762 // If BRTWOWAY_CC is legal for this target, then simply expand this node
763 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
764 // BRCOND/BR pair.
765 if (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other) ==
766 TargetLowering::Legal) {
767 if (Tmp2.getOpcode() == ISD::SETCC) {
768 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
769 Tmp2.getOperand(0), Tmp2.getOperand(1),
770 Node->getOperand(2), Node->getOperand(3));
771 } else {
772 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
773 DAG.getConstant(0, Tmp2.getValueType()),
774 Node->getOperand(2), Node->getOperand(3));
775 }
776 } else {
777 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000778 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000779 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
780 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000781 break;
782 }
783 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000784 case ISD::BRTWOWAY_CC:
785 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
786 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
787 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
788 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
789 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
790 Tmp3 != Node->getOperand(3)) {
791 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
792 Node->getOperand(4), Node->getOperand(5));
793 }
794 break;
795 } else {
796 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
797 Node->getOperand(2), // LHS
798 Node->getOperand(3), // RHS
799 Node->getOperand(1)));
800 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
801 // pair.
802 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
803 default: assert(0 && "This action is not supported yet!");
804 case TargetLowering::Legal:
805 // If we get a SETCC back from legalizing the SETCC node we just
806 // created, then use its LHS, RHS, and CC directly in creating a new
807 // node. Otherwise, select between the true and false value based on
808 // comparing the result of the legalized with zero.
809 if (Tmp2.getOpcode() == ISD::SETCC) {
810 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
811 Tmp2.getOperand(0), Tmp2.getOperand(1),
812 Node->getOperand(4), Node->getOperand(5));
813 } else {
814 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
815 DAG.getConstant(0, Tmp2.getValueType()),
816 Node->getOperand(4), Node->getOperand(5));
817 }
818 break;
819 case TargetLowering::Expand:
820 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
821 Node->getOperand(4));
822 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
823 break;
824 }
825 }
826 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000827 case ISD::LOAD:
828 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
829 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000830
Chris Lattnerdc750592005-01-07 07:47:09 +0000831 if (Tmp1 != Node->getOperand(0) ||
832 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000833 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
834 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000835 else
836 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000837
Chris Lattnerea4ca942005-01-07 22:28:47 +0000838 // Since loads produce two values, make sure to remember that we legalized
839 // both of them.
840 AddLegalizedOperand(SDOperand(Node, 0), Result);
841 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
842 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000843
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000844 case ISD::EXTLOAD:
845 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000846 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
848 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000849
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000850 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000851 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000852 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000853 case TargetLowering::Promote:
854 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000855 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
856 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000857 // Since loads produce two values, make sure to remember that we legalized
858 // both of them.
859 AddLegalizedOperand(SDOperand(Node, 0), Result);
860 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
861 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000862
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000863 case TargetLowering::Legal:
864 if (Tmp1 != Node->getOperand(0) ||
865 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000866 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
867 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000868 else
869 Result = SDOperand(Node, 0);
870
871 // Since loads produce two values, make sure to remember that we legalized
872 // both of them.
873 AddLegalizedOperand(SDOperand(Node, 0), Result);
874 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
875 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000876 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000877 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
878 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
879 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000880 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000881 if (Op.ResNo)
882 return Load.getValue(1);
883 return Result;
884 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000885 assert(Node->getOpcode() != ISD::EXTLOAD &&
886 "EXTLOAD should always be supported!");
887 // Turn the unsupported load into an EXTLOAD followed by an explicit
888 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000889 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
890 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000891 SDOperand ValRes;
892 if (Node->getOpcode() == ISD::SEXTLOAD)
893 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000894 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000895 else
896 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000897 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
898 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
899 if (Op.ResNo)
900 return Result.getValue(1);
901 return ValRes;
902 }
903 assert(0 && "Unreachable");
904 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000905 case ISD::EXTRACT_ELEMENT:
906 // Get both the low and high parts.
907 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
908 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
909 Result = Tmp2; // 1 -> Hi
910 else
911 Result = Tmp1; // 0 -> Lo
912 break;
913
914 case ISD::CopyToReg:
915 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000916
Chris Lattner33182322005-08-16 21:55:35 +0000917 assert(getTypeAction(Node->getOperand(2).getValueType()) == Legal &&
918 "Register type must be legal!");
919 // Legalize the incoming value (must be legal).
920 Tmp2 = LegalizeOp(Node->getOperand(2));
921 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
922 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
923 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000924 break;
925
926 case ISD::RET:
927 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
928 switch (Node->getNumOperands()) {
929 case 2: // ret val
930 switch (getTypeAction(Node->getOperand(1).getValueType())) {
931 case Legal:
932 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000933 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000934 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
935 break;
936 case Expand: {
937 SDOperand Lo, Hi;
938 ExpandOp(Node->getOperand(1), Lo, Hi);
939 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000940 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000941 }
942 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000943 Tmp2 = PromoteOp(Node->getOperand(1));
944 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
945 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000946 }
947 break;
948 case 1: // ret void
949 if (Tmp1 != Node->getOperand(0))
950 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
951 break;
952 default: { // ret <values>
953 std::vector<SDOperand> NewValues;
954 NewValues.push_back(Tmp1);
955 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
956 switch (getTypeAction(Node->getOperand(i).getValueType())) {
957 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000958 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000959 break;
960 case Expand: {
961 SDOperand Lo, Hi;
962 ExpandOp(Node->getOperand(i), Lo, Hi);
963 NewValues.push_back(Lo);
964 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000965 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000966 }
967 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000968 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000969 }
970 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
971 break;
972 }
973 }
974 break;
975 case ISD::STORE:
976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
977 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
978
Chris Lattnere69daaf2005-01-08 06:25:56 +0000979 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000980 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000981 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000982 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000983 DAG.getConstant(FloatToBits(CFP->getValue()),
984 MVT::i32),
985 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000986 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000987 } else {
988 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000989 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +0000990 DAG.getConstant(DoubleToBits(CFP->getValue()),
991 MVT::i64),
992 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000993 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000994 }
Chris Lattnera4743132005-02-22 07:23:39 +0000995 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000996 }
997
Chris Lattnerdc750592005-01-07 07:47:09 +0000998 switch (getTypeAction(Node->getOperand(1).getValueType())) {
999 case Legal: {
1000 SDOperand Val = LegalizeOp(Node->getOperand(1));
1001 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1002 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001003 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1004 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001005 break;
1006 }
1007 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001008 // Truncate the value and store the result.
1009 Tmp3 = PromoteOp(Node->getOperand(1));
1010 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001011 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001012 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001013 break;
1014
Chris Lattnerdc750592005-01-07 07:47:09 +00001015 case Expand:
1016 SDOperand Lo, Hi;
1017 ExpandOp(Node->getOperand(1), Lo, Hi);
1018
1019 if (!TLI.isLittleEndian())
1020 std::swap(Lo, Hi);
1021
Chris Lattner55e9cde2005-05-11 04:51:16 +00001022 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1023 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001024 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001025 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1026 getIntPtrConstant(IncrementSize));
1027 assert(isTypeLegal(Tmp2.getValueType()) &&
1028 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001029 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001030 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1031 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001032 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1033 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001034 }
1035 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001036 case ISD::PCMARKER:
1037 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001038 if (Tmp1 != Node->getOperand(0))
1039 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001040 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001041 case ISD::TRUNCSTORE:
1042 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1043 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1044
1045 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1046 case Legal:
1047 Tmp2 = LegalizeOp(Node->getOperand(1));
1048 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1049 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +00001050 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001051 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001052 break;
1053 case Promote:
1054 case Expand:
1055 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1056 }
1057 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001058 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1060 case Expand: assert(0 && "It's impossible to expand bools");
1061 case Legal:
1062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1063 break;
1064 case Promote:
1065 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1066 break;
1067 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001068 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001069 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001070
Nate Begeman987121a2005-08-23 04:29:48 +00001071 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001072 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001073 case TargetLowering::Expand:
1074 if (Tmp1.getOpcode() == ISD::SETCC) {
1075 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1076 Tmp2, Tmp3,
1077 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1078 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001079 // Make sure the condition is either zero or one. It may have been
1080 // promoted from something else.
1081 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001082 Result = DAG.getSelectCC(Tmp1,
1083 DAG.getConstant(0, Tmp1.getValueType()),
1084 Tmp2, Tmp3, ISD::SETNE);
1085 }
1086 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001087 case TargetLowering::Legal:
1088 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1089 Tmp3 != Node->getOperand(2))
1090 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1091 Tmp1, Tmp2, Tmp3);
1092 break;
1093 case TargetLowering::Promote: {
1094 MVT::ValueType NVT =
1095 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1096 unsigned ExtOp, TruncOp;
1097 if (MVT::isInteger(Tmp2.getValueType())) {
1098 ExtOp = ISD::ZERO_EXTEND;
1099 TruncOp = ISD::TRUNCATE;
1100 } else {
1101 ExtOp = ISD::FP_EXTEND;
1102 TruncOp = ISD::FP_ROUND;
1103 }
1104 // Promote each of the values to the new type.
1105 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1106 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1107 // Perform the larger operation, then round down.
1108 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1109 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1110 break;
1111 }
1112 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001113 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001114 case ISD::SELECT_CC:
1115 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1116 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1117
1118 if (getTypeAction(Node->getOperand(0).getValueType()) == Legal) {
1119 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1120 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1121 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1122 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1123 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1124 Tmp3, Tmp4, Node->getOperand(4));
1125 }
1126 break;
1127 } else {
1128 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1129 Node->getOperand(0), // LHS
1130 Node->getOperand(1), // RHS
1131 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001132 // If we get a SETCC back from legalizing the SETCC node we just
1133 // created, then use its LHS, RHS, and CC directly in creating a new
1134 // node. Otherwise, select between the true and false value based on
1135 // comparing the result of the legalized with zero.
1136 if (Tmp1.getOpcode() == ISD::SETCC) {
1137 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1138 Tmp1.getOperand(0), Tmp1.getOperand(1),
1139 Tmp3, Tmp4, Tmp1.getOperand(2));
1140 } else {
1141 Result = DAG.getSelectCC(Tmp1,
1142 DAG.getConstant(0, Tmp1.getValueType()),
1143 Tmp3, Tmp4, ISD::SETNE);
1144 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001145 }
1146 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001147 case ISD::SETCC:
1148 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1149 case Legal:
1150 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1151 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001152 break;
1153 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001154 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1155 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1156
1157 // If this is an FP compare, the operands have already been extended.
1158 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1159 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001160 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001161
1162 // Otherwise, we have to insert explicit sign or zero extends. Note
1163 // that we could insert sign extends for ALL conditions, but zero extend
1164 // is cheaper on many machines (an AND instead of two shifts), so prefer
1165 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001166 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001167 default: assert(0 && "Unknown integer comparison!");
1168 case ISD::SETEQ:
1169 case ISD::SETNE:
1170 case ISD::SETUGE:
1171 case ISD::SETUGT:
1172 case ISD::SETULE:
1173 case ISD::SETULT:
1174 // ALL of these operations will work if we either sign or zero extend
1175 // the operands (including the unsigned comparisons!). Zero extend is
1176 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001177 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1178 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001179 break;
1180 case ISD::SETGE:
1181 case ISD::SETGT:
1182 case ISD::SETLT:
1183 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001184 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1185 DAG.getValueType(VT));
1186 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1187 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001188 break;
1189 }
Chris Lattner4d978642005-01-15 22:16:26 +00001190 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001191 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001192 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001193 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1194 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1195 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001196 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001197 case ISD::SETEQ:
1198 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001199 if (RHSLo == RHSHi)
1200 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1201 if (RHSCST->isAllOnesValue()) {
1202 // Comparison to -1.
1203 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001204 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001205 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001206 }
1207
Chris Lattnerdc750592005-01-07 07:47:09 +00001208 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1209 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1210 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001211 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001212 break;
1213 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001214 // If this is a comparison of the sign bit, just look at the top part.
1215 // X > -1, x < 0
1216 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001217 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001218 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001219 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001220 (CST->isAllOnesValue()))) { // X > -1
1221 Tmp1 = LHSHi;
1222 Tmp2 = RHSHi;
1223 break;
1224 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001225
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 // FIXME: This generated code sucks.
1227 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001228 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001229 default: assert(0 && "Unknown integer setcc!");
1230 case ISD::SETLT:
1231 case ISD::SETULT: LowCC = ISD::SETULT; break;
1232 case ISD::SETGT:
1233 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1234 case ISD::SETLE:
1235 case ISD::SETULE: LowCC = ISD::SETULE; break;
1236 case ISD::SETGE:
1237 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1238 }
Misha Brukman835702a2005-04-21 22:36:52 +00001239
Chris Lattnerdc750592005-01-07 07:47:09 +00001240 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1241 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1242 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1243
1244 // NOTE: on targets without efficient SELECT of bools, we can always use
1245 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001246 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1247 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1248 Node->getOperand(2));
1249 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001250 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1251 Result, Tmp1, Tmp2));
1252 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001253 }
1254 }
Nate Begeman987121a2005-08-23 04:29:48 +00001255
1256 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1257 default:
1258 assert(0 && "Cannot handle this action for SETCC yet!");
1259 break;
1260 case TargetLowering::Legal:
1261 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1262 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1263 Node->getOperand(2));
1264 break;
1265 case TargetLowering::Expand:
1266 // Expand a setcc node into a select_cc of the same condition, lhs, and
1267 // rhs that selects between const 1 (true) and const 0 (false).
1268 MVT::ValueType VT = Node->getValueType(0);
1269 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1270 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1271 Node->getOperand(2));
1272 Result = LegalizeOp(Result);
1273 break;
1274 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001275 break;
1276
Chris Lattner85d70c62005-01-11 05:57:22 +00001277 case ISD::MEMSET:
1278 case ISD::MEMCPY:
1279 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001280 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001281 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1282
1283 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1284 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1285 case Expand: assert(0 && "Cannot expand a byte!");
1286 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001287 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001288 break;
1289 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001290 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001291 break;
1292 }
1293 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001294 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001295 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001296
1297 SDOperand Tmp4;
1298 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001299 case Expand: {
1300 // Length is too big, just take the lo-part of the length.
1301 SDOperand HiPart;
1302 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1303 break;
1304 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001305 case Legal:
1306 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001307 break;
1308 case Promote:
1309 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001310 break;
1311 }
1312
1313 SDOperand Tmp5;
1314 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1315 case Expand: assert(0 && "Cannot expand this yet!");
1316 case Legal:
1317 Tmp5 = LegalizeOp(Node->getOperand(4));
1318 break;
1319 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001320 Tmp5 = PromoteOp(Node->getOperand(4));
1321 break;
1322 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001323
1324 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1325 default: assert(0 && "This action not implemented for this operation!");
1326 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001327 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1328 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1329 Tmp5 != Node->getOperand(4)) {
1330 std::vector<SDOperand> Ops;
1331 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1332 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1333 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1334 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001335 break;
1336 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001337 // Otherwise, the target does not support this operation. Lower the
1338 // operation to an explicit libcall as appropriate.
1339 MVT::ValueType IntPtr = TLI.getPointerTy();
1340 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1341 std::vector<std::pair<SDOperand, const Type*> > Args;
1342
Reid Spencer6dced922005-01-12 14:53:45 +00001343 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001344 if (Node->getOpcode() == ISD::MEMSET) {
1345 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1346 // Extend the ubyte argument to be an int value for the call.
1347 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1348 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1349 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1350
1351 FnName = "memset";
1352 } else if (Node->getOpcode() == ISD::MEMCPY ||
1353 Node->getOpcode() == ISD::MEMMOVE) {
1354 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1355 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1356 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1357 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1358 } else {
1359 assert(0 && "Unknown op!");
1360 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001361
Chris Lattner85d70c62005-01-11 05:57:22 +00001362 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001363 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001364 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001365 Result = CallResult.second;
1366 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001367 break;
1368 }
1369 case TargetLowering::Custom:
1370 std::vector<SDOperand> Ops;
1371 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1372 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1373 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001374 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001375 Result = LegalizeOp(Result);
1376 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001377 }
1378 break;
1379 }
Chris Lattner5385db52005-05-09 20:23:03 +00001380
1381 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001382 Tmp1 = LegalizeOp(Node->getOperand(0));
1383 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001384
Chris Lattner86535992005-05-14 07:45:46 +00001385 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1386 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1387 std::vector<SDOperand> Ops;
1388 Ops.push_back(Tmp1);
1389 Ops.push_back(Tmp2);
1390 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1391 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001392 Result = SDOperand(Node, 0);
1393 // Since these produce two values, make sure to remember that we legalized
1394 // both of them.
1395 AddLegalizedOperand(SDOperand(Node, 0), Result);
1396 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1397 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001398 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001399 Tmp1 = LegalizeOp(Node->getOperand(0));
1400 Tmp2 = LegalizeOp(Node->getOperand(1));
1401 Tmp3 = LegalizeOp(Node->getOperand(2));
1402 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1403 Tmp3 != Node->getOperand(2))
1404 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1405 break;
1406
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001407 case ISD::READIO:
1408 Tmp1 = LegalizeOp(Node->getOperand(0));
1409 Tmp2 = LegalizeOp(Node->getOperand(1));
1410
1411 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1412 case TargetLowering::Custom:
1413 default: assert(0 && "This action not implemented for this operation!");
1414 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001415 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1416 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1417 std::vector<SDOperand> Ops;
1418 Ops.push_back(Tmp1);
1419 Ops.push_back(Tmp2);
1420 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1421 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001422 Result = SDOperand(Node, 0);
1423 break;
1424 case TargetLowering::Expand:
1425 // Replace this with a load from memory.
1426 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1427 Node->getOperand(1), DAG.getSrcValue(NULL));
1428 Result = LegalizeOp(Result);
1429 break;
1430 }
1431
1432 // Since these produce two values, make sure to remember that we legalized
1433 // both of them.
1434 AddLegalizedOperand(SDOperand(Node, 0), Result);
1435 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1436 return Result.getValue(Op.ResNo);
1437
1438 case ISD::WRITEIO:
1439 Tmp1 = LegalizeOp(Node->getOperand(0));
1440 Tmp2 = LegalizeOp(Node->getOperand(1));
1441 Tmp3 = LegalizeOp(Node->getOperand(2));
1442
1443 switch (TLI.getOperationAction(Node->getOpcode(),
1444 Node->getOperand(1).getValueType())) {
1445 case TargetLowering::Custom:
1446 default: assert(0 && "This action not implemented for this operation!");
1447 case TargetLowering::Legal:
1448 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1449 Tmp3 != Node->getOperand(2))
1450 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1451 break;
1452 case TargetLowering::Expand:
1453 // Replace this with a store to memory.
1454 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1455 Node->getOperand(1), Node->getOperand(2),
1456 DAG.getSrcValue(NULL));
1457 Result = LegalizeOp(Result);
1458 break;
1459 }
1460 break;
1461
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001462 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001463 case ISD::SUB_PARTS:
1464 case ISD::SHL_PARTS:
1465 case ISD::SRA_PARTS:
1466 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001467 std::vector<SDOperand> Ops;
1468 bool Changed = false;
1469 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1470 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1471 Changed |= Ops.back() != Node->getOperand(i);
1472 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001473 if (Changed) {
1474 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1475 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1476 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001477
1478 // Since these produce multiple values, make sure to remember that we
1479 // legalized all of them.
1480 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1481 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1482 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001483 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001484
1485 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001486 case ISD::ADD:
1487 case ISD::SUB:
1488 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001489 case ISD::MULHS:
1490 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001491 case ISD::UDIV:
1492 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001493 case ISD::AND:
1494 case ISD::OR:
1495 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001496 case ISD::SHL:
1497 case ISD::SRL:
1498 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001499 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001500 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1501 case Expand: assert(0 && "Not possible");
1502 case Legal:
1503 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1504 break;
1505 case Promote:
1506 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1507 break;
1508 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001509 if (Tmp1 != Node->getOperand(0) ||
1510 Tmp2 != Node->getOperand(1))
1511 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1512 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001513
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001514 case ISD::UREM:
1515 case ISD::SREM:
1516 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1517 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1518 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1519 case TargetLowering::Legal:
1520 if (Tmp1 != Node->getOperand(0) ||
1521 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001522 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001523 Tmp2);
1524 break;
1525 case TargetLowering::Promote:
1526 case TargetLowering::Custom:
1527 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001528 case TargetLowering::Expand:
1529 if (MVT::isInteger(Node->getValueType(0))) {
1530 MVT::ValueType VT = Node->getValueType(0);
1531 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1532 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1533 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1534 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1535 } else {
1536 // Floating point mod -> fmod libcall.
1537 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1538 SDOperand Dummy;
1539 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001540 }
1541 break;
1542 }
1543 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001544
Andrew Lenharth5e177822005-05-03 17:19:30 +00001545 case ISD::CTPOP:
1546 case ISD::CTTZ:
1547 case ISD::CTLZ:
1548 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1549 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1550 case TargetLowering::Legal:
1551 if (Tmp1 != Node->getOperand(0))
1552 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1553 break;
1554 case TargetLowering::Promote: {
1555 MVT::ValueType OVT = Tmp1.getValueType();
1556 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001557
1558 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001559 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1560 // Perform the larger operation, then subtract if needed.
1561 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1562 switch(Node->getOpcode())
1563 {
1564 case ISD::CTPOP:
1565 Result = Tmp1;
1566 break;
1567 case ISD::CTTZ:
1568 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001569 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1570 DAG.getConstant(getSizeInBits(NVT), NVT),
1571 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001572 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001573 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1574 break;
1575 case ISD::CTLZ:
1576 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001577 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1578 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001579 getSizeInBits(OVT), NVT));
1580 break;
1581 }
1582 break;
1583 }
1584 case TargetLowering::Custom:
1585 assert(0 && "Cannot custom handle this yet!");
1586 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001587 switch(Node->getOpcode())
1588 {
1589 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001590 static const uint64_t mask[6] = {
1591 0x5555555555555555ULL, 0x3333333333333333ULL,
1592 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1593 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1594 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001595 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001596 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1597 unsigned len = getSizeInBits(VT);
1598 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001599 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001600 Tmp2 = DAG.getConstant(mask[i], VT);
1601 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001602 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001603 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1604 DAG.getNode(ISD::AND, VT,
1605 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1606 Tmp2));
1607 }
1608 Result = Tmp1;
1609 break;
1610 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001611 case ISD::CTLZ: {
1612 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001613 x = x | (x >> 1);
1614 x = x | (x >> 2);
1615 ...
1616 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001617 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001618 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001619
Chris Lattner56add052005-05-11 18:35:21 +00001620 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1621 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001622 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1623 unsigned len = getSizeInBits(VT);
1624 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1625 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001626 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001627 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1628 }
1629 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001630 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001631 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001632 }
1633 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001634 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001635 // unless the target has ctlz but not ctpop, in which case we use:
1636 // { return 32 - nlz(~x & (x-1)); }
1637 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001638 MVT::ValueType VT = Tmp1.getValueType();
1639 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001640 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001641 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1642 DAG.getNode(ISD::SUB, VT, Tmp1,
1643 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001644 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1645 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1646 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001647 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001648 DAG.getConstant(getSizeInBits(VT), VT),
1649 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1650 } else {
1651 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1652 }
Chris Lattner72473242005-05-11 05:27:09 +00001653 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001654 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001655 default:
1656 assert(0 && "Cannot expand this yet!");
1657 break;
1658 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001659 break;
1660 }
1661 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001662
Chris Lattner13fe99c2005-04-02 05:00:07 +00001663 // Unary operators
1664 case ISD::FABS:
1665 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001666 case ISD::FSQRT:
1667 case ISD::FSIN:
1668 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001669 Tmp1 = LegalizeOp(Node->getOperand(0));
1670 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1671 case TargetLowering::Legal:
1672 if (Tmp1 != Node->getOperand(0))
1673 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1674 break;
1675 case TargetLowering::Promote:
1676 case TargetLowering::Custom:
1677 assert(0 && "Cannot promote/custom handle this yet!");
1678 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001679 switch(Node->getOpcode()) {
1680 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001681 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1682 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1683 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1684 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001685 break;
1686 }
1687 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001688 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1689 MVT::ValueType VT = Node->getValueType(0);
1690 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001691 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001692 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1693 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1694 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001695 break;
1696 }
1697 case ISD::FSQRT:
1698 case ISD::FSIN:
1699 case ISD::FCOS: {
1700 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001701 const char *FnName = 0;
1702 switch(Node->getOpcode()) {
1703 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1704 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1705 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1706 default: assert(0 && "Unreachable!");
1707 }
Nate Begeman77558da2005-08-04 21:43:28 +00001708 SDOperand Dummy;
1709 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001710 break;
1711 }
1712 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001713 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001714 }
1715 break;
1716 }
1717 break;
1718
1719 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001720 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001721 case ISD::UINT_TO_FP: {
1722 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001723 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1724 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001725 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001726 Node->getOperand(0).getValueType())) {
1727 default: assert(0 && "Unknown operation action!");
1728 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001729 Result = ExpandLegalINT_TO_FP(isSigned,
1730 LegalizeOp(Node->getOperand(0)),
1731 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001732 AddLegalizedOperand(Op, Result);
1733 return Result;
1734 case TargetLowering::Promote:
1735 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1736 Node->getValueType(0),
1737 isSigned);
1738 AddLegalizedOperand(Op, Result);
1739 return Result;
1740 case TargetLowering::Legal:
1741 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001742 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001743
Chris Lattnerdc750592005-01-07 07:47:09 +00001744 Tmp1 = LegalizeOp(Node->getOperand(0));
1745 if (Tmp1 != Node->getOperand(0))
1746 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1747 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001748 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001749 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1750 Node->getValueType(0), Node->getOperand(0));
1751 break;
1752 case Promote:
1753 if (isSigned) {
1754 Result = PromoteOp(Node->getOperand(0));
1755 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1756 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1757 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1758 } else {
1759 Result = PromoteOp(Node->getOperand(0));
1760 Result = DAG.getZeroExtendInReg(Result,
1761 Node->getOperand(0).getValueType());
1762 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001763 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001764 break;
1765 }
1766 break;
1767 }
1768 case ISD::TRUNCATE:
1769 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1770 case Legal:
1771 Tmp1 = LegalizeOp(Node->getOperand(0));
1772 if (Tmp1 != Node->getOperand(0))
1773 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1774 break;
1775 case Expand:
1776 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1777
1778 // Since the result is legal, we should just be able to truncate the low
1779 // part of the source.
1780 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1781 break;
1782 case Promote:
1783 Result = PromoteOp(Node->getOperand(0));
1784 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1785 break;
1786 }
1787 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001788
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001789 case ISD::FP_TO_SINT:
1790 case ISD::FP_TO_UINT:
1791 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1792 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001793 Tmp1 = LegalizeOp(Node->getOperand(0));
1794
Chris Lattner44fe26f2005-07-29 00:11:56 +00001795 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1796 default: assert(0 && "Unknown operation action!");
1797 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00001798 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1799 SDOperand True, False;
1800 MVT::ValueType VT = Node->getOperand(0).getValueType();
1801 MVT::ValueType NVT = Node->getValueType(0);
1802 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1803 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1804 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1805 Node->getOperand(0), Tmp2, ISD::SETLT);
1806 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1807 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1808 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1809 Tmp2));
1810 False = DAG.getNode(ISD::XOR, NVT, False,
1811 DAG.getConstant(1ULL << ShiftAmt, NVT));
1812 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00001813 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00001814 } else {
1815 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1816 }
1817 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001818 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001819 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001820 Node->getOpcode() == ISD::FP_TO_SINT);
1821 AddLegalizedOperand(Op, Result);
1822 return Result;
1823 case TargetLowering::Legal:
1824 break;
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001825 case TargetLowering::Custom:
1826 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1827 Result = TLI.LowerOperation(Result, DAG);
1828 AddLegalizedOperand(Op, Result);
1829 NeedsAnotherIteration = true;
1830 return Result;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001831 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001832
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001833 if (Tmp1 != Node->getOperand(0))
1834 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1835 break;
1836 case Expand:
1837 assert(0 && "Shouldn't need to expand other operators here!");
1838 case Promote:
1839 Result = PromoteOp(Node->getOperand(0));
1840 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1841 break;
1842 }
1843 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001844
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001845 case ISD::ZERO_EXTEND:
1846 case ISD::SIGN_EXTEND:
1847 case ISD::FP_EXTEND:
1848 case ISD::FP_ROUND:
1849 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1850 case Legal:
1851 Tmp1 = LegalizeOp(Node->getOperand(0));
1852 if (Tmp1 != Node->getOperand(0))
1853 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1854 break;
1855 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001856 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001857
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001858 case Promote:
1859 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001860 case ISD::ZERO_EXTEND:
1861 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001862 // NOTE: Any extend would work here...
1863 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001864 Result = DAG.getZeroExtendInReg(Result,
1865 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001866 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001867 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001868 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001869 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001870 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001871 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001872 Result,
1873 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001874 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001875 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001876 Result = PromoteOp(Node->getOperand(0));
1877 if (Result.getValueType() != Op.getValueType())
1878 // Dynamically dead while we have only 2 FP types.
1879 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1880 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001881 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001882 Result = PromoteOp(Node->getOperand(0));
1883 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1884 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001885 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001886 }
1887 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001888 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001889 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001890 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001891 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001892
1893 // If this operation is not supported, convert it to a shl/shr or load/store
1894 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001895 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1896 default: assert(0 && "This action not supported for this op yet!");
1897 case TargetLowering::Legal:
1898 if (Tmp1 != Node->getOperand(0))
1899 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001900 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001901 break;
1902 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001903 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001904 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001905 // NOTE: we could fall back on load/store here too for targets without
1906 // SAR. However, it is doubtful that any exist.
1907 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1908 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001909 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001910 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1911 Node->getOperand(0), ShiftCst);
1912 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1913 Result, ShiftCst);
1914 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1915 // The only way we can lower this is to turn it into a STORETRUNC,
1916 // EXTLOAD pair, targetting a temporary location (a stack slot).
1917
1918 // NOTE: there is a choice here between constantly creating new stack
1919 // slots and always reusing the same one. We currently always create
1920 // new ones, as reuse may inhibit scheduling.
1921 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1922 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1923 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1924 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001925 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001926 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1927 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1928 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001929 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001930 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001931 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1932 Result, StackSlot, DAG.getSrcValue(NULL),
1933 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001934 } else {
1935 assert(0 && "Unknown op");
1936 }
1937 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001938 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001939 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001940 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001941 }
Chris Lattner99222f72005-01-15 07:15:18 +00001942 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001943
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001944 // Note that LegalizeOp may be reentered even from single-use nodes, which
1945 // means that we always must cache transformed nodes.
1946 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001947 return Result;
1948}
1949
Chris Lattner4d978642005-01-15 22:16:26 +00001950/// PromoteOp - Given an operation that produces a value in an invalid type,
1951/// promote it to compute the value into a larger type. The produced value will
1952/// have the correct bits for the low portion of the register, but no guarantee
1953/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001954SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1955 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001956 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001957 assert(getTypeAction(VT) == Promote &&
1958 "Caller should expand or legalize operands that are not promotable!");
1959 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1960 "Cannot promote to smaller type!");
1961
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001962 SDOperand Tmp1, Tmp2, Tmp3;
1963
1964 SDOperand Result;
1965 SDNode *Node = Op.Val;
1966
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001967 if (!Node->hasOneUse()) {
1968 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1969 if (I != PromotedNodes.end()) return I->second;
1970 } else {
1971 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1972 }
1973
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001974 // Promotion needs an optimization step to clean up after it, and is not
1975 // careful to avoid operations the target does not support. Make sure that
1976 // all generated operations are legalized in the next iteration.
1977 NeedsAnotherIteration = true;
1978
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001979 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00001980 case ISD::CopyFromReg:
1981 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001982 default:
1983 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1984 assert(0 && "Do not know how to promote this operator!");
1985 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001986 case ISD::UNDEF:
1987 Result = DAG.getNode(ISD::UNDEF, NVT);
1988 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001989 case ISD::Constant:
1990 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1991 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1992 break;
1993 case ISD::ConstantFP:
1994 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1995 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1996 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001997
Chris Lattner2cb338d2005-01-18 02:59:52 +00001998 case ISD::SETCC:
1999 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
2000 "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002001 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2002 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002003 Result = LegalizeOp(Result);
2004 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002005
2006 case ISD::TRUNCATE:
2007 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2008 case Legal:
2009 Result = LegalizeOp(Node->getOperand(0));
2010 assert(Result.getValueType() >= NVT &&
2011 "This truncation doesn't make sense!");
2012 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2013 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2014 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002015 case Promote:
2016 // The truncation is not required, because we don't guarantee anything
2017 // about high bits anyway.
2018 Result = PromoteOp(Node->getOperand(0));
2019 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002020 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002021 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2022 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002023 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002024 }
2025 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002026 case ISD::SIGN_EXTEND:
2027 case ISD::ZERO_EXTEND:
2028 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2029 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2030 case Legal:
2031 // Input is legal? Just do extend all the way to the larger type.
2032 Result = LegalizeOp(Node->getOperand(0));
2033 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2034 break;
2035 case Promote:
2036 // Promote the reg if it's smaller.
2037 Result = PromoteOp(Node->getOperand(0));
2038 // The high bits are not guaranteed to be anything. Insert an extend.
2039 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002040 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002041 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002042 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002043 Result = DAG.getZeroExtendInReg(Result,
2044 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002045 break;
2046 }
2047 break;
2048
2049 case ISD::FP_EXTEND:
2050 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2051 case ISD::FP_ROUND:
2052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2053 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2054 case Promote: assert(0 && "Unreachable with 2 FP types!");
2055 case Legal:
2056 // Input is legal? Do an FP_ROUND_INREG.
2057 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002058 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2059 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002060 break;
2061 }
2062 break;
2063
2064 case ISD::SINT_TO_FP:
2065 case ISD::UINT_TO_FP:
2066 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2067 case Legal:
2068 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002069 // No extra round required here.
2070 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002071 break;
2072
2073 case Promote:
2074 Result = PromoteOp(Node->getOperand(0));
2075 if (Node->getOpcode() == ISD::SINT_TO_FP)
2076 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002077 Result,
2078 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002079 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002080 Result = DAG.getZeroExtendInReg(Result,
2081 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002082 // No extra round required here.
2083 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002084 break;
2085 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002086 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2087 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002088 // Round if we cannot tolerate excess precision.
2089 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002090 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2091 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002092 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002093 }
Chris Lattner4d978642005-01-15 22:16:26 +00002094 break;
2095
2096 case ISD::FP_TO_SINT:
2097 case ISD::FP_TO_UINT:
2098 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2099 case Legal:
2100 Tmp1 = LegalizeOp(Node->getOperand(0));
2101 break;
2102 case Promote:
2103 // The input result is prerounded, so we don't have to do anything
2104 // special.
2105 Tmp1 = PromoteOp(Node->getOperand(0));
2106 break;
2107 case Expand:
2108 assert(0 && "not implemented");
2109 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002110 // If we're promoting a UINT to a larger size, check to see if the new node
2111 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2112 // we can use that instead. This allows us to generate better code for
2113 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2114 // legal, such as PowerPC.
2115 if (Node->getOpcode() == ISD::FP_TO_UINT &&
2116 TargetLowering::Legal != TLI.getOperationAction(ISD::FP_TO_UINT, NVT) &&
2117 TargetLowering::Legal == TLI.getOperationAction(ISD::FP_TO_SINT, NVT)) {
2118 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2119 } else {
2120 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2121 }
Chris Lattner4d978642005-01-15 22:16:26 +00002122 break;
2123
Chris Lattner13fe99c2005-04-02 05:00:07 +00002124 case ISD::FABS:
2125 case ISD::FNEG:
2126 Tmp1 = PromoteOp(Node->getOperand(0));
2127 assert(Tmp1.getValueType() == NVT);
2128 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2129 // NOTE: we do not have to do any extra rounding here for
2130 // NoExcessFPPrecision, because we know the input will have the appropriate
2131 // precision, and these operations don't modify precision at all.
2132 break;
2133
Chris Lattner9d6fa982005-04-28 21:44:33 +00002134 case ISD::FSQRT:
2135 case ISD::FSIN:
2136 case ISD::FCOS:
2137 Tmp1 = PromoteOp(Node->getOperand(0));
2138 assert(Tmp1.getValueType() == NVT);
2139 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2140 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002141 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2142 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002143 break;
2144
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002145 case ISD::AND:
2146 case ISD::OR:
2147 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002148 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002149 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002150 case ISD::MUL:
2151 // The input may have strange things in the top bits of the registers, but
2152 // these operations don't care. They may have wierd bits going out, but
2153 // that too is okay if they are integer operations.
2154 Tmp1 = PromoteOp(Node->getOperand(0));
2155 Tmp2 = PromoteOp(Node->getOperand(1));
2156 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2157 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2158
2159 // However, if this is a floating point operation, they will give excess
2160 // precision that we may not be able to tolerate. If we DO allow excess
2161 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002162 // FIXME: Why would we need to round FP ops more than integer ones?
2163 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002164 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002165 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2166 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002167 break;
2168
Chris Lattner4d978642005-01-15 22:16:26 +00002169 case ISD::SDIV:
2170 case ISD::SREM:
2171 // These operators require that their input be sign extended.
2172 Tmp1 = PromoteOp(Node->getOperand(0));
2173 Tmp2 = PromoteOp(Node->getOperand(1));
2174 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002175 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2176 DAG.getValueType(VT));
2177 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2178 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002179 }
2180 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2181
2182 // Perform FP_ROUND: this is probably overly pessimistic.
2183 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002184 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2185 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002186 break;
2187
2188 case ISD::UDIV:
2189 case ISD::UREM:
2190 // These operators require that their input be zero extended.
2191 Tmp1 = PromoteOp(Node->getOperand(0));
2192 Tmp2 = PromoteOp(Node->getOperand(1));
2193 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002194 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2195 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002196 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2197 break;
2198
2199 case ISD::SHL:
2200 Tmp1 = PromoteOp(Node->getOperand(0));
2201 Tmp2 = LegalizeOp(Node->getOperand(1));
2202 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2203 break;
2204 case ISD::SRA:
2205 // The input value must be properly sign extended.
2206 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002207 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2208 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002209 Tmp2 = LegalizeOp(Node->getOperand(1));
2210 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2211 break;
2212 case ISD::SRL:
2213 // The input value must be properly zero extended.
2214 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002215 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002216 Tmp2 = LegalizeOp(Node->getOperand(1));
2217 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2218 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002219 case ISD::LOAD:
2220 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2221 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00002222 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00002223 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002224 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2225 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00002226 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002227 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2228 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002229
2230 // Remember that we legalized the chain.
2231 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2232 break;
2233 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002234 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2235 case Expand: assert(0 && "It's impossible to expand bools");
2236 case Legal:
2237 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2238 break;
2239 case Promote:
2240 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2241 break;
2242 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002243 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2244 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2245 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2246 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002247 case ISD::SELECT_CC:
2248 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2249 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2250 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2251 Node->getOperand(1), Tmp2, Tmp3,
2252 Node->getOperand(4));
2253 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002254 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002255 case ISD::CALL: {
2256 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2257 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2258
Chris Lattner3d95c142005-01-19 20:24:35 +00002259 std::vector<SDOperand> Ops;
2260 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2261 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2262
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002263 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2264 "Can only promote single result calls");
2265 std::vector<MVT::ValueType> RetTyVTs;
2266 RetTyVTs.reserve(2);
2267 RetTyVTs.push_back(NVT);
2268 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002269 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2270 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002271 Result = SDOperand(NC, 0);
2272
2273 // Insert the new chain mapping.
2274 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2275 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002276 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002277 case ISD::CTPOP:
2278 case ISD::CTTZ:
2279 case ISD::CTLZ:
2280 Tmp1 = Node->getOperand(0);
2281 //Zero extend the argument
2282 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2283 // Perform the larger operation, then subtract if needed.
2284 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2285 switch(Node->getOpcode())
2286 {
2287 case ISD::CTPOP:
2288 Result = Tmp1;
2289 break;
2290 case ISD::CTTZ:
2291 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002292 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002293 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002294 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002295 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2296 break;
2297 case ISD::CTLZ:
2298 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002299 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2300 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002301 getSizeInBits(VT), NVT));
2302 break;
2303 }
2304 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002305 }
2306
2307 assert(Result.Val && "Didn't set a result!");
2308 AddPromotedOperand(Op, Result);
2309 return Result;
2310}
Chris Lattnerdc750592005-01-07 07:47:09 +00002311
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002312/// ExpandAddSub - Find a clever way to expand this add operation into
2313/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002314void SelectionDAGLegalize::
2315ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2316 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002317 // Expand the subcomponents.
2318 SDOperand LHSL, LHSH, RHSL, RHSH;
2319 ExpandOp(LHS, LHSL, LHSH);
2320 ExpandOp(RHS, RHSL, RHSH);
2321
Chris Lattner8ffd0042005-04-11 20:29:59 +00002322 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002323 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2324 if (LHSL.getValueType() == MVT::i32) {
2325 SDOperand LowEl;
2326 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2327 if (C->getValue() == 0)
2328 LowEl = RHSL;
2329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2330 if (C->getValue() == 0)
2331 LowEl = LHSL;
2332 if (LowEl.Val) {
2333 // Turn this into an add/sub of the high part only.
2334 SDOperand HiEl =
2335 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2336 LowEl.getValueType(), LHSH, RHSH);
2337 Lo = LowEl;
2338 Hi = HiEl;
2339 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002340 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002341 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002342
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002343 std::vector<SDOperand> Ops;
2344 Ops.push_back(LHSL);
2345 Ops.push_back(LHSH);
2346 Ops.push_back(RHSL);
2347 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002348
2349 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2350 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002351 Hi = Lo.getValue(1);
2352}
2353
Chris Lattner4157c412005-04-02 04:00:59 +00002354void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2355 SDOperand Op, SDOperand Amt,
2356 SDOperand &Lo, SDOperand &Hi) {
2357 // Expand the subcomponents.
2358 SDOperand LHSL, LHSH;
2359 ExpandOp(Op, LHSL, LHSH);
2360
2361 std::vector<SDOperand> Ops;
2362 Ops.push_back(LHSL);
2363 Ops.push_back(LHSH);
2364 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002365 std::vector<MVT::ValueType> VTs;
2366 VTs.push_back(LHSL.getValueType());
2367 VTs.push_back(LHSH.getValueType());
2368 VTs.push_back(Amt.getValueType());
2369 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002370 Hi = Lo.getValue(1);
2371}
2372
2373
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002374/// ExpandShift - Try to find a clever way to expand this shift operation out to
2375/// smaller elements. If we can't find a way that is more efficient than a
2376/// libcall on this target, return false. Otherwise, return true with the
2377/// low-parts expanded into Lo and Hi.
2378bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2379 SDOperand &Lo, SDOperand &Hi) {
2380 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2381 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002382
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002383 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002384 SDOperand ShAmt = LegalizeOp(Amt);
2385 MVT::ValueType ShTy = ShAmt.getValueType();
2386 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2387 unsigned NVTBits = MVT::getSizeInBits(NVT);
2388
2389 // Handle the case when Amt is an immediate. Other cases are currently broken
2390 // and are disabled.
2391 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2392 unsigned Cst = CN->getValue();
2393 // Expand the incoming operand to be shifted, so that we have its parts
2394 SDOperand InL, InH;
2395 ExpandOp(Op, InL, InH);
2396 switch(Opc) {
2397 case ISD::SHL:
2398 if (Cst > VTBits) {
2399 Lo = DAG.getConstant(0, NVT);
2400 Hi = DAG.getConstant(0, NVT);
2401 } else if (Cst > NVTBits) {
2402 Lo = DAG.getConstant(0, NVT);
2403 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002404 } else if (Cst == NVTBits) {
2405 Lo = DAG.getConstant(0, NVT);
2406 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002407 } else {
2408 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2409 Hi = DAG.getNode(ISD::OR, NVT,
2410 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2411 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2412 }
2413 return true;
2414 case ISD::SRL:
2415 if (Cst > VTBits) {
2416 Lo = DAG.getConstant(0, NVT);
2417 Hi = DAG.getConstant(0, NVT);
2418 } else if (Cst > NVTBits) {
2419 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2420 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002421 } else if (Cst == NVTBits) {
2422 Lo = InH;
2423 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002424 } else {
2425 Lo = DAG.getNode(ISD::OR, NVT,
2426 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2427 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2428 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2429 }
2430 return true;
2431 case ISD::SRA:
2432 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002433 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002434 DAG.getConstant(NVTBits-1, ShTy));
2435 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002436 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002437 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002438 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002439 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002440 } else if (Cst == NVTBits) {
2441 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002442 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002443 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002444 } else {
2445 Lo = DAG.getNode(ISD::OR, NVT,
2446 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2447 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2448 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2449 }
2450 return true;
2451 }
2452 }
2453 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2454 // so disable it for now. Currently targets are handling this via SHL_PARTS
2455 // and friends.
2456 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002457
2458 // If we have an efficient select operation (or if the selects will all fold
2459 // away), lower to some complex code, otherwise just emit the libcall.
2460 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2461 !isa<ConstantSDNode>(Amt))
2462 return false;
2463
2464 SDOperand InL, InH;
2465 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002466 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2467 DAG.getConstant(NVTBits, ShTy), ShAmt);
2468
Chris Lattner4d25c042005-01-20 20:29:23 +00002469 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002470 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2471 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002472
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002473 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2474 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2475 DAG.getConstant(NVTBits-1, ShTy));
2476 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2477 DAG.getConstant(NVTBits-1, ShTy));
2478 }
2479
2480 if (Opc == ISD::SHL) {
2481 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2482 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2483 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002484 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002485
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002486 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2487 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2488 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002489 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002490 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2491 DAG.getConstant(32, ShTy),
2492 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002493 DAG.getConstant(0, NVT),
2494 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002495 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002496 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002497 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002498 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002499
2500 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002501 if (Opc == ISD::SRA)
2502 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2503 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002504 else
2505 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002506 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002507 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002508 }
2509 return true;
2510}
Chris Lattneraac464e2005-01-21 06:05:23 +00002511
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002512/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2513/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002514/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002515static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002516 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002517
Chris Lattner2dce7032005-05-12 23:24:06 +00002518 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002519 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002520 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002521 Found = Node;
2522 return;
2523 }
2524
2525 // Otherwise, scan the operands of Node to see if any of them is a call.
2526 assert(Node->getNumOperands() != 0 &&
2527 "All leaves should have depth equal to the entry node!");
2528 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002529 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002530
2531 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002532 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002533 Found);
2534}
2535
2536
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002537/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2538/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002539/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002540static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2541 std::set<SDNode*> &Visited) {
2542 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2543 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002544
Chris Lattner2dce7032005-05-12 23:24:06 +00002545 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002546 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002547 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002548 Found = Node;
2549 return;
2550 }
2551
2552 // Otherwise, scan the operands of Node to see if any of them is a call.
2553 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2554 if (UI == E) return;
2555 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002556 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002557
2558 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002559 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002560}
2561
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002562/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002563/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002564static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002565 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002566 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002567 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002568 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002569
2570 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002571 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002572
Chris Lattner4add7e32005-01-23 04:42:50 +00002573 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002574 if (TheChain.getValueType() != MVT::Other)
2575 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002576 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002577
2578 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002579 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002580
Chris Lattner4add7e32005-01-23 04:42:50 +00002581 // Make sure to only follow users of our token chain.
2582 SDNode *User = *UI;
2583 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2584 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002585 if (SDNode *Result = FindCallSeqEnd(User))
2586 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002587 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002588 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002589}
2590
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002591/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002592/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002593static SDNode *FindCallSeqStart(SDNode *Node) {
2594 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002595 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002596
2597 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2598 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002599 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002600}
2601
2602
Chris Lattner4add7e32005-01-23 04:42:50 +00002603/// FindInputOutputChains - If we are replacing an operation with a call we need
2604/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002605/// properly serialize the calls in the block. The returned operand is the
2606/// input chain value for the new call (e.g. the entry node or the previous
2607/// call), and OutChain is set to be the chain node to update to point to the
2608/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002609static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2610 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002611 SDNode *LatestCallSeqStart = Entry.Val;
2612 SDNode *LatestCallSeqEnd = 0;
2613 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2614 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002615
Chris Lattner2dce7032005-05-12 23:24:06 +00002616 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002617 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002618 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2619 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002620 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2621 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002622 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002623 LatestCallSeqEnd = Entry.Val;
2624 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002625
Chris Lattner06bbeb62005-05-11 19:02:11 +00002626 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002627 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002628 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002629 std::set<SDNode*> Visited;
2630 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002631
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002632 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002633 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002634 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002635
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002636 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002637}
2638
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002639/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002640void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2641 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002642 // Nothing to splice it into?
2643 if (OutChain == 0) return;
2644
2645 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2646 //OutChain->dump();
2647
2648 // Form a token factor node merging the old inval and the new inval.
2649 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2650 OutChain->getOperand(0));
2651 // Change the node to refer to the new token.
2652 OutChain->setAdjCallChain(InToken);
2653}
Chris Lattner4add7e32005-01-23 04:42:50 +00002654
2655
Chris Lattneraac464e2005-01-21 06:05:23 +00002656// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2657// does not fit into a register, return the lo part and set the hi part to the
2658// by-reg argument. If it does fit into a single register, return the result
2659// and leave the Hi part unset.
2660SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2661 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002662 SDNode *OutChain;
2663 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2664 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002665 if (InChain.Val == 0)
2666 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002667
Chris Lattneraac464e2005-01-21 06:05:23 +00002668 TargetLowering::ArgListTy Args;
2669 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2670 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2671 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2672 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2673 }
2674 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002675
Chris Lattner06bbeb62005-05-11 19:02:11 +00002676 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002677 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002678 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002679 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2680 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002681 SpliceCallInto(CallInfo.second, OutChain);
2682
2683 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002684
2685 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002686 default: assert(0 && "Unknown thing");
2687 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002688 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002689 case Promote:
2690 assert(0 && "Cannot promote this yet!");
2691 case Expand:
2692 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002693 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002694 return Lo;
2695 }
2696}
2697
Chris Lattner4add7e32005-01-23 04:42:50 +00002698
Chris Lattneraac464e2005-01-21 06:05:23 +00002699/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2700/// destination type is legal.
2701SDOperand SelectionDAGLegalize::
2702ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2703 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2704 assert(getTypeAction(Source.getValueType()) == Expand &&
2705 "This is not an expansion!");
2706 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2707
Chris Lattner06bbeb62005-05-11 19:02:11 +00002708 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002709 assert(Source.getValueType() == MVT::i64 &&
2710 "This only works for 64-bit -> FP");
2711 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2712 // incoming integer is set. To handle this, we dynamically test to see if
2713 // it is set, and, if so, add a fudge factor.
2714 SDOperand Lo, Hi;
2715 ExpandOp(Source, Lo, Hi);
2716
Chris Lattner2a4f7312005-05-13 04:45:13 +00002717 // If this is unsigned, and not supported, first perform the conversion to
2718 // signed, then adjust the result if the sign bit is set.
2719 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2720 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2721
Chris Lattnerd47675e2005-08-09 20:20:18 +00002722 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2723 DAG.getConstant(0, Hi.getValueType()),
2724 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002725 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2726 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2727 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002728 uint64_t FF = 0x5f800000ULL;
2729 if (TLI.isLittleEndian()) FF <<= 32;
2730 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002731
2732 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2733 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2734 TLI.getPointerTy());
2735 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2736 SDOperand FudgeInReg;
2737 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002738 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2739 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002740 else {
2741 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002742 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2743 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002744 }
2745 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002746 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002747
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002748 // Check to see if the target has a custom way to lower this. If so, use it.
2749 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2750 default: assert(0 && "This action not implemented for this operation!");
2751 case TargetLowering::Legal:
2752 case TargetLowering::Expand:
2753 break; // This case is handled below.
2754 case TargetLowering::Custom:
2755 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002756 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002757 }
2758
Chris Lattner153587e2005-05-12 07:00:44 +00002759 // Expand the source, then glue it back together for the call. We must expand
2760 // the source in case it is shared (this pass of legalize must traverse it).
2761 SDOperand SrcLo, SrcHi;
2762 ExpandOp(Source, SrcLo, SrcHi);
2763 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2764
Chris Lattner06bbeb62005-05-11 19:02:11 +00002765 SDNode *OutChain = 0;
2766 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2767 DAG.getEntryNode());
2768 const char *FnName = 0;
2769 if (DestTy == MVT::f32)
2770 FnName = "__floatdisf";
2771 else {
2772 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2773 FnName = "__floatdidf";
2774 }
2775
Chris Lattneraac464e2005-01-21 06:05:23 +00002776 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2777
2778 TargetLowering::ArgListTy Args;
2779 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002780
Chris Lattneraac464e2005-01-21 06:05:23 +00002781 Args.push_back(std::make_pair(Source, ArgTy));
2782
2783 // We don't care about token chains for libcalls. We just use the entry
2784 // node as our input and ignore the output chain. This allows us to place
2785 // calls wherever we need them to satisfy data dependences.
2786 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002787
2788 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002789 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2790 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002791
Chris Lattnera5bf1032005-05-12 04:49:08 +00002792 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002793 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002794}
Misha Brukman835702a2005-04-21 22:36:52 +00002795
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002796
2797
Chris Lattnerdc750592005-01-07 07:47:09 +00002798/// ExpandOp - Expand the specified SDOperand into its two component pieces
2799/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2800/// LegalizeNodes map is filled in for any results that are not expanded, the
2801/// ExpandedNodes map is filled in for any results that are expanded, and the
2802/// Lo/Hi values are returned.
2803void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2804 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002805 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002806 SDNode *Node = Op.Val;
2807 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2808 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2809 assert(MVT::isInteger(NVT) && NVT < VT &&
2810 "Cannot expand to FP value or to larger int value!");
2811
2812 // If there is more than one use of this, see if we already expanded it.
2813 // There is no use remembering values that only have a single use, as the map
2814 // entries will never be reused.
2815 if (!Node->hasOneUse()) {
2816 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2817 = ExpandedNodes.find(Op);
2818 if (I != ExpandedNodes.end()) {
2819 Lo = I->second.first;
2820 Hi = I->second.second;
2821 return;
2822 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002823 } else {
2824 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002825 }
2826
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002827 // Expanding to multiple registers needs to perform an optimization step, and
2828 // is not careful to avoid operations the target does not support. Make sure
2829 // that all generated operations are legalized in the next iteration.
2830 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002831
Chris Lattnerdc750592005-01-07 07:47:09 +00002832 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002833 case ISD::CopyFromReg:
2834 assert(0 && "CopyFromReg must be legal!");
2835 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00002836 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2837 assert(0 && "Do not know how to expand this operator!");
2838 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002839 case ISD::UNDEF:
2840 Lo = DAG.getNode(ISD::UNDEF, NVT);
2841 Hi = DAG.getNode(ISD::UNDEF, NVT);
2842 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002843 case ISD::Constant: {
2844 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2845 Lo = DAG.getConstant(Cst, NVT);
2846 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2847 break;
2848 }
2849
Chris Lattner32e08b72005-03-28 22:03:13 +00002850 case ISD::BUILD_PAIR:
2851 // Legalize both operands. FIXME: in the future we should handle the case
2852 // where the two elements are not legal.
2853 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2854 Lo = LegalizeOp(Node->getOperand(0));
2855 Hi = LegalizeOp(Node->getOperand(1));
2856 break;
2857
Chris Lattner55e9cde2005-05-11 04:51:16 +00002858 case ISD::CTPOP:
2859 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002860 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2861 DAG.getNode(ISD::CTPOP, NVT, Lo),
2862 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002863 Hi = DAG.getConstant(0, NVT);
2864 break;
2865
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002866 case ISD::CTLZ: {
2867 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002868 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002869 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2870 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002871 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2872 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002873 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2874 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2875
2876 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2877 Hi = DAG.getConstant(0, NVT);
2878 break;
2879 }
2880
2881 case ISD::CTTZ: {
2882 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002883 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002884 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2885 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002886 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2887 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002888 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2889 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2890
2891 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2892 Hi = DAG.getConstant(0, NVT);
2893 break;
2894 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002895
Chris Lattnerdc750592005-01-07 07:47:09 +00002896 case ISD::LOAD: {
2897 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2898 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002899 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002900
2901 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002902 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002903 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2904 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002905 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002906 //are from the same instruction?
2907 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002908
2909 // Build a factor node to remember that this load is independent of the
2910 // other one.
2911 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2912 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002913
Chris Lattnerdc750592005-01-07 07:47:09 +00002914 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002915 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002916 if (!TLI.isLittleEndian())
2917 std::swap(Lo, Hi);
2918 break;
2919 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002920 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002921 case ISD::CALL: {
2922 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2923 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2924
Chris Lattner3d95c142005-01-19 20:24:35 +00002925 bool Changed = false;
2926 std::vector<SDOperand> Ops;
2927 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2928 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2929 Changed |= Ops.back() != Node->getOperand(i);
2930 }
2931
Chris Lattnerdc750592005-01-07 07:47:09 +00002932 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2933 "Can only expand a call once so far, not i64 -> i16!");
2934
2935 std::vector<MVT::ValueType> RetTyVTs;
2936 RetTyVTs.reserve(3);
2937 RetTyVTs.push_back(NVT);
2938 RetTyVTs.push_back(NVT);
2939 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002940 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2941 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002942 Lo = SDOperand(NC, 0);
2943 Hi = SDOperand(NC, 1);
2944
2945 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002946 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002947 break;
2948 }
2949 case ISD::AND:
2950 case ISD::OR:
2951 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2952 SDOperand LL, LH, RL, RH;
2953 ExpandOp(Node->getOperand(0), LL, LH);
2954 ExpandOp(Node->getOperand(1), RL, RH);
2955 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2956 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2957 break;
2958 }
2959 case ISD::SELECT: {
2960 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002961
2962 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2963 case Expand: assert(0 && "It's impossible to expand bools");
2964 case Legal:
2965 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2966 break;
2967 case Promote:
2968 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2969 break;
2970 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002971 ExpandOp(Node->getOperand(1), LL, LH);
2972 ExpandOp(Node->getOperand(2), RL, RH);
2973 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2974 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2975 break;
2976 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002977 case ISD::SELECT_CC: {
2978 SDOperand TL, TH, FL, FH;
2979 ExpandOp(Node->getOperand(2), TL, TH);
2980 ExpandOp(Node->getOperand(3), FL, FH);
2981 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2982 Node->getOperand(1), TL, FL, Node->getOperand(4));
2983 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2984 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00002985 Lo = LegalizeOp(Lo);
2986 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00002987 break;
2988 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002989 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002990 SDOperand In;
2991 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2992 case Expand: assert(0 && "expand-expand not implemented yet!");
2993 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2994 case Promote:
2995 In = PromoteOp(Node->getOperand(0));
2996 // Emit the appropriate sign_extend_inreg to get the value we want.
2997 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002998 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002999 break;
3000 }
3001
Chris Lattnerdc750592005-01-07 07:47:09 +00003002 // The low part is just a sign extension of the input (which degenerates to
3003 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003004 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003005
Chris Lattnerdc750592005-01-07 07:47:09 +00003006 // The high part is obtained by SRA'ing all but one of the bits of the lo
3007 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003008 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003009 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3010 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003011 break;
3012 }
Chris Lattner47844892005-04-03 23:41:52 +00003013 case ISD::ZERO_EXTEND: {
3014 SDOperand In;
3015 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3016 case Expand: assert(0 && "expand-expand not implemented yet!");
3017 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3018 case Promote:
3019 In = PromoteOp(Node->getOperand(0));
3020 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003021 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003022 break;
3023 }
3024
Chris Lattnerdc750592005-01-07 07:47:09 +00003025 // The low part is just a zero extension of the input (which degenerates to
3026 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003027 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003028
Chris Lattnerdc750592005-01-07 07:47:09 +00003029 // The high part is just a zero.
3030 Hi = DAG.getConstant(0, NVT);
3031 break;
Chris Lattner47844892005-04-03 23:41:52 +00003032 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003033 // These operators cannot be expanded directly, emit them as calls to
3034 // library functions.
3035 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003036 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003037 SDOperand Op;
3038 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3039 case Expand: assert(0 && "cannot expand FP!");
3040 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3041 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3042 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003043
Chris Lattner941d84a2005-07-30 01:40:57 +00003044 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3045
Chris Lattnerfe68d752005-07-29 00:33:32 +00003046 // Now that the custom expander is done, expand the result, which is still
3047 // VT.
Chris Lattner941d84a2005-07-30 01:40:57 +00003048 ExpandOp(Op, Lo, Hi);
Chris Lattnerfe68d752005-07-29 00:33:32 +00003049 break;
3050 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003051
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003052 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003053 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003054 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003055 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003056 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003057
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003058 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003059 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3060 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3061 LegalizeOp(Node->getOperand(0)));
3062 // Now that the custom expander is done, expand the result, which is still
3063 // VT.
3064 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
3065 break;
3066 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003067
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003068 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003069 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003070 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003071 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003072 break;
3073
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003074 case ISD::SHL:
3075 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003076 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003077 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003078
3079 // If this target supports SHL_PARTS, use it.
3080 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003081 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3082 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003083 break;
3084 }
3085
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003086 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003087 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003088 break;
3089
3090 case ISD::SRA:
3091 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003092 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003093 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003094
3095 // If this target supports SRA_PARTS, use it.
3096 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003097 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3098 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003099 break;
3100 }
3101
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003102 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003103 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003104 break;
3105 case ISD::SRL:
3106 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003107 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003108 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003109
3110 // If this target supports SRL_PARTS, use it.
3111 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00003112 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3113 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003114 break;
3115 }
3116
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003117 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003118 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003119 break;
3120
Misha Brukman835702a2005-04-21 22:36:52 +00003121 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003122 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3123 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003124 break;
3125 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003126 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3127 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003128 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003129 case ISD::MUL: {
3130 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
3131 SDOperand LL, LH, RL, RH;
3132 ExpandOp(Node->getOperand(0), LL, LH);
3133 ExpandOp(Node->getOperand(1), RL, RH);
3134 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3135 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3136 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3137 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3138 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3139 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3140 } else {
3141 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3142 }
3143 break;
3144 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003145 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3146 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3147 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3148 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003149 }
3150
3151 // Remember in a map if the values will be reused later.
3152 if (!Node->hasOneUse()) {
3153 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3154 std::make_pair(Lo, Hi))).second;
3155 assert(isNew && "Value already expanded?!?");
3156 }
3157}
3158
3159
3160// SelectionDAG::Legalize - This is the entry point for the file.
3161//
Chris Lattner4add7e32005-01-23 04:42:50 +00003162void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003163 /// run - This is the main entry point to this class.
3164 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003165 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003166}
3167