blob: 57a9b1d7765d2c2e7b70976d0d04058b56d61d1e [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman6a648612005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000056
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner03c85462005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattner8afc48e2005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000080 LegalizedNodes.insert(std::make_pair(From, To));
81 // If someone requests legalization of the new node, return itself.
82 if (From != To)
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000084 }
Chris Lattner03c85462005-01-15 05:21:40 +000085 void AddPromotedOperand(SDOperand From, SDOperand To) {
86 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
87 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +000088 // If someone requests legalization of the new node, return itself.
89 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +000090 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000091
Chris Lattner3e928bb2005-01-07 07:47:09 +000092public:
93
Chris Lattner9c32d3b2005-01-23 04:42:50 +000094 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000095
96 /// Run - While there is still lowering to do, perform a pass over the DAG.
97 /// Most regularization can be done in a single pass, but targets that require
98 /// large values to be split into registers multiple times (e.g. i64 -> 4x
99 /// i16) require iteration for these values (the first iteration will demote
100 /// to i32, the second will demote to i16).
101 void Run() {
102 do {
103 NeedsAnotherIteration = false;
104 LegalizeDAG();
105 } while (NeedsAnotherIteration);
106 }
107
108 /// getTypeAction - Return how we should legalize values of this type, either
109 /// it is already legal or we need to expand it into multiple registers of
110 /// smaller integer type, or we need to promote it to a larger type.
111 LegalizeAction getTypeAction(MVT::ValueType VT) const {
112 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
113 }
114
115 /// isTypeLegal - Return true if this type is legal on this target.
116 ///
117 bool isTypeLegal(MVT::ValueType VT) const {
118 return getTypeAction(VT) == Legal;
119 }
120
121private:
122 void LegalizeDAG();
123
124 SDOperand LegalizeOp(SDOperand O);
125 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000126 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127
Chris Lattner77e77a62005-01-21 06:05:23 +0000128 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
129 SDOperand &Hi);
130 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
131 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000132
Chris Lattner35481892005-12-23 00:16:34 +0000133 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000134 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
135 SDOperand LegalOp,
136 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000137 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
138 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000139 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
140 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000141
Chris Lattnere34b3962005-01-19 04:19:40 +0000142 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
143 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000144 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
145 SDOperand &Lo, SDOperand &Hi);
146 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000147 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000148
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000149 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
150
Chris Lattner3e928bb2005-01-07 07:47:09 +0000151 SDOperand getIntPtrConstant(uint64_t Val) {
152 return DAG.getConstant(Val, TLI.getPointerTy());
153 }
154};
155}
156
Chris Lattnerb89175f2005-11-19 05:51:46 +0000157static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000158 switch (VecOp) {
159 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000160 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
161 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
162 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
163 }
164}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000165
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000171}
172
Jim Laskey6269ed12005-08-17 00:39:29 +0000173/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
174/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000175/// we expand it. At this point, we know that the result and operand types are
176/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000177SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
178 SDOperand Op0,
179 MVT::ValueType DestVT) {
180 if (Op0.getValueType() == MVT::i32) {
181 // simple 32-bit [signed|unsigned] integer to float/double expansion
182
183 // get the stack frame index of a 8 byte buffer
184 MachineFunction &MF = DAG.getMachineFunction();
185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
186 // get address of 8 byte buffer
187 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
188 // word offset constant for Hi/Lo address computation
189 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
190 // set up Hi and Lo (into buffer) address based on endian
191 SDOperand Hi, Lo;
192 if (TLI.isLittleEndian()) {
193 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
194 Lo = StackSlot;
195 } else {
196 Hi = StackSlot;
197 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
198 }
199 // if signed map to unsigned space
200 SDOperand Op0Mapped;
201 if (isSigned) {
202 // constant used to invert sign bit (signed to unsigned mapping)
203 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
204 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
205 } else {
206 Op0Mapped = Op0;
207 }
208 // store the lo of the constructed double - based on integer input
209 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
210 Op0Mapped, Lo, DAG.getSrcValue(NULL));
211 // initial hi portion of constructed double
212 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
213 // store the hi of the constructed double - biased exponent
214 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
215 InitialHi, Hi, DAG.getSrcValue(NULL));
216 // load the constructed double
217 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
218 DAG.getSrcValue(NULL));
219 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000220 SDOperand Bias = DAG.getConstantFP(isSigned ?
221 BitsToDouble(0x4330000080000000ULL)
222 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000223 MVT::f64);
224 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000226 // final result
227 SDOperand Result;
228 // handle final rounding
229 if (DestVT == MVT::f64) {
230 // do nothing
231 Result = Sub;
232 } else {
233 // if f32 then cast to f32
234 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
235 }
Chris Lattner69a889e2005-12-20 00:53:54 +0000236 return LegalizeOp(Result);
Jim Laskey6269ed12005-08-17 00:39:29 +0000237 }
238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
242 DAG.getConstant(0, Op0.getValueType()),
243 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000244 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
245 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
246 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Jim Laskey6269ed12005-08-17 00:39:29 +0000248 // If the sign bit of the integer is set, the large number will be treated
249 // as a negative number. To counteract this, the dynamic code adds an
250 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000251 uint64_t FF;
252 switch (Op0.getValueType()) {
253 default: assert(0 && "Unsupported integer type!");
254 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
255 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
256 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
257 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
258 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000259 if (TLI.isLittleEndian()) FF <<= 32;
260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000261
Chris Lattner5839bf22005-08-26 17:15:30 +0000262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000263 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
264 SDOperand FudgeInReg;
265 if (DestVT == MVT::f32)
266 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
267 DAG.getSrcValue(NULL));
268 else {
269 assert(DestVT == MVT::f64 && "Unexpected conversion");
270 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
271 DAG.getEntryNode(), CPIdx,
272 DAG.getSrcValue(NULL), MVT::f32));
273 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000274
Chris Lattner69a889e2005-12-20 00:53:54 +0000275 return LegalizeOp(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
Chris Lattnercad063f2005-07-16 00:19:57 +0000276}
277
Chris Lattner149c58c2005-08-16 18:17:10 +0000278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000279/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000280/// we promote it. At this point, we know that the result and operand types are
281/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
282/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
284 MVT::ValueType DestVT,
285 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000286 // First step, figure out the appropriate *INT_TO_FP operation to use.
287 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000288
Chris Lattnercad063f2005-07-16 00:19:57 +0000289 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000290
Chris Lattnercad063f2005-07-16 00:19:57 +0000291 // Scan for the appropriate larger type to use.
292 while (1) {
293 NewInTy = (MVT::ValueType)(NewInTy+1);
294 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000295
Chris Lattnercad063f2005-07-16 00:19:57 +0000296 // If the target supports SINT_TO_FP of this type, use it.
297 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
298 default: break;
299 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000300 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000301 break; // Can't use this datatype.
302 // FALL THROUGH.
303 case TargetLowering::Custom:
304 OpToUse = ISD::SINT_TO_FP;
305 break;
306 }
307 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000308 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000309
Chris Lattnercad063f2005-07-16 00:19:57 +0000310 // If the target supports UINT_TO_FP of this type, use it.
311 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
312 default: break;
313 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000314 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000315 break; // Can't use this datatype.
316 // FALL THROUGH.
317 case TargetLowering::Custom:
318 OpToUse = ISD::UINT_TO_FP;
319 break;
320 }
321 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000322
Chris Lattnercad063f2005-07-16 00:19:57 +0000323 // Otherwise, try a larger type.
324 }
325
Chris Lattnercad063f2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
Chris Lattner69a889e2005-12-20 00:53:54 +0000328 SDOperand N = DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattner69a889e2005-12-20 00:53:54 +0000331 // Make sure to legalize any nodes we create here.
332 return LegalizeOp(N);
Chris Lattnercad063f2005-07-16 00:19:57 +0000333}
334
Chris Lattner1618beb2005-07-29 00:11:56 +0000335/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
336/// FP_TO_*INT operation of the specified operand when the target requests that
337/// we promote it. At this point, we know that the result and operand types are
338/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
339/// operation that returns a larger result.
340SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
341 MVT::ValueType DestVT,
342 bool isSigned) {
343 // First step, figure out the appropriate FP_TO*INT operation to use.
344 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000345
Chris Lattner1618beb2005-07-29 00:11:56 +0000346 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000347
Chris Lattner1618beb2005-07-29 00:11:56 +0000348 // Scan for the appropriate larger type to use.
349 while (1) {
350 NewOutTy = (MVT::ValueType)(NewOutTy+1);
351 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000352
Chris Lattner1618beb2005-07-29 00:11:56 +0000353 // If the target supports FP_TO_SINT returning this type, use it.
354 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
355 default: break;
356 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000357 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000358 break; // Can't use this datatype.
359 // FALL THROUGH.
360 case TargetLowering::Custom:
361 OpToUse = ISD::FP_TO_SINT;
362 break;
363 }
364 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000365
Chris Lattner1618beb2005-07-29 00:11:56 +0000366 // If the target supports FP_TO_UINT of this type, use it.
367 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
368 default: break;
369 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000370 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000371 break; // Can't use this datatype.
372 // FALL THROUGH.
373 case TargetLowering::Custom:
374 OpToUse = ISD::FP_TO_UINT;
375 break;
376 }
377 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000378
Chris Lattner1618beb2005-07-29 00:11:56 +0000379 // Otherwise, try a larger type.
380 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000381
Chris Lattner1618beb2005-07-29 00:11:56 +0000382 // Okay, we found the operation and type to use. Truncate the result of the
383 // extended FP_TO_*INT operation to the desired size.
Chris Lattner69a889e2005-12-20 00:53:54 +0000384 SDOperand N = DAG.getNode(ISD::TRUNCATE, DestVT,
385 DAG.getNode(OpToUse, NewOutTy, LegalOp));
386 // Make sure to legalize any nodes we create here in the next pass.
387 return LegalizeOp(N);
Chris Lattner1618beb2005-07-29 00:11:56 +0000388}
389
Chris Lattner32fca002005-10-06 01:20:27 +0000390/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
391/// not been visited yet and if all of its operands have already been visited.
392static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
393 std::map<SDNode*, unsigned> &Visited) {
394 if (++Visited[N] != N->getNumOperands())
395 return; // Haven't visited all operands yet
396
397 Order.push_back(N);
398
399 if (N->hasOneUse()) { // Tail recurse in common case.
400 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
401 return;
402 }
403
404 // Now that we have N in, add anything that uses it if all of their operands
405 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000406 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
407 ComputeTopDownOrdering(*UI, Order, Visited);
408}
409
Chris Lattner1618beb2005-07-29 00:11:56 +0000410
Chris Lattner3e928bb2005-01-07 07:47:09 +0000411void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000412 // The legalize process is inherently a bottom-up recursive process (users
413 // legalize their uses before themselves). Given infinite stack space, we
414 // could just start legalizing on the root and traverse the whole graph. In
415 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000416 // blocks. To avoid this problem, compute an ordering of the nodes where each
417 // node is only legalized after all of its operands are legalized.
418 std::map<SDNode*, unsigned> Visited;
419 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000420
Chris Lattner32fca002005-10-06 01:20:27 +0000421 // Compute ordering from all of the leaves in the graphs, those (like the
422 // entry node) that have no operands.
423 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
424 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000425 if (I->getNumOperands() == 0) {
426 Visited[I] = 0 - 1U;
427 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000428 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000429 }
430
Chris Lattnerde202b32005-11-09 23:47:37 +0000431 assert(Order.size() == Visited.size() &&
432 Order.size() ==
433 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000434 "Error: DAG is cyclic!");
435 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000436
Chris Lattner32fca002005-10-06 01:20:27 +0000437 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
438 SDNode *N = Order[i];
439 switch (getTypeAction(N->getValueType(0))) {
440 default: assert(0 && "Bad type action!");
441 case Legal:
442 LegalizeOp(SDOperand(N, 0));
443 break;
444 case Promote:
445 PromoteOp(SDOperand(N, 0));
446 break;
447 case Expand: {
448 SDOperand X, Y;
449 ExpandOp(SDOperand(N, 0), X, Y);
450 break;
451 }
452 }
453 }
454
455 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000456 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000457 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
458 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000459
460 ExpandedNodes.clear();
461 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000462 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463
464 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000465 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466}
467
468SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000469 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000470 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000471 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000472
Chris Lattner3e928bb2005-01-07 07:47:09 +0000473 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000474 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000475 if (Node->getNumValues() > 1) {
476 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
477 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000478 case Legal: break; // Nothing to do.
479 case Expand: {
480 SDOperand T1, T2;
481 ExpandOp(Op.getValue(i), T1, T2);
482 assert(LegalizedNodes.count(Op) &&
483 "Expansion didn't add legal operands!");
484 return LegalizedNodes[Op];
485 }
486 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000487 PromoteOp(Op.getValue(i));
488 assert(LegalizedNodes.count(Op) &&
489 "Expansion didn't add legal operands!");
490 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000491 }
492 }
493
Chris Lattner45982da2005-05-12 16:53:42 +0000494 // Note that LegalizeOp may be reentered even from single-use nodes, which
495 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000496 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
497 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000498
Nate Begeman9373a812005-08-10 20:51:12 +0000499 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000500
501 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000502
503 switch (Node->getOpcode()) {
504 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000505 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
506 // If this is a target node, legalize it by legalizing the operands then
507 // passing it through.
508 std::vector<SDOperand> Ops;
509 bool Changed = false;
510 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
511 Ops.push_back(LegalizeOp(Node->getOperand(i)));
512 Changed = Changed || Node->getOperand(i) != Ops.back();
513 }
514 if (Changed)
515 if (Node->getNumValues() == 1)
516 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
517 else {
518 std::vector<MVT::ValueType> VTs(Node->value_begin(),
519 Node->value_end());
520 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
521 }
522
523 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
524 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
525 return Result.getValue(Op.ResNo);
526 }
527 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000528 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
529 assert(0 && "Do not know how to legalize this operator!");
530 abort();
531 case ISD::EntryToken:
532 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000533 case ISD::TargetFrameIndex:
534 case ISD::Register:
535 case ISD::TargetConstant:
Nate Begeman28a6b022005-12-10 02:36:00 +0000536 case ISD::TargetConstantPool:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000537 case ISD::GlobalAddress:
Chris Lattnerb9debbf2005-11-17 05:52:24 +0000538 case ISD::TargetGlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000539 case ISD::ExternalSymbol:
Andrew Lenharthe8f65f12005-12-24 23:42:32 +0000540 case ISD::TargetExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000541 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000542 case ISD::BasicBlock:
543 case ISD::CONDCODE:
544 case ISD::VALUETYPE:
545 case ISD::SRCVALUE:
Chris Lattner36ce6912005-11-29 06:21:05 +0000546 case ISD::STRING:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000547 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
548 default: assert(0 && "This action is not supported yet!");
549 case TargetLowering::Custom: {
550 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
551 if (Tmp.Val) {
552 Result = LegalizeOp(Tmp);
553 break;
554 }
555 } // FALLTHROUGH if the target doesn't want to lower this op after all.
556 case TargetLowering::Legal:
557 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
558 break;
559 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000560 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000561 case ISD::AssertSext:
562 case ISD::AssertZext:
563 Tmp1 = LegalizeOp(Node->getOperand(0));
564 if (Tmp1 != Node->getOperand(0))
565 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
566 Node->getOperand(1));
567 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000568 case ISD::MERGE_VALUES:
569 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner69a52152005-01-14 22:38:01 +0000570 case ISD::CopyFromReg:
571 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000572 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000573 if (Node->getNumValues() == 2) {
Chris Lattner7310fb12005-12-18 15:27:43 +0000574 if (Tmp1 != Node->getOperand(0))
575 Result = DAG.getCopyFromReg(Tmp1,
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000576 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
Chris Lattner7310fb12005-12-18 15:27:43 +0000577 Node->getValueType(0));
578 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000579 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
580 if (Node->getNumOperands() == 3)
581 Tmp2 = LegalizeOp(Node->getOperand(2));
582 if (Tmp1 != Node->getOperand(0) ||
583 (Node->getNumOperands() == 3 && Tmp2 != Node->getOperand(2)))
Chris Lattner7310fb12005-12-18 15:27:43 +0000584 Result = DAG.getCopyFromReg(Tmp1,
585 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
586 Node->getValueType(0), Tmp2);
587 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
588 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000589 // Since CopyFromReg produces two values, make sure to remember that we
590 // legalized both of them.
591 AddLegalizedOperand(Op.getValue(0), Result);
592 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
593 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000594 case ISD::UNDEF: {
595 MVT::ValueType VT = Op.getValueType();
596 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000597 default: assert(0 && "This action is not supported yet!");
598 case TargetLowering::Expand:
599 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000600 if (MVT::isInteger(VT))
601 Result = DAG.getConstant(0, VT);
602 else if (MVT::isFloatingPoint(VT))
603 Result = DAG.getConstantFP(0, VT);
604 else
605 assert(0 && "Unknown value type!");
606 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000607 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000608 break;
609 }
610 break;
611 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000612
613 case ISD::LOCATION:
614 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
616
617 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
618 case TargetLowering::Promote:
619 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000620 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000621 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000622 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
623 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
624
625 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
626 const std::string &FName =
627 cast<StringSDNode>(Node->getOperand(3))->getValue();
628 const std::string &DirName =
629 cast<StringSDNode>(Node->getOperand(4))->getValue();
630 unsigned SrcFile = DebugInfo->getUniqueSourceID(FName, DirName);
631
Jim Laskeye81aecb2005-12-21 20:51:37 +0000632 std::vector<SDOperand> Ops;
633 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000634 SDOperand LineOp = Node->getOperand(1);
635 SDOperand ColOp = Node->getOperand(2);
636
637 if (useDEBUG_LOC) {
638 Ops.push_back(LineOp); // line #
639 Ops.push_back(ColOp); // col #
640 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
641 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
642 } else {
643 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
644 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
645 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
646 Ops.push_back(DAG.getConstant(ID, MVT::i32));
647 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
648 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000649 } else {
650 Result = Tmp1; // chain
651 }
Chris Lattnere7736732005-12-18 23:54:29 +0000652 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner36ce6912005-11-29 06:21:05 +0000653 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000654 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000655 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000656 if (Tmp1 != Node->getOperand(0) ||
657 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000658 std::vector<SDOperand> Ops;
659 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000660 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
661 Ops.push_back(Node->getOperand(1)); // line # must be legal.
662 Ops.push_back(Node->getOperand(2)); // col # must be legal.
663 } else {
664 // Otherwise promote them.
665 Ops.push_back(PromoteOp(Node->getOperand(1)));
666 Ops.push_back(PromoteOp(Node->getOperand(2)));
667 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000668 Ops.push_back(Node->getOperand(3)); // filename must be legal.
669 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
670 Result = DAG.getNode(ISD::LOCATION, MVT::Other, Ops);
671 }
672 break;
673 }
674 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000675
676 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000677 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000678 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
679 case TargetLowering::Promote:
680 case TargetLowering::Expand:
681 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000682 case TargetLowering::Legal:
683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
684 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
685 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
686 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
687
688 if (Tmp1 != Node->getOperand(0) ||
689 Tmp2 != Node->getOperand(1) ||
690 Tmp3 != Node->getOperand(2) ||
691 Tmp4 != Node->getOperand(3)) {
692 Result = DAG.getNode(ISD::DEBUG_LOC,MVT::Other, Tmp1, Tmp2, Tmp3, Tmp4);
693 }
694 break;
695 }
696 break;
697
698 case ISD::DEBUG_LABEL:
699 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
700 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
701 case TargetLowering::Promote:
702 case TargetLowering::Expand:
703 default: assert(0 && "This action is not supported yet!");
704 case TargetLowering::Legal:
705 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
706 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
707
708 if (Tmp1 != Node->getOperand(0) ||
709 Tmp2 != Node->getOperand(1)) {
710 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000711 }
712 break;
713 }
714 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000715
Chris Lattner3e928bb2005-01-07 07:47:09 +0000716 case ISD::Constant:
717 // We know we don't need to expand constants here, constants only have one
718 // value and we check that it is fine above.
719
720 // FIXME: Maybe we should handle things like targets that don't support full
721 // 32-bit immediates?
722 break;
723 case ISD::ConstantFP: {
724 // Spill FP immediates to the constant pool if the target cannot directly
725 // codegen them. Targets often have some immediate values that can be
726 // efficiently generated into an FP register without a load. We explicitly
727 // leave these constants as ConstantFP nodes for the target to deal with.
728
729 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
730
731 // Check to see if this FP immediate is already legal.
732 bool isLegal = false;
733 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
734 E = TLI.legal_fpimm_end(); I != E; ++I)
735 if (CFP->isExactlyValue(*I)) {
736 isLegal = true;
737 break;
738 }
739
740 if (!isLegal) {
741 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000742 bool Extend = false;
743
744 // If a FP immediate is precise when represented as a float, we put it
745 // into the constant pool as a float, even if it's is statically typed
746 // as a double.
747 MVT::ValueType VT = CFP->getValueType(0);
748 bool isDouble = VT == MVT::f64;
749 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
750 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000751 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
752 // Only do this if the target has a native EXTLOAD instruction from
753 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000754 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000755 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
756 VT = MVT::f32;
757 Extend = true;
758 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000759
Nate Begeman28a6b022005-12-10 02:36:00 +0000760 SDOperand CPIdx =
761 LegalizeOp(DAG.getConstantPool(LLVMC, TLI.getPointerTy()));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000762 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000763 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
764 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000765 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000766 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
767 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000768 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 }
770 break;
771 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000772 case ISD::ConstantVec: {
773 // We assume that vector constants are not legal, and will be immediately
774 // spilled to the constant pool.
775 //
776 // FIXME: revisit this when we have some kind of mechanism by which targets
777 // can decided legality of vector constants, of which there may be very
778 // many.
779 //
780 // Create a ConstantPacked, and put it in the constant pool.
781 std::vector<Constant*> CV;
782 MVT::ValueType VT = Node->getValueType(0);
783 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
784 SDOperand OpN = Node->getOperand(I);
785 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
786 if (MVT::isFloatingPoint(VT))
787 CV.push_back(ConstantFP::get(OpNTy,
788 cast<ConstantFPSDNode>(OpN)->getValue()));
789 else
790 CV.push_back(ConstantUInt::get(OpNTy,
791 cast<ConstantSDNode>(OpN)->getValue()));
792 }
793 Constant *CP = ConstantPacked::get(CV);
Nate Begemand7d746f2005-12-13 03:03:23 +0000794 SDOperand CPIdx = LegalizeOp(DAG.getConstantPool(CP, TLI.getPointerTy()));
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000795 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
796 break;
797 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000798 case ISD::TokenFactor:
799 if (Node->getNumOperands() == 2) {
800 bool Changed = false;
801 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
802 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
803 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
804 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
805 } else {
806 std::vector<SDOperand> Ops;
807 bool Changed = false;
808 // Legalize the operands.
809 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
810 SDOperand Op = Node->getOperand(i);
811 Ops.push_back(LegalizeOp(Op));
812 Changed |= Ops[i] != Op;
813 }
814 if (Changed)
815 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000816 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000817 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000818
Chris Lattner16cd04d2005-05-12 23:24:06 +0000819 case ISD::CALLSEQ_START:
820 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000822 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000823 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000824 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000825 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000826
Chris Lattner16cd04d2005-05-12 23:24:06 +0000827 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000828 // nodes are treated specially and are mutated in place. This makes the dag
829 // legalization process more efficient and also makes libcall insertion
830 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000831 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000832 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000833 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
834 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
835 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
836 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000837 Tmp3 != Node->getOperand(2)) {
838 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
839 std::vector<SDOperand> Ops;
840 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
841 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
842 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000843 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000844
Evan Chenga7dce3c2006-01-11 22:14:47 +0000845 switch (TLI.getOperationAction(Node->getOpcode(),
846 Node->getValueType(0))) {
847 default: assert(0 && "This action is not supported yet!");
848 case TargetLowering::Custom: {
849 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
850 if (Tmp.Val) {
851 Result = LegalizeOp(Tmp);
852 }
853 // FALLTHROUGH if the target thinks it is legal.
854 }
855 case TargetLowering::Legal:
856 // Since this op produce two values, make sure to remember that we
857 // legalized both of them.
858 AddLegalizedOperand(SDOperand(Node, 0), Result);
859 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
860 return Result.getValue(Op.ResNo);
861 }
862 assert(0 && "Unreachable");
863 }
Chris Lattnerd71c0412005-05-13 18:43:43 +0000864 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000865 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000866 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
867 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000868
869 bool Changed = false;
870 std::vector<SDOperand> Ops;
871 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
872 Ops.push_back(LegalizeOp(Node->getOperand(i)));
873 Changed |= Ops.back() != Node->getOperand(i);
874 }
875
876 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000877 std::vector<MVT::ValueType> RetTyVTs;
878 RetTyVTs.reserve(Node->getNumValues());
879 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000880 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000881 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
882 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000883 } else {
884 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000885 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000886 // Since calls produce multiple values, make sure to remember that we
887 // legalized all of them.
888 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
889 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
890 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000891 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000892 case ISD::BR:
893 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
894 if (Tmp1 != Node->getOperand(0))
895 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
896 break;
897
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000898 case ISD::BRCOND:
899 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000900
Chris Lattner47e92232005-01-18 19:27:06 +0000901 switch (getTypeAction(Node->getOperand(1).getValueType())) {
902 case Expand: assert(0 && "It's impossible to expand bools");
903 case Legal:
904 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
905 break;
906 case Promote:
907 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
908 break;
909 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000910
911 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
912 default: assert(0 && "This action is not supported yet!");
913 case TargetLowering::Expand:
914 // Expand brcond's setcc into its constituent parts and create a BR_CC
915 // Node.
916 if (Tmp2.getOpcode() == ISD::SETCC) {
917 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
918 Tmp2.getOperand(0), Tmp2.getOperand(1),
919 Node->getOperand(2));
920 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000921 // Make sure the condition is either zero or one. It may have been
922 // promoted from something else.
923 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
924
Nate Begeman7cbd5252005-08-16 19:49:35 +0000925 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
926 DAG.getCondCode(ISD::SETNE), Tmp2,
927 DAG.getConstant(0, Tmp2.getValueType()),
928 Node->getOperand(2));
929 }
Chris Lattnere7736732005-12-18 23:54:29 +0000930 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000931 break;
Evan Cheng898101c2005-12-19 23:12:38 +0000932 case TargetLowering::Custom: {
933 SDOperand Tmp =
934 TLI.LowerOperation(DAG.getNode(ISD::BRCOND, Node->getValueType(0),
935 Tmp1, Tmp2, Node->getOperand(2)), DAG);
936 if (Tmp.Val) {
937 Result = LegalizeOp(Tmp);
938 break;
939 }
940 // FALLTHROUGH if the target thinks it is legal.
941 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000942 case TargetLowering::Legal:
943 // Basic block destination (Op#2) is always legal.
944 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
945 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
946 Node->getOperand(2));
947 break;
948 }
949 break;
950 case ISD::BR_CC:
951 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner181b7a32005-12-17 23:46:46 +0000952 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000953 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
954 Node->getOperand(2), // LHS
955 Node->getOperand(3), // RHS
956 Node->getOperand(1)));
957 // If we get a SETCC back from legalizing the SETCC node we just
958 // created, then use its LHS, RHS, and CC directly in creating a new
959 // node. Otherwise, select between the true and false value based on
960 // comparing the result of the legalized with zero.
961 if (Tmp2.getOpcode() == ISD::SETCC) {
962 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
963 Tmp2.getOperand(0), Tmp2.getOperand(1),
964 Node->getOperand(4));
965 } else {
966 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
967 DAG.getCondCode(ISD::SETNE),
968 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
969 Node->getOperand(4));
970 }
Chris Lattner181b7a32005-12-17 23:46:46 +0000971 break;
972 }
973
974 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
975 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
976
977 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
978 default: assert(0 && "Unexpected action for BR_CC!");
979 case TargetLowering::Custom: {
980 Tmp4 = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
981 Tmp2, Tmp3, Node->getOperand(4));
982 Tmp4 = TLI.LowerOperation(Tmp4, DAG);
983 if (Tmp4.Val) {
984 Result = LegalizeOp(Tmp4);
985 break;
986 }
987 } // FALLTHROUGH if the target doesn't want to lower this op after all.
988 case TargetLowering::Legal:
989 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
990 Tmp3 != Node->getOperand(3)) {
991 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
992 Tmp2, Tmp3, Node->getOperand(4));
993 }
994 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000995 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000996 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000997 case ISD::BRCONDTWOWAY:
998 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
999 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1000 case Expand: assert(0 && "It's impossible to expand bools");
1001 case Legal:
1002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1003 break;
1004 case Promote:
1005 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1006 break;
1007 }
1008 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
1009 // pair.
1010 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
1011 case TargetLowering::Promote:
1012 default: assert(0 && "This action is not supported yet!");
1013 case TargetLowering::Legal:
1014 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1015 std::vector<SDOperand> Ops;
1016 Ops.push_back(Tmp1);
1017 Ops.push_back(Tmp2);
1018 Ops.push_back(Node->getOperand(2));
1019 Ops.push_back(Node->getOperand(3));
1020 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
1021 }
1022 break;
1023 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +00001024 // If BRTWOWAY_CC is legal for this target, then simply expand this node
1025 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
1026 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001027 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001028 if (Tmp2.getOpcode() == ISD::SETCC) {
1029 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1030 Tmp2.getOperand(0), Tmp2.getOperand(1),
1031 Node->getOperand(2), Node->getOperand(3));
1032 } else {
1033 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1034 DAG.getConstant(0, Tmp2.getValueType()),
1035 Node->getOperand(2), Node->getOperand(3));
1036 }
1037 } else {
1038 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +00001039 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001040 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
1041 }
Chris Lattnere7736732005-12-18 23:54:29 +00001042 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner411e8882005-04-09 03:30:19 +00001043 break;
1044 }
1045 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001046 case ISD::BRTWOWAY_CC:
1047 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001048 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +00001049 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
1050 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
1051 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
1052 Tmp3 != Node->getOperand(3)) {
1053 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
1054 Node->getOperand(4), Node->getOperand(5));
1055 }
1056 break;
1057 } else {
1058 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1059 Node->getOperand(2), // LHS
1060 Node->getOperand(3), // RHS
1061 Node->getOperand(1)));
1062 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
1063 // pair.
1064 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1065 default: assert(0 && "This action is not supported yet!");
1066 case TargetLowering::Legal:
1067 // If we get a SETCC back from legalizing the SETCC node we just
1068 // created, then use its LHS, RHS, and CC directly in creating a new
1069 // node. Otherwise, select between the true and false value based on
1070 // comparing the result of the legalized with zero.
1071 if (Tmp2.getOpcode() == ISD::SETCC) {
1072 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
1073 Tmp2.getOperand(0), Tmp2.getOperand(1),
1074 Node->getOperand(4), Node->getOperand(5));
1075 } else {
1076 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
1077 DAG.getConstant(0, Tmp2.getValueType()),
1078 Node->getOperand(4), Node->getOperand(5));
1079 }
1080 break;
1081 case TargetLowering::Expand:
1082 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
1083 Node->getOperand(4));
1084 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
1085 break;
1086 }
Chris Lattnerf9dee6a2005-12-21 19:40:42 +00001087 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman7cbd5252005-08-16 19:49:35 +00001088 }
1089 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001090 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001091 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1092 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001093
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001094 MVT::ValueType VT = Node->getValueType(0);
1095 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1096 default: assert(0 && "This action is not supported yet!");
1097 case TargetLowering::Custom: {
1098 SDOperand Op = DAG.getLoad(Node->getValueType(0),
1099 Tmp1, Tmp2, Node->getOperand(2));
1100 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1101 if (Tmp.Val) {
1102 Result = LegalizeOp(Tmp);
1103 // Since loads produce two values, make sure to remember that we legalized
1104 // both of them.
1105 AddLegalizedOperand(SDOperand(Node, 0), Result);
1106 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1107 return Result.getValue(Op.ResNo);
1108 }
1109 // FALLTHROUGH if the target thinks it is legal.
1110 }
1111 case TargetLowering::Legal:
1112 if (Tmp1 != Node->getOperand(0) ||
1113 Tmp2 != Node->getOperand(1))
1114 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
1115 Node->getOperand(2));
1116 else
1117 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001118
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001119 // Since loads produce two values, make sure to remember that we legalized
1120 // both of them.
1121 AddLegalizedOperand(SDOperand(Node, 0), Result);
1122 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1123 return Result.getValue(Op.ResNo);
1124 }
1125 assert(0 && "Unreachable");
1126 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001127 case ISD::EXTLOAD:
1128 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001129 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001130 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1131 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001132
Chris Lattner5f056bf2005-07-10 01:55:33 +00001133 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001134 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001135 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001136 case TargetLowering::Promote:
1137 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001138 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1139 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001140 // Since loads produce two values, make sure to remember that we legalized
1141 // both of them.
1142 AddLegalizedOperand(SDOperand(Node, 0), Result);
1143 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1144 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001145
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001146 case TargetLowering::Custom: {
1147 SDOperand Op = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1148 Tmp1, Tmp2, Node->getOperand(2),
1149 SrcVT);
1150 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
1151 if (Tmp.Val) {
1152 Result = LegalizeOp(Tmp);
1153 // Since loads produce two values, make sure to remember that we legalized
1154 // both of them.
1155 AddLegalizedOperand(SDOperand(Node, 0), Result);
1156 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1157 return Result.getValue(Op.ResNo);
1158 }
1159 // FALLTHROUGH if the target thinks it is legal.
1160 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001161 case TargetLowering::Legal:
1162 if (Tmp1 != Node->getOperand(0) ||
1163 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001164 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
1165 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001166 else
1167 Result = SDOperand(Node, 0);
1168
1169 // Since loads produce two values, make sure to remember that we legalized
1170 // both of them.
1171 AddLegalizedOperand(SDOperand(Node, 0), Result);
1172 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1173 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +00001174 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001175 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001176 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1177 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001178 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnere7736732005-12-18 23:54:29 +00001179 Result = LegalizeOp(Result); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001180 Load = LegalizeOp(Load);
1181 AddLegalizedOperand(SDOperand(Node, 0), Result);
1182 AddLegalizedOperand(SDOperand(Node, 1), Load.getValue(1));
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001183 if (Op.ResNo)
1184 return Load.getValue(1);
1185 return Result;
1186 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001187 assert(Node->getOpcode() != ISD::EXTLOAD &&
1188 "EXTLOAD should always be supported!");
1189 // Turn the unsupported load into an EXTLOAD followed by an explicit
1190 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001191 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1192 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001193 SDOperand ValRes;
1194 if (Node->getOpcode() == ISD::SEXTLOAD)
1195 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001196 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001197 else
1198 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnere7736732005-12-18 23:54:29 +00001199 Result = LegalizeOp(Result); // Relegalize new nodes.
1200 ValRes = LegalizeOp(ValRes); // Relegalize new nodes.
Chris Lattner69a889e2005-12-20 00:53:54 +00001201 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1202 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattner01ff7212005-04-10 22:54:25 +00001203 if (Op.ResNo)
1204 return Result.getValue(1);
1205 return ValRes;
1206 }
1207 assert(0 && "Unreachable");
1208 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001209 case ISD::EXTRACT_ELEMENT: {
1210 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1211 switch (getTypeAction(OpTy)) {
1212 default:
1213 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1214 break;
1215 case Legal:
1216 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1217 // 1 -> Hi
1218 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1219 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1220 TLI.getShiftAmountTy()));
1221 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1222 } else {
1223 // 0 -> Lo
1224 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1225 Node->getOperand(0));
1226 }
1227 Result = LegalizeOp(Result);
1228 break;
1229 case Expand:
1230 // Get both the low and high parts.
1231 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1232 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1233 Result = Tmp2; // 1 -> Hi
1234 else
1235 Result = Tmp1; // 0 -> Lo
1236 break;
1237 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001238 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001239 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001240
1241 case ISD::CopyToReg:
1242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001243
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001244 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001245 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001246 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001247 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001248 if (Node->getNumValues() == 1) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001249 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1250 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1251 Node->getOperand(1), Tmp2);
1252 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001253 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
1254 if (Node->getNumOperands() == 4)
1255 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattner7310fb12005-12-18 15:27:43 +00001256 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001257 (Node->getNumOperands() == 4 && Tmp3 != Node->getOperand(3))) {
Chris Lattner7310fb12005-12-18 15:27:43 +00001258 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
1259 Result = DAG.getCopyToReg(Tmp1, Reg, Tmp2, Tmp3);
1260 }
1261
1262 // Since this produces two values, make sure to remember that we legalized
1263 // both of them.
1264 AddLegalizedOperand(SDOperand(Node, 0), Result);
1265 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1266 return Result.getValue(Op.ResNo);
1267 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001268 break;
1269
1270 case ISD::RET:
1271 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1272 switch (Node->getNumOperands()) {
1273 case 2: // ret val
1274 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1275 case Legal:
1276 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001277 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001278 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1279 break;
1280 case Expand: {
1281 SDOperand Lo, Hi;
1282 ExpandOp(Node->getOperand(1), Lo, Hi);
1283 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001284 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001285 }
1286 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001287 Tmp2 = PromoteOp(Node->getOperand(1));
1288 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1289 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001290 }
1291 break;
1292 case 1: // ret void
1293 if (Tmp1 != Node->getOperand(0))
1294 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1295 break;
1296 default: { // ret <values>
1297 std::vector<SDOperand> NewValues;
1298 NewValues.push_back(Tmp1);
1299 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1300 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1301 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001302 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001303 break;
1304 case Expand: {
1305 SDOperand Lo, Hi;
1306 ExpandOp(Node->getOperand(i), Lo, Hi);
1307 NewValues.push_back(Lo);
1308 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001309 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001310 }
1311 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001312 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001313 }
1314 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1315 break;
1316 }
1317 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001318
Chris Lattner47f5bea2006-01-06 05:47:48 +00001319 switch (TLI.getOperationAction(Node->getOpcode(),
1320 Node->getValueType(0))) {
Evan Cheng17c428e2006-01-06 00:41:43 +00001321 default: assert(0 && "This action is not supported yet!");
1322 case TargetLowering::Custom: {
1323 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1324 if (Tmp.Val) {
1325 Result = LegalizeOp(Tmp);
1326 break;
1327 }
1328 // FALLTHROUGH if the target thinks it is legal.
1329 }
1330 case TargetLowering::Legal:
1331 // Nothing to do.
1332 break;
1333 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001334 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001335 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001336 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1337 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1338
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001339 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001340 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001341 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001342 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001343 DAG.getConstant(FloatToBits(CFP->getValue()),
1344 MVT::i32),
1345 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001346 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001347 } else {
1348 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001349 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001350 DAG.getConstant(DoubleToBits(CFP->getValue()),
1351 MVT::i64),
1352 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001353 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001354 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001355 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001356 }
1357
Chris Lattner3e928bb2005-01-07 07:47:09 +00001358 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1359 case Legal: {
1360 SDOperand Val = LegalizeOp(Node->getOperand(1));
1361 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1362 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001363 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1364 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001365
1366 MVT::ValueType VT = Result.Val->getOperand(1).getValueType();
1367 switch (TLI.getOperationAction(Result.Val->getOpcode(), VT)) {
1368 default: assert(0 && "This action is not supported yet!");
1369 case TargetLowering::Custom: {
1370 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1371 if (Tmp.Val) {
1372 Result = LegalizeOp(Tmp);
1373 break;
1374 }
1375 // FALLTHROUGH if the target thinks it is legal.
1376 }
1377 case TargetLowering::Legal:
1378 // Nothing to do.
1379 break;
1380 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001381 break;
1382 }
1383 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001384 // Truncate the value and store the result.
1385 Tmp3 = PromoteOp(Node->getOperand(1));
1386 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001387 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001388 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001389 break;
1390
Chris Lattner3e928bb2005-01-07 07:47:09 +00001391 case Expand:
1392 SDOperand Lo, Hi;
Nate Begemanab48be32005-11-22 18:16:00 +00001393 unsigned IncrementSize;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001394 ExpandOp(Node->getOperand(1), Lo, Hi);
1395
1396 if (!TLI.isLittleEndian())
1397 std::swap(Lo, Hi);
1398
Chris Lattneredb1add2005-05-11 04:51:16 +00001399 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1400 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001401 // If this is a vector type, then we have to calculate the increment as
1402 // the product of the element size in bytes, and the number of elements
1403 // in the high half of the vector.
1404 if (MVT::Vector == Hi.getValueType()) {
1405 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1406 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1407 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1408 } else {
1409 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1410 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001411 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1412 getIntPtrConstant(IncrementSize));
1413 assert(isTypeLegal(Tmp2.getValueType()) &&
1414 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001415 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001416 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1417 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001418 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1419 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001420 }
1421 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001422 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001423 case ISD::PCMARKER:
1424 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001425 if (Tmp1 != Node->getOperand(0))
1426 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001427 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001428 case ISD::STACKSAVE:
1429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1430 if (Tmp1 != Node->getOperand(0)) {
1431 std::vector<MVT::ValueType> VTs;
1432 VTs.push_back(Node->getValueType(0));
1433 VTs.push_back(MVT::Other);
1434 std::vector<SDOperand> Ops;
1435 Ops.push_back(Tmp1);
1436 Result = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1437 }
1438
1439 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1440 default: assert(0 && "This action is not supported yet!");
1441 case TargetLowering::Custom: {
1442 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1443 if (Tmp.Val) {
1444 Result = LegalizeOp(Tmp);
1445 break;
1446 }
1447 // FALLTHROUGH if the target thinks it is legal.
1448 }
1449 case TargetLowering::Legal:
1450 // Since stacksave produce two values, make sure to remember that we
1451 // legalized both of them.
1452 AddLegalizedOperand(SDOperand(Node, 0), Result);
1453 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1454 return Result.getValue(Op.ResNo);
1455 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001456 // Expand to CopyFromReg if the target set
1457 // StackPointerRegisterToSaveRestore.
1458 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1459 Tmp1 = DAG.getCopyFromReg(Node->getOperand(0), SP,
1460 Node->getValueType(0));
1461 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1462 AddLegalizedOperand(SDOperand(Node, 1), Tmp1.getValue(1));
1463 return Tmp1.getValue(Op.ResNo);
1464 } else {
1465 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1466 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1467 AddLegalizedOperand(SDOperand(Node, 1), Node->getOperand(0));
1468 return Op.ResNo ? Node->getOperand(0) : Tmp1;
1469 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001470 }
1471
1472 case ISD::STACKRESTORE:
1473 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1474 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1475 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1476 Result = DAG.getNode(ISD::STACKRESTORE, MVT::Other, Tmp1, Tmp2);
1477
1478 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1479 default: assert(0 && "This action is not supported yet!");
1480 case TargetLowering::Custom: {
1481 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1482 if (Tmp.Val) {
1483 Result = LegalizeOp(Tmp);
1484 break;
1485 }
1486 // FALLTHROUGH if the target thinks it is legal.
1487 }
1488 case TargetLowering::Legal:
1489 break;
1490 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001491 // Expand to CopyToReg if the target set
1492 // StackPointerRegisterToSaveRestore.
1493 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1494 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1495 } else {
1496 Result = Tmp1;
1497 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001498 break;
1499 }
1500 break;
1501
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001502 case ISD::READCYCLECOUNTER:
1503 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Andrew Lenharthcde0f5c2005-12-02 06:08:08 +00001504 if (Tmp1 != Node->getOperand(0)) {
1505 std::vector<MVT::ValueType> rtypes;
1506 std::vector<SDOperand> rvals;
1507 rtypes.push_back(MVT::i64);
1508 rtypes.push_back(MVT::Other);
1509 rvals.push_back(Tmp1);
1510 Result = DAG.getNode(ISD::READCYCLECOUNTER, rtypes, rvals);
1511 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001512
1513 // Since rdcc produce two values, make sure to remember that we legalized
1514 // both of them.
1515 AddLegalizedOperand(SDOperand(Node, 0), Result);
1516 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1517 return Result.getValue(Op.ResNo);
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001518
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001519 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001520 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1521 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1522
1523 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattnerc26f7a02005-12-23 16:12:20 +00001524 case Promote:
1525 case Expand:
1526 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
Chris Lattner0f69b292005-01-15 06:18:18 +00001527 case Legal:
1528 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001529
1530 // The only promote case we handle is TRUNCSTORE:i1 X into
1531 // -> TRUNCSTORE:i8 (and X, 1)
1532 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1533 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1534 TargetLowering::Promote) {
1535 // Promote the bool to a mask then store.
1536 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1537 DAG.getConstant(1, Tmp2.getValueType()));
1538 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1539 Node->getOperand(3), DAG.getValueType(MVT::i8));
1540
1541 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1542 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001543 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001544 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001545 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001546
1547 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1548 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1549 default: assert(0 && "This action is not supported yet!");
1550 case TargetLowering::Custom: {
1551 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
1552 if (Tmp.Val) {
1553 Result = LegalizeOp(Tmp);
1554 break;
1555 }
1556 // FALLTHROUGH if the target thinks it is legal.
1557 }
1558 case TargetLowering::Legal:
1559 // Nothing to do.
1560 break;
1561 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001562 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001563 }
1564 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001565 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001566 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1568 case Expand: assert(0 && "It's impossible to expand bools");
1569 case Legal:
1570 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1571 break;
1572 case Promote:
1573 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1574 break;
1575 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001576 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001577 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001578
Nate Begemanb942a3d2005-08-23 04:29:48 +00001579 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001580 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001581 case TargetLowering::Expand:
1582 if (Tmp1.getOpcode() == ISD::SETCC) {
1583 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1584 Tmp2, Tmp3,
1585 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1586 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001587 // Make sure the condition is either zero or one. It may have been
1588 // promoted from something else.
1589 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001590 Result = DAG.getSelectCC(Tmp1,
1591 DAG.getConstant(0, Tmp1.getValueType()),
1592 Tmp2, Tmp3, ISD::SETNE);
1593 }
Chris Lattnere7736732005-12-18 23:54:29 +00001594 Result = LegalizeOp(Result); // Relegalize new nodes.
Nate Begeman9373a812005-08-10 20:51:12 +00001595 break;
Evan Cheng7df96d62005-12-17 01:21:05 +00001596 case TargetLowering::Custom: {
1597 SDOperand Tmp =
1598 TLI.LowerOperation(DAG.getNode(ISD::SELECT, Node->getValueType(0),
1599 Tmp1, Tmp2, Tmp3), DAG);
1600 if (Tmp.Val) {
1601 Result = LegalizeOp(Tmp);
1602 break;
1603 }
1604 // FALLTHROUGH if the target thinks it is legal.
1605 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001606 case TargetLowering::Legal:
1607 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1608 Tmp3 != Node->getOperand(2))
1609 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1610 Tmp1, Tmp2, Tmp3);
1611 break;
1612 case TargetLowering::Promote: {
1613 MVT::ValueType NVT =
1614 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1615 unsigned ExtOp, TruncOp;
1616 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001617 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001618 TruncOp = ISD::TRUNCATE;
1619 } else {
1620 ExtOp = ISD::FP_EXTEND;
1621 TruncOp = ISD::FP_ROUND;
1622 }
1623 // Promote each of the values to the new type.
1624 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1625 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1626 // Perform the larger operation, then round down.
1627 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1628 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1629 break;
1630 }
1631 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001632 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001633 case ISD::SELECT_CC:
1634 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1635 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1636
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001637 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001638 // Everything is legal, see if we should expand this op or something.
1639 switch (TLI.getOperationAction(ISD::SELECT_CC,
1640 Node->getOperand(0).getValueType())) {
1641 default: assert(0 && "This action is not supported yet!");
1642 case TargetLowering::Custom: {
1643 SDOperand Tmp =
1644 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1645 Node->getOperand(0),
1646 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001647 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001648 if (Tmp.Val) {
1649 Result = LegalizeOp(Tmp);
1650 break;
1651 }
1652 } // FALLTHROUGH if the target can't lower this operation after all.
1653 case TargetLowering::Legal:
1654 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1655 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1656 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1657 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001658 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1,Tmp2,
Chris Lattner23004e52005-08-26 00:23:59 +00001659 Tmp3, Tmp4, Node->getOperand(4));
1660 }
1661 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001662 }
1663 break;
1664 } else {
1665 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1666 Node->getOperand(0), // LHS
1667 Node->getOperand(1), // RHS
1668 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001669 // If we get a SETCC back from legalizing the SETCC node we just
1670 // created, then use its LHS, RHS, and CC directly in creating a new
1671 // node. Otherwise, select between the true and false value based on
1672 // comparing the result of the legalized with zero.
1673 if (Tmp1.getOpcode() == ISD::SETCC) {
1674 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1675 Tmp1.getOperand(0), Tmp1.getOperand(1),
1676 Tmp3, Tmp4, Tmp1.getOperand(2));
1677 } else {
1678 Result = DAG.getSelectCC(Tmp1,
1679 DAG.getConstant(0, Tmp1.getValueType()),
1680 Tmp3, Tmp4, ISD::SETNE);
1681 }
Nate Begeman9373a812005-08-10 20:51:12 +00001682 }
1683 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001684 case ISD::SETCC:
1685 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1686 case Legal:
1687 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1688 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001689 break;
1690 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001691 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1692 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1693
1694 // If this is an FP compare, the operands have already been extended.
1695 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1696 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001697 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001698
1699 // Otherwise, we have to insert explicit sign or zero extends. Note
1700 // that we could insert sign extends for ALL conditions, but zero extend
1701 // is cheaper on many machines (an AND instead of two shifts), so prefer
1702 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001703 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001704 default: assert(0 && "Unknown integer comparison!");
1705 case ISD::SETEQ:
1706 case ISD::SETNE:
1707 case ISD::SETUGE:
1708 case ISD::SETUGT:
1709 case ISD::SETULE:
1710 case ISD::SETULT:
1711 // ALL of these operations will work if we either sign or zero extend
1712 // the operands (including the unsigned comparisons!). Zero extend is
1713 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001714 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1715 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001716 break;
1717 case ISD::SETGE:
1718 case ISD::SETGT:
1719 case ISD::SETLT:
1720 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001721 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1722 DAG.getValueType(VT));
1723 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1724 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001725 break;
1726 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001727 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001728 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001729 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001730 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1731 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1732 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001733 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001734 case ISD::SETEQ:
1735 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001736 if (RHSLo == RHSHi)
1737 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1738 if (RHSCST->isAllOnesValue()) {
1739 // Comparison to -1.
1740 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001741 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001742 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001743 }
1744
Chris Lattner3e928bb2005-01-07 07:47:09 +00001745 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1746 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1747 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001748 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001749 break;
1750 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001751 // If this is a comparison of the sign bit, just look at the top part.
1752 // X > -1, x < 0
1753 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001754 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001755 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001756 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001757 (CST->isAllOnesValue()))) { // X > -1
1758 Tmp1 = LHSHi;
1759 Tmp2 = RHSHi;
1760 break;
1761 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001762
Chris Lattner3e928bb2005-01-07 07:47:09 +00001763 // FIXME: This generated code sucks.
1764 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001765 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001766 default: assert(0 && "Unknown integer setcc!");
1767 case ISD::SETLT:
1768 case ISD::SETULT: LowCC = ISD::SETULT; break;
1769 case ISD::SETGT:
1770 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1771 case ISD::SETLE:
1772 case ISD::SETULE: LowCC = ISD::SETULE; break;
1773 case ISD::SETGE:
1774 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1775 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001776
Chris Lattner3e928bb2005-01-07 07:47:09 +00001777 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1778 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1779 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1780
1781 // NOTE: on targets without efficient SELECT of bools, we can always use
1782 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001783 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1784 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1785 Node->getOperand(2));
1786 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001787 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1788 Result, Tmp1, Tmp2));
Chris Lattner69a889e2005-12-20 00:53:54 +00001789 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001790 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001791 }
1792 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001793
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001794 switch(TLI.getOperationAction(ISD::SETCC,
1795 Node->getOperand(0).getValueType())) {
Nate Begemanb942a3d2005-08-23 04:29:48 +00001796 default:
1797 assert(0 && "Cannot handle this action for SETCC yet!");
1798 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001799 case TargetLowering::Promote: {
1800 // First step, figure out the appropriate operation to use.
1801 // Allow SETCC to not be supported for all legal data types
1802 // Mostly this targets FP
1803 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1804 MVT::ValueType OldVT = NewInTy;
1805
1806 // Scan for the appropriate larger type to use.
1807 while (1) {
1808 NewInTy = (MVT::ValueType)(NewInTy+1);
1809
1810 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1811 "Fell off of the edge of the integer world");
1812 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1813 "Fell off of the edge of the floating point world");
1814
1815 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001816 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001817 break;
1818 }
1819 if (MVT::isInteger(NewInTy))
1820 assert(0 && "Cannot promote Legal Integer SETCC yet");
1821 else {
1822 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1823 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1824 }
1825
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001826 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1827 Node->getOperand(2));
1828 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001829 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001830 case TargetLowering::Custom: {
1831 SDOperand Tmp =
1832 TLI.LowerOperation(DAG.getNode(ISD::SETCC, Node->getValueType(0),
1833 Tmp1, Tmp2, Node->getOperand(2)), DAG);
1834 if (Tmp.Val) {
1835 Result = LegalizeOp(Tmp);
1836 break;
1837 }
1838 // FALLTHROUGH if the target thinks it is legal.
1839 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001840 case TargetLowering::Legal:
1841 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1842 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1843 Node->getOperand(2));
1844 break;
1845 case TargetLowering::Expand:
1846 // Expand a setcc node into a select_cc of the same condition, lhs, and
1847 // rhs that selects between const 1 (true) and const 0 (false).
1848 MVT::ValueType VT = Node->getValueType(0);
1849 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1850 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1851 Node->getOperand(2));
1852 Result = LegalizeOp(Result);
1853 break;
1854 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001855 break;
1856
Chris Lattnere1bd8222005-01-11 05:57:22 +00001857 case ISD::MEMSET:
1858 case ISD::MEMCPY:
1859 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001861 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1862
1863 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1864 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1865 case Expand: assert(0 && "Cannot expand a byte!");
1866 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001867 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001868 break;
1869 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001870 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001871 break;
1872 }
1873 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001874 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001875 }
Chris Lattner272455b2005-02-02 03:44:41 +00001876
1877 SDOperand Tmp4;
1878 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001879 case Expand: {
1880 // Length is too big, just take the lo-part of the length.
1881 SDOperand HiPart;
1882 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1883 break;
1884 }
Chris Lattnere5605212005-01-28 22:29:18 +00001885 case Legal:
1886 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001887 break;
1888 case Promote:
1889 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001890 break;
1891 }
1892
1893 SDOperand Tmp5;
1894 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1895 case Expand: assert(0 && "Cannot expand this yet!");
1896 case Legal:
1897 Tmp5 = LegalizeOp(Node->getOperand(4));
1898 break;
1899 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001900 Tmp5 = PromoteOp(Node->getOperand(4));
1901 break;
1902 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001903
1904 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1905 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001906 case TargetLowering::Custom: {
1907 SDOperand Tmp =
1908 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1909 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1910 if (Tmp.Val) {
1911 Result = LegalizeOp(Tmp);
1912 break;
1913 }
1914 // FALLTHROUGH if the target thinks it is legal.
1915 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001916 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001917 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1918 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1919 Tmp5 != Node->getOperand(4)) {
1920 std::vector<SDOperand> Ops;
1921 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1922 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1923 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1924 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001925 break;
1926 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001927 // Otherwise, the target does not support this operation. Lower the
1928 // operation to an explicit libcall as appropriate.
1929 MVT::ValueType IntPtr = TLI.getPointerTy();
1930 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1931 std::vector<std::pair<SDOperand, const Type*> > Args;
1932
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001933 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001934 if (Node->getOpcode() == ISD::MEMSET) {
1935 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1936 // Extend the ubyte argument to be an int value for the call.
1937 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1938 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1939 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1940
1941 FnName = "memset";
1942 } else if (Node->getOpcode() == ISD::MEMCPY ||
1943 Node->getOpcode() == ISD::MEMMOVE) {
1944 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1945 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1946 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1947 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1948 } else {
1949 assert(0 && "Unknown op!");
1950 }
Chris Lattner45982da2005-05-12 16:53:42 +00001951
Chris Lattnere1bd8222005-01-11 05:57:22 +00001952 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001953 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001954 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner69a889e2005-12-20 00:53:54 +00001955 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001956 break;
1957 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001958 }
1959 break;
1960 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001961
1962 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001963 Tmp1 = LegalizeOp(Node->getOperand(0));
1964 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001965
Chris Lattner3e011362005-05-14 07:45:46 +00001966 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1967 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1968 std::vector<SDOperand> Ops;
1969 Ops.push_back(Tmp1);
1970 Ops.push_back(Tmp2);
1971 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1972 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001973 Result = SDOperand(Node, 0);
1974 // Since these produce two values, make sure to remember that we legalized
1975 // both of them.
1976 AddLegalizedOperand(SDOperand(Node, 0), Result);
1977 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1978 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001979 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001980 Tmp1 = LegalizeOp(Node->getOperand(0));
1981 Tmp2 = LegalizeOp(Node->getOperand(1));
1982 Tmp3 = LegalizeOp(Node->getOperand(2));
1983 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1984 Tmp3 != Node->getOperand(2))
1985 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1986 break;
1987
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001988 case ISD::READIO:
1989 Tmp1 = LegalizeOp(Node->getOperand(0));
1990 Tmp2 = LegalizeOp(Node->getOperand(1));
1991
1992 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1993 case TargetLowering::Custom:
1994 default: assert(0 && "This action not implemented for this operation!");
1995 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001996 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1997 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1998 std::vector<SDOperand> Ops;
1999 Ops.push_back(Tmp1);
2000 Ops.push_back(Tmp2);
2001 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
2002 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00002003 Result = SDOperand(Node, 0);
2004 break;
2005 case TargetLowering::Expand:
2006 // Replace this with a load from memory.
2007 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
2008 Node->getOperand(1), DAG.getSrcValue(NULL));
2009 Result = LegalizeOp(Result);
2010 break;
2011 }
2012
2013 // Since these produce two values, make sure to remember that we legalized
2014 // both of them.
2015 AddLegalizedOperand(SDOperand(Node, 0), Result);
2016 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2017 return Result.getValue(Op.ResNo);
2018
2019 case ISD::WRITEIO:
2020 Tmp1 = LegalizeOp(Node->getOperand(0));
2021 Tmp2 = LegalizeOp(Node->getOperand(1));
2022 Tmp3 = LegalizeOp(Node->getOperand(2));
2023
2024 switch (TLI.getOperationAction(Node->getOpcode(),
2025 Node->getOperand(1).getValueType())) {
2026 case TargetLowering::Custom:
2027 default: assert(0 && "This action not implemented for this operation!");
2028 case TargetLowering::Legal:
2029 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
2030 Tmp3 != Node->getOperand(2))
2031 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
2032 break;
2033 case TargetLowering::Expand:
2034 // Replace this with a store to memory.
2035 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
2036 Node->getOperand(1), Node->getOperand(2),
2037 DAG.getSrcValue(NULL));
2038 Result = LegalizeOp(Result);
2039 break;
2040 }
2041 break;
2042
Chris Lattner84f67882005-01-20 18:52:28 +00002043 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00002044 case ISD::SUB_PARTS:
2045 case ISD::SHL_PARTS:
2046 case ISD::SRA_PARTS:
2047 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00002048 std::vector<SDOperand> Ops;
2049 bool Changed = false;
2050 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2051 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2052 Changed |= Ops.back() != Node->getOperand(i);
2053 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002054 if (Changed) {
2055 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
2056 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
2057 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002058
Evan Cheng05a2d562006-01-09 18:31:59 +00002059 switch (TLI.getOperationAction(Node->getOpcode(),
2060 Node->getValueType(0))) {
2061 default: assert(0 && "This action is not supported yet!");
2062 case TargetLowering::Custom: {
2063 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2064 if (Tmp.Val) {
Chris Lattner269f8c02006-01-10 19:43:26 +00002065 SDOperand Tmp2, RetVal(0,0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002066 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
2067 Tmp2 = LegalizeOp(Tmp.getValue(i));
2068 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2069 if (i == Op.ResNo)
2070 RetVal = Tmp;
2071 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002072 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002073 return RetVal;
2074 }
2075 // FALLTHROUGH if the target thinks it is legal.
2076 }
2077 case TargetLowering::Legal:
2078 // Nothing to do.
2079 break;
2080 }
2081
Chris Lattner2c8086f2005-04-02 05:00:07 +00002082 // Since these produce multiple values, make sure to remember that we
2083 // legalized all of them.
2084 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2085 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2086 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002087 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002088
2089 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002090 case ISD::ADD:
2091 case ISD::SUB:
2092 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002093 case ISD::MULHS:
2094 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002095 case ISD::UDIV:
2096 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002097 case ISD::AND:
2098 case ISD::OR:
2099 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002100 case ISD::SHL:
2101 case ISD::SRL:
2102 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002103 case ISD::FADD:
2104 case ISD::FSUB:
2105 case ISD::FMUL:
2106 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002107 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002108 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2109 case Expand: assert(0 && "Not possible");
2110 case Legal:
2111 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2112 break;
2113 case Promote:
2114 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2115 break;
2116 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002117 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002118 case TargetLowering::Custom: {
2119 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2120 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2121 if (Tmp.Val) {
2122 Tmp = LegalizeOp(Tmp); // Relegalize input.
2123 AddLegalizedOperand(Op, Tmp);
2124 return Tmp;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002125 } //else it was considered legal and we fall through
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002126 }
Andrew Lenharth57030e32005-12-25 01:07:37 +00002127 case TargetLowering::Legal:
2128 if (Tmp1 != Node->getOperand(0) ||
2129 Tmp2 != Node->getOperand(1))
2130 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
2131 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002132 default:
2133 assert(0 && "Operation not supported");
2134 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002135 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002136
Nate Begeman419f8b62005-10-18 00:27:41 +00002137 case ISD::BUILD_PAIR: {
2138 MVT::ValueType PairTy = Node->getValueType(0);
2139 // TODO: handle the case where the Lo and Hi operands are not of legal type
2140 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2141 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2142 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
2143 case TargetLowering::Legal:
2144 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2145 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2146 break;
2147 case TargetLowering::Promote:
2148 case TargetLowering::Custom:
2149 assert(0 && "Cannot promote/custom this yet!");
2150 case TargetLowering::Expand:
2151 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2152 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2153 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2154 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2155 TLI.getShiftAmountTy()));
2156 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
2157 break;
2158 }
2159 break;
2160 }
2161
Nate Begemanc105e192005-04-06 00:23:54 +00002162 case ISD::UREM:
2163 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002164 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002165 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2166 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2167 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth57030e32005-12-25 01:07:37 +00002168 case TargetLowering::Custom: {
2169 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2);
2170 SDOperand Tmp = TLI.LowerOperation(Result, DAG);
2171 if (Tmp.Val) {
2172 Tmp = LegalizeOp(Tmp); // Relegalize input.
2173 AddLegalizedOperand(Op, Tmp);
2174 return Tmp;
2175 } //else it was considered legal and we fall through
2176 }
Nate Begemanc105e192005-04-06 00:23:54 +00002177 case TargetLowering::Legal:
2178 if (Tmp1 != Node->getOperand(0) ||
2179 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00002180 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00002181 Tmp2);
2182 break;
2183 case TargetLowering::Promote:
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002184 assert(0 && "Cannot promote handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00002185 case TargetLowering::Expand:
2186 if (MVT::isInteger(Node->getValueType(0))) {
2187 MVT::ValueType VT = Node->getValueType(0);
2188 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
2189 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2190 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2191 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2192 } else {
2193 // Floating point mod -> fmod libcall.
2194 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2195 SDOperand Dummy;
2196 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002197 }
2198 break;
2199 }
2200 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002201
Nate Begeman35ef9132006-01-11 21:21:00 +00002202 case ISD::ROTL:
2203 case ISD::ROTR:
2204 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2205 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2206 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2207 case TargetLowering::Custom:
2208 case TargetLowering::Promote:
2209 case TargetLowering::Expand:
2210 assert(0 && "Cannot handle this yet!");
2211 case TargetLowering::Legal:
2212 if (Tmp1 != Node->getOperand(0) ||
2213 Tmp2 != Node->getOperand(1))
2214 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
2215 Tmp2);
2216 break;
2217 }
2218 break;
2219
Nate Begemand88fc032006-01-14 03:14:10 +00002220 case ISD::BSWAP:
2221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2222 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2223 case TargetLowering::Legal:
2224 if (Tmp1 != Node->getOperand(0))
2225 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2226 break;
2227 case TargetLowering::Promote: {
2228 MVT::ValueType OVT = Tmp1.getValueType();
2229 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2230 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
2231
2232 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2233 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2234 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2235 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2236 break;
2237 }
2238 case TargetLowering::Custom:
2239 assert(0 && "Cannot custom legalize this yet!");
2240 case TargetLowering::Expand: {
2241 MVT::ValueType VT = Tmp1.getValueType();
2242 switch (VT) {
2243 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
2244 case MVT::i16:
2245 Tmp2 = DAG.getNode(ISD::SHL, VT, Tmp1,
2246 DAG.getConstant(8, TLI.getShiftAmountTy()));
2247 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2248 DAG.getConstant(8, TLI.getShiftAmountTy()));
2249 Result = DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
2250 break;
2251 case MVT::i32:
2252 Tmp4 = DAG.getNode(ISD::SHL, VT, Tmp1,
2253 DAG.getConstant(24, TLI.getShiftAmountTy()));
2254 Tmp3 = DAG.getNode(ISD::SHL, VT, Tmp1,
2255 DAG.getConstant(8, TLI.getShiftAmountTy()));
2256 Tmp2 = DAG.getNode(ISD::SRL, VT, Tmp1,
2257 DAG.getConstant(8, TLI.getShiftAmountTy()));
2258 Tmp1 = DAG.getNode(ISD::SRL, VT, Tmp1,
2259 DAG.getConstant(24, TLI.getShiftAmountTy()));
2260 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
2261 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
2262 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
2263 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
2264 Result = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
2265 break;
2266 }
2267 break;
2268 }
2269 }
2270 break;
2271
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002272 case ISD::CTPOP:
2273 case ISD::CTTZ:
2274 case ISD::CTLZ:
2275 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2276 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2277 case TargetLowering::Legal:
2278 if (Tmp1 != Node->getOperand(0))
2279 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2280 break;
2281 case TargetLowering::Promote: {
2282 MVT::ValueType OVT = Tmp1.getValueType();
2283 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002284
2285 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002286 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2287 // Perform the larger operation, then subtract if needed.
2288 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2289 switch(Node->getOpcode())
2290 {
2291 case ISD::CTPOP:
2292 Result = Tmp1;
2293 break;
2294 case ISD::CTTZ:
2295 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002296 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2297 DAG.getConstant(getSizeInBits(NVT), NVT),
2298 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002299 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002300 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2301 break;
2302 case ISD::CTLZ:
2303 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002304 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2305 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002306 getSizeInBits(OVT), NVT));
2307 break;
2308 }
2309 break;
2310 }
2311 case TargetLowering::Custom:
2312 assert(0 && "Cannot custom handle this yet!");
2313 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002314 switch(Node->getOpcode())
2315 {
2316 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002317 static const uint64_t mask[6] = {
2318 0x5555555555555555ULL, 0x3333333333333333ULL,
2319 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
2320 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
2321 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002322 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002323 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2324 unsigned len = getSizeInBits(VT);
2325 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002326 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00002327 Tmp2 = DAG.getConstant(mask[i], VT);
2328 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002329 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002330 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
2331 DAG.getNode(ISD::AND, VT,
2332 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
2333 Tmp2));
2334 }
2335 Result = Tmp1;
2336 break;
2337 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00002338 case ISD::CTLZ: {
2339 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002340 x = x | (x >> 1);
2341 x = x | (x >> 2);
2342 ...
2343 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00002344 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002345 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00002346
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002347 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
2348 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00002349 MVT::ValueType ShVT = TLI.getShiftAmountTy();
2350 unsigned len = getSizeInBits(VT);
2351 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
2352 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002353 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00002354 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
2355 }
2356 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002357 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00002358 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002359 }
2360 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00002361 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002362 // unless the target has ctlz but not ctpop, in which case we use:
2363 // { return 32 - nlz(~x & (x-1)); }
2364 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002365 MVT::ValueType VT = Tmp1.getValueType();
2366 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00002367 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00002368 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
2369 DAG.getNode(ISD::SUB, VT, Tmp1,
2370 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002371 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002372 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
2373 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00002374 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002375 DAG.getConstant(getSizeInBits(VT), VT),
2376 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
2377 } else {
2378 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
2379 }
Chris Lattner18aa6802005-05-11 05:27:09 +00002380 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00002381 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00002382 default:
2383 assert(0 && "Cannot expand this yet!");
2384 break;
2385 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002386 break;
2387 }
2388 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002389
Chris Lattner2c8086f2005-04-02 05:00:07 +00002390 // Unary operators
2391 case ISD::FABS:
2392 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002393 case ISD::FSQRT:
2394 case ISD::FSIN:
2395 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002396 Tmp1 = LegalizeOp(Node->getOperand(0));
2397 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2398 case TargetLowering::Legal:
2399 if (Tmp1 != Node->getOperand(0))
2400 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2401 break;
2402 case TargetLowering::Promote:
2403 case TargetLowering::Custom:
2404 assert(0 && "Cannot promote/custom handle this yet!");
2405 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002406 switch(Node->getOpcode()) {
2407 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00002408 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2409 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00002410 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00002411 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002412 break;
2413 }
2414 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002415 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2416 MVT::ValueType VT = Node->getValueType(0);
2417 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002418 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002419 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2420 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
2421 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002422 break;
2423 }
2424 case ISD::FSQRT:
2425 case ISD::FSIN:
2426 case ISD::FCOS: {
2427 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002428 const char *FnName = 0;
2429 switch(Node->getOpcode()) {
2430 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2431 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2432 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2433 default: assert(0 && "Unreachable!");
2434 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002435 SDOperand Dummy;
2436 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002437 break;
2438 }
2439 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002440 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00002441 }
2442 break;
2443 }
2444 break;
Chris Lattner35481892005-12-23 00:16:34 +00002445
2446 case ISD::BIT_CONVERT:
2447 if (!isTypeLegal(Node->getOperand(0).getValueType()))
2448 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2449 else {
2450 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2451 Node->getOperand(0).getValueType())) {
2452 default: assert(0 && "Unknown operation action!");
2453 case TargetLowering::Expand:
2454 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2455 break;
2456 case TargetLowering::Legal:
2457 Tmp1 = LegalizeOp(Node->getOperand(0));
2458 if (Tmp1 != Node->getOperand(0))
2459 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Tmp1);
2460 break;
2461 }
2462 }
2463 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002464 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002465 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002466 case ISD::UINT_TO_FP: {
2467 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002468 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2469 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002470 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002471 Node->getOperand(0).getValueType())) {
2472 default: assert(0 && "Unknown operation action!");
2473 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002474 Result = ExpandLegalINT_TO_FP(isSigned,
2475 LegalizeOp(Node->getOperand(0)),
2476 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002477 AddLegalizedOperand(Op, Result);
2478 return Result;
2479 case TargetLowering::Promote:
2480 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2481 Node->getValueType(0),
2482 isSigned);
2483 AddLegalizedOperand(Op, Result);
2484 return Result;
2485 case TargetLowering::Legal:
2486 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002487 case TargetLowering::Custom: {
2488 Tmp1 = LegalizeOp(Node->getOperand(0));
2489 SDOperand Tmp =
2490 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2491 Tmp = TLI.LowerOperation(Tmp, DAG);
2492 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002493 Tmp = LegalizeOp(Tmp); // Relegalize input.
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002494 AddLegalizedOperand(Op, Tmp);
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002495 return Tmp;
2496 } else {
2497 assert(0 && "Target Must Lower this");
2498 }
2499 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00002500 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002501
Chris Lattner3e928bb2005-01-07 07:47:09 +00002502 Tmp1 = LegalizeOp(Node->getOperand(0));
2503 if (Tmp1 != Node->getOperand(0))
2504 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2505 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002506 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002507 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2508 Node->getValueType(0), Node->getOperand(0));
2509 break;
2510 case Promote:
2511 if (isSigned) {
2512 Result = PromoteOp(Node->getOperand(0));
2513 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2514 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
2515 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
2516 } else {
2517 Result = PromoteOp(Node->getOperand(0));
2518 Result = DAG.getZeroExtendInReg(Result,
2519 Node->getOperand(0).getValueType());
2520 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00002521 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002522 break;
2523 }
2524 break;
2525 }
2526 case ISD::TRUNCATE:
2527 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2528 case Legal:
2529 Tmp1 = LegalizeOp(Node->getOperand(0));
2530 if (Tmp1 != Node->getOperand(0))
2531 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2532 break;
2533 case Expand:
2534 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2535
2536 // Since the result is legal, we should just be able to truncate the low
2537 // part of the source.
2538 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2539 break;
2540 case Promote:
2541 Result = PromoteOp(Node->getOperand(0));
2542 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2543 break;
2544 }
2545 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002546
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002547 case ISD::FP_TO_SINT:
2548 case ISD::FP_TO_UINT:
2549 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2550 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002551 Tmp1 = LegalizeOp(Node->getOperand(0));
2552
Chris Lattner1618beb2005-07-29 00:11:56 +00002553 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2554 default: assert(0 && "Unknown operation action!");
2555 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002556 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2557 SDOperand True, False;
2558 MVT::ValueType VT = Node->getOperand(0).getValueType();
2559 MVT::ValueType NVT = Node->getValueType(0);
2560 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2561 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2562 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2563 Node->getOperand(0), Tmp2, ISD::SETLT);
2564 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2565 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002566 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002567 Tmp2));
2568 False = DAG.getNode(ISD::XOR, NVT, False,
2569 DAG.getConstant(1ULL << ShiftAmt, NVT));
2570 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Chris Lattner69a889e2005-12-20 00:53:54 +00002571 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman2d56e722005-08-14 18:38:32 +00002572 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002573 } else {
2574 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2575 }
2576 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002577 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002578 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002579 Node->getOpcode() == ISD::FP_TO_SINT);
2580 AddLegalizedOperand(Op, Result);
2581 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002582 case TargetLowering::Custom: {
2583 SDOperand Tmp =
2584 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2585 Tmp = TLI.LowerOperation(Tmp, DAG);
2586 if (Tmp.Val) {
Chris Lattner69a889e2005-12-20 00:53:54 +00002587 Tmp = LegalizeOp(Tmp);
Chris Lattner07dffd62005-08-26 00:14:16 +00002588 AddLegalizedOperand(Op, Tmp);
Chris Lattner507f7522005-08-29 17:30:00 +00002589 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002590 } else {
2591 // The target thinks this is legal afterall.
2592 break;
2593 }
2594 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002595 case TargetLowering::Legal:
2596 break;
2597 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002598
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002599 if (Tmp1 != Node->getOperand(0))
2600 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2601 break;
2602 case Expand:
2603 assert(0 && "Shouldn't need to expand other operators here!");
2604 case Promote:
2605 Result = PromoteOp(Node->getOperand(0));
2606 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2607 break;
2608 }
2609 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002610
Chris Lattner13c78e22005-09-02 00:18:10 +00002611 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002612 case ISD::ZERO_EXTEND:
2613 case ISD::SIGN_EXTEND:
2614 case ISD::FP_EXTEND:
2615 case ISD::FP_ROUND:
2616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2617 case Legal:
2618 Tmp1 = LegalizeOp(Node->getOperand(0));
2619 if (Tmp1 != Node->getOperand(0))
2620 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2621 break;
2622 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002623 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002624
Chris Lattner03c85462005-01-15 05:21:40 +00002625 case Promote:
2626 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002627 case ISD::ANY_EXTEND:
2628 Result = PromoteOp(Node->getOperand(0));
2629 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2630 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002631 case ISD::ZERO_EXTEND:
2632 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002633 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002634 Result = DAG.getZeroExtendInReg(Result,
2635 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002636 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002637 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002638 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002639 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002640 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002641 Result,
2642 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002643 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002644 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002645 Result = PromoteOp(Node->getOperand(0));
2646 if (Result.getValueType() != Op.getValueType())
2647 // Dynamically dead while we have only 2 FP types.
2648 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2649 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002650 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002651 Result = PromoteOp(Node->getOperand(0));
2652 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2653 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002654 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002655 }
2656 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002657 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002658 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002659 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002660 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002661
2662 // If this operation is not supported, convert it to a shl/shr or load/store
2663 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002664 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2665 default: assert(0 && "This action not supported for this op yet!");
2666 case TargetLowering::Legal:
2667 if (Tmp1 != Node->getOperand(0))
2668 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002669 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002670 break;
2671 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002672 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002673 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002674 // NOTE: we could fall back on load/store here too for targets without
2675 // SAR. However, it is doubtful that any exist.
2676 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2677 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002678 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002679 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2680 Node->getOperand(0), ShiftCst);
2681 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2682 Result, ShiftCst);
2683 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2684 // The only way we can lower this is to turn it into a STORETRUNC,
2685 // EXTLOAD pair, targetting a temporary location (a stack slot).
2686
2687 // NOTE: there is a choice here between constantly creating new stack
2688 // slots and always reusing the same one. We currently always create
2689 // new ones, as reuse may inhibit scheduling.
2690 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2691 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2692 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2693 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002694 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002695 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2696 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2697 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002698 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002699 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002700 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2701 Result, StackSlot, DAG.getSrcValue(NULL),
2702 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002703 } else {
2704 assert(0 && "Unknown op");
2705 }
2706 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002707 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002708 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002709 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002710 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002711 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002712
Chris Lattner45982da2005-05-12 16:53:42 +00002713 // Note that LegalizeOp may be reentered even from single-use nodes, which
2714 // means that we always must cache transformed nodes.
2715 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002716 return Result;
2717}
2718
Chris Lattner8b6fa222005-01-15 22:16:26 +00002719/// PromoteOp - Given an operation that produces a value in an invalid type,
2720/// promote it to compute the value into a larger type. The produced value will
2721/// have the correct bits for the low portion of the register, but no guarantee
2722/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002723SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2724 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002725 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002726 assert(getTypeAction(VT) == Promote &&
2727 "Caller should expand or legalize operands that are not promotable!");
2728 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2729 "Cannot promote to smaller type!");
2730
Chris Lattner03c85462005-01-15 05:21:40 +00002731 SDOperand Tmp1, Tmp2, Tmp3;
2732
2733 SDOperand Result;
2734 SDNode *Node = Op.Val;
2735
Chris Lattner6fdcb252005-09-02 20:32:45 +00002736 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2737 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002738
Chris Lattner0f69b292005-01-15 06:18:18 +00002739 // Promotion needs an optimization step to clean up after it, and is not
2740 // careful to avoid operations the target does not support. Make sure that
2741 // all generated operations are legalized in the next iteration.
2742 NeedsAnotherIteration = true;
2743
Chris Lattner03c85462005-01-15 05:21:40 +00002744 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002745 case ISD::CopyFromReg:
2746 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002747 default:
2748 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2749 assert(0 && "Do not know how to promote this operator!");
2750 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002751 case ISD::UNDEF:
2752 Result = DAG.getNode(ISD::UNDEF, NVT);
2753 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002754 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002755 if (VT != MVT::i1)
2756 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2757 else
2758 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002759 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2760 break;
2761 case ISD::ConstantFP:
2762 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2763 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2764 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002765
Chris Lattner82fbfb62005-01-18 02:59:52 +00002766 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002767 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002768 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2769 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002770 Result = LegalizeOp(Result);
2771 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002772
2773 case ISD::TRUNCATE:
2774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2775 case Legal:
2776 Result = LegalizeOp(Node->getOperand(0));
2777 assert(Result.getValueType() >= NVT &&
2778 "This truncation doesn't make sense!");
2779 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2780 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2781 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002782 case Promote:
2783 // The truncation is not required, because we don't guarantee anything
2784 // about high bits anyway.
2785 Result = PromoteOp(Node->getOperand(0));
2786 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002787 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002788 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2789 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002790 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002791 }
2792 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002793 case ISD::SIGN_EXTEND:
2794 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002795 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002796 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2797 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2798 case Legal:
2799 // Input is legal? Just do extend all the way to the larger type.
2800 Result = LegalizeOp(Node->getOperand(0));
2801 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2802 break;
2803 case Promote:
2804 // Promote the reg if it's smaller.
2805 Result = PromoteOp(Node->getOperand(0));
2806 // The high bits are not guaranteed to be anything. Insert an extend.
2807 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002808 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002809 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002810 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002811 Result = DAG.getZeroExtendInReg(Result,
2812 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002813 break;
2814 }
2815 break;
Chris Lattner35481892005-12-23 00:16:34 +00002816 case ISD::BIT_CONVERT:
2817 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2818 Result = PromoteOp(Result);
2819 break;
2820
Chris Lattner8b6fa222005-01-15 22:16:26 +00002821 case ISD::FP_EXTEND:
2822 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2823 case ISD::FP_ROUND:
2824 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2825 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2826 case Promote: assert(0 && "Unreachable with 2 FP types!");
2827 case Legal:
2828 // Input is legal? Do an FP_ROUND_INREG.
2829 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002830 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2831 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002832 break;
2833 }
2834 break;
2835
2836 case ISD::SINT_TO_FP:
2837 case ISD::UINT_TO_FP:
2838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2839 case Legal:
2840 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002841 // No extra round required here.
2842 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002843 break;
2844
2845 case Promote:
2846 Result = PromoteOp(Node->getOperand(0));
2847 if (Node->getOpcode() == ISD::SINT_TO_FP)
2848 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002849 Result,
2850 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002851 else
Chris Lattner23993562005-04-13 02:38:47 +00002852 Result = DAG.getZeroExtendInReg(Result,
2853 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002854 // No extra round required here.
2855 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002856 break;
2857 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002858 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2859 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002860 // Round if we cannot tolerate excess precision.
2861 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002862 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2863 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002864 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002865 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002866 break;
2867
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002868 case ISD::SIGN_EXTEND_INREG:
2869 Result = PromoteOp(Node->getOperand(0));
2870 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2871 Node->getOperand(1));
2872 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002873 case ISD::FP_TO_SINT:
2874 case ISD::FP_TO_UINT:
2875 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2876 case Legal:
2877 Tmp1 = LegalizeOp(Node->getOperand(0));
2878 break;
2879 case Promote:
2880 // The input result is prerounded, so we don't have to do anything
2881 // special.
2882 Tmp1 = PromoteOp(Node->getOperand(0));
2883 break;
2884 case Expand:
2885 assert(0 && "not implemented");
2886 }
Nate Begemand2558e32005-08-14 01:20:53 +00002887 // If we're promoting a UINT to a larger size, check to see if the new node
2888 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2889 // we can use that instead. This allows us to generate better code for
2890 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2891 // legal, such as PowerPC.
2892 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002893 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002894 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2895 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002896 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2897 } else {
2898 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2899 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002900 break;
2901
Chris Lattner2c8086f2005-04-02 05:00:07 +00002902 case ISD::FABS:
2903 case ISD::FNEG:
2904 Tmp1 = PromoteOp(Node->getOperand(0));
2905 assert(Tmp1.getValueType() == NVT);
2906 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2907 // NOTE: we do not have to do any extra rounding here for
2908 // NoExcessFPPrecision, because we know the input will have the appropriate
2909 // precision, and these operations don't modify precision at all.
2910 break;
2911
Chris Lattnerda6ba872005-04-28 21:44:33 +00002912 case ISD::FSQRT:
2913 case ISD::FSIN:
2914 case ISD::FCOS:
2915 Tmp1 = PromoteOp(Node->getOperand(0));
2916 assert(Tmp1.getValueType() == NVT);
2917 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2918 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002919 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2920 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002921 break;
2922
Chris Lattner03c85462005-01-15 05:21:40 +00002923 case ISD::AND:
2924 case ISD::OR:
2925 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002926 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002927 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002928 case ISD::MUL:
2929 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002930 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002931 // that too is okay if they are integer operations.
2932 Tmp1 = PromoteOp(Node->getOperand(0));
2933 Tmp2 = PromoteOp(Node->getOperand(1));
2934 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2935 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002936 break;
2937 case ISD::FADD:
2938 case ISD::FSUB:
2939 case ISD::FMUL:
2940 // The input may have strange things in the top bits of the registers, but
2941 // these operations don't care.
2942 Tmp1 = PromoteOp(Node->getOperand(0));
2943 Tmp2 = PromoteOp(Node->getOperand(1));
2944 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2945 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2946
2947 // Floating point operations will give excess precision that we may not be
2948 // able to tolerate. If we DO allow excess precision, just leave it,
2949 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002950 // FIXME: Why would we need to round FP ops more than integer ones?
2951 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002952 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002953 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2954 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002955 break;
2956
Chris Lattner8b6fa222005-01-15 22:16:26 +00002957 case ISD::SDIV:
2958 case ISD::SREM:
2959 // These operators require that their input be sign extended.
2960 Tmp1 = PromoteOp(Node->getOperand(0));
2961 Tmp2 = PromoteOp(Node->getOperand(1));
2962 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002963 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2964 DAG.getValueType(VT));
2965 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2966 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002967 }
2968 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2969
2970 // Perform FP_ROUND: this is probably overly pessimistic.
2971 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002972 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2973 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002974 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002975 case ISD::FDIV:
2976 case ISD::FREM:
2977 // These operators require that their input be fp extended.
2978 Tmp1 = PromoteOp(Node->getOperand(0));
2979 Tmp2 = PromoteOp(Node->getOperand(1));
2980 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2981
2982 // Perform FP_ROUND: this is probably overly pessimistic.
2983 if (NoExcessFPPrecision)
2984 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2985 DAG.getValueType(VT));
2986 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002987
2988 case ISD::UDIV:
2989 case ISD::UREM:
2990 // These operators require that their input be zero extended.
2991 Tmp1 = PromoteOp(Node->getOperand(0));
2992 Tmp2 = PromoteOp(Node->getOperand(1));
2993 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002994 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2995 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002996 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2997 break;
2998
2999 case ISD::SHL:
3000 Tmp1 = PromoteOp(Node->getOperand(0));
3001 Tmp2 = LegalizeOp(Node->getOperand(1));
3002 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
3003 break;
3004 case ISD::SRA:
3005 // The input value must be properly sign extended.
3006 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003007 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3008 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003009 Tmp2 = LegalizeOp(Node->getOperand(1));
3010 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
3011 break;
3012 case ISD::SRL:
3013 // The input value must be properly zero extended.
3014 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003015 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003016 Tmp2 = LegalizeOp(Node->getOperand(1));
3017 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
3018 break;
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003019
Chris Lattner03c85462005-01-15 05:21:40 +00003020 case ISD::LOAD:
3021 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3022 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00003023 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
3024 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003025 // Remember that we legalized the chain.
3026 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3027 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003028 case ISD::SEXTLOAD:
3029 case ISD::ZEXTLOAD:
3030 case ISD::EXTLOAD:
3031 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3032 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00003033 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
3034 Node->getOperand(2),
3035 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00003036 // Remember that we legalized the chain.
3037 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3038 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003039 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00003040 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3041 case Expand: assert(0 && "It's impossible to expand bools");
3042 case Legal:
3043 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
3044 break;
3045 case Promote:
3046 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
3047 break;
3048 }
Chris Lattner03c85462005-01-15 05:21:40 +00003049 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3050 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
3051 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
3052 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003053 case ISD::SELECT_CC:
3054 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3055 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3056 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3057 Node->getOperand(1), Tmp2, Tmp3,
3058 Node->getOperand(4));
3059 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00003060 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00003061 case ISD::CALL: {
3062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3063 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3064
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003065 std::vector<SDOperand> Ops;
3066 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
3067 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3068
Chris Lattner8ac532c2005-01-16 19:46:48 +00003069 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3070 "Can only promote single result calls");
3071 std::vector<MVT::ValueType> RetTyVTs;
3072 RetTyVTs.reserve(2);
3073 RetTyVTs.push_back(NVT);
3074 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003075 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
3076 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00003077 Result = SDOperand(NC, 0);
3078
3079 // Insert the new chain mapping.
3080 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
3081 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003082 }
Nate Begemand88fc032006-01-14 03:14:10 +00003083 case ISD::BSWAP:
3084 Tmp1 = Node->getOperand(0);
3085 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3086 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3087 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3088 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3089 TLI.getShiftAmountTy()));
3090 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003091 case ISD::CTPOP:
3092 case ISD::CTTZ:
3093 case ISD::CTLZ:
3094 Tmp1 = Node->getOperand(0);
3095 //Zero extend the argument
3096 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3097 // Perform the larger operation, then subtract if needed.
3098 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3099 switch(Node->getOpcode())
3100 {
3101 case ISD::CTPOP:
3102 Result = Tmp1;
3103 break;
3104 case ISD::CTTZ:
3105 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003106 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003107 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003108 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003109 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
3110 break;
3111 case ISD::CTLZ:
3112 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003113 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3114 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003115 getSizeInBits(VT), NVT));
3116 break;
3117 }
3118 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003119 }
3120
3121 assert(Result.Val && "Didn't set a result!");
3122 AddPromotedOperand(Op, Result);
3123 return Result;
3124}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003125
Chris Lattner35481892005-12-23 00:16:34 +00003126/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003127/// The resultant code need not be legal. Note that SrcOp is the input operand
3128/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003129SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3130 SDOperand SrcOp) {
3131 // Create the stack frame object.
3132 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3133 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00003134 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00003135 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3136
3137 // Emit a store to the stack slot.
3138 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003139 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003140 // Result is a load from the stack slot.
3141 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3142}
3143
Chris Lattner84f67882005-01-20 18:52:28 +00003144/// ExpandAddSub - Find a clever way to expand this add operation into
3145/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00003146void SelectionDAGLegalize::
3147ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
3148 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00003149 // Expand the subcomponents.
3150 SDOperand LHSL, LHSH, RHSL, RHSH;
3151 ExpandOp(LHS, LHSL, LHSH);
3152 ExpandOp(RHS, RHSL, RHSH);
3153
Chris Lattner84f67882005-01-20 18:52:28 +00003154 std::vector<SDOperand> Ops;
3155 Ops.push_back(LHSL);
3156 Ops.push_back(LHSH);
3157 Ops.push_back(RHSL);
3158 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00003159 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3160 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00003161 Hi = Lo.getValue(1);
3162}
3163
Chris Lattner5b359c62005-04-02 04:00:59 +00003164void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3165 SDOperand Op, SDOperand Amt,
3166 SDOperand &Lo, SDOperand &Hi) {
3167 // Expand the subcomponents.
3168 SDOperand LHSL, LHSH;
3169 ExpandOp(Op, LHSL, LHSH);
3170
3171 std::vector<SDOperand> Ops;
3172 Ops.push_back(LHSL);
3173 Ops.push_back(LHSH);
3174 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003175 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003176 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003177 Hi = Lo.getValue(1);
3178}
3179
3180
Chris Lattnere34b3962005-01-19 04:19:40 +00003181/// ExpandShift - Try to find a clever way to expand this shift operation out to
3182/// smaller elements. If we can't find a way that is more efficient than a
3183/// libcall on this target, return false. Otherwise, return true with the
3184/// low-parts expanded into Lo and Hi.
3185bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3186 SDOperand &Lo, SDOperand &Hi) {
3187 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3188 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003189
Chris Lattnere34b3962005-01-19 04:19:40 +00003190 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003191 SDOperand ShAmt = LegalizeOp(Amt);
3192 MVT::ValueType ShTy = ShAmt.getValueType();
3193 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3194 unsigned NVTBits = MVT::getSizeInBits(NVT);
3195
3196 // Handle the case when Amt is an immediate. Other cases are currently broken
3197 // and are disabled.
3198 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3199 unsigned Cst = CN->getValue();
3200 // Expand the incoming operand to be shifted, so that we have its parts
3201 SDOperand InL, InH;
3202 ExpandOp(Op, InL, InH);
3203 switch(Opc) {
3204 case ISD::SHL:
3205 if (Cst > VTBits) {
3206 Lo = DAG.getConstant(0, NVT);
3207 Hi = DAG.getConstant(0, NVT);
3208 } else if (Cst > NVTBits) {
3209 Lo = DAG.getConstant(0, NVT);
3210 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003211 } else if (Cst == NVTBits) {
3212 Lo = DAG.getConstant(0, NVT);
3213 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003214 } else {
3215 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3216 Hi = DAG.getNode(ISD::OR, NVT,
3217 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3218 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3219 }
3220 return true;
3221 case ISD::SRL:
3222 if (Cst > VTBits) {
3223 Lo = DAG.getConstant(0, NVT);
3224 Hi = DAG.getConstant(0, NVT);
3225 } else if (Cst > NVTBits) {
3226 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3227 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003228 } else if (Cst == NVTBits) {
3229 Lo = InH;
3230 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003231 } else {
3232 Lo = DAG.getNode(ISD::OR, NVT,
3233 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3234 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3235 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3236 }
3237 return true;
3238 case ISD::SRA:
3239 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003240 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003241 DAG.getConstant(NVTBits-1, ShTy));
3242 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003243 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003244 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003245 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003246 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003247 } else if (Cst == NVTBits) {
3248 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003249 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003250 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003251 } else {
3252 Lo = DAG.getNode(ISD::OR, NVT,
3253 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3254 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3255 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3256 }
3257 return true;
3258 }
3259 }
3260 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
3261 // so disable it for now. Currently targets are handling this via SHL_PARTS
3262 // and friends.
3263 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003264
3265 // If we have an efficient select operation (or if the selects will all fold
3266 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003267 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00003268 return false;
3269
3270 SDOperand InL, InH;
3271 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00003272 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
3273 DAG.getConstant(NVTBits, ShTy), ShAmt);
3274
Chris Lattnere5544f82005-01-20 20:29:23 +00003275 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003276 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
3277 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00003278
Chris Lattnere34b3962005-01-19 04:19:40 +00003279 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
3280 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
3281 DAG.getConstant(NVTBits-1, ShTy));
3282 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
3283 DAG.getConstant(NVTBits-1, ShTy));
3284 }
3285
3286 if (Opc == ISD::SHL) {
3287 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
3288 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
3289 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003290 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00003291
Chris Lattnere34b3962005-01-19 04:19:40 +00003292 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
3293 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
3294 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00003295 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003296 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
3297 DAG.getConstant(32, ShTy),
3298 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00003299 DAG.getConstant(0, NVT),
3300 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00003301 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00003302 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00003303 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00003304 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00003305
3306 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00003307 if (Opc == ISD::SRA)
3308 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
3309 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00003310 else
3311 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00003312 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00003313 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00003314 }
3315 return true;
3316}
Chris Lattner77e77a62005-01-21 06:05:23 +00003317
Chris Lattner9530ddc2005-05-13 05:09:11 +00003318/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
3319/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003320/// Found.
Chris Lattnerde387ce2006-01-09 23:21:49 +00003321static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
3322 std::set<SDNode*> &Visited) {
3323 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
3324 !Visited.insert(Node).second) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00003325
Chris Lattner16cd04d2005-05-12 23:24:06 +00003326 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003327 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003328 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003329 Found = Node;
3330 return;
3331 }
3332
3333 // Otherwise, scan the operands of Node to see if any of them is a call.
3334 assert(Node->getNumOperands() != 0 &&
3335 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00003336 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattnerde387ce2006-01-09 23:21:49 +00003337 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003338
3339 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003340 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattnerde387ce2006-01-09 23:21:49 +00003341 Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003342}
3343
3344
Chris Lattner9530ddc2005-05-13 05:09:11 +00003345/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
3346/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003347/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00003348static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
3349 std::set<SDNode*> &Visited) {
3350 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
3351 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003352
Chris Lattner16cd04d2005-05-12 23:24:06 +00003353 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003354 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00003355 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003356 Found = Node;
3357 return;
3358 }
3359
3360 // Otherwise, scan the operands of Node to see if any of them is a call.
3361 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
3362 if (UI == E) return;
3363 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00003364 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003365
3366 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00003367 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003368}
3369
Chris Lattner9530ddc2005-05-13 05:09:11 +00003370/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003371/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003372static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00003373 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003374 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00003375 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00003376 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003377
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003378 // The chain is usually at the end.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003379 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003380 if (TheChain.getValueType() != MVT::Other) {
3381 // Sometimes it's at the beginning.
Chris Lattner2789bde2005-05-14 08:34:53 +00003382 TheChain = SDOperand(Node, 0);
Chris Lattnerc0ab5222006-01-14 22:41:46 +00003383 if (TheChain.getValueType() != MVT::Other) {
3384 // Otherwise, hunt for it.
3385 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
3386 if (Node->getValueType(i) == MVT::Other) {
3387 TheChain = SDOperand(Node, i);
3388 break;
3389 }
3390
3391 // Otherwise, we walked into a node without a chain.
3392 if (TheChain.getValueType() != MVT::Other)
3393 return 0;
3394 }
3395 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003396
3397 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00003398 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003399
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003400 // Make sure to only follow users of our token chain.
3401 SDNode *User = *UI;
3402 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3403 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00003404 if (SDNode *Result = FindCallSeqEnd(User))
3405 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003406 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00003407 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003408}
3409
Chris Lattner9530ddc2005-05-13 05:09:11 +00003410/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00003411/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00003412static SDNode *FindCallSeqStart(SDNode *Node) {
3413 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00003414 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003415
3416 assert(Node->getOperand(0).getValueType() == MVT::Other &&
3417 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00003418 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003419}
3420
3421
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003422/// FindInputOutputChains - If we are replacing an operation with a call we need
3423/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003424/// properly serialize the calls in the block. The returned operand is the
3425/// input chain value for the new call (e.g. the entry node or the previous
3426/// call), and OutChain is set to be the chain node to update to point to the
3427/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003428static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
3429 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003430 SDNode *LatestCallSeqStart = Entry.Val;
3431 SDNode *LatestCallSeqEnd = 0;
Chris Lattnerde387ce2006-01-09 23:21:49 +00003432 std::set<SDNode*> Visited;
3433 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
3434 Visited.clear();
Chris Lattner9530ddc2005-05-13 05:09:11 +00003435 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003436
Chris Lattner16cd04d2005-05-12 23:24:06 +00003437 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00003438 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00003439 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
3440 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00003441 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003442 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00003443 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
3444 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00003445 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00003446 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00003447 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00003448
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003449 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00003450 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003451 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00003452 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattnerde387ce2006-01-09 23:21:49 +00003453 Visited.clear();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003454
Chris Lattner9530ddc2005-05-13 05:09:11 +00003455 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003456 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00003457 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003458
Chris Lattner9530ddc2005-05-13 05:09:11 +00003459 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003460}
3461
Jeff Cohen00b168892005-07-27 06:12:32 +00003462/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003463void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
3464 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003465 // Nothing to splice it into?
3466 if (OutChain == 0) return;
3467
3468 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
3469 //OutChain->dump();
3470
3471 // Form a token factor node merging the old inval and the new inval.
3472 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
3473 OutChain->getOperand(0));
3474 // Change the node to refer to the new token.
3475 OutChain->setAdjCallChain(InToken);
3476}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003477
3478
Chris Lattner77e77a62005-01-21 06:05:23 +00003479// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3480// does not fit into a register, return the lo part and set the hi part to the
3481// by-reg argument. If it does fit into a single register, return the result
3482// and leave the Hi part unset.
3483SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3484 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003485 SDNode *OutChain;
3486 SDOperand InChain = FindInputOutputChains(Node, OutChain,
3487 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00003488 if (InChain.Val == 0)
3489 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003490
Chris Lattner77e77a62005-01-21 06:05:23 +00003491 TargetLowering::ArgListTy Args;
3492 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3493 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3494 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3495 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3496 }
3497 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003498
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003499 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003500 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003501 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003502 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3503 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003504
Chris Lattner99c25b82005-09-02 20:26:58 +00003505 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003506 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003507 default: assert(0 && "Unknown thing");
3508 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00003509 Result = CallInfo.first;
3510 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003511 case Promote:
3512 assert(0 && "Cannot promote this yet!");
3513 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003514 ExpandOp(CallInfo.first, Result, Hi);
3515 CallInfo.second = LegalizeOp(CallInfo.second);
3516 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003517 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003518
3519 SpliceCallInto(CallInfo.second, OutChain);
3520 NeedsAnotherIteration = true;
3521 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003522}
3523
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003524
Chris Lattner77e77a62005-01-21 06:05:23 +00003525/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3526/// destination type is legal.
3527SDOperand SelectionDAGLegalize::
3528ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003529 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003530 assert(getTypeAction(Source.getValueType()) == Expand &&
3531 "This is not an expansion!");
3532 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3533
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003534 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003535 assert(Source.getValueType() == MVT::i64 &&
3536 "This only works for 64-bit -> FP");
3537 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3538 // incoming integer is set. To handle this, we dynamically test to see if
3539 // it is set, and, if so, add a fudge factor.
3540 SDOperand Lo, Hi;
3541 ExpandOp(Source, Lo, Hi);
3542
Chris Lattner66de05b2005-05-13 04:45:13 +00003543 // If this is unsigned, and not supported, first perform the conversion to
3544 // signed, then adjust the result if the sign bit is set.
3545 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3546 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3547
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003548 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3549 DAG.getConstant(0, Hi.getValueType()),
3550 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003551 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3552 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3553 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003554 uint64_t FF = 0x5f800000ULL;
3555 if (TLI.isLittleEndian()) FF <<= 32;
3556 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003557
Chris Lattner5839bf22005-08-26 17:15:30 +00003558 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003559 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3560 SDOperand FudgeInReg;
3561 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003562 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3563 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003564 else {
3565 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003566 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3567 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003568 }
Chris Lattner473a9902005-09-29 06:44:39 +00003569 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003570 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003571
Chris Lattnera88a2602005-05-14 05:33:54 +00003572 // Check to see if the target has a custom way to lower this. If so, use it.
3573 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3574 default: assert(0 && "This action not implemented for this operation!");
3575 case TargetLowering::Legal:
3576 case TargetLowering::Expand:
3577 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003578 case TargetLowering::Custom: {
3579 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3580 Source), DAG);
3581 if (NV.Val)
3582 return LegalizeOp(NV);
3583 break; // The target decided this was legal after all
3584 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003585 }
3586
Chris Lattner13689e22005-05-12 07:00:44 +00003587 // Expand the source, then glue it back together for the call. We must expand
3588 // the source in case it is shared (this pass of legalize must traverse it).
3589 SDOperand SrcLo, SrcHi;
3590 ExpandOp(Source, SrcLo, SrcHi);
3591 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3592
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003593 SDNode *OutChain = 0;
3594 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3595 DAG.getEntryNode());
3596 const char *FnName = 0;
3597 if (DestTy == MVT::f32)
3598 FnName = "__floatdisf";
3599 else {
3600 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3601 FnName = "__floatdidf";
3602 }
3603
Chris Lattner77e77a62005-01-21 06:05:23 +00003604 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3605
3606 TargetLowering::ArgListTy Args;
3607 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003608
Chris Lattner77e77a62005-01-21 06:05:23 +00003609 Args.push_back(std::make_pair(Source, ArgTy));
3610
3611 // We don't care about token chains for libcalls. We just use the entry
3612 // node as our input and ignore the output chain. This allows us to place
3613 // calls wherever we need them to satisfy data dependences.
3614 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003615
3616 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003617 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3618 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003619
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003620 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003621 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003622}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003623
Chris Lattnere34b3962005-01-19 04:19:40 +00003624
3625
Chris Lattner3e928bb2005-01-07 07:47:09 +00003626/// ExpandOp - Expand the specified SDOperand into its two component pieces
3627/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3628/// LegalizeNodes map is filled in for any results that are not expanded, the
3629/// ExpandedNodes map is filled in for any results that are expanded, and the
3630/// Lo/Hi values are returned.
3631void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3632 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003633 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003634 SDNode *Node = Op.Val;
3635 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003636 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3637 "Cannot expand FP values!");
3638 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003639 "Cannot expand to FP value or to larger int value!");
3640
Chris Lattner6fdcb252005-09-02 20:32:45 +00003641 // See if we already expanded it.
3642 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3643 = ExpandedNodes.find(Op);
3644 if (I != ExpandedNodes.end()) {
3645 Lo = I->second.first;
3646 Hi = I->second.second;
3647 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003648 }
3649
Chris Lattner4e6c7462005-01-08 19:27:05 +00003650 // Expanding to multiple registers needs to perform an optimization step, and
3651 // is not careful to avoid operations the target does not support. Make sure
3652 // that all generated operations are legalized in the next iteration.
3653 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003654
Chris Lattner3e928bb2005-01-07 07:47:09 +00003655 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003656 case ISD::CopyFromReg:
3657 assert(0 && "CopyFromReg must be legal!");
3658 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003659 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3660 assert(0 && "Do not know how to expand this operator!");
3661 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003662 case ISD::UNDEF:
3663 Lo = DAG.getNode(ISD::UNDEF, NVT);
3664 Hi = DAG.getNode(ISD::UNDEF, NVT);
3665 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003666 case ISD::Constant: {
3667 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3668 Lo = DAG.getConstant(Cst, NVT);
3669 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3670 break;
3671 }
Nate Begemancc827e62005-12-07 19:48:11 +00003672 case ISD::ConstantVec: {
3673 unsigned NumElements = Node->getNumOperands();
3674 // If we only have two elements left in the constant vector, just break it
3675 // apart into the two scalar constants it contains. Otherwise, bisect the
3676 // ConstantVec, and return each half as a new ConstantVec.
3677 // FIXME: this is hard coded as big endian, it may have to change to support
3678 // SSE and Alpha MVI
3679 if (NumElements == 2) {
3680 Hi = Node->getOperand(0);
3681 Lo = Node->getOperand(1);
3682 } else {
3683 NumElements /= 2;
3684 std::vector<SDOperand> LoOps, HiOps;
3685 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3686 HiOps.push_back(Node->getOperand(I));
3687 LoOps.push_back(Node->getOperand(I+NumElements));
3688 }
3689 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3690 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3691 }
3692 break;
3693 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003694
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003695 case ISD::BUILD_PAIR:
3696 // Legalize both operands. FIXME: in the future we should handle the case
3697 // where the two elements are not legal.
3698 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3699 Lo = LegalizeOp(Node->getOperand(0));
3700 Hi = LegalizeOp(Node->getOperand(1));
3701 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003702
3703 case ISD::SIGN_EXTEND_INREG:
3704 ExpandOp(Node->getOperand(0), Lo, Hi);
3705 // Sign extend the lo-part.
3706 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3707 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3708 TLI.getShiftAmountTy()));
3709 // sext_inreg the low part if needed.
3710 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3711 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003712
Nate Begemand88fc032006-01-14 03:14:10 +00003713 case ISD::BSWAP: {
3714 ExpandOp(Node->getOperand(0), Lo, Hi);
3715 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3716 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3717 Lo = TempLo;
3718 break;
3719 }
3720
Chris Lattneredb1add2005-05-11 04:51:16 +00003721 case ISD::CTPOP:
3722 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003723 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3724 DAG.getNode(ISD::CTPOP, NVT, Lo),
3725 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003726 Hi = DAG.getConstant(0, NVT);
3727 break;
3728
Chris Lattner39a8f332005-05-12 19:05:01 +00003729 case ISD::CTLZ: {
3730 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003731 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003732 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3733 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003734 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3735 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003736 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3737 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3738
3739 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3740 Hi = DAG.getConstant(0, NVT);
3741 break;
3742 }
3743
3744 case ISD::CTTZ: {
3745 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003746 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003747 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3748 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003749 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3750 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003751 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3752 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3753
3754 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3755 Hi = DAG.getConstant(0, NVT);
3756 break;
3757 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003758
Chris Lattner3e928bb2005-01-07 07:47:09 +00003759 case ISD::LOAD: {
3760 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3761 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003762 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003763
3764 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003765 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003766 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3767 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003768 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003769 //are from the same instruction?
3770 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003771
3772 // Build a factor node to remember that this load is independent of the
3773 // other one.
3774 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3775 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003776
Chris Lattner3e928bb2005-01-07 07:47:09 +00003777 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003778 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003779 if (!TLI.isLittleEndian())
3780 std::swap(Lo, Hi);
3781 break;
3782 }
Nate Begemanab48be32005-11-22 18:16:00 +00003783 case ISD::VLOAD: {
3784 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3785 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3786 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3787 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3788
3789 // If we only have two elements, turn into a pair of scalar loads.
3790 // FIXME: handle case where a vector of two elements is fine, such as
3791 // 2 x double on SSE2.
3792 if (NumElements == 2) {
3793 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3794 // Increment the pointer to the other half.
3795 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3796 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3797 getIntPtrConstant(IncrementSize));
3798 //Is this safe? declaring that the two parts of the split load
3799 //are from the same instruction?
3800 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3801 } else {
3802 NumElements /= 2; // Split the vector in half
3803 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3804 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3805 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3806 getIntPtrConstant(IncrementSize));
3807 //Is this safe? declaring that the two parts of the split load
3808 //are from the same instruction?
3809 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3810 }
3811
3812 // Build a factor node to remember that this load is independent of the
3813 // other one.
3814 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3815 Hi.getValue(1));
3816
3817 // Remember that we legalized the chain.
3818 AddLegalizedOperand(Op.getValue(1), TF);
3819 if (!TLI.isLittleEndian())
3820 std::swap(Lo, Hi);
3821 break;
3822 }
3823 case ISD::VADD:
3824 case ISD::VSUB:
3825 case ISD::VMUL: {
3826 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3827 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3828 SDOperand LL, LH, RL, RH;
3829
3830 ExpandOp(Node->getOperand(0), LL, LH);
3831 ExpandOp(Node->getOperand(1), RL, RH);
3832
3833 // If we only have two elements, turn into a pair of scalar loads.
3834 // FIXME: handle case where a vector of two elements is fine, such as
3835 // 2 x double on SSE2.
3836 if (NumElements == 2) {
3837 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3838 Lo = DAG.getNode(Opc, EVT, LL, RL);
3839 Hi = DAG.getNode(Opc, EVT, LH, RH);
3840 } else {
3841 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3842 LL.getOperand(3));
3843 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3844 LH.getOperand(3));
3845 }
3846 break;
3847 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003848 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003849 case ISD::CALL: {
3850 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3851 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3852
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003853 bool Changed = false;
3854 std::vector<SDOperand> Ops;
3855 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3856 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3857 Changed |= Ops.back() != Node->getOperand(i);
3858 }
3859
Chris Lattner3e928bb2005-01-07 07:47:09 +00003860 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3861 "Can only expand a call once so far, not i64 -> i16!");
3862
3863 std::vector<MVT::ValueType> RetTyVTs;
3864 RetTyVTs.reserve(3);
3865 RetTyVTs.push_back(NVT);
3866 RetTyVTs.push_back(NVT);
3867 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003868 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3869 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003870 Lo = SDOperand(NC, 0);
3871 Hi = SDOperand(NC, 1);
3872
3873 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003874 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003875 break;
3876 }
3877 case ISD::AND:
3878 case ISD::OR:
3879 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3880 SDOperand LL, LH, RL, RH;
3881 ExpandOp(Node->getOperand(0), LL, LH);
3882 ExpandOp(Node->getOperand(1), RL, RH);
3883 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3884 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3885 break;
3886 }
3887 case ISD::SELECT: {
3888 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003889
3890 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3891 case Expand: assert(0 && "It's impossible to expand bools");
3892 case Legal:
3893 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3894 break;
3895 case Promote:
3896 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3897 break;
3898 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003899 ExpandOp(Node->getOperand(1), LL, LH);
3900 ExpandOp(Node->getOperand(2), RL, RH);
3901 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3902 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3903 break;
3904 }
Nate Begeman9373a812005-08-10 20:51:12 +00003905 case ISD::SELECT_CC: {
3906 SDOperand TL, TH, FL, FH;
3907 ExpandOp(Node->getOperand(2), TL, TH);
3908 ExpandOp(Node->getOperand(3), FL, FH);
3909 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3910 Node->getOperand(1), TL, FL, Node->getOperand(4));
3911 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3912 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003913 Lo = LegalizeOp(Lo);
3914 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003915 break;
3916 }
Nate Begeman144ff662005-10-13 17:15:37 +00003917 case ISD::SEXTLOAD: {
3918 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3919 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3920 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3921
3922 if (EVT == NVT)
3923 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3924 else
3925 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3926 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003927
3928 // Remember that we legalized the chain.
3929 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3930
Nate Begeman144ff662005-10-13 17:15:37 +00003931 // The high part is obtained by SRA'ing all but one of the bits of the lo
3932 // part.
3933 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3934 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3935 TLI.getShiftAmountTy()));
3936 Lo = LegalizeOp(Lo);
3937 Hi = LegalizeOp(Hi);
3938 break;
3939 }
3940 case ISD::ZEXTLOAD: {
3941 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3942 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3943 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3944
3945 if (EVT == NVT)
3946 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3947 else
3948 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3949 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003950
3951 // Remember that we legalized the chain.
3952 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3953
Nate Begeman144ff662005-10-13 17:15:37 +00003954 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003955 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003956 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003957 break;
3958 }
3959 case ISD::EXTLOAD: {
3960 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3961 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3962 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3963
3964 if (EVT == NVT)
3965 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3966 else
3967 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3968 EVT);
3969
3970 // Remember that we legalized the chain.
3971 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3972
3973 // The high part is undefined.
3974 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3975 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003976 break;
3977 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003978 case ISD::ANY_EXTEND: {
3979 SDOperand In;
3980 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3981 case Expand: assert(0 && "expand-expand not implemented yet!");
3982 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3983 case Promote:
3984 In = PromoteOp(Node->getOperand(0));
3985 break;
3986 }
3987
3988 // The low part is any extension of the input (which degenerates to a copy).
3989 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3990 // The high part is undefined.
3991 Hi = DAG.getNode(ISD::UNDEF, NVT);
3992 break;
3993 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003994 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003995 SDOperand In;
3996 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3997 case Expand: assert(0 && "expand-expand not implemented yet!");
3998 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3999 case Promote:
4000 In = PromoteOp(Node->getOperand(0));
4001 // Emit the appropriate sign_extend_inreg to get the value we want.
4002 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00004003 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00004004 break;
4005 }
4006
Chris Lattner3e928bb2005-01-07 07:47:09 +00004007 // The low part is just a sign extension of the input (which degenerates to
4008 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00004009 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004010
Chris Lattner3e928bb2005-01-07 07:47:09 +00004011 // The high part is obtained by SRA'ing all but one of the bits of the lo
4012 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004013 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00004014 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4015 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004016 break;
4017 }
Chris Lattner06098e02005-04-03 23:41:52 +00004018 case ISD::ZERO_EXTEND: {
4019 SDOperand In;
4020 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4021 case Expand: assert(0 && "expand-expand not implemented yet!");
4022 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
4023 case Promote:
4024 In = PromoteOp(Node->getOperand(0));
4025 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00004026 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00004027 break;
4028 }
4029
Chris Lattner3e928bb2005-01-07 07:47:09 +00004030 // The low part is just a zero extension of the input (which degenerates to
4031 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00004032 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00004033
Chris Lattner3e928bb2005-01-07 07:47:09 +00004034 // The high part is just a zero.
4035 Hi = DAG.getConstant(0, NVT);
4036 break;
Chris Lattner06098e02005-04-03 23:41:52 +00004037 }
Chris Lattner35481892005-12-23 00:16:34 +00004038
4039 case ISD::BIT_CONVERT: {
4040 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4041 Node->getOperand(0));
4042 ExpandOp(Tmp, Lo, Hi);
4043 break;
4044 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004045
Chris Lattner308575b2005-11-20 22:56:56 +00004046 case ISD::READCYCLECOUNTER: {
4047 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4048 TargetLowering::Custom &&
4049 "Must custom expand ReadCycleCounter");
4050 SDOperand T = TLI.LowerOperation(Op, DAG);
4051 assert(T.Val && "Node must be custom expanded!");
4052 Lo = LegalizeOp(T.getValue(0));
4053 Hi = LegalizeOp(T.getValue(1));
4054 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
4055 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004056 break;
Chris Lattner308575b2005-11-20 22:56:56 +00004057 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004058
Chris Lattner4e6c7462005-01-08 19:27:05 +00004059 // These operators cannot be expanded directly, emit them as calls to
4060 // library functions.
4061 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004062 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004063 SDOperand Op;
4064 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4065 case Expand: assert(0 && "cannot expand FP!");
4066 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4067 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
4068 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004069
Chris Lattnerf20d1832005-07-30 01:40:57 +00004070 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4071
Chris Lattner80a3e942005-07-29 00:33:32 +00004072 // Now that the custom expander is done, expand the result, which is still
4073 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004074 if (Op.Val) {
4075 ExpandOp(Op, Lo, Hi);
4076 break;
4077 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004078 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004079
Chris Lattner4e6c7462005-01-08 19:27:05 +00004080 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004081 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004082 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004083 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004084 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004085
Chris Lattner4e6c7462005-01-08 19:27:05 +00004086 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004087 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
4088 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
4089 LegalizeOp(Node->getOperand(0)));
4090 // Now that the custom expander is done, expand the result, which is still
4091 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004092 Op = TLI.LowerOperation(Op, DAG);
4093 if (Op.Val) {
4094 ExpandOp(Op, Lo, Hi);
4095 break;
4096 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004097 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004098
Chris Lattner4e6c7462005-01-08 19:27:05 +00004099 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004100 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004101 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004102 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004103 break;
4104
Evan Cheng05a2d562006-01-09 18:31:59 +00004105 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004106 // If the target wants custom lowering, do so.
4107 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
4108 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
4109 LegalizeOp(Node->getOperand(1)));
4110 Op = TLI.LowerOperation(Op, DAG);
4111 if (Op.Val) {
4112 // Now that the custom expander is done, expand the result, which is
4113 // still VT.
4114 ExpandOp(Op, Lo, Hi);
4115 break;
4116 }
4117 }
4118
Chris Lattnere34b3962005-01-19 04:19:40 +00004119 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00004120 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004121 break;
Chris Lattner47599822005-04-02 03:38:53 +00004122
4123 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004124 TargetLowering::LegalizeAction Action =
4125 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4126 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4127 Action == TargetLowering::Custom) {
Chris Lattner5b359c62005-04-02 04:00:59 +00004128 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
4129 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004130 break;
4131 }
4132
Chris Lattnere34b3962005-01-19 04:19:40 +00004133 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004134 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004135 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004136 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004137
Evan Cheng05a2d562006-01-09 18:31:59 +00004138 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004139 // If the target wants custom lowering, do so.
4140 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
4141 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
4142 LegalizeOp(Node->getOperand(1)));
4143 Op = TLI.LowerOperation(Op, DAG);
4144 if (Op.Val) {
4145 // Now that the custom expander is done, expand the result, which is
4146 // still VT.
4147 ExpandOp(Op, Lo, Hi);
4148 break;
4149 }
4150 }
4151
Chris Lattnere34b3962005-01-19 04:19:40 +00004152 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00004153 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004154 break;
Chris Lattner47599822005-04-02 03:38:53 +00004155
4156 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004157 TargetLowering::LegalizeAction Action =
4158 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4159 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4160 Action == TargetLowering::Custom) {
Chris Lattner5b359c62005-04-02 04:00:59 +00004161 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
4162 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004163 break;
4164 }
4165
Chris Lattnere34b3962005-01-19 04:19:40 +00004166 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004167 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004168 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004169 }
4170
4171 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004172 // If the target wants custom lowering, do so.
4173 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
4174 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
4175 LegalizeOp(Node->getOperand(1)));
4176 Op = TLI.LowerOperation(Op, DAG);
4177 if (Op.Val) {
4178 // Now that the custom expander is done, expand the result, which is
4179 // still VT.
4180 ExpandOp(Op, Lo, Hi);
4181 break;
4182 }
4183 }
4184
Chris Lattnere34b3962005-01-19 04:19:40 +00004185 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00004186 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004187 break;
Chris Lattner47599822005-04-02 03:38:53 +00004188
4189 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004190 TargetLowering::LegalizeAction Action =
4191 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4192 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4193 Action == TargetLowering::Custom) {
Chris Lattner5b359c62005-04-02 04:00:59 +00004194 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
4195 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004196 break;
4197 }
4198
Chris Lattnere34b3962005-01-19 04:19:40 +00004199 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004200 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004201 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004202 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004203
Misha Brukmanedf128a2005-04-21 22:36:52 +00004204 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00004205 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
4206 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00004207 break;
4208 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00004209 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
4210 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00004211 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00004212 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004213 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004214 SDOperand LL, LH, RL, RH;
4215 ExpandOp(Node->getOperand(0), LL, LH);
4216 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004217 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4218 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4219 // extended the sign bit of the low half through the upper half, and if so
4220 // emit a MULHS instead of the alternate sequence that is valid for any
4221 // i64 x i64 multiply.
4222 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4223 // is RH an extension of the sign bit of RL?
4224 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4225 RH.getOperand(1).getOpcode() == ISD::Constant &&
4226 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4227 // is LH an extension of the sign bit of LL?
4228 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4229 LH.getOperand(1).getOpcode() == ISD::Constant &&
4230 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4231 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4232 } else {
4233 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4234 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4235 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4236 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4237 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4238 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004239 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4240 } else {
4241 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
4242 }
4243 break;
4244 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004245 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4246 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4247 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4248 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004249 }
4250
Chris Lattner83397362005-12-21 18:02:52 +00004251 // Make sure the resultant values have been legalized themselves, unless this
4252 // is a type that requires multi-step expansion.
4253 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4254 Lo = LegalizeOp(Lo);
4255 Hi = LegalizeOp(Hi);
4256 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004257
4258 // Remember in a map if the values will be reused later.
4259 bool isNew =
4260 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4261 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004262}
4263
4264
4265// SelectionDAG::Legalize - This is the entry point for the file.
4266//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004267void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004268 /// run - This is the main entry point to this class.
4269 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004270 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004271}
4272