blob: e17a9080cc3db7d506b8270eabcf476f77ccca7d [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)".
55 unsigned ValueTypeActions;
56
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) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner03c85462005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000087
Chris Lattner3e928bb2005-01-07 07:47:09 +000088public:
89
Chris Lattner9c32d3b2005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner77e77a62005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000128
Jim Laskey6269ed12005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000136
Chris Lattnere34b3962005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000143
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
152
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000153SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
154 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
155 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156 assert(MVT::LAST_VALUETYPE <= 16 &&
157 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
Jim Laskey6269ed12005-08-17 00:39:29 +0000160/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
161/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000162/// we expand it. At this point, we know that the result and operand types are
163/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000164SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
165 SDOperand Op0,
166 MVT::ValueType DestVT) {
167 if (Op0.getValueType() == MVT::i32) {
168 // simple 32-bit [signed|unsigned] integer to float/double expansion
169
170 // get the stack frame index of a 8 byte buffer
171 MachineFunction &MF = DAG.getMachineFunction();
172 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
173 // get address of 8 byte buffer
174 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
175 // word offset constant for Hi/Lo address computation
176 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
177 // set up Hi and Lo (into buffer) address based on endian
178 SDOperand Hi, Lo;
179 if (TLI.isLittleEndian()) {
180 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
181 Lo = StackSlot;
182 } else {
183 Hi = StackSlot;
184 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
185 }
186 // if signed map to unsigned space
187 SDOperand Op0Mapped;
188 if (isSigned) {
189 // constant used to invert sign bit (signed to unsigned mapping)
190 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
191 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
192 } else {
193 Op0Mapped = Op0;
194 }
195 // store the lo of the constructed double - based on integer input
196 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
197 Op0Mapped, Lo, DAG.getSrcValue(NULL));
198 // initial hi portion of constructed double
199 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
200 // store the hi of the constructed double - biased exponent
201 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
202 InitialHi, Hi, DAG.getSrcValue(NULL));
203 // load the constructed double
204 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
205 DAG.getSrcValue(NULL));
206 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000207 SDOperand Bias = DAG.getConstantFP(isSigned ?
208 BitsToDouble(0x4330000080000000ULL)
209 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000210 MVT::f64);
211 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000212 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000213 // final result
214 SDOperand Result;
215 // handle final rounding
216 if (DestVT == MVT::f64) {
217 // do nothing
218 Result = Sub;
219 } else {
220 // if f32 then cast to f32
221 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
222 }
223 NeedsAnotherIteration = true;
224 return Result;
225 }
226 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000227 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000229 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
230 DAG.getConstant(0, Op0.getValueType()),
231 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000232 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
233 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
234 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000235
Jim Laskey6269ed12005-08-17 00:39:29 +0000236 // If the sign bit of the integer is set, the large number will be treated
237 // as a negative number. To counteract this, the dynamic code adds an
238 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000239 uint64_t FF;
240 switch (Op0.getValueType()) {
241 default: assert(0 && "Unsupported integer type!");
242 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
243 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
244 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
245 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
246 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000247 if (TLI.isLittleEndian()) FF <<= 32;
248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000249
Chris Lattner5839bf22005-08-26 17:15:30 +0000250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000251 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
252 SDOperand FudgeInReg;
253 if (DestVT == MVT::f32)
254 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
255 DAG.getSrcValue(NULL));
256 else {
257 assert(DestVT == MVT::f64 && "Unexpected conversion");
258 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
259 DAG.getEntryNode(), CPIdx,
260 DAG.getSrcValue(NULL), MVT::f32));
261 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000262
Chris Lattnercad063f2005-07-16 00:19:57 +0000263 NeedsAnotherIteration = true;
Chris Lattner473a9902005-09-29 06:44:39 +0000264 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnercad063f2005-07-16 00:19:57 +0000265}
266
Chris Lattner149c58c2005-08-16 18:17:10 +0000267/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000268/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000269/// we promote it. At this point, we know that the result and operand types are
270/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
271/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000272SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
273 MVT::ValueType DestVT,
274 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000275 // First step, figure out the appropriate *INT_TO_FP operation to use.
276 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000277
Chris Lattnercad063f2005-07-16 00:19:57 +0000278 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000279
Chris Lattnercad063f2005-07-16 00:19:57 +0000280 // Scan for the appropriate larger type to use.
281 while (1) {
282 NewInTy = (MVT::ValueType)(NewInTy+1);
283 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000284
Chris Lattnercad063f2005-07-16 00:19:57 +0000285 // If the target supports SINT_TO_FP of this type, use it.
286 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
287 default: break;
288 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000289 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000290 break; // Can't use this datatype.
291 // FALL THROUGH.
292 case TargetLowering::Custom:
293 OpToUse = ISD::SINT_TO_FP;
294 break;
295 }
296 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000297 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000298
Chris Lattnercad063f2005-07-16 00:19:57 +0000299 // If the target supports UINT_TO_FP of this type, use it.
300 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
301 default: break;
302 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000303 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000304 break; // Can't use this datatype.
305 // FALL THROUGH.
306 case TargetLowering::Custom:
307 OpToUse = ISD::UINT_TO_FP;
308 break;
309 }
310 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000311
Chris Lattnercad063f2005-07-16 00:19:57 +0000312 // Otherwise, try a larger type.
313 }
314
315 // Make sure to legalize any nodes we create here in the next pass.
316 NeedsAnotherIteration = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000317
Chris Lattnercad063f2005-07-16 00:19:57 +0000318 // Okay, we found the operation and type to use. Zero extend our input to the
319 // desired type then run the operation on it.
320 return DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000321 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
322 NewInTy, LegalOp));
Chris Lattnercad063f2005-07-16 00:19:57 +0000323}
324
Chris Lattner1618beb2005-07-29 00:11:56 +0000325/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
326/// FP_TO_*INT operation of the specified operand when the target requests that
327/// we promote it. At this point, we know that the result and operand types are
328/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
329/// operation that returns a larger result.
330SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
331 MVT::ValueType DestVT,
332 bool isSigned) {
333 // First step, figure out the appropriate FP_TO*INT operation to use.
334 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000335
Chris Lattner1618beb2005-07-29 00:11:56 +0000336 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000337
Chris Lattner1618beb2005-07-29 00:11:56 +0000338 // Scan for the appropriate larger type to use.
339 while (1) {
340 NewOutTy = (MVT::ValueType)(NewOutTy+1);
341 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000342
Chris Lattner1618beb2005-07-29 00:11:56 +0000343 // If the target supports FP_TO_SINT returning this type, use it.
344 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
345 default: break;
346 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000347 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000348 break; // Can't use this datatype.
349 // FALL THROUGH.
350 case TargetLowering::Custom:
351 OpToUse = ISD::FP_TO_SINT;
352 break;
353 }
354 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000355
Chris Lattner1618beb2005-07-29 00:11:56 +0000356 // If the target supports FP_TO_UINT of this type, use it.
357 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
358 default: break;
359 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000360 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000361 break; // Can't use this datatype.
362 // FALL THROUGH.
363 case TargetLowering::Custom:
364 OpToUse = ISD::FP_TO_UINT;
365 break;
366 }
367 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000368
Chris Lattner1618beb2005-07-29 00:11:56 +0000369 // Otherwise, try a larger type.
370 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000371
Chris Lattner1618beb2005-07-29 00:11:56 +0000372 // Make sure to legalize any nodes we create here in the next pass.
373 NeedsAnotherIteration = true;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000374
Chris Lattner1618beb2005-07-29 00:11:56 +0000375 // Okay, we found the operation and type to use. Truncate the result of the
376 // extended FP_TO_*INT operation to the desired size.
377 return DAG.getNode(ISD::TRUNCATE, DestVT,
378 DAG.getNode(OpToUse, NewOutTy, LegalOp));
379}
380
Chris Lattner32fca002005-10-06 01:20:27 +0000381/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
382/// not been visited yet and if all of its operands have already been visited.
383static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
384 std::map<SDNode*, unsigned> &Visited) {
385 if (++Visited[N] != N->getNumOperands())
386 return; // Haven't visited all operands yet
387
388 Order.push_back(N);
389
390 if (N->hasOneUse()) { // Tail recurse in common case.
391 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
392 return;
393 }
394
395 // Now that we have N in, add anything that uses it if all of their operands
396 // are now done.
397
398 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
399 ComputeTopDownOrdering(*UI, Order, Visited);
400}
401
Chris Lattner1618beb2005-07-29 00:11:56 +0000402
Chris Lattner3e928bb2005-01-07 07:47:09 +0000403void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000404 // The legalize process is inherently a bottom-up recursive process (users
405 // legalize their uses before themselves). Given infinite stack space, we
406 // could just start legalizing on the root and traverse the whole graph. In
407 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000408 // blocks. To avoid this problem, compute an ordering of the nodes where each
409 // node is only legalized after all of its operands are legalized.
410 std::map<SDNode*, unsigned> Visited;
411 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000412
Chris Lattner32fca002005-10-06 01:20:27 +0000413 // Compute ordering from all of the leaves in the graphs, those (like the
414 // entry node) that have no operands.
415 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
416 E = DAG.allnodes_end(); I != E; ++I) {
417 if ((*I)->getNumOperands() == 0) {
418 Visited[*I] = 0 - 1U;
419 ComputeTopDownOrdering(*I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000420 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000421 }
422
Chris Lattner32fca002005-10-06 01:20:27 +0000423 assert(Order.size() == Visited.size() && Order.size() == DAG.allnodes_size()&&
424 "Error: DAG is cyclic!");
425 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000426
Chris Lattner32fca002005-10-06 01:20:27 +0000427 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
428 SDNode *N = Order[i];
429 switch (getTypeAction(N->getValueType(0))) {
430 default: assert(0 && "Bad type action!");
431 case Legal:
432 LegalizeOp(SDOperand(N, 0));
433 break;
434 case Promote:
435 PromoteOp(SDOperand(N, 0));
436 break;
437 case Expand: {
438 SDOperand X, Y;
439 ExpandOp(SDOperand(N, 0), X, Y);
440 break;
441 }
442 }
443 }
444
445 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000446 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000447 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
448 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000449
450 ExpandedNodes.clear();
451 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000452 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000453
454 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000455 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000456}
457
458SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000459 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000460 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000461 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000462
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000464 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000465 if (Node->getNumValues() > 1) {
466 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
467 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000468 case Legal: break; // Nothing to do.
469 case Expand: {
470 SDOperand T1, T2;
471 ExpandOp(Op.getValue(i), T1, T2);
472 assert(LegalizedNodes.count(Op) &&
473 "Expansion didn't add legal operands!");
474 return LegalizedNodes[Op];
475 }
476 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000477 PromoteOp(Op.getValue(i));
478 assert(LegalizedNodes.count(Op) &&
479 "Expansion didn't add legal operands!");
480 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000481 }
482 }
483
Chris Lattner45982da2005-05-12 16:53:42 +0000484 // Note that LegalizeOp may be reentered even from single-use nodes, which
485 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000486 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
487 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000488
Nate Begeman9373a812005-08-10 20:51:12 +0000489 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490
491 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000492
493 switch (Node->getOpcode()) {
494 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000495 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
496 // If this is a target node, legalize it by legalizing the operands then
497 // passing it through.
498 std::vector<SDOperand> Ops;
499 bool Changed = false;
500 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
501 Ops.push_back(LegalizeOp(Node->getOperand(i)));
502 Changed = Changed || Node->getOperand(i) != Ops.back();
503 }
504 if (Changed)
505 if (Node->getNumValues() == 1)
506 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
507 else {
508 std::vector<MVT::ValueType> VTs(Node->value_begin(),
509 Node->value_end());
510 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
511 }
512
513 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
514 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
515 return Result.getValue(Op.ResNo);
516 }
517 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000518 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
519 assert(0 && "Do not know how to legalize this operator!");
520 abort();
521 case ISD::EntryToken:
522 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000523 case ISD::TargetFrameIndex:
524 case ISD::Register:
525 case ISD::TargetConstant:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000526 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000527 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000528 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000529 case ISD::BasicBlock:
530 case ISD::CONDCODE:
531 case ISD::VALUETYPE:
532 case ISD::SRCVALUE:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000533 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000534 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000535 case ISD::AssertSext:
536 case ISD::AssertZext:
537 Tmp1 = LegalizeOp(Node->getOperand(0));
538 if (Tmp1 != Node->getOperand(0))
539 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
540 Node->getOperand(1));
541 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000542 case ISD::CopyFromReg:
543 Tmp1 = LegalizeOp(Node->getOperand(0));
544 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000545 Result = DAG.getCopyFromReg(Tmp1,
546 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
547 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000548 else
549 Result = Op.getValue(0);
550
551 // Since CopyFromReg produces two values, make sure to remember that we
552 // legalized both of them.
553 AddLegalizedOperand(Op.getValue(0), Result);
554 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
555 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000556 case ISD::ImplicitDef:
557 Tmp1 = LegalizeOp(Node->getOperand(0));
558 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000559 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
560 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000561 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000562 case ISD::UNDEF: {
563 MVT::ValueType VT = Op.getValueType();
564 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000565 default: assert(0 && "This action is not supported yet!");
566 case TargetLowering::Expand:
567 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000568 if (MVT::isInteger(VT))
569 Result = DAG.getConstant(0, VT);
570 else if (MVT::isFloatingPoint(VT))
571 Result = DAG.getConstantFP(0, VT);
572 else
573 assert(0 && "Unknown value type!");
574 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000575 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000576 break;
577 }
578 break;
579 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000580 case ISD::Constant:
581 // We know we don't need to expand constants here, constants only have one
582 // value and we check that it is fine above.
583
584 // FIXME: Maybe we should handle things like targets that don't support full
585 // 32-bit immediates?
586 break;
587 case ISD::ConstantFP: {
588 // Spill FP immediates to the constant pool if the target cannot directly
589 // codegen them. Targets often have some immediate values that can be
590 // efficiently generated into an FP register without a load. We explicitly
591 // leave these constants as ConstantFP nodes for the target to deal with.
592
593 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
594
595 // Check to see if this FP immediate is already legal.
596 bool isLegal = false;
597 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
598 E = TLI.legal_fpimm_end(); I != E; ++I)
599 if (CFP->isExactlyValue(*I)) {
600 isLegal = true;
601 break;
602 }
603
604 if (!isLegal) {
605 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000606 bool Extend = false;
607
608 // If a FP immediate is precise when represented as a float, we put it
609 // into the constant pool as a float, even if it's is statically typed
610 // as a double.
611 MVT::ValueType VT = CFP->getValueType(0);
612 bool isDouble = VT == MVT::f64;
613 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
614 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000615 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
616 // Only do this if the target has a native EXTLOAD instruction from
617 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000618 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000619 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
620 VT = MVT::f32;
621 Extend = true;
622 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000623
Chris Lattner5839bf22005-08-26 17:15:30 +0000624 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000625 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000626 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
627 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000628 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000629 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
630 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000631 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000632 }
633 break;
634 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000635 case ISD::TokenFactor: {
636 std::vector<SDOperand> Ops;
637 bool Changed = false;
Nate Begemanded49632005-10-13 03:11:28 +0000638 // Legalize the operands
Chris Lattnera385e9b2005-01-13 17:59:25 +0000639 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000640 SDOperand Op = Node->getOperand(i);
Nate Begemanded49632005-10-13 03:11:28 +0000641 Ops.push_back(LegalizeOp(Op));
642 Changed |= Ops[i] != Op;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000643 }
644 if (Changed)
645 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
646 break;
647 }
648
Chris Lattner16cd04d2005-05-12 23:24:06 +0000649 case ISD::CALLSEQ_START:
650 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000651 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000652 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000653 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000654 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000655 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000656
Chris Lattner16cd04d2005-05-12 23:24:06 +0000657 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000658 // nodes are treated specially and are mutated in place. This makes the dag
659 // legalization process more efficient and also makes libcall insertion
660 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000661 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000662 case ISD::DYNAMIC_STACKALLOC:
663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
665 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
666 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000667 Tmp3 != Node->getOperand(2)) {
668 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
669 std::vector<SDOperand> Ops;
670 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
671 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
672 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000673 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000674
675 // Since this op produces two values, make sure to remember that we
676 // legalized both of them.
677 AddLegalizedOperand(SDOperand(Node, 0), Result);
678 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
679 return Result.getValue(Op.ResNo);
680
Chris Lattnerd71c0412005-05-13 18:43:43 +0000681 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000682 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
684 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000685
686 bool Changed = false;
687 std::vector<SDOperand> Ops;
688 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
689 Ops.push_back(LegalizeOp(Node->getOperand(i)));
690 Changed |= Ops.back() != Node->getOperand(i);
691 }
692
693 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000694 std::vector<MVT::ValueType> RetTyVTs;
695 RetTyVTs.reserve(Node->getNumValues());
696 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000697 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000698 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
699 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000700 } else {
701 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000702 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000703 // Since calls produce multiple values, make sure to remember that we
704 // legalized all of them.
705 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
706 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
707 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000708 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000709 case ISD::BR:
710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
711 if (Tmp1 != Node->getOperand(0))
712 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
713 break;
714
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000715 case ISD::BRCOND:
716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000717
Chris Lattner47e92232005-01-18 19:27:06 +0000718 switch (getTypeAction(Node->getOperand(1).getValueType())) {
719 case Expand: assert(0 && "It's impossible to expand bools");
720 case Legal:
721 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
722 break;
723 case Promote:
724 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
725 break;
726 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000727
728 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
729 default: assert(0 && "This action is not supported yet!");
730 case TargetLowering::Expand:
731 // Expand brcond's setcc into its constituent parts and create a BR_CC
732 // Node.
733 if (Tmp2.getOpcode() == ISD::SETCC) {
734 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
735 Tmp2.getOperand(0), Tmp2.getOperand(1),
736 Node->getOperand(2));
737 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000738 // Make sure the condition is either zero or one. It may have been
739 // promoted from something else.
740 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
741
Nate Begeman7cbd5252005-08-16 19:49:35 +0000742 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
743 DAG.getCondCode(ISD::SETNE), Tmp2,
744 DAG.getConstant(0, Tmp2.getValueType()),
745 Node->getOperand(2));
746 }
747 break;
748 case TargetLowering::Legal:
749 // Basic block destination (Op#2) is always legal.
750 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
751 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
752 Node->getOperand(2));
753 break;
754 }
755 break;
756 case ISD::BR_CC:
757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
758
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000759 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000760 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
761 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
762 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
763 Tmp3 != Node->getOperand(3)) {
764 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
765 Tmp2, Tmp3, Node->getOperand(4));
766 }
767 break;
768 } else {
769 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
770 Node->getOperand(2), // LHS
771 Node->getOperand(3), // RHS
772 Node->getOperand(1)));
773 // If we get a SETCC back from legalizing the SETCC node we just
774 // created, then use its LHS, RHS, and CC directly in creating a new
775 // node. Otherwise, select between the true and false value based on
776 // comparing the result of the legalized with zero.
777 if (Tmp2.getOpcode() == ISD::SETCC) {
778 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
779 Tmp2.getOperand(0), Tmp2.getOperand(1),
780 Node->getOperand(4));
781 } else {
782 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
783 DAG.getCondCode(ISD::SETNE),
784 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
785 Node->getOperand(4));
786 }
787 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000788 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000789 case ISD::BRCONDTWOWAY:
790 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
791 switch (getTypeAction(Node->getOperand(1).getValueType())) {
792 case Expand: assert(0 && "It's impossible to expand bools");
793 case Legal:
794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
795 break;
796 case Promote:
797 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
798 break;
799 }
800 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
801 // pair.
802 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
803 case TargetLowering::Promote:
804 default: assert(0 && "This action is not supported yet!");
805 case TargetLowering::Legal:
806 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
807 std::vector<SDOperand> Ops;
808 Ops.push_back(Tmp1);
809 Ops.push_back(Tmp2);
810 Ops.push_back(Node->getOperand(2));
811 Ops.push_back(Node->getOperand(3));
812 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
813 }
814 break;
815 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000816 // If BRTWOWAY_CC is legal for this target, then simply expand this node
817 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
818 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000819 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000820 if (Tmp2.getOpcode() == ISD::SETCC) {
821 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
822 Tmp2.getOperand(0), Tmp2.getOperand(1),
823 Node->getOperand(2), Node->getOperand(3));
824 } else {
825 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
826 DAG.getConstant(0, Tmp2.getValueType()),
827 Node->getOperand(2), Node->getOperand(3));
828 }
829 } else {
830 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000831 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000832 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
833 }
Chris Lattner411e8882005-04-09 03:30:19 +0000834 break;
835 }
836 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000837 case ISD::BRTWOWAY_CC:
838 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000839 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000840 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
841 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
842 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
843 Tmp3 != Node->getOperand(3)) {
844 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
845 Node->getOperand(4), Node->getOperand(5));
846 }
847 break;
848 } else {
849 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
850 Node->getOperand(2), // LHS
851 Node->getOperand(3), // RHS
852 Node->getOperand(1)));
853 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
854 // pair.
855 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
856 default: assert(0 && "This action is not supported yet!");
857 case TargetLowering::Legal:
858 // If we get a SETCC back from legalizing the SETCC node we just
859 // created, then use its LHS, RHS, and CC directly in creating a new
860 // node. Otherwise, select between the true and false value based on
861 // comparing the result of the legalized with zero.
862 if (Tmp2.getOpcode() == ISD::SETCC) {
863 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
864 Tmp2.getOperand(0), Tmp2.getOperand(1),
865 Node->getOperand(4), Node->getOperand(5));
866 } else {
867 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
868 DAG.getConstant(0, Tmp2.getValueType()),
869 Node->getOperand(4), Node->getOperand(5));
870 }
871 break;
872 case TargetLowering::Expand:
873 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
874 Node->getOperand(4));
875 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
876 break;
877 }
878 }
879 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000880 case ISD::LOAD:
881 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
882 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000883
Chris Lattner3e928bb2005-01-07 07:47:09 +0000884 if (Tmp1 != Node->getOperand(0) ||
885 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000886 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
887 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000888 else
889 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000890
Chris Lattner8afc48e2005-01-07 22:28:47 +0000891 // Since loads produce two values, make sure to remember that we legalized
892 // both of them.
893 AddLegalizedOperand(SDOperand(Node, 0), Result);
894 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
895 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000896
Chris Lattner0f69b292005-01-15 06:18:18 +0000897 case ISD::EXTLOAD:
898 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000899 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000900 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
901 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000902
Chris Lattner5f056bf2005-07-10 01:55:33 +0000903 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000904 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000905 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000906 case TargetLowering::Promote:
907 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000908 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
909 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000910 // Since loads produce two values, make sure to remember that we legalized
911 // both of them.
912 AddLegalizedOperand(SDOperand(Node, 0), Result);
913 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
914 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000915
Chris Lattner01ff7212005-04-10 22:54:25 +0000916 case TargetLowering::Legal:
917 if (Tmp1 != Node->getOperand(0) ||
918 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000919 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
920 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000921 else
922 Result = SDOperand(Node, 0);
923
924 // Since loads produce two values, make sure to remember that we legalized
925 // both of them.
926 AddLegalizedOperand(SDOperand(Node, 0), Result);
927 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
928 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000929 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000930 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
931 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
932 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000933 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000934 if (Op.ResNo)
935 return Load.getValue(1);
936 return Result;
937 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000938 assert(Node->getOpcode() != ISD::EXTLOAD &&
939 "EXTLOAD should always be supported!");
940 // Turn the unsupported load into an EXTLOAD followed by an explicit
941 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000942 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
943 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000944 SDOperand ValRes;
945 if (Node->getOpcode() == ISD::SEXTLOAD)
946 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000947 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000948 else
949 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000950 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
951 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
952 if (Op.ResNo)
953 return Result.getValue(1);
954 return ValRes;
955 }
956 assert(0 && "Unreachable");
957 }
Nate Begeman5dc897b2005-10-19 00:06:56 +0000958 case ISD::EXTRACT_ELEMENT: {
959 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
960 switch (getTypeAction(OpTy)) {
961 default:
962 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
963 break;
964 case Legal:
965 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
966 // 1 -> Hi
967 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
968 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
969 TLI.getShiftAmountTy()));
970 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
971 } else {
972 // 0 -> Lo
973 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
974 Node->getOperand(0));
975 }
976 Result = LegalizeOp(Result);
977 break;
978 case Expand:
979 // Get both the low and high parts.
980 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
981 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
982 Result = Tmp2; // 1 -> Hi
983 else
984 Result = Tmp1; // 0 -> Lo
985 break;
986 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000987 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +0000988 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000989
990 case ISD::CopyToReg:
991 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000992
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000993 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000994 "Register type must be legal!");
995 // Legalize the incoming value (must be legal).
996 Tmp2 = LegalizeOp(Node->getOperand(2));
997 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
998 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
999 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001000 break;
1001
1002 case ISD::RET:
1003 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1004 switch (Node->getNumOperands()) {
1005 case 2: // ret val
1006 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1007 case Legal:
1008 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001009 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001010 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1011 break;
1012 case Expand: {
1013 SDOperand Lo, Hi;
1014 ExpandOp(Node->getOperand(1), Lo, Hi);
1015 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001016 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001017 }
1018 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001019 Tmp2 = PromoteOp(Node->getOperand(1));
1020 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1021 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001022 }
1023 break;
1024 case 1: // ret void
1025 if (Tmp1 != Node->getOperand(0))
1026 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1027 break;
1028 default: { // ret <values>
1029 std::vector<SDOperand> NewValues;
1030 NewValues.push_back(Tmp1);
1031 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1032 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1033 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001034 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001035 break;
1036 case Expand: {
1037 SDOperand Lo, Hi;
1038 ExpandOp(Node->getOperand(i), Lo, Hi);
1039 NewValues.push_back(Lo);
1040 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001041 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001042 }
1043 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001044 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001045 }
1046 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1047 break;
1048 }
1049 }
1050 break;
1051 case ISD::STORE:
1052 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1053 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1054
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001055 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001056 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001057 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001058 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001059 DAG.getConstant(FloatToBits(CFP->getValue()),
1060 MVT::i32),
1061 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001062 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001063 } else {
1064 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001065 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001066 DAG.getConstant(DoubleToBits(CFP->getValue()),
1067 MVT::i64),
1068 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001069 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001070 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001071 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001072 }
1073
Chris Lattner3e928bb2005-01-07 07:47:09 +00001074 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1075 case Legal: {
1076 SDOperand Val = LegalizeOp(Node->getOperand(1));
1077 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1078 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001079 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1080 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001081 break;
1082 }
1083 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001084 // Truncate the value and store the result.
1085 Tmp3 = PromoteOp(Node->getOperand(1));
1086 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001087 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001088 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001089 break;
1090
Chris Lattner3e928bb2005-01-07 07:47:09 +00001091 case Expand:
1092 SDOperand Lo, Hi;
1093 ExpandOp(Node->getOperand(1), Lo, Hi);
1094
1095 if (!TLI.isLittleEndian())
1096 std::swap(Lo, Hi);
1097
Chris Lattneredb1add2005-05-11 04:51:16 +00001098 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1099 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001100 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001101 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1102 getIntPtrConstant(IncrementSize));
1103 assert(isTypeLegal(Tmp2.getValueType()) &&
1104 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001105 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001106 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1107 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001108 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1109 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001110 }
1111 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001112 case ISD::PCMARKER:
1113 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001114 if (Tmp1 != Node->getOperand(0))
1115 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001116 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001117 case ISD::TRUNCSTORE:
1118 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1119 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1120
1121 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1122 case Legal:
1123 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001124
1125 // The only promote case we handle is TRUNCSTORE:i1 X into
1126 // -> TRUNCSTORE:i8 (and X, 1)
1127 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1128 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1129 TargetLowering::Promote) {
1130 // Promote the bool to a mask then store.
1131 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1132 DAG.getConstant(1, Tmp2.getValueType()));
1133 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1134 Node->getOperand(3), DAG.getValueType(MVT::i8));
1135
1136 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1137 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001138 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001139 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001140 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001141 break;
1142 case Promote:
1143 case Expand:
1144 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1145 }
1146 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001147 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001148 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1149 case Expand: assert(0 && "It's impossible to expand bools");
1150 case Legal:
1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1152 break;
1153 case Promote:
1154 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1155 break;
1156 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001157 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001158 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001159
Nate Begemanb942a3d2005-08-23 04:29:48 +00001160 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001161 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001162 case TargetLowering::Expand:
1163 if (Tmp1.getOpcode() == ISD::SETCC) {
1164 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1165 Tmp2, Tmp3,
1166 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1167 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001168 // Make sure the condition is either zero or one. It may have been
1169 // promoted from something else.
1170 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001171 Result = DAG.getSelectCC(Tmp1,
1172 DAG.getConstant(0, Tmp1.getValueType()),
1173 Tmp2, Tmp3, ISD::SETNE);
1174 }
1175 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001176 case TargetLowering::Legal:
1177 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1178 Tmp3 != Node->getOperand(2))
1179 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1180 Tmp1, Tmp2, Tmp3);
1181 break;
1182 case TargetLowering::Promote: {
1183 MVT::ValueType NVT =
1184 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1185 unsigned ExtOp, TruncOp;
1186 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001187 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001188 TruncOp = ISD::TRUNCATE;
1189 } else {
1190 ExtOp = ISD::FP_EXTEND;
1191 TruncOp = ISD::FP_ROUND;
1192 }
1193 // Promote each of the values to the new type.
1194 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1195 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1196 // Perform the larger operation, then round down.
1197 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1198 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1199 break;
1200 }
1201 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001202 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001203 case ISD::SELECT_CC:
1204 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1205 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1206
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001207 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001208 // Everything is legal, see if we should expand this op or something.
1209 switch (TLI.getOperationAction(ISD::SELECT_CC,
1210 Node->getOperand(0).getValueType())) {
1211 default: assert(0 && "This action is not supported yet!");
1212 case TargetLowering::Custom: {
1213 SDOperand Tmp =
1214 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1215 Node->getOperand(0),
1216 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001217 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001218 if (Tmp.Val) {
1219 Result = LegalizeOp(Tmp);
1220 break;
1221 }
1222 } // FALLTHROUGH if the target can't lower this operation after all.
1223 case TargetLowering::Legal:
1224 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1225 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1226 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1227 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1228 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1229 Tmp3, Tmp4, Node->getOperand(4));
1230 }
1231 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001232 }
1233 break;
1234 } else {
1235 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1236 Node->getOperand(0), // LHS
1237 Node->getOperand(1), // RHS
1238 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001239 // If we get a SETCC back from legalizing the SETCC node we just
1240 // created, then use its LHS, RHS, and CC directly in creating a new
1241 // node. Otherwise, select between the true and false value based on
1242 // comparing the result of the legalized with zero.
1243 if (Tmp1.getOpcode() == ISD::SETCC) {
1244 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1245 Tmp1.getOperand(0), Tmp1.getOperand(1),
1246 Tmp3, Tmp4, Tmp1.getOperand(2));
1247 } else {
1248 Result = DAG.getSelectCC(Tmp1,
1249 DAG.getConstant(0, Tmp1.getValueType()),
1250 Tmp3, Tmp4, ISD::SETNE);
1251 }
Nate Begeman9373a812005-08-10 20:51:12 +00001252 }
1253 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001254 case ISD::SETCC:
1255 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1256 case Legal:
1257 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1258 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001259 break;
1260 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001261 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1262 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1263
1264 // If this is an FP compare, the operands have already been extended.
1265 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1266 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001267 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001268
1269 // Otherwise, we have to insert explicit sign or zero extends. Note
1270 // that we could insert sign extends for ALL conditions, but zero extend
1271 // is cheaper on many machines (an AND instead of two shifts), so prefer
1272 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001273 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001274 default: assert(0 && "Unknown integer comparison!");
1275 case ISD::SETEQ:
1276 case ISD::SETNE:
1277 case ISD::SETUGE:
1278 case ISD::SETUGT:
1279 case ISD::SETULE:
1280 case ISD::SETULT:
1281 // ALL of these operations will work if we either sign or zero extend
1282 // the operands (including the unsigned comparisons!). Zero extend is
1283 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001284 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1285 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001286 break;
1287 case ISD::SETGE:
1288 case ISD::SETGT:
1289 case ISD::SETLT:
1290 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001291 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1292 DAG.getValueType(VT));
1293 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1294 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001295 break;
1296 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001298 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001299 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001300 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1301 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1302 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001303 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001304 case ISD::SETEQ:
1305 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001306 if (RHSLo == RHSHi)
1307 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1308 if (RHSCST->isAllOnesValue()) {
1309 // Comparison to -1.
1310 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001311 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001312 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001313 }
1314
Chris Lattner3e928bb2005-01-07 07:47:09 +00001315 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1316 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1317 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001318 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001319 break;
1320 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001321 // If this is a comparison of the sign bit, just look at the top part.
1322 // X > -1, x < 0
1323 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001324 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001325 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001326 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001327 (CST->isAllOnesValue()))) { // X > -1
1328 Tmp1 = LHSHi;
1329 Tmp2 = RHSHi;
1330 break;
1331 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001332
Chris Lattner3e928bb2005-01-07 07:47:09 +00001333 // FIXME: This generated code sucks.
1334 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001335 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001336 default: assert(0 && "Unknown integer setcc!");
1337 case ISD::SETLT:
1338 case ISD::SETULT: LowCC = ISD::SETULT; break;
1339 case ISD::SETGT:
1340 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1341 case ISD::SETLE:
1342 case ISD::SETULE: LowCC = ISD::SETULE; break;
1343 case ISD::SETGE:
1344 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1345 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001346
Chris Lattner3e928bb2005-01-07 07:47:09 +00001347 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1348 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1349 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1350
1351 // NOTE: on targets without efficient SELECT of bools, we can always use
1352 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001353 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1354 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1355 Node->getOperand(2));
1356 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001357 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1358 Result, Tmp1, Tmp2));
1359 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001360 }
1361 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001362
1363 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1364 default:
1365 assert(0 && "Cannot handle this action for SETCC yet!");
1366 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001367 case TargetLowering::Promote:
1368 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1369 Node->getOperand(2));
1370 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001371 case TargetLowering::Legal:
1372 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1373 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1374 Node->getOperand(2));
1375 break;
1376 case TargetLowering::Expand:
1377 // Expand a setcc node into a select_cc of the same condition, lhs, and
1378 // rhs that selects between const 1 (true) and const 0 (false).
1379 MVT::ValueType VT = Node->getValueType(0);
1380 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1381 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1382 Node->getOperand(2));
1383 Result = LegalizeOp(Result);
1384 break;
1385 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001386 break;
1387
Chris Lattnere1bd8222005-01-11 05:57:22 +00001388 case ISD::MEMSET:
1389 case ISD::MEMCPY:
1390 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001391 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001392 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1393
1394 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1395 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1396 case Expand: assert(0 && "Cannot expand a byte!");
1397 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001398 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001399 break;
1400 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001401 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001402 break;
1403 }
1404 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001405 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001406 }
Chris Lattner272455b2005-02-02 03:44:41 +00001407
1408 SDOperand Tmp4;
1409 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001410 case Expand: {
1411 // Length is too big, just take the lo-part of the length.
1412 SDOperand HiPart;
1413 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1414 break;
1415 }
Chris Lattnere5605212005-01-28 22:29:18 +00001416 case Legal:
1417 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001418 break;
1419 case Promote:
1420 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001421 break;
1422 }
1423
1424 SDOperand Tmp5;
1425 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1426 case Expand: assert(0 && "Cannot expand this yet!");
1427 case Legal:
1428 Tmp5 = LegalizeOp(Node->getOperand(4));
1429 break;
1430 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001431 Tmp5 = PromoteOp(Node->getOperand(4));
1432 break;
1433 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001434
1435 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1436 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001437 case TargetLowering::Custom: {
1438 SDOperand Tmp =
1439 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1440 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1441 if (Tmp.Val) {
1442 Result = LegalizeOp(Tmp);
1443 break;
1444 }
1445 // FALLTHROUGH if the target thinks it is legal.
1446 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001447 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001448 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1449 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1450 Tmp5 != Node->getOperand(4)) {
1451 std::vector<SDOperand> Ops;
1452 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1453 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1454 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1455 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001456 break;
1457 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001458 // Otherwise, the target does not support this operation. Lower the
1459 // operation to an explicit libcall as appropriate.
1460 MVT::ValueType IntPtr = TLI.getPointerTy();
1461 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1462 std::vector<std::pair<SDOperand, const Type*> > Args;
1463
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001464 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001465 if (Node->getOpcode() == ISD::MEMSET) {
1466 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1467 // Extend the ubyte argument to be an int value for the call.
1468 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1469 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1470 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1471
1472 FnName = "memset";
1473 } else if (Node->getOpcode() == ISD::MEMCPY ||
1474 Node->getOpcode() == ISD::MEMMOVE) {
1475 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1476 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1477 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1478 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1479 } else {
1480 assert(0 && "Unknown op!");
1481 }
Chris Lattner45982da2005-05-12 16:53:42 +00001482
Chris Lattnere1bd8222005-01-11 05:57:22 +00001483 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001484 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001485 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001486 Result = CallResult.second;
1487 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001488 break;
1489 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001490 }
1491 break;
1492 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001493
1494 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001495 Tmp1 = LegalizeOp(Node->getOperand(0));
1496 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001497
Chris Lattner3e011362005-05-14 07:45:46 +00001498 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1499 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1500 std::vector<SDOperand> Ops;
1501 Ops.push_back(Tmp1);
1502 Ops.push_back(Tmp2);
1503 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1504 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001505 Result = SDOperand(Node, 0);
1506 // Since these produce two values, make sure to remember that we legalized
1507 // both of them.
1508 AddLegalizedOperand(SDOperand(Node, 0), Result);
1509 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1510 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001511 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001512 Tmp1 = LegalizeOp(Node->getOperand(0));
1513 Tmp2 = LegalizeOp(Node->getOperand(1));
1514 Tmp3 = LegalizeOp(Node->getOperand(2));
1515 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1516 Tmp3 != Node->getOperand(2))
1517 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1518 break;
1519
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001520 case ISD::READIO:
1521 Tmp1 = LegalizeOp(Node->getOperand(0));
1522 Tmp2 = LegalizeOp(Node->getOperand(1));
1523
1524 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1525 case TargetLowering::Custom:
1526 default: assert(0 && "This action not implemented for this operation!");
1527 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001528 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1529 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1530 std::vector<SDOperand> Ops;
1531 Ops.push_back(Tmp1);
1532 Ops.push_back(Tmp2);
1533 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1534 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001535 Result = SDOperand(Node, 0);
1536 break;
1537 case TargetLowering::Expand:
1538 // Replace this with a load from memory.
1539 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1540 Node->getOperand(1), DAG.getSrcValue(NULL));
1541 Result = LegalizeOp(Result);
1542 break;
1543 }
1544
1545 // Since these produce two values, make sure to remember that we legalized
1546 // both of them.
1547 AddLegalizedOperand(SDOperand(Node, 0), Result);
1548 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1549 return Result.getValue(Op.ResNo);
1550
1551 case ISD::WRITEIO:
1552 Tmp1 = LegalizeOp(Node->getOperand(0));
1553 Tmp2 = LegalizeOp(Node->getOperand(1));
1554 Tmp3 = LegalizeOp(Node->getOperand(2));
1555
1556 switch (TLI.getOperationAction(Node->getOpcode(),
1557 Node->getOperand(1).getValueType())) {
1558 case TargetLowering::Custom:
1559 default: assert(0 && "This action not implemented for this operation!");
1560 case TargetLowering::Legal:
1561 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1562 Tmp3 != Node->getOperand(2))
1563 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1564 break;
1565 case TargetLowering::Expand:
1566 // Replace this with a store to memory.
1567 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1568 Node->getOperand(1), Node->getOperand(2),
1569 DAG.getSrcValue(NULL));
1570 Result = LegalizeOp(Result);
1571 break;
1572 }
1573 break;
1574
Chris Lattner84f67882005-01-20 18:52:28 +00001575 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001576 case ISD::SUB_PARTS:
1577 case ISD::SHL_PARTS:
1578 case ISD::SRA_PARTS:
1579 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001580 std::vector<SDOperand> Ops;
1581 bool Changed = false;
1582 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1583 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1584 Changed |= Ops.back() != Node->getOperand(i);
1585 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001586 if (Changed) {
1587 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1588 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1589 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001590
1591 // Since these produce multiple values, make sure to remember that we
1592 // legalized all of them.
1593 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1594 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1595 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001596 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001597
1598 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001599 case ISD::ADD:
1600 case ISD::SUB:
1601 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001602 case ISD::MULHS:
1603 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001604 case ISD::UDIV:
1605 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001606 case ISD::AND:
1607 case ISD::OR:
1608 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001609 case ISD::SHL:
1610 case ISD::SRL:
1611 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001612 case ISD::FADD:
1613 case ISD::FSUB:
1614 case ISD::FMUL:
1615 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001616 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001617 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1618 case Expand: assert(0 && "Not possible");
1619 case Legal:
1620 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1621 break;
1622 case Promote:
1623 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1624 break;
1625 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001626 if (Tmp1 != Node->getOperand(0) ||
1627 Tmp2 != Node->getOperand(1))
1628 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1629 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001630
Nate Begeman419f8b62005-10-18 00:27:41 +00001631 case ISD::BUILD_PAIR: {
1632 MVT::ValueType PairTy = Node->getValueType(0);
1633 // TODO: handle the case where the Lo and Hi operands are not of legal type
1634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1636 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1637 case TargetLowering::Legal:
1638 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1639 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1640 break;
1641 case TargetLowering::Promote:
1642 case TargetLowering::Custom:
1643 assert(0 && "Cannot promote/custom this yet!");
1644 case TargetLowering::Expand:
1645 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1646 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1647 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1648 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1649 TLI.getShiftAmountTy()));
1650 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1651 break;
1652 }
1653 break;
1654 }
1655
Nate Begemanc105e192005-04-06 00:23:54 +00001656 case ISD::UREM:
1657 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001658 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001659 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1660 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1661 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1662 case TargetLowering::Legal:
1663 if (Tmp1 != Node->getOperand(0) ||
1664 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001665 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001666 Tmp2);
1667 break;
1668 case TargetLowering::Promote:
1669 case TargetLowering::Custom:
1670 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001671 case TargetLowering::Expand:
1672 if (MVT::isInteger(Node->getValueType(0))) {
1673 MVT::ValueType VT = Node->getValueType(0);
1674 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1675 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1676 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1677 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1678 } else {
1679 // Floating point mod -> fmod libcall.
1680 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1681 SDOperand Dummy;
1682 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001683 }
1684 break;
1685 }
1686 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001687
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001688 case ISD::CTPOP:
1689 case ISD::CTTZ:
1690 case ISD::CTLZ:
1691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1692 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1693 case TargetLowering::Legal:
1694 if (Tmp1 != Node->getOperand(0))
1695 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1696 break;
1697 case TargetLowering::Promote: {
1698 MVT::ValueType OVT = Tmp1.getValueType();
1699 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001700
1701 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001702 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1703 // Perform the larger operation, then subtract if needed.
1704 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1705 switch(Node->getOpcode())
1706 {
1707 case ISD::CTPOP:
1708 Result = Tmp1;
1709 break;
1710 case ISD::CTTZ:
1711 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001712 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1713 DAG.getConstant(getSizeInBits(NVT), NVT),
1714 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001715 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001716 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1717 break;
1718 case ISD::CTLZ:
1719 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001720 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1721 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001722 getSizeInBits(OVT), NVT));
1723 break;
1724 }
1725 break;
1726 }
1727 case TargetLowering::Custom:
1728 assert(0 && "Cannot custom handle this yet!");
1729 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001730 switch(Node->getOpcode())
1731 {
1732 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001733 static const uint64_t mask[6] = {
1734 0x5555555555555555ULL, 0x3333333333333333ULL,
1735 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1736 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1737 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001738 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001739 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1740 unsigned len = getSizeInBits(VT);
1741 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001742 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001743 Tmp2 = DAG.getConstant(mask[i], VT);
1744 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001745 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001746 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1747 DAG.getNode(ISD::AND, VT,
1748 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1749 Tmp2));
1750 }
1751 Result = Tmp1;
1752 break;
1753 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001754 case ISD::CTLZ: {
1755 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001756 x = x | (x >> 1);
1757 x = x | (x >> 2);
1758 ...
1759 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001760 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001761 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001762
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001763 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1764 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001765 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1766 unsigned len = getSizeInBits(VT);
1767 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1768 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001769 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001770 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1771 }
1772 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001773 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001774 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001775 }
1776 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001777 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001778 // unless the target has ctlz but not ctpop, in which case we use:
1779 // { return 32 - nlz(~x & (x-1)); }
1780 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001781 MVT::ValueType VT = Tmp1.getValueType();
1782 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001783 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001784 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1785 DAG.getNode(ISD::SUB, VT, Tmp1,
1786 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001787 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001788 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1789 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001790 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001791 DAG.getConstant(getSizeInBits(VT), VT),
1792 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1793 } else {
1794 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1795 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001796 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001797 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001798 default:
1799 assert(0 && "Cannot expand this yet!");
1800 break;
1801 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001802 break;
1803 }
1804 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001805
Chris Lattner2c8086f2005-04-02 05:00:07 +00001806 // Unary operators
1807 case ISD::FABS:
1808 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001809 case ISD::FSQRT:
1810 case ISD::FSIN:
1811 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001812 Tmp1 = LegalizeOp(Node->getOperand(0));
1813 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1814 case TargetLowering::Legal:
1815 if (Tmp1 != Node->getOperand(0))
1816 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1817 break;
1818 case TargetLowering::Promote:
1819 case TargetLowering::Custom:
1820 assert(0 && "Cannot promote/custom handle this yet!");
1821 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001822 switch(Node->getOpcode()) {
1823 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001824 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1825 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001826 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00001827 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001828 break;
1829 }
1830 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001831 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1832 MVT::ValueType VT = Node->getValueType(0);
1833 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001834 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001835 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1836 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1837 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001838 break;
1839 }
1840 case ISD::FSQRT:
1841 case ISD::FSIN:
1842 case ISD::FCOS: {
1843 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001844 const char *FnName = 0;
1845 switch(Node->getOpcode()) {
1846 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1847 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1848 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1849 default: assert(0 && "Unreachable!");
1850 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001851 SDOperand Dummy;
1852 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001853 break;
1854 }
1855 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001856 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001857 }
1858 break;
1859 }
1860 break;
1861
1862 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001863 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001864 case ISD::UINT_TO_FP: {
1865 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001866 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1867 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001868 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001869 Node->getOperand(0).getValueType())) {
1870 default: assert(0 && "Unknown operation action!");
1871 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001872 Result = ExpandLegalINT_TO_FP(isSigned,
1873 LegalizeOp(Node->getOperand(0)),
1874 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001875 AddLegalizedOperand(Op, Result);
1876 return Result;
1877 case TargetLowering::Promote:
1878 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1879 Node->getValueType(0),
1880 isSigned);
1881 AddLegalizedOperand(Op, Result);
1882 return Result;
1883 case TargetLowering::Legal:
1884 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001885 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001886
Chris Lattner3e928bb2005-01-07 07:47:09 +00001887 Tmp1 = LegalizeOp(Node->getOperand(0));
1888 if (Tmp1 != Node->getOperand(0))
1889 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1890 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001891 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001892 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1893 Node->getValueType(0), Node->getOperand(0));
1894 break;
1895 case Promote:
1896 if (isSigned) {
1897 Result = PromoteOp(Node->getOperand(0));
1898 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1899 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1900 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1901 } else {
1902 Result = PromoteOp(Node->getOperand(0));
1903 Result = DAG.getZeroExtendInReg(Result,
1904 Node->getOperand(0).getValueType());
1905 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001906 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001907 break;
1908 }
1909 break;
1910 }
1911 case ISD::TRUNCATE:
1912 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1913 case Legal:
1914 Tmp1 = LegalizeOp(Node->getOperand(0));
1915 if (Tmp1 != Node->getOperand(0))
1916 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1917 break;
1918 case Expand:
1919 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1920
1921 // Since the result is legal, we should just be able to truncate the low
1922 // part of the source.
1923 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1924 break;
1925 case Promote:
1926 Result = PromoteOp(Node->getOperand(0));
1927 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1928 break;
1929 }
1930 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001931
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001932 case ISD::FP_TO_SINT:
1933 case ISD::FP_TO_UINT:
1934 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1935 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001936 Tmp1 = LegalizeOp(Node->getOperand(0));
1937
Chris Lattner1618beb2005-07-29 00:11:56 +00001938 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1939 default: assert(0 && "Unknown operation action!");
1940 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001941 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1942 SDOperand True, False;
1943 MVT::ValueType VT = Node->getOperand(0).getValueType();
1944 MVT::ValueType NVT = Node->getValueType(0);
1945 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1946 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1947 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1948 Node->getOperand(0), Tmp2, ISD::SETLT);
1949 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1950 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00001951 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00001952 Tmp2));
1953 False = DAG.getNode(ISD::XOR, NVT, False,
1954 DAG.getConstant(1ULL << ShiftAmt, NVT));
1955 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001956 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001957 } else {
1958 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1959 }
1960 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001961 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001962 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001963 Node->getOpcode() == ISD::FP_TO_SINT);
1964 AddLegalizedOperand(Op, Result);
1965 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001966 case TargetLowering::Custom: {
1967 SDOperand Tmp =
1968 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1969 Tmp = TLI.LowerOperation(Tmp, DAG);
1970 if (Tmp.Val) {
1971 AddLegalizedOperand(Op, Tmp);
1972 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001973 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001974 } else {
1975 // The target thinks this is legal afterall.
1976 break;
1977 }
1978 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001979 case TargetLowering::Legal:
1980 break;
1981 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001982
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001983 if (Tmp1 != Node->getOperand(0))
1984 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1985 break;
1986 case Expand:
1987 assert(0 && "Shouldn't need to expand other operators here!");
1988 case Promote:
1989 Result = PromoteOp(Node->getOperand(0));
1990 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1991 break;
1992 }
1993 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001994
Chris Lattner13c78e22005-09-02 00:18:10 +00001995 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001996 case ISD::ZERO_EXTEND:
1997 case ISD::SIGN_EXTEND:
1998 case ISD::FP_EXTEND:
1999 case ISD::FP_ROUND:
2000 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2001 case Legal:
2002 Tmp1 = LegalizeOp(Node->getOperand(0));
2003 if (Tmp1 != Node->getOperand(0))
2004 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2005 break;
2006 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002007 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002008
Chris Lattner03c85462005-01-15 05:21:40 +00002009 case Promote:
2010 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002011 case ISD::ANY_EXTEND:
2012 Result = PromoteOp(Node->getOperand(0));
2013 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2014 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002015 case ISD::ZERO_EXTEND:
2016 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002017 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002018 Result = DAG.getZeroExtendInReg(Result,
2019 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002020 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002021 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002022 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002023 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002024 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002025 Result,
2026 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002027 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002028 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002029 Result = PromoteOp(Node->getOperand(0));
2030 if (Result.getValueType() != Op.getValueType())
2031 // Dynamically dead while we have only 2 FP types.
2032 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2033 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002034 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002035 Result = PromoteOp(Node->getOperand(0));
2036 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2037 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002038 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002039 }
2040 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002041 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002042 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002043 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002044 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002045
2046 // If this operation is not supported, convert it to a shl/shr or load/store
2047 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002048 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2049 default: assert(0 && "This action not supported for this op yet!");
2050 case TargetLowering::Legal:
2051 if (Tmp1 != Node->getOperand(0))
2052 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002053 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002054 break;
2055 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002056 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002057 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002058 // NOTE: we could fall back on load/store here too for targets without
2059 // SAR. However, it is doubtful that any exist.
2060 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2061 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002062 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002063 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2064 Node->getOperand(0), ShiftCst);
2065 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2066 Result, ShiftCst);
2067 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2068 // The only way we can lower this is to turn it into a STORETRUNC,
2069 // EXTLOAD pair, targetting a temporary location (a stack slot).
2070
2071 // NOTE: there is a choice here between constantly creating new stack
2072 // slots and always reusing the same one. We currently always create
2073 // new ones, as reuse may inhibit scheduling.
2074 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2075 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2076 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2077 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002078 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002079 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2080 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2081 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002082 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002083 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002084 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2085 Result, StackSlot, DAG.getSrcValue(NULL),
2086 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002087 } else {
2088 assert(0 && "Unknown op");
2089 }
2090 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002091 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002092 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002093 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002094 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002095 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002096
Chris Lattner45982da2005-05-12 16:53:42 +00002097 // Note that LegalizeOp may be reentered even from single-use nodes, which
2098 // means that we always must cache transformed nodes.
2099 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002100 return Result;
2101}
2102
Chris Lattner8b6fa222005-01-15 22:16:26 +00002103/// PromoteOp - Given an operation that produces a value in an invalid type,
2104/// promote it to compute the value into a larger type. The produced value will
2105/// have the correct bits for the low portion of the register, but no guarantee
2106/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002107SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2108 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002109 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002110 assert(getTypeAction(VT) == Promote &&
2111 "Caller should expand or legalize operands that are not promotable!");
2112 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2113 "Cannot promote to smaller type!");
2114
Chris Lattner03c85462005-01-15 05:21:40 +00002115 SDOperand Tmp1, Tmp2, Tmp3;
2116
2117 SDOperand Result;
2118 SDNode *Node = Op.Val;
2119
Chris Lattner6fdcb252005-09-02 20:32:45 +00002120 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2121 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002122
Chris Lattner0f69b292005-01-15 06:18:18 +00002123 // Promotion needs an optimization step to clean up after it, and is not
2124 // careful to avoid operations the target does not support. Make sure that
2125 // all generated operations are legalized in the next iteration.
2126 NeedsAnotherIteration = true;
2127
Chris Lattner03c85462005-01-15 05:21:40 +00002128 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002129 case ISD::CopyFromReg:
2130 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002131 default:
2132 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2133 assert(0 && "Do not know how to promote this operator!");
2134 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002135 case ISD::UNDEF:
2136 Result = DAG.getNode(ISD::UNDEF, NVT);
2137 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002138 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002139 if (VT != MVT::i1)
2140 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2141 else
2142 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002143 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2144 break;
2145 case ISD::ConstantFP:
2146 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2147 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2148 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002149
Chris Lattner82fbfb62005-01-18 02:59:52 +00002150 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002151 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002152 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2153 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002154 Result = LegalizeOp(Result);
2155 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002156
2157 case ISD::TRUNCATE:
2158 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2159 case Legal:
2160 Result = LegalizeOp(Node->getOperand(0));
2161 assert(Result.getValueType() >= NVT &&
2162 "This truncation doesn't make sense!");
2163 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2164 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2165 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002166 case Promote:
2167 // The truncation is not required, because we don't guarantee anything
2168 // about high bits anyway.
2169 Result = PromoteOp(Node->getOperand(0));
2170 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002171 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002172 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2173 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002174 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002175 }
2176 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002177 case ISD::SIGN_EXTEND:
2178 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002179 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002180 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2181 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2182 case Legal:
2183 // Input is legal? Just do extend all the way to the larger type.
2184 Result = LegalizeOp(Node->getOperand(0));
2185 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2186 break;
2187 case Promote:
2188 // Promote the reg if it's smaller.
2189 Result = PromoteOp(Node->getOperand(0));
2190 // The high bits are not guaranteed to be anything. Insert an extend.
2191 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002192 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002193 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002194 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002195 Result = DAG.getZeroExtendInReg(Result,
2196 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002197 break;
2198 }
2199 break;
2200
2201 case ISD::FP_EXTEND:
2202 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2203 case ISD::FP_ROUND:
2204 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2205 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2206 case Promote: assert(0 && "Unreachable with 2 FP types!");
2207 case Legal:
2208 // Input is legal? Do an FP_ROUND_INREG.
2209 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002210 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2211 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002212 break;
2213 }
2214 break;
2215
2216 case ISD::SINT_TO_FP:
2217 case ISD::UINT_TO_FP:
2218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2219 case Legal:
2220 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002221 // No extra round required here.
2222 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002223 break;
2224
2225 case Promote:
2226 Result = PromoteOp(Node->getOperand(0));
2227 if (Node->getOpcode() == ISD::SINT_TO_FP)
2228 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002229 Result,
2230 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002231 else
Chris Lattner23993562005-04-13 02:38:47 +00002232 Result = DAG.getZeroExtendInReg(Result,
2233 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002234 // No extra round required here.
2235 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002236 break;
2237 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002238 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2239 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002240 // Round if we cannot tolerate excess precision.
2241 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002242 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2243 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002244 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002245 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002246 break;
2247
2248 case ISD::FP_TO_SINT:
2249 case ISD::FP_TO_UINT:
2250 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2251 case Legal:
2252 Tmp1 = LegalizeOp(Node->getOperand(0));
2253 break;
2254 case Promote:
2255 // The input result is prerounded, so we don't have to do anything
2256 // special.
2257 Tmp1 = PromoteOp(Node->getOperand(0));
2258 break;
2259 case Expand:
2260 assert(0 && "not implemented");
2261 }
Nate Begemand2558e32005-08-14 01:20:53 +00002262 // If we're promoting a UINT to a larger size, check to see if the new node
2263 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2264 // we can use that instead. This allows us to generate better code for
2265 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2266 // legal, such as PowerPC.
2267 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002268 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002269 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2270 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002271 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2272 } else {
2273 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2274 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002275 break;
2276
Chris Lattner2c8086f2005-04-02 05:00:07 +00002277 case ISD::FABS:
2278 case ISD::FNEG:
2279 Tmp1 = PromoteOp(Node->getOperand(0));
2280 assert(Tmp1.getValueType() == NVT);
2281 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2282 // NOTE: we do not have to do any extra rounding here for
2283 // NoExcessFPPrecision, because we know the input will have the appropriate
2284 // precision, and these operations don't modify precision at all.
2285 break;
2286
Chris Lattnerda6ba872005-04-28 21:44:33 +00002287 case ISD::FSQRT:
2288 case ISD::FSIN:
2289 case ISD::FCOS:
2290 Tmp1 = PromoteOp(Node->getOperand(0));
2291 assert(Tmp1.getValueType() == NVT);
2292 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2293 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002294 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2295 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002296 break;
2297
Chris Lattner03c85462005-01-15 05:21:40 +00002298 case ISD::AND:
2299 case ISD::OR:
2300 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002301 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002302 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002303 case ISD::MUL:
2304 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002305 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002306 // that too is okay if they are integer operations.
2307 Tmp1 = PromoteOp(Node->getOperand(0));
2308 Tmp2 = PromoteOp(Node->getOperand(1));
2309 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2310 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002311 break;
2312 case ISD::FADD:
2313 case ISD::FSUB:
2314 case ISD::FMUL:
2315 // The input may have strange things in the top bits of the registers, but
2316 // these operations don't care.
2317 Tmp1 = PromoteOp(Node->getOperand(0));
2318 Tmp2 = PromoteOp(Node->getOperand(1));
2319 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2320 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2321
2322 // Floating point operations will give excess precision that we may not be
2323 // able to tolerate. If we DO allow excess precision, just leave it,
2324 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002325 // FIXME: Why would we need to round FP ops more than integer ones?
2326 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002327 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002328 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2329 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002330 break;
2331
Chris Lattner8b6fa222005-01-15 22:16:26 +00002332 case ISD::SDIV:
2333 case ISD::SREM:
2334 // These operators require that their input be sign extended.
2335 Tmp1 = PromoteOp(Node->getOperand(0));
2336 Tmp2 = PromoteOp(Node->getOperand(1));
2337 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002338 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2339 DAG.getValueType(VT));
2340 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2341 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002342 }
2343 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2344
2345 // Perform FP_ROUND: this is probably overly pessimistic.
2346 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002347 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2348 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002349 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002350 case ISD::FDIV:
2351 case ISD::FREM:
2352 // These operators require that their input be fp extended.
2353 Tmp1 = PromoteOp(Node->getOperand(0));
2354 Tmp2 = PromoteOp(Node->getOperand(1));
2355 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2356
2357 // Perform FP_ROUND: this is probably overly pessimistic.
2358 if (NoExcessFPPrecision)
2359 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2360 DAG.getValueType(VT));
2361 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002362
2363 case ISD::UDIV:
2364 case ISD::UREM:
2365 // These operators require that their input be zero extended.
2366 Tmp1 = PromoteOp(Node->getOperand(0));
2367 Tmp2 = PromoteOp(Node->getOperand(1));
2368 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002369 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2370 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002371 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2372 break;
2373
2374 case ISD::SHL:
2375 Tmp1 = PromoteOp(Node->getOperand(0));
2376 Tmp2 = LegalizeOp(Node->getOperand(1));
2377 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2378 break;
2379 case ISD::SRA:
2380 // The input value must be properly sign extended.
2381 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002382 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2383 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002384 Tmp2 = LegalizeOp(Node->getOperand(1));
2385 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2386 break;
2387 case ISD::SRL:
2388 // The input value must be properly zero extended.
2389 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002390 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002391 Tmp2 = LegalizeOp(Node->getOperand(1));
2392 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2393 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002394 case ISD::LOAD:
2395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2396 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00002397 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2398 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002399 // Remember that we legalized the chain.
2400 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2401 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002402 case ISD::SEXTLOAD:
2403 case ISD::ZEXTLOAD:
2404 case ISD::EXTLOAD:
2405 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2406 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00002407 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2408 Node->getOperand(2),
2409 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002410 // Remember that we legalized the chain.
2411 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2412 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002413 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002414 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2415 case Expand: assert(0 && "It's impossible to expand bools");
2416 case Legal:
2417 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2418 break;
2419 case Promote:
2420 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2421 break;
2422 }
Chris Lattner03c85462005-01-15 05:21:40 +00002423 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2424 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2425 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2426 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002427 case ISD::SELECT_CC:
2428 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2429 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2430 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2431 Node->getOperand(1), Tmp2, Tmp3,
2432 Node->getOperand(4));
2433 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002434 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002435 case ISD::CALL: {
2436 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2437 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2438
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002439 std::vector<SDOperand> Ops;
2440 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2441 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2442
Chris Lattner8ac532c2005-01-16 19:46:48 +00002443 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2444 "Can only promote single result calls");
2445 std::vector<MVT::ValueType> RetTyVTs;
2446 RetTyVTs.reserve(2);
2447 RetTyVTs.push_back(NVT);
2448 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002449 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2450 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002451 Result = SDOperand(NC, 0);
2452
2453 // Insert the new chain mapping.
2454 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2455 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002456 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002457 case ISD::CTPOP:
2458 case ISD::CTTZ:
2459 case ISD::CTLZ:
2460 Tmp1 = Node->getOperand(0);
2461 //Zero extend the argument
2462 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2463 // Perform the larger operation, then subtract if needed.
2464 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2465 switch(Node->getOpcode())
2466 {
2467 case ISD::CTPOP:
2468 Result = Tmp1;
2469 break;
2470 case ISD::CTTZ:
2471 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002472 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002473 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002474 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002475 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2476 break;
2477 case ISD::CTLZ:
2478 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002479 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2480 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002481 getSizeInBits(VT), NVT));
2482 break;
2483 }
2484 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002485 }
2486
2487 assert(Result.Val && "Didn't set a result!");
2488 AddPromotedOperand(Op, Result);
2489 return Result;
2490}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002491
Chris Lattner84f67882005-01-20 18:52:28 +00002492/// ExpandAddSub - Find a clever way to expand this add operation into
2493/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002494void SelectionDAGLegalize::
2495ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2496 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002497 // Expand the subcomponents.
2498 SDOperand LHSL, LHSH, RHSL, RHSH;
2499 ExpandOp(LHS, LHSL, LHSH);
2500 ExpandOp(RHS, RHSL, RHSH);
2501
Chris Lattner84f67882005-01-20 18:52:28 +00002502 std::vector<SDOperand> Ops;
2503 Ops.push_back(LHSL);
2504 Ops.push_back(LHSH);
2505 Ops.push_back(RHSL);
2506 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002507 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2508 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002509 Hi = Lo.getValue(1);
2510}
2511
Chris Lattner5b359c62005-04-02 04:00:59 +00002512void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2513 SDOperand Op, SDOperand Amt,
2514 SDOperand &Lo, SDOperand &Hi) {
2515 // Expand the subcomponents.
2516 SDOperand LHSL, LHSH;
2517 ExpandOp(Op, LHSL, LHSH);
2518
2519 std::vector<SDOperand> Ops;
2520 Ops.push_back(LHSL);
2521 Ops.push_back(LHSH);
2522 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002523 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002524 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002525 Hi = Lo.getValue(1);
2526}
2527
2528
Chris Lattnere34b3962005-01-19 04:19:40 +00002529/// ExpandShift - Try to find a clever way to expand this shift operation out to
2530/// smaller elements. If we can't find a way that is more efficient than a
2531/// libcall on this target, return false. Otherwise, return true with the
2532/// low-parts expanded into Lo and Hi.
2533bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2534 SDOperand &Lo, SDOperand &Hi) {
2535 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2536 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002537
Chris Lattnere34b3962005-01-19 04:19:40 +00002538 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002539 SDOperand ShAmt = LegalizeOp(Amt);
2540 MVT::ValueType ShTy = ShAmt.getValueType();
2541 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2542 unsigned NVTBits = MVT::getSizeInBits(NVT);
2543
2544 // Handle the case when Amt is an immediate. Other cases are currently broken
2545 // and are disabled.
2546 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2547 unsigned Cst = CN->getValue();
2548 // Expand the incoming operand to be shifted, so that we have its parts
2549 SDOperand InL, InH;
2550 ExpandOp(Op, InL, InH);
2551 switch(Opc) {
2552 case ISD::SHL:
2553 if (Cst > VTBits) {
2554 Lo = DAG.getConstant(0, NVT);
2555 Hi = DAG.getConstant(0, NVT);
2556 } else if (Cst > NVTBits) {
2557 Lo = DAG.getConstant(0, NVT);
2558 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002559 } else if (Cst == NVTBits) {
2560 Lo = DAG.getConstant(0, NVT);
2561 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002562 } else {
2563 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2564 Hi = DAG.getNode(ISD::OR, NVT,
2565 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2566 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2567 }
2568 return true;
2569 case ISD::SRL:
2570 if (Cst > VTBits) {
2571 Lo = DAG.getConstant(0, NVT);
2572 Hi = DAG.getConstant(0, NVT);
2573 } else if (Cst > NVTBits) {
2574 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2575 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002576 } else if (Cst == NVTBits) {
2577 Lo = InH;
2578 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002579 } else {
2580 Lo = DAG.getNode(ISD::OR, NVT,
2581 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2582 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2583 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2584 }
2585 return true;
2586 case ISD::SRA:
2587 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002588 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002589 DAG.getConstant(NVTBits-1, ShTy));
2590 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002591 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002592 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002593 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002594 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002595 } else if (Cst == NVTBits) {
2596 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002597 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002598 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002599 } else {
2600 Lo = DAG.getNode(ISD::OR, NVT,
2601 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2602 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2603 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2604 }
2605 return true;
2606 }
2607 }
2608 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2609 // so disable it for now. Currently targets are handling this via SHL_PARTS
2610 // and friends.
2611 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002612
2613 // If we have an efficient select operation (or if the selects will all fold
2614 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002615 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002616 return false;
2617
2618 SDOperand InL, InH;
2619 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002620 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2621 DAG.getConstant(NVTBits, ShTy), ShAmt);
2622
Chris Lattnere5544f82005-01-20 20:29:23 +00002623 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002624 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2625 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002626
Chris Lattnere34b3962005-01-19 04:19:40 +00002627 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2628 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2629 DAG.getConstant(NVTBits-1, ShTy));
2630 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2631 DAG.getConstant(NVTBits-1, ShTy));
2632 }
2633
2634 if (Opc == ISD::SHL) {
2635 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2636 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2637 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002638 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002639
Chris Lattnere34b3962005-01-19 04:19:40 +00002640 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2641 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2642 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002643 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002644 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2645 DAG.getConstant(32, ShTy),
2646 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002647 DAG.getConstant(0, NVT),
2648 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002649 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002650 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002651 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002652 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002653
2654 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002655 if (Opc == ISD::SRA)
2656 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2657 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002658 else
2659 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002660 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002661 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002662 }
2663 return true;
2664}
Chris Lattner77e77a62005-01-21 06:05:23 +00002665
Chris Lattner9530ddc2005-05-13 05:09:11 +00002666/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2667/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002668/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002669static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002670 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002671
Chris Lattner16cd04d2005-05-12 23:24:06 +00002672 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002673 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002674 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002675 Found = Node;
2676 return;
2677 }
2678
2679 // Otherwise, scan the operands of Node to see if any of them is a call.
2680 assert(Node->getNumOperands() != 0 &&
2681 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002682 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002683 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002684
2685 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002686 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002687 Found);
2688}
2689
2690
Chris Lattner9530ddc2005-05-13 05:09:11 +00002691/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2692/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002693/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002694static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2695 std::set<SDNode*> &Visited) {
2696 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2697 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002698
Chris Lattner16cd04d2005-05-12 23:24:06 +00002699 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002700 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002701 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002702 Found = Node;
2703 return;
2704 }
2705
2706 // Otherwise, scan the operands of Node to see if any of them is a call.
2707 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2708 if (UI == E) return;
2709 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002710 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002711
2712 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002713 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002714}
2715
Chris Lattner9530ddc2005-05-13 05:09:11 +00002716/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002717/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002718static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002719 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002720 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002721 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002722 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002723
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002724 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002725 if (TheChain.getValueType() != MVT::Other)
2726 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00002727 if (TheChain.getValueType() != MVT::Other)
2728 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002729
2730 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002731 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002732
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002733 // Make sure to only follow users of our token chain.
2734 SDNode *User = *UI;
2735 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2736 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002737 if (SDNode *Result = FindCallSeqEnd(User))
2738 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002739 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002740 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002741}
2742
Chris Lattner9530ddc2005-05-13 05:09:11 +00002743/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002744/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002745static SDNode *FindCallSeqStart(SDNode *Node) {
2746 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002747 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002748
2749 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2750 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002751 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002752}
2753
2754
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002755/// FindInputOutputChains - If we are replacing an operation with a call we need
2756/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002757/// properly serialize the calls in the block. The returned operand is the
2758/// input chain value for the new call (e.g. the entry node or the previous
2759/// call), and OutChain is set to be the chain node to update to point to the
2760/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002761static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2762 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002763 SDNode *LatestCallSeqStart = Entry.Val;
2764 SDNode *LatestCallSeqEnd = 0;
2765 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2766 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002767
Chris Lattner16cd04d2005-05-12 23:24:06 +00002768 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002769 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002770 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2771 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00002772 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002773 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00002774 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2775 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002776 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00002777 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00002778 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002779
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002780 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002781 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002782 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002783 std::set<SDNode*> Visited;
2784 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002785
Chris Lattner9530ddc2005-05-13 05:09:11 +00002786 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002787 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002788 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002789
Chris Lattner9530ddc2005-05-13 05:09:11 +00002790 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002791}
2792
Jeff Cohen00b168892005-07-27 06:12:32 +00002793/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002794void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2795 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002796 // Nothing to splice it into?
2797 if (OutChain == 0) return;
2798
2799 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2800 //OutChain->dump();
2801
2802 // Form a token factor node merging the old inval and the new inval.
2803 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2804 OutChain->getOperand(0));
2805 // Change the node to refer to the new token.
2806 OutChain->setAdjCallChain(InToken);
2807}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002808
2809
Chris Lattner77e77a62005-01-21 06:05:23 +00002810// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2811// does not fit into a register, return the lo part and set the hi part to the
2812// by-reg argument. If it does fit into a single register, return the result
2813// and leave the Hi part unset.
2814SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2815 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002816 SDNode *OutChain;
2817 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2818 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002819 if (InChain.Val == 0)
2820 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002821
Chris Lattner77e77a62005-01-21 06:05:23 +00002822 TargetLowering::ArgListTy Args;
2823 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2824 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2825 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2826 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2827 }
2828 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002829
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002830 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002831 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002832 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002833 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2834 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002835
Chris Lattner99c25b82005-09-02 20:26:58 +00002836 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002837 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002838 default: assert(0 && "Unknown thing");
2839 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002840 Result = CallInfo.first;
2841 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002842 case Promote:
2843 assert(0 && "Cannot promote this yet!");
2844 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002845 ExpandOp(CallInfo.first, Result, Hi);
2846 CallInfo.second = LegalizeOp(CallInfo.second);
2847 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002848 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002849
2850 SpliceCallInto(CallInfo.second, OutChain);
2851 NeedsAnotherIteration = true;
2852 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002853}
2854
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002855
Chris Lattner77e77a62005-01-21 06:05:23 +00002856/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2857/// destination type is legal.
2858SDOperand SelectionDAGLegalize::
2859ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002860 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002861 assert(getTypeAction(Source.getValueType()) == Expand &&
2862 "This is not an expansion!");
2863 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2864
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002865 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002866 assert(Source.getValueType() == MVT::i64 &&
2867 "This only works for 64-bit -> FP");
2868 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2869 // incoming integer is set. To handle this, we dynamically test to see if
2870 // it is set, and, if so, add a fudge factor.
2871 SDOperand Lo, Hi;
2872 ExpandOp(Source, Lo, Hi);
2873
Chris Lattner66de05b2005-05-13 04:45:13 +00002874 // If this is unsigned, and not supported, first perform the conversion to
2875 // signed, then adjust the result if the sign bit is set.
2876 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2877 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2878
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002879 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2880 DAG.getConstant(0, Hi.getValueType()),
2881 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002882 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2883 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2884 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002885 uint64_t FF = 0x5f800000ULL;
2886 if (TLI.isLittleEndian()) FF <<= 32;
2887 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002888
Chris Lattner5839bf22005-08-26 17:15:30 +00002889 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002890 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2891 SDOperand FudgeInReg;
2892 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002893 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2894 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002895 else {
2896 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002897 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2898 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002899 }
Chris Lattner473a9902005-09-29 06:44:39 +00002900 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002901 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002902
Chris Lattnera88a2602005-05-14 05:33:54 +00002903 // Check to see if the target has a custom way to lower this. If so, use it.
2904 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2905 default: assert(0 && "This action not implemented for this operation!");
2906 case TargetLowering::Legal:
2907 case TargetLowering::Expand:
2908 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002909 case TargetLowering::Custom: {
2910 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2911 Source), DAG);
2912 if (NV.Val)
2913 return LegalizeOp(NV);
2914 break; // The target decided this was legal after all
2915 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002916 }
2917
Chris Lattner13689e22005-05-12 07:00:44 +00002918 // Expand the source, then glue it back together for the call. We must expand
2919 // the source in case it is shared (this pass of legalize must traverse it).
2920 SDOperand SrcLo, SrcHi;
2921 ExpandOp(Source, SrcLo, SrcHi);
2922 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2923
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002924 SDNode *OutChain = 0;
2925 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2926 DAG.getEntryNode());
2927 const char *FnName = 0;
2928 if (DestTy == MVT::f32)
2929 FnName = "__floatdisf";
2930 else {
2931 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2932 FnName = "__floatdidf";
2933 }
2934
Chris Lattner77e77a62005-01-21 06:05:23 +00002935 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2936
2937 TargetLowering::ArgListTy Args;
2938 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002939
Chris Lattner77e77a62005-01-21 06:05:23 +00002940 Args.push_back(std::make_pair(Source, ArgTy));
2941
2942 // We don't care about token chains for libcalls. We just use the entry
2943 // node as our input and ignore the output chain. This allows us to place
2944 // calls wherever we need them to satisfy data dependences.
2945 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002946
2947 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002948 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2949 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002950
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002951 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002952 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002953}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002954
Chris Lattnere34b3962005-01-19 04:19:40 +00002955
2956
Chris Lattner3e928bb2005-01-07 07:47:09 +00002957/// ExpandOp - Expand the specified SDOperand into its two component pieces
2958/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2959/// LegalizeNodes map is filled in for any results that are not expanded, the
2960/// ExpandedNodes map is filled in for any results that are expanded, and the
2961/// Lo/Hi values are returned.
2962void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2963 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002964 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002965 SDNode *Node = Op.Val;
2966 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2967 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2968 assert(MVT::isInteger(NVT) && NVT < VT &&
2969 "Cannot expand to FP value or to larger int value!");
2970
Chris Lattner6fdcb252005-09-02 20:32:45 +00002971 // See if we already expanded it.
2972 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2973 = ExpandedNodes.find(Op);
2974 if (I != ExpandedNodes.end()) {
2975 Lo = I->second.first;
2976 Hi = I->second.second;
2977 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002978 }
2979
Chris Lattner4e6c7462005-01-08 19:27:05 +00002980 // Expanding to multiple registers needs to perform an optimization step, and
2981 // is not careful to avoid operations the target does not support. Make sure
2982 // that all generated operations are legalized in the next iteration.
2983 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002984
Chris Lattner3e928bb2005-01-07 07:47:09 +00002985 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002986 case ISD::CopyFromReg:
2987 assert(0 && "CopyFromReg must be legal!");
2988 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002989 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2990 assert(0 && "Do not know how to expand this operator!");
2991 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002992 case ISD::UNDEF:
2993 Lo = DAG.getNode(ISD::UNDEF, NVT);
2994 Hi = DAG.getNode(ISD::UNDEF, NVT);
2995 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002996 case ISD::Constant: {
2997 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2998 Lo = DAG.getConstant(Cst, NVT);
2999 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3000 break;
3001 }
3002
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003003 case ISD::BUILD_PAIR:
3004 // Legalize both operands. FIXME: in the future we should handle the case
3005 // where the two elements are not legal.
3006 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3007 Lo = LegalizeOp(Node->getOperand(0));
3008 Hi = LegalizeOp(Node->getOperand(1));
3009 break;
3010
Chris Lattneredb1add2005-05-11 04:51:16 +00003011 case ISD::CTPOP:
3012 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003013 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3014 DAG.getNode(ISD::CTPOP, NVT, Lo),
3015 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003016 Hi = DAG.getConstant(0, NVT);
3017 break;
3018
Chris Lattner39a8f332005-05-12 19:05:01 +00003019 case ISD::CTLZ: {
3020 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003021 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003022 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3023 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003024 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3025 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003026 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3027 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3028
3029 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3030 Hi = DAG.getConstant(0, NVT);
3031 break;
3032 }
3033
3034 case ISD::CTTZ: {
3035 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003036 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003037 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3038 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003039 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3040 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003041 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3042 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3043
3044 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3045 Hi = DAG.getConstant(0, NVT);
3046 break;
3047 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003048
Chris Lattner3e928bb2005-01-07 07:47:09 +00003049 case ISD::LOAD: {
3050 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3051 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003052 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003053
3054 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003055 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003056 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3057 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003058 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003059 //are from the same instruction?
3060 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003061
3062 // Build a factor node to remember that this load is independent of the
3063 // other one.
3064 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3065 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003066
Chris Lattner3e928bb2005-01-07 07:47:09 +00003067 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003068 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003069 if (!TLI.isLittleEndian())
3070 std::swap(Lo, Hi);
3071 break;
3072 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003073 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003074 case ISD::CALL: {
3075 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3076 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3077
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003078 bool Changed = false;
3079 std::vector<SDOperand> Ops;
3080 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3081 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3082 Changed |= Ops.back() != Node->getOperand(i);
3083 }
3084
Chris Lattner3e928bb2005-01-07 07:47:09 +00003085 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3086 "Can only expand a call once so far, not i64 -> i16!");
3087
3088 std::vector<MVT::ValueType> RetTyVTs;
3089 RetTyVTs.reserve(3);
3090 RetTyVTs.push_back(NVT);
3091 RetTyVTs.push_back(NVT);
3092 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003093 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3094 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003095 Lo = SDOperand(NC, 0);
3096 Hi = SDOperand(NC, 1);
3097
3098 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003099 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003100 break;
3101 }
3102 case ISD::AND:
3103 case ISD::OR:
3104 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3105 SDOperand LL, LH, RL, RH;
3106 ExpandOp(Node->getOperand(0), LL, LH);
3107 ExpandOp(Node->getOperand(1), RL, RH);
3108 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3109 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3110 break;
3111 }
3112 case ISD::SELECT: {
3113 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003114
3115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3116 case Expand: assert(0 && "It's impossible to expand bools");
3117 case Legal:
3118 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3119 break;
3120 case Promote:
3121 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3122 break;
3123 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003124 ExpandOp(Node->getOperand(1), LL, LH);
3125 ExpandOp(Node->getOperand(2), RL, RH);
3126 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3127 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3128 break;
3129 }
Nate Begeman9373a812005-08-10 20:51:12 +00003130 case ISD::SELECT_CC: {
3131 SDOperand TL, TH, FL, FH;
3132 ExpandOp(Node->getOperand(2), TL, TH);
3133 ExpandOp(Node->getOperand(3), FL, FH);
3134 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3135 Node->getOperand(1), TL, FL, Node->getOperand(4));
3136 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3137 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003138 Lo = LegalizeOp(Lo);
3139 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003140 break;
3141 }
Nate Begeman144ff662005-10-13 17:15:37 +00003142 case ISD::SEXTLOAD: {
3143 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3144 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3145 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3146
3147 if (EVT == NVT)
3148 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3149 else
3150 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3151 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003152
3153 // Remember that we legalized the chain.
3154 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3155
Nate Begeman144ff662005-10-13 17:15:37 +00003156 // The high part is obtained by SRA'ing all but one of the bits of the lo
3157 // part.
3158 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3159 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3160 TLI.getShiftAmountTy()));
3161 Lo = LegalizeOp(Lo);
3162 Hi = LegalizeOp(Hi);
3163 break;
3164 }
3165 case ISD::ZEXTLOAD: {
3166 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3167 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3168 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3169
3170 if (EVT == NVT)
3171 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3172 else
3173 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3174 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003175
3176 // Remember that we legalized the chain.
3177 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3178
Nate Begeman144ff662005-10-13 17:15:37 +00003179 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003180 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003181 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003182 break;
3183 }
3184 case ISD::EXTLOAD: {
3185 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3186 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3187 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3188
3189 if (EVT == NVT)
3190 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3191 else
3192 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3193 EVT);
3194
3195 // Remember that we legalized the chain.
3196 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3197
3198 // The high part is undefined.
3199 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3200 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003201 break;
3202 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003203 case ISD::ANY_EXTEND: {
3204 SDOperand In;
3205 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3206 case Expand: assert(0 && "expand-expand not implemented yet!");
3207 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3208 case Promote:
3209 In = PromoteOp(Node->getOperand(0));
3210 break;
3211 }
3212
3213 // The low part is any extension of the input (which degenerates to a copy).
3214 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3215 // The high part is undefined.
3216 Hi = DAG.getNode(ISD::UNDEF, NVT);
3217 break;
3218 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003219 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003220 SDOperand In;
3221 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3222 case Expand: assert(0 && "expand-expand not implemented yet!");
3223 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3224 case Promote:
3225 In = PromoteOp(Node->getOperand(0));
3226 // Emit the appropriate sign_extend_inreg to get the value we want.
3227 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003228 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003229 break;
3230 }
3231
Chris Lattner3e928bb2005-01-07 07:47:09 +00003232 // The low part is just a sign extension of the input (which degenerates to
3233 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003234 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003235
Chris Lattner3e928bb2005-01-07 07:47:09 +00003236 // The high part is obtained by SRA'ing all but one of the bits of the lo
3237 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003238 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003239 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3240 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003241 break;
3242 }
Chris Lattner06098e02005-04-03 23:41:52 +00003243 case ISD::ZERO_EXTEND: {
3244 SDOperand In;
3245 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3246 case Expand: assert(0 && "expand-expand not implemented yet!");
3247 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3248 case Promote:
3249 In = PromoteOp(Node->getOperand(0));
3250 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003251 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003252 break;
3253 }
3254
Chris Lattner3e928bb2005-01-07 07:47:09 +00003255 // The low part is just a zero extension of the input (which degenerates to
3256 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003257 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003258
Chris Lattner3e928bb2005-01-07 07:47:09 +00003259 // The high part is just a zero.
3260 Hi = DAG.getConstant(0, NVT);
3261 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003262 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003263 // These operators cannot be expanded directly, emit them as calls to
3264 // library functions.
3265 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003266 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003267 SDOperand Op;
3268 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3269 case Expand: assert(0 && "cannot expand FP!");
3270 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3271 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3272 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003273
Chris Lattnerf20d1832005-07-30 01:40:57 +00003274 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3275
Chris Lattner80a3e942005-07-29 00:33:32 +00003276 // Now that the custom expander is done, expand the result, which is still
3277 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003278 if (Op.Val) {
3279 ExpandOp(Op, Lo, Hi);
3280 break;
3281 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003282 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003283
Chris Lattner4e6c7462005-01-08 19:27:05 +00003284 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003285 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003286 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003287 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003288 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003289
Chris Lattner4e6c7462005-01-08 19:27:05 +00003290 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003291 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3292 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3293 LegalizeOp(Node->getOperand(0)));
3294 // Now that the custom expander is done, expand the result, which is still
3295 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003296 Op = TLI.LowerOperation(Op, DAG);
3297 if (Op.Val) {
3298 ExpandOp(Op, Lo, Hi);
3299 break;
3300 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003301 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003302
Chris Lattner4e6c7462005-01-08 19:27:05 +00003303 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003304 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003305 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003306 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003307 break;
3308
Chris Lattnere34b3962005-01-19 04:19:40 +00003309 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003310 // If the target wants custom lowering, do so.
3311 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3312 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3313 LegalizeOp(Node->getOperand(1)));
3314 Op = TLI.LowerOperation(Op, DAG);
3315 if (Op.Val) {
3316 // Now that the custom expander is done, expand the result, which is
3317 // still VT.
3318 ExpandOp(Op, Lo, Hi);
3319 break;
3320 }
3321 }
3322
Chris Lattnere34b3962005-01-19 04:19:40 +00003323 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003324 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003325 break;
Chris Lattner47599822005-04-02 03:38:53 +00003326
3327 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003328 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003329 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3330 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003331 break;
3332 }
3333
Chris Lattnere34b3962005-01-19 04:19:40 +00003334 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003335 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003336 break;
3337
3338 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003339 // If the target wants custom lowering, do so.
3340 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3341 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3342 LegalizeOp(Node->getOperand(1)));
3343 Op = TLI.LowerOperation(Op, DAG);
3344 if (Op.Val) {
3345 // Now that the custom expander is done, expand the result, which is
3346 // still VT.
3347 ExpandOp(Op, Lo, Hi);
3348 break;
3349 }
3350 }
3351
Chris Lattnere34b3962005-01-19 04:19:40 +00003352 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003353 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003354 break;
Chris Lattner47599822005-04-02 03:38:53 +00003355
3356 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003357 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003358 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3359 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003360 break;
3361 }
3362
Chris Lattnere34b3962005-01-19 04:19:40 +00003363 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003364 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003365 break;
3366 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003367 // If the target wants custom lowering, do so.
3368 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3369 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3370 LegalizeOp(Node->getOperand(1)));
3371 Op = TLI.LowerOperation(Op, DAG);
3372 if (Op.Val) {
3373 // Now that the custom expander is done, expand the result, which is
3374 // still VT.
3375 ExpandOp(Op, Lo, Hi);
3376 break;
3377 }
3378 }
3379
Chris Lattnere34b3962005-01-19 04:19:40 +00003380 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003381 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003382 break;
Chris Lattner47599822005-04-02 03:38:53 +00003383
3384 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003385 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003386 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3387 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003388 break;
3389 }
3390
Chris Lattnere34b3962005-01-19 04:19:40 +00003391 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003392 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003393 break;
3394
Misha Brukmanedf128a2005-04-21 22:36:52 +00003395 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003396 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3397 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003398 break;
3399 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003400 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3401 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003402 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003403 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003404 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003405 SDOperand LL, LH, RL, RH;
3406 ExpandOp(Node->getOperand(0), LL, LH);
3407 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003408 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3409 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3410 // extended the sign bit of the low half through the upper half, and if so
3411 // emit a MULHS instead of the alternate sequence that is valid for any
3412 // i64 x i64 multiply.
3413 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3414 // is RH an extension of the sign bit of RL?
3415 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3416 RH.getOperand(1).getOpcode() == ISD::Constant &&
3417 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3418 // is LH an extension of the sign bit of LL?
3419 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3420 LH.getOperand(1).getOpcode() == ISD::Constant &&
3421 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3422 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3423 } else {
3424 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3425 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3426 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3427 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3428 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3429 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003430 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3431 } else {
3432 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3433 }
3434 break;
3435 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003436 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3437 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3438 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3439 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003440 }
3441
3442 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003443 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3444 std::make_pair(Lo, Hi))).second;
3445 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003446}
3447
3448
3449// SelectionDAG::Legalize - This is the entry point for the file.
3450//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003451void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003452 /// run - This is the main entry point to this class.
3453 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003454 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003455}
3456