blob: db3b33abc51ebd8d2903de6daf287d664126fb90 [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
212 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
213 // 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;
264 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
265}
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
381
Chris Lattner3e928bb2005-01-07 07:47:09 +0000382void SelectionDAGLegalize::LegalizeDAG() {
383 SDOperand OldRoot = DAG.getRoot();
384 SDOperand NewRoot = LegalizeOp(OldRoot);
385 DAG.setRoot(NewRoot);
386
387 ExpandedNodes.clear();
388 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000389 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000390
391 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000392 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000393}
394
395SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000396 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000397 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000398 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000399
Chris Lattner3e928bb2005-01-07 07:47:09 +0000400 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000401 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000402 if (Node->getNumValues() > 1) {
403 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
404 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000405 case Legal: break; // Nothing to do.
406 case Expand: {
407 SDOperand T1, T2;
408 ExpandOp(Op.getValue(i), T1, T2);
409 assert(LegalizedNodes.count(Op) &&
410 "Expansion didn't add legal operands!");
411 return LegalizedNodes[Op];
412 }
413 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000414 PromoteOp(Op.getValue(i));
415 assert(LegalizedNodes.count(Op) &&
416 "Expansion didn't add legal operands!");
417 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000418 }
419 }
420
Chris Lattner45982da2005-05-12 16:53:42 +0000421 // Note that LegalizeOp may be reentered even from single-use nodes, which
422 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000423 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
424 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000425
Nate Begeman9373a812005-08-10 20:51:12 +0000426 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000427
428 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429
430 switch (Node->getOpcode()) {
431 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000432 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
433 // If this is a target node, legalize it by legalizing the operands then
434 // passing it through.
435 std::vector<SDOperand> Ops;
436 bool Changed = false;
437 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
438 Ops.push_back(LegalizeOp(Node->getOperand(i)));
439 Changed = Changed || Node->getOperand(i) != Ops.back();
440 }
441 if (Changed)
442 if (Node->getNumValues() == 1)
443 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
444 else {
445 std::vector<MVT::ValueType> VTs(Node->value_begin(),
446 Node->value_end());
447 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
448 }
449
450 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
451 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
452 return Result.getValue(Op.ResNo);
453 }
454 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000455 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
456 assert(0 && "Do not know how to legalize this operator!");
457 abort();
458 case ISD::EntryToken:
Nate Begeman56eb8682005-08-30 02:44:00 +0000459 case ISD::AssertSext:
460 case ISD::AssertZext:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000461 case ISD::FrameIndex:
462 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000463 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000464 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000465 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000467 case ISD::CopyFromReg:
468 Tmp1 = LegalizeOp(Node->getOperand(0));
469 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000470 Result = DAG.getCopyFromReg(Tmp1,
471 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
472 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000473 else
474 Result = Op.getValue(0);
475
476 // Since CopyFromReg produces two values, make sure to remember that we
477 // legalized both of them.
478 AddLegalizedOperand(Op.getValue(0), Result);
479 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
480 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000481 case ISD::ImplicitDef:
482 Tmp1 = LegalizeOp(Node->getOperand(0));
483 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000484 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
485 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000486 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000487 case ISD::UNDEF: {
488 MVT::ValueType VT = Op.getValueType();
489 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000490 default: assert(0 && "This action is not supported yet!");
491 case TargetLowering::Expand:
492 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000493 if (MVT::isInteger(VT))
494 Result = DAG.getConstant(0, VT);
495 else if (MVT::isFloatingPoint(VT))
496 Result = DAG.getConstantFP(0, VT);
497 else
498 assert(0 && "Unknown value type!");
499 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000500 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000501 break;
502 }
503 break;
504 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000505 case ISD::Constant:
506 // We know we don't need to expand constants here, constants only have one
507 // value and we check that it is fine above.
508
509 // FIXME: Maybe we should handle things like targets that don't support full
510 // 32-bit immediates?
511 break;
512 case ISD::ConstantFP: {
513 // Spill FP immediates to the constant pool if the target cannot directly
514 // codegen them. Targets often have some immediate values that can be
515 // efficiently generated into an FP register without a load. We explicitly
516 // leave these constants as ConstantFP nodes for the target to deal with.
517
518 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
519
520 // Check to see if this FP immediate is already legal.
521 bool isLegal = false;
522 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
523 E = TLI.legal_fpimm_end(); I != E; ++I)
524 if (CFP->isExactlyValue(*I)) {
525 isLegal = true;
526 break;
527 }
528
529 if (!isLegal) {
530 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000531 bool Extend = false;
532
533 // If a FP immediate is precise when represented as a float, we put it
534 // into the constant pool as a float, even if it's is statically typed
535 // as a double.
536 MVT::ValueType VT = CFP->getValueType(0);
537 bool isDouble = VT == MVT::f64;
538 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
539 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000540 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
541 // Only do this if the target has a native EXTLOAD instruction from
542 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000543 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000544 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
545 VT = MVT::f32;
546 Extend = true;
547 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000548
Chris Lattner5839bf22005-08-26 17:15:30 +0000549 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000550 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000551 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
552 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000553 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000554 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
555 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000556 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000557 }
558 break;
559 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000560 case ISD::TokenFactor: {
561 std::vector<SDOperand> Ops;
562 bool Changed = false;
563 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000564 SDOperand Op = Node->getOperand(i);
565 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000566 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000567 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
568 Changed = true;
569 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
570 Ops.push_back(LegalizeOp(Op.getOperand(j)));
571 } else {
572 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
573 Changed |= Ops[i] != Op;
574 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000575 }
576 if (Changed)
577 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
578 break;
579 }
580
Chris Lattner16cd04d2005-05-12 23:24:06 +0000581 case ISD::CALLSEQ_START:
582 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000583 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000584 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000585 Tmp2 = Node->getOperand(0);
586 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000587 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000588
589 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
590 // this will cause the maps used to memoize results to get confused.
591 // Create and add a dummy use, just to increase its use count. This will
592 // be removed at the end of legalize when dead nodes are removed.
593 if (Tmp2.Val->hasOneUse())
594 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
595 DAG.getConstant(0, MVT::i32));
596 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000597 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000598 // nodes are treated specially and are mutated in place. This makes the dag
599 // legalization process more efficient and also makes libcall insertion
600 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000601 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000602 case ISD::DYNAMIC_STACKALLOC:
603 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
604 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
605 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
606 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000607 Tmp3 != Node->getOperand(2)) {
608 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
609 std::vector<SDOperand> Ops;
610 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
611 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
612 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000613 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000614
615 // Since this op produces two values, make sure to remember that we
616 // legalized both of them.
617 AddLegalizedOperand(SDOperand(Node, 0), Result);
618 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
619 return Result.getValue(Op.ResNo);
620
Chris Lattnerd71c0412005-05-13 18:43:43 +0000621 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000622 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
624 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000625
626 bool Changed = false;
627 std::vector<SDOperand> Ops;
628 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
629 Ops.push_back(LegalizeOp(Node->getOperand(i)));
630 Changed |= Ops.back() != Node->getOperand(i);
631 }
632
633 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000634 std::vector<MVT::ValueType> RetTyVTs;
635 RetTyVTs.reserve(Node->getNumValues());
636 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000637 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000638 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
639 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000640 } else {
641 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000642 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000643 // Since calls produce multiple values, make sure to remember that we
644 // legalized all of them.
645 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
646 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
647 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000648 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000649 case ISD::BR:
650 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
651 if (Tmp1 != Node->getOperand(0))
652 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
653 break;
654
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000655 case ISD::BRCOND:
656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000657
Chris Lattner47e92232005-01-18 19:27:06 +0000658 switch (getTypeAction(Node->getOperand(1).getValueType())) {
659 case Expand: assert(0 && "It's impossible to expand bools");
660 case Legal:
661 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
662 break;
663 case Promote:
664 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
665 break;
666 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000667
668 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
669 default: assert(0 && "This action is not supported yet!");
670 case TargetLowering::Expand:
671 // Expand brcond's setcc into its constituent parts and create a BR_CC
672 // Node.
673 if (Tmp2.getOpcode() == ISD::SETCC) {
674 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
675 Tmp2.getOperand(0), Tmp2.getOperand(1),
676 Node->getOperand(2));
677 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000678 // Make sure the condition is either zero or one. It may have been
679 // promoted from something else.
680 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
681
Nate Begeman7cbd5252005-08-16 19:49:35 +0000682 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
683 DAG.getCondCode(ISD::SETNE), Tmp2,
684 DAG.getConstant(0, Tmp2.getValueType()),
685 Node->getOperand(2));
686 }
687 break;
688 case TargetLowering::Legal:
689 // Basic block destination (Op#2) is always legal.
690 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
691 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
692 Node->getOperand(2));
693 break;
694 }
695 break;
696 case ISD::BR_CC:
697 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
698
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000699 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000700 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
701 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
702 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
703 Tmp3 != Node->getOperand(3)) {
704 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
705 Tmp2, Tmp3, Node->getOperand(4));
706 }
707 break;
708 } else {
709 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
710 Node->getOperand(2), // LHS
711 Node->getOperand(3), // RHS
712 Node->getOperand(1)));
713 // If we get a SETCC back from legalizing the SETCC node we just
714 // created, then use its LHS, RHS, and CC directly in creating a new
715 // node. Otherwise, select between the true and false value based on
716 // comparing the result of the legalized with zero.
717 if (Tmp2.getOpcode() == ISD::SETCC) {
718 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
719 Tmp2.getOperand(0), Tmp2.getOperand(1),
720 Node->getOperand(4));
721 } else {
722 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
723 DAG.getCondCode(ISD::SETNE),
724 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
725 Node->getOperand(4));
726 }
727 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000728 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000729 case ISD::BRCONDTWOWAY:
730 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
731 switch (getTypeAction(Node->getOperand(1).getValueType())) {
732 case Expand: assert(0 && "It's impossible to expand bools");
733 case Legal:
734 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
735 break;
736 case Promote:
737 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
738 break;
739 }
740 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
741 // pair.
742 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
743 case TargetLowering::Promote:
744 default: assert(0 && "This action is not supported yet!");
745 case TargetLowering::Legal:
746 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
747 std::vector<SDOperand> Ops;
748 Ops.push_back(Tmp1);
749 Ops.push_back(Tmp2);
750 Ops.push_back(Node->getOperand(2));
751 Ops.push_back(Node->getOperand(3));
752 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
753 }
754 break;
755 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000756 // If BRTWOWAY_CC is legal for this target, then simply expand this node
757 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
758 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000759 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000760 if (Tmp2.getOpcode() == ISD::SETCC) {
761 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
762 Tmp2.getOperand(0), Tmp2.getOperand(1),
763 Node->getOperand(2), Node->getOperand(3));
764 } else {
765 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
766 DAG.getConstant(0, Tmp2.getValueType()),
767 Node->getOperand(2), Node->getOperand(3));
768 }
769 } else {
770 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000771 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000772 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
773 }
Chris Lattner411e8882005-04-09 03:30:19 +0000774 break;
775 }
776 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000777 case ISD::BRTWOWAY_CC:
778 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000779 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000780 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
781 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
782 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
783 Tmp3 != Node->getOperand(3)) {
784 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
785 Node->getOperand(4), Node->getOperand(5));
786 }
787 break;
788 } else {
789 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
790 Node->getOperand(2), // LHS
791 Node->getOperand(3), // RHS
792 Node->getOperand(1)));
793 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
794 // pair.
795 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
796 default: assert(0 && "This action is not supported yet!");
797 case TargetLowering::Legal:
798 // If we get a SETCC back from legalizing the SETCC node we just
799 // created, then use its LHS, RHS, and CC directly in creating a new
800 // node. Otherwise, select between the true and false value based on
801 // comparing the result of the legalized with zero.
802 if (Tmp2.getOpcode() == ISD::SETCC) {
803 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
804 Tmp2.getOperand(0), Tmp2.getOperand(1),
805 Node->getOperand(4), Node->getOperand(5));
806 } else {
807 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
808 DAG.getConstant(0, Tmp2.getValueType()),
809 Node->getOperand(4), Node->getOperand(5));
810 }
811 break;
812 case TargetLowering::Expand:
813 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
814 Node->getOperand(4));
815 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
816 break;
817 }
818 }
819 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000820 case ISD::LOAD:
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
822 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000823
Chris Lattner3e928bb2005-01-07 07:47:09 +0000824 if (Tmp1 != Node->getOperand(0) ||
825 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000826 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
827 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000828 else
829 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000830
Chris Lattner8afc48e2005-01-07 22:28:47 +0000831 // Since loads produce two values, make sure to remember that we legalized
832 // both of them.
833 AddLegalizedOperand(SDOperand(Node, 0), Result);
834 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
835 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000836
Chris Lattner0f69b292005-01-15 06:18:18 +0000837 case ISD::EXTLOAD:
838 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000839 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
841 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000842
Chris Lattner5f056bf2005-07-10 01:55:33 +0000843 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000844 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000845 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000846 case TargetLowering::Promote:
847 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000848 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
849 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000850 // Since loads produce two values, make sure to remember that we legalized
851 // both of them.
852 AddLegalizedOperand(SDOperand(Node, 0), Result);
853 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
854 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000855
Chris Lattner01ff7212005-04-10 22:54:25 +0000856 case TargetLowering::Legal:
857 if (Tmp1 != Node->getOperand(0) ||
858 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000859 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
860 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000861 else
862 Result = SDOperand(Node, 0);
863
864 // Since loads produce two values, make sure to remember that we legalized
865 // both of them.
866 AddLegalizedOperand(SDOperand(Node, 0), Result);
867 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
868 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000869 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000870 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
871 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
872 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000873 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000874 if (Op.ResNo)
875 return Load.getValue(1);
876 return Result;
877 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000878 assert(Node->getOpcode() != ISD::EXTLOAD &&
879 "EXTLOAD should always be supported!");
880 // Turn the unsupported load into an EXTLOAD followed by an explicit
881 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000882 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
883 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000884 SDOperand ValRes;
885 if (Node->getOpcode() == ISD::SEXTLOAD)
886 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000887 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000888 else
889 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000890 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
891 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
892 if (Op.ResNo)
893 return Result.getValue(1);
894 return ValRes;
895 }
896 assert(0 && "Unreachable");
897 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000898 case ISD::EXTRACT_ELEMENT:
899 // Get both the low and high parts.
900 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
901 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
902 Result = Tmp2; // 1 -> Hi
903 else
904 Result = Tmp1; // 0 -> Lo
905 break;
906
907 case ISD::CopyToReg:
908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000909
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000910 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000911 "Register type must be legal!");
912 // Legalize the incoming value (must be legal).
913 Tmp2 = LegalizeOp(Node->getOperand(2));
914 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
915 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
916 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000917 break;
918
919 case ISD::RET:
920 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
921 switch (Node->getNumOperands()) {
922 case 2: // ret val
923 switch (getTypeAction(Node->getOperand(1).getValueType())) {
924 case Legal:
925 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000926 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000927 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
928 break;
929 case Expand: {
930 SDOperand Lo, Hi;
931 ExpandOp(Node->getOperand(1), Lo, Hi);
932 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000933 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000934 }
935 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000936 Tmp2 = PromoteOp(Node->getOperand(1));
937 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
938 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000939 }
940 break;
941 case 1: // ret void
942 if (Tmp1 != Node->getOperand(0))
943 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
944 break;
945 default: { // ret <values>
946 std::vector<SDOperand> NewValues;
947 NewValues.push_back(Tmp1);
948 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
949 switch (getTypeAction(Node->getOperand(i).getValueType())) {
950 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000951 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000952 break;
953 case Expand: {
954 SDOperand Lo, Hi;
955 ExpandOp(Node->getOperand(i), Lo, Hi);
956 NewValues.push_back(Lo);
957 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000958 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000959 }
960 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000961 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000962 }
963 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
964 break;
965 }
966 }
967 break;
968 case ISD::STORE:
969 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
970 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
971
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000972 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000973 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000974 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000975 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +0000976 DAG.getConstant(FloatToBits(CFP->getValue()),
977 MVT::i32),
978 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000979 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000980 } else {
981 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000982 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +0000983 DAG.getConstant(DoubleToBits(CFP->getValue()),
984 MVT::i64),
985 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000986 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000987 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000988 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000989 }
990
Chris Lattner3e928bb2005-01-07 07:47:09 +0000991 switch (getTypeAction(Node->getOperand(1).getValueType())) {
992 case Legal: {
993 SDOperand Val = LegalizeOp(Node->getOperand(1));
994 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
995 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000996 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
997 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000998 break;
999 }
1000 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001001 // Truncate the value and store the result.
1002 Tmp3 = PromoteOp(Node->getOperand(1));
1003 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001004 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001005 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001006 break;
1007
Chris Lattner3e928bb2005-01-07 07:47:09 +00001008 case Expand:
1009 SDOperand Lo, Hi;
1010 ExpandOp(Node->getOperand(1), Lo, Hi);
1011
1012 if (!TLI.isLittleEndian())
1013 std::swap(Lo, Hi);
1014
Chris Lattneredb1add2005-05-11 04:51:16 +00001015 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1016 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001017 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001018 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1019 getIntPtrConstant(IncrementSize));
1020 assert(isTypeLegal(Tmp2.getValueType()) &&
1021 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001022 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001023 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1024 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001025 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1026 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001027 }
1028 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001029 case ISD::PCMARKER:
1030 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001031 if (Tmp1 != Node->getOperand(0))
1032 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001033 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001034 case ISD::TRUNCSTORE:
1035 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1036 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1037
1038 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1039 case Legal:
1040 Tmp2 = LegalizeOp(Node->getOperand(1));
1041 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1042 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +00001043 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001044 Node->getOperand(3), Node->getOperand(4));
Chris Lattner0f69b292005-01-15 06:18:18 +00001045 break;
1046 case Promote:
1047 case Expand:
1048 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1049 }
1050 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001051 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1053 case Expand: assert(0 && "It's impossible to expand bools");
1054 case Legal:
1055 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1056 break;
1057 case Promote:
1058 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1059 break;
1060 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001061 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001062 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001063
Nate Begemanb942a3d2005-08-23 04:29:48 +00001064 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001065 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001066 case TargetLowering::Expand:
1067 if (Tmp1.getOpcode() == ISD::SETCC) {
1068 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1069 Tmp2, Tmp3,
1070 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1071 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001072 // Make sure the condition is either zero or one. It may have been
1073 // promoted from something else.
1074 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001075 Result = DAG.getSelectCC(Tmp1,
1076 DAG.getConstant(0, Tmp1.getValueType()),
1077 Tmp2, Tmp3, ISD::SETNE);
1078 }
1079 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001080 case TargetLowering::Legal:
1081 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1082 Tmp3 != Node->getOperand(2))
1083 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1084 Tmp1, Tmp2, Tmp3);
1085 break;
1086 case TargetLowering::Promote: {
1087 MVT::ValueType NVT =
1088 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1089 unsigned ExtOp, TruncOp;
1090 if (MVT::isInteger(Tmp2.getValueType())) {
1091 ExtOp = ISD::ZERO_EXTEND;
1092 TruncOp = ISD::TRUNCATE;
1093 } else {
1094 ExtOp = ISD::FP_EXTEND;
1095 TruncOp = ISD::FP_ROUND;
1096 }
1097 // Promote each of the values to the new type.
1098 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1099 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1100 // Perform the larger operation, then round down.
1101 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1102 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1103 break;
1104 }
1105 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001106 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001107 case ISD::SELECT_CC:
1108 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1109 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1110
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001111 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001112 // Everything is legal, see if we should expand this op or something.
1113 switch (TLI.getOperationAction(ISD::SELECT_CC,
1114 Node->getOperand(0).getValueType())) {
1115 default: assert(0 && "This action is not supported yet!");
1116 case TargetLowering::Custom: {
1117 SDOperand Tmp =
1118 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1119 Node->getOperand(0),
1120 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001121 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001122 if (Tmp.Val) {
1123 Result = LegalizeOp(Tmp);
1124 break;
1125 }
1126 } // FALLTHROUGH if the target can't lower this operation after all.
1127 case TargetLowering::Legal:
1128 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1129 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1130 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1131 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1132 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1133 Tmp3, Tmp4, Node->getOperand(4));
1134 }
1135 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001136 }
1137 break;
1138 } else {
1139 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1140 Node->getOperand(0), // LHS
1141 Node->getOperand(1), // RHS
1142 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001143 // If we get a SETCC back from legalizing the SETCC node we just
1144 // created, then use its LHS, RHS, and CC directly in creating a new
1145 // node. Otherwise, select between the true and false value based on
1146 // comparing the result of the legalized with zero.
1147 if (Tmp1.getOpcode() == ISD::SETCC) {
1148 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1149 Tmp1.getOperand(0), Tmp1.getOperand(1),
1150 Tmp3, Tmp4, Tmp1.getOperand(2));
1151 } else {
1152 Result = DAG.getSelectCC(Tmp1,
1153 DAG.getConstant(0, Tmp1.getValueType()),
1154 Tmp3, Tmp4, ISD::SETNE);
1155 }
Nate Begeman9373a812005-08-10 20:51:12 +00001156 }
1157 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001158 case ISD::SETCC:
1159 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1160 case Legal:
1161 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1162 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001163 break;
1164 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001165 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1166 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1167
1168 // If this is an FP compare, the operands have already been extended.
1169 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1170 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001171 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001172
1173 // Otherwise, we have to insert explicit sign or zero extends. Note
1174 // that we could insert sign extends for ALL conditions, but zero extend
1175 // is cheaper on many machines (an AND instead of two shifts), so prefer
1176 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001177 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001178 default: assert(0 && "Unknown integer comparison!");
1179 case ISD::SETEQ:
1180 case ISD::SETNE:
1181 case ISD::SETUGE:
1182 case ISD::SETUGT:
1183 case ISD::SETULE:
1184 case ISD::SETULT:
1185 // ALL of these operations will work if we either sign or zero extend
1186 // the operands (including the unsigned comparisons!). Zero extend is
1187 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001188 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1189 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001190 break;
1191 case ISD::SETGE:
1192 case ISD::SETGT:
1193 case ISD::SETLT:
1194 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001195 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1196 DAG.getValueType(VT));
1197 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1198 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001199 break;
1200 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001201 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001202 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001203 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001204 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1205 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1206 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001207 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001208 case ISD::SETEQ:
1209 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001210 if (RHSLo == RHSHi)
1211 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1212 if (RHSCST->isAllOnesValue()) {
1213 // Comparison to -1.
1214 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001215 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001216 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001217 }
1218
Chris Lattner3e928bb2005-01-07 07:47:09 +00001219 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1220 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1221 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001222 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001223 break;
1224 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001225 // If this is a comparison of the sign bit, just look at the top part.
1226 // X > -1, x < 0
1227 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001228 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001229 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001230 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001231 (CST->isAllOnesValue()))) { // X > -1
1232 Tmp1 = LHSHi;
1233 Tmp2 = RHSHi;
1234 break;
1235 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001236
Chris Lattner3e928bb2005-01-07 07:47:09 +00001237 // FIXME: This generated code sucks.
1238 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001239 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001240 default: assert(0 && "Unknown integer setcc!");
1241 case ISD::SETLT:
1242 case ISD::SETULT: LowCC = ISD::SETULT; break;
1243 case ISD::SETGT:
1244 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1245 case ISD::SETLE:
1246 case ISD::SETULE: LowCC = ISD::SETULE; break;
1247 case ISD::SETGE:
1248 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1249 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001250
Chris Lattner3e928bb2005-01-07 07:47:09 +00001251 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1252 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1253 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1254
1255 // NOTE: on targets without efficient SELECT of bools, we can always use
1256 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001257 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1258 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1259 Node->getOperand(2));
1260 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001261 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1262 Result, Tmp1, Tmp2));
1263 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001264 }
1265 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001266
1267 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1268 default:
1269 assert(0 && "Cannot handle this action for SETCC yet!");
1270 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001271 case TargetLowering::Promote:
1272 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1273 Node->getOperand(2));
1274 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001275 case TargetLowering::Legal:
1276 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1277 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1278 Node->getOperand(2));
1279 break;
1280 case TargetLowering::Expand:
1281 // Expand a setcc node into a select_cc of the same condition, lhs, and
1282 // rhs that selects between const 1 (true) and const 0 (false).
1283 MVT::ValueType VT = Node->getValueType(0);
1284 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1285 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1286 Node->getOperand(2));
1287 Result = LegalizeOp(Result);
1288 break;
1289 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001290 break;
1291
Chris Lattnere1bd8222005-01-11 05:57:22 +00001292 case ISD::MEMSET:
1293 case ISD::MEMCPY:
1294 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001295 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001296 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1297
1298 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1299 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1300 case Expand: assert(0 && "Cannot expand a byte!");
1301 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001302 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001303 break;
1304 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001305 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001306 break;
1307 }
1308 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001309 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001310 }
Chris Lattner272455b2005-02-02 03:44:41 +00001311
1312 SDOperand Tmp4;
1313 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001314 case Expand: {
1315 // Length is too big, just take the lo-part of the length.
1316 SDOperand HiPart;
1317 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1318 break;
1319 }
Chris Lattnere5605212005-01-28 22:29:18 +00001320 case Legal:
1321 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001322 break;
1323 case Promote:
1324 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001325 break;
1326 }
1327
1328 SDOperand Tmp5;
1329 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1330 case Expand: assert(0 && "Cannot expand this yet!");
1331 case Legal:
1332 Tmp5 = LegalizeOp(Node->getOperand(4));
1333 break;
1334 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001335 Tmp5 = PromoteOp(Node->getOperand(4));
1336 break;
1337 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001338
1339 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1340 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001341 case TargetLowering::Custom: {
1342 SDOperand Tmp =
1343 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1344 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1345 if (Tmp.Val) {
1346 Result = LegalizeOp(Tmp);
1347 break;
1348 }
1349 // FALLTHROUGH if the target thinks it is legal.
1350 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001351 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001352 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1353 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1354 Tmp5 != Node->getOperand(4)) {
1355 std::vector<SDOperand> Ops;
1356 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1357 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1358 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1359 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001360 break;
1361 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001362 // Otherwise, the target does not support this operation. Lower the
1363 // operation to an explicit libcall as appropriate.
1364 MVT::ValueType IntPtr = TLI.getPointerTy();
1365 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1366 std::vector<std::pair<SDOperand, const Type*> > Args;
1367
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001368 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001369 if (Node->getOpcode() == ISD::MEMSET) {
1370 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1371 // Extend the ubyte argument to be an int value for the call.
1372 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1373 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1374 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1375
1376 FnName = "memset";
1377 } else if (Node->getOpcode() == ISD::MEMCPY ||
1378 Node->getOpcode() == ISD::MEMMOVE) {
1379 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1380 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1381 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1382 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1383 } else {
1384 assert(0 && "Unknown op!");
1385 }
Chris Lattner45982da2005-05-12 16:53:42 +00001386
Chris Lattnere1bd8222005-01-11 05:57:22 +00001387 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001388 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001389 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001390 Result = CallResult.second;
1391 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001392 break;
1393 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001394 }
1395 break;
1396 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001397
1398 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001399 Tmp1 = LegalizeOp(Node->getOperand(0));
1400 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001401
Chris Lattner3e011362005-05-14 07:45:46 +00001402 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1403 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1404 std::vector<SDOperand> Ops;
1405 Ops.push_back(Tmp1);
1406 Ops.push_back(Tmp2);
1407 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1408 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001409 Result = SDOperand(Node, 0);
1410 // Since these produce two values, make sure to remember that we legalized
1411 // both of them.
1412 AddLegalizedOperand(SDOperand(Node, 0), Result);
1413 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1414 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001415 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001416 Tmp1 = LegalizeOp(Node->getOperand(0));
1417 Tmp2 = LegalizeOp(Node->getOperand(1));
1418 Tmp3 = LegalizeOp(Node->getOperand(2));
1419 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1420 Tmp3 != Node->getOperand(2))
1421 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1422 break;
1423
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001424 case ISD::READIO:
1425 Tmp1 = LegalizeOp(Node->getOperand(0));
1426 Tmp2 = LegalizeOp(Node->getOperand(1));
1427
1428 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1429 case TargetLowering::Custom:
1430 default: assert(0 && "This action not implemented for this operation!");
1431 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001432 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1433 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1434 std::vector<SDOperand> Ops;
1435 Ops.push_back(Tmp1);
1436 Ops.push_back(Tmp2);
1437 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1438 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001439 Result = SDOperand(Node, 0);
1440 break;
1441 case TargetLowering::Expand:
1442 // Replace this with a load from memory.
1443 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1444 Node->getOperand(1), DAG.getSrcValue(NULL));
1445 Result = LegalizeOp(Result);
1446 break;
1447 }
1448
1449 // Since these produce two values, make sure to remember that we legalized
1450 // both of them.
1451 AddLegalizedOperand(SDOperand(Node, 0), Result);
1452 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1453 return Result.getValue(Op.ResNo);
1454
1455 case ISD::WRITEIO:
1456 Tmp1 = LegalizeOp(Node->getOperand(0));
1457 Tmp2 = LegalizeOp(Node->getOperand(1));
1458 Tmp3 = LegalizeOp(Node->getOperand(2));
1459
1460 switch (TLI.getOperationAction(Node->getOpcode(),
1461 Node->getOperand(1).getValueType())) {
1462 case TargetLowering::Custom:
1463 default: assert(0 && "This action not implemented for this operation!");
1464 case TargetLowering::Legal:
1465 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1466 Tmp3 != Node->getOperand(2))
1467 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1468 break;
1469 case TargetLowering::Expand:
1470 // Replace this with a store to memory.
1471 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1472 Node->getOperand(1), Node->getOperand(2),
1473 DAG.getSrcValue(NULL));
1474 Result = LegalizeOp(Result);
1475 break;
1476 }
1477 break;
1478
Chris Lattner84f67882005-01-20 18:52:28 +00001479 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001480 case ISD::SUB_PARTS:
1481 case ISD::SHL_PARTS:
1482 case ISD::SRA_PARTS:
1483 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001484 std::vector<SDOperand> Ops;
1485 bool Changed = false;
1486 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1487 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1488 Changed |= Ops.back() != Node->getOperand(i);
1489 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001490 if (Changed) {
1491 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1492 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1493 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001494
1495 // Since these produce multiple values, make sure to remember that we
1496 // legalized all of them.
1497 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1498 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1499 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001500 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001501
1502 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001503 case ISD::ADD:
1504 case ISD::SUB:
1505 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001506 case ISD::MULHS:
1507 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001508 case ISD::UDIV:
1509 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001510 case ISD::AND:
1511 case ISD::OR:
1512 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001513 case ISD::SHL:
1514 case ISD::SRL:
1515 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001516 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001517 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1518 case Expand: assert(0 && "Not possible");
1519 case Legal:
1520 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1521 break;
1522 case Promote:
1523 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1524 break;
1525 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001526 if (Tmp1 != Node->getOperand(0) ||
1527 Tmp2 != Node->getOperand(1))
1528 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1529 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001530
Nate Begemanc105e192005-04-06 00:23:54 +00001531 case ISD::UREM:
1532 case ISD::SREM:
1533 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1534 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1535 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1536 case TargetLowering::Legal:
1537 if (Tmp1 != Node->getOperand(0) ||
1538 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001539 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001540 Tmp2);
1541 break;
1542 case TargetLowering::Promote:
1543 case TargetLowering::Custom:
1544 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001545 case TargetLowering::Expand:
1546 if (MVT::isInteger(Node->getValueType(0))) {
1547 MVT::ValueType VT = Node->getValueType(0);
1548 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1549 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1550 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1551 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1552 } else {
1553 // Floating point mod -> fmod libcall.
1554 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1555 SDOperand Dummy;
1556 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001557 }
1558 break;
1559 }
1560 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001561
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001562 case ISD::CTPOP:
1563 case ISD::CTTZ:
1564 case ISD::CTLZ:
1565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1566 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1567 case TargetLowering::Legal:
1568 if (Tmp1 != Node->getOperand(0))
1569 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1570 break;
1571 case TargetLowering::Promote: {
1572 MVT::ValueType OVT = Tmp1.getValueType();
1573 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001574
1575 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001576 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1577 // Perform the larger operation, then subtract if needed.
1578 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1579 switch(Node->getOpcode())
1580 {
1581 case ISD::CTPOP:
1582 Result = Tmp1;
1583 break;
1584 case ISD::CTTZ:
1585 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001586 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1587 DAG.getConstant(getSizeInBits(NVT), NVT),
1588 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001589 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001590 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1591 break;
1592 case ISD::CTLZ:
1593 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001594 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1595 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001596 getSizeInBits(OVT), NVT));
1597 break;
1598 }
1599 break;
1600 }
1601 case TargetLowering::Custom:
1602 assert(0 && "Cannot custom handle this yet!");
1603 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001604 switch(Node->getOpcode())
1605 {
1606 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001607 static const uint64_t mask[6] = {
1608 0x5555555555555555ULL, 0x3333333333333333ULL,
1609 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1610 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1611 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001612 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001613 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1614 unsigned len = getSizeInBits(VT);
1615 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001616 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001617 Tmp2 = DAG.getConstant(mask[i], VT);
1618 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001619 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001620 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1621 DAG.getNode(ISD::AND, VT,
1622 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1623 Tmp2));
1624 }
1625 Result = Tmp1;
1626 break;
1627 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001628 case ISD::CTLZ: {
1629 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001630 x = x | (x >> 1);
1631 x = x | (x >> 2);
1632 ...
1633 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001634 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001635 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001636
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001637 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1638 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001639 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1640 unsigned len = getSizeInBits(VT);
1641 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1642 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001643 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001644 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1645 }
1646 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001647 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001648 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001649 }
1650 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001651 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001652 // unless the target has ctlz but not ctpop, in which case we use:
1653 // { return 32 - nlz(~x & (x-1)); }
1654 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001655 MVT::ValueType VT = Tmp1.getValueType();
1656 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001657 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001658 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1659 DAG.getNode(ISD::SUB, VT, Tmp1,
1660 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001661 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001662 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1663 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001664 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001665 DAG.getConstant(getSizeInBits(VT), VT),
1666 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1667 } else {
1668 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1669 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001670 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001671 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001672 default:
1673 assert(0 && "Cannot expand this yet!");
1674 break;
1675 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001676 break;
1677 }
1678 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001679
Chris Lattner2c8086f2005-04-02 05:00:07 +00001680 // Unary operators
1681 case ISD::FABS:
1682 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001683 case ISD::FSQRT:
1684 case ISD::FSIN:
1685 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001686 Tmp1 = LegalizeOp(Node->getOperand(0));
1687 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1688 case TargetLowering::Legal:
1689 if (Tmp1 != Node->getOperand(0))
1690 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1691 break;
1692 case TargetLowering::Promote:
1693 case TargetLowering::Custom:
1694 assert(0 && "Cannot promote/custom handle this yet!");
1695 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001696 switch(Node->getOpcode()) {
1697 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001698 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1699 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1700 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1701 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001702 break;
1703 }
1704 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001705 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1706 MVT::ValueType VT = Node->getValueType(0);
1707 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001708 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001709 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1710 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1711 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001712 break;
1713 }
1714 case ISD::FSQRT:
1715 case ISD::FSIN:
1716 case ISD::FCOS: {
1717 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001718 const char *FnName = 0;
1719 switch(Node->getOpcode()) {
1720 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1721 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1722 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1723 default: assert(0 && "Unreachable!");
1724 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001725 SDOperand Dummy;
1726 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001727 break;
1728 }
1729 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001730 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001731 }
1732 break;
1733 }
1734 break;
1735
1736 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001737 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001738 case ISD::UINT_TO_FP: {
1739 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001740 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1741 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001742 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001743 Node->getOperand(0).getValueType())) {
1744 default: assert(0 && "Unknown operation action!");
1745 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001746 Result = ExpandLegalINT_TO_FP(isSigned,
1747 LegalizeOp(Node->getOperand(0)),
1748 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001749 AddLegalizedOperand(Op, Result);
1750 return Result;
1751 case TargetLowering::Promote:
1752 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1753 Node->getValueType(0),
1754 isSigned);
1755 AddLegalizedOperand(Op, Result);
1756 return Result;
1757 case TargetLowering::Legal:
1758 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001759 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001760
Chris Lattner3e928bb2005-01-07 07:47:09 +00001761 Tmp1 = LegalizeOp(Node->getOperand(0));
1762 if (Tmp1 != Node->getOperand(0))
1763 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1764 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001765 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001766 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1767 Node->getValueType(0), Node->getOperand(0));
1768 break;
1769 case Promote:
1770 if (isSigned) {
1771 Result = PromoteOp(Node->getOperand(0));
1772 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1773 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1774 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1775 } else {
1776 Result = PromoteOp(Node->getOperand(0));
1777 Result = DAG.getZeroExtendInReg(Result,
1778 Node->getOperand(0).getValueType());
1779 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001780 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001781 break;
1782 }
1783 break;
1784 }
1785 case ISD::TRUNCATE:
1786 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1787 case Legal:
1788 Tmp1 = LegalizeOp(Node->getOperand(0));
1789 if (Tmp1 != Node->getOperand(0))
1790 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1791 break;
1792 case Expand:
1793 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1794
1795 // Since the result is legal, we should just be able to truncate the low
1796 // part of the source.
1797 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1798 break;
1799 case Promote:
1800 Result = PromoteOp(Node->getOperand(0));
1801 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1802 break;
1803 }
1804 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001805
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001806 case ISD::FP_TO_SINT:
1807 case ISD::FP_TO_UINT:
1808 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1809 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001810 Tmp1 = LegalizeOp(Node->getOperand(0));
1811
Chris Lattner1618beb2005-07-29 00:11:56 +00001812 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1813 default: assert(0 && "Unknown operation action!");
1814 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001815 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1816 SDOperand True, False;
1817 MVT::ValueType VT = Node->getOperand(0).getValueType();
1818 MVT::ValueType NVT = Node->getValueType(0);
1819 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1820 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1821 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1822 Node->getOperand(0), Tmp2, ISD::SETLT);
1823 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1824 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1825 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1826 Tmp2));
1827 False = DAG.getNode(ISD::XOR, NVT, False,
1828 DAG.getConstant(1ULL << ShiftAmt, NVT));
1829 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001830 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001831 } else {
1832 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1833 }
1834 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001835 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001836 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001837 Node->getOpcode() == ISD::FP_TO_SINT);
1838 AddLegalizedOperand(Op, Result);
1839 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00001840 case TargetLowering::Custom: {
1841 SDOperand Tmp =
1842 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1843 Tmp = TLI.LowerOperation(Tmp, DAG);
1844 if (Tmp.Val) {
1845 AddLegalizedOperand(Op, Tmp);
1846 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00001847 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00001848 } else {
1849 // The target thinks this is legal afterall.
1850 break;
1851 }
1852 }
Chris Lattner1618beb2005-07-29 00:11:56 +00001853 case TargetLowering::Legal:
1854 break;
1855 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001856
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001857 if (Tmp1 != Node->getOperand(0))
1858 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1859 break;
1860 case Expand:
1861 assert(0 && "Shouldn't need to expand other operators here!");
1862 case Promote:
1863 Result = PromoteOp(Node->getOperand(0));
1864 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1865 break;
1866 }
1867 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001868
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001869 case ISD::ZERO_EXTEND:
1870 case ISD::SIGN_EXTEND:
1871 case ISD::FP_EXTEND:
1872 case ISD::FP_ROUND:
1873 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1874 case Legal:
1875 Tmp1 = LegalizeOp(Node->getOperand(0));
1876 if (Tmp1 != Node->getOperand(0))
1877 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1878 break;
1879 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001880 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001881
Chris Lattner03c85462005-01-15 05:21:40 +00001882 case Promote:
1883 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001884 case ISD::ZERO_EXTEND:
1885 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001886 // NOTE: Any extend would work here...
1887 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001888 Result = DAG.getZeroExtendInReg(Result,
1889 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001890 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001891 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001892 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001893 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001894 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001895 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001896 Result,
1897 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001898 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001899 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001900 Result = PromoteOp(Node->getOperand(0));
1901 if (Result.getValueType() != Op.getValueType())
1902 // Dynamically dead while we have only 2 FP types.
1903 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1904 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001905 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001906 Result = PromoteOp(Node->getOperand(0));
1907 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1908 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001909 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001910 }
1911 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001912 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001913 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001914 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001915 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001916
1917 // If this operation is not supported, convert it to a shl/shr or load/store
1918 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001919 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1920 default: assert(0 && "This action not supported for this op yet!");
1921 case TargetLowering::Legal:
1922 if (Tmp1 != Node->getOperand(0))
1923 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001924 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001925 break;
1926 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001927 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001928 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001929 // NOTE: we could fall back on load/store here too for targets without
1930 // SAR. However, it is doubtful that any exist.
1931 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1932 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001933 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001934 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1935 Node->getOperand(0), ShiftCst);
1936 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1937 Result, ShiftCst);
1938 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1939 // The only way we can lower this is to turn it into a STORETRUNC,
1940 // EXTLOAD pair, targetting a temporary location (a stack slot).
1941
1942 // NOTE: there is a choice here between constantly creating new stack
1943 // slots and always reusing the same one. We currently always create
1944 // new ones, as reuse may inhibit scheduling.
1945 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1946 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1947 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1948 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001949 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001950 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1951 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1952 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001953 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001954 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00001955 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1956 Result, StackSlot, DAG.getSrcValue(NULL),
1957 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001958 } else {
1959 assert(0 && "Unknown op");
1960 }
1961 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001962 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001963 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001964 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001965 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001966 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001967
Chris Lattner45982da2005-05-12 16:53:42 +00001968 // Note that LegalizeOp may be reentered even from single-use nodes, which
1969 // means that we always must cache transformed nodes.
1970 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001971 return Result;
1972}
1973
Chris Lattner8b6fa222005-01-15 22:16:26 +00001974/// PromoteOp - Given an operation that produces a value in an invalid type,
1975/// promote it to compute the value into a larger type. The produced value will
1976/// have the correct bits for the low portion of the register, but no guarantee
1977/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001978SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1979 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001980 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001981 assert(getTypeAction(VT) == Promote &&
1982 "Caller should expand or legalize operands that are not promotable!");
1983 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1984 "Cannot promote to smaller type!");
1985
Chris Lattner03c85462005-01-15 05:21:40 +00001986 SDOperand Tmp1, Tmp2, Tmp3;
1987
1988 SDOperand Result;
1989 SDNode *Node = Op.Val;
1990
Chris Lattner45982da2005-05-12 16:53:42 +00001991 if (!Node->hasOneUse()) {
1992 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1993 if (I != PromotedNodes.end()) return I->second;
1994 } else {
1995 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1996 }
1997
Chris Lattner0f69b292005-01-15 06:18:18 +00001998 // Promotion needs an optimization step to clean up after it, and is not
1999 // careful to avoid operations the target does not support. Make sure that
2000 // all generated operations are legalized in the next iteration.
2001 NeedsAnotherIteration = true;
2002
Chris Lattner03c85462005-01-15 05:21:40 +00002003 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002004 case ISD::CopyFromReg:
2005 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002006 default:
2007 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2008 assert(0 && "Do not know how to promote this operator!");
2009 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002010 case ISD::UNDEF:
2011 Result = DAG.getNode(ISD::UNDEF, NVT);
2012 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002013 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002014 if (VT != MVT::i1)
2015 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2016 else
2017 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002018 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2019 break;
2020 case ISD::ConstantFP:
2021 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2022 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2023 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002024
Chris Lattner82fbfb62005-01-18 02:59:52 +00002025 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002026 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002027 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2028 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002029 Result = LegalizeOp(Result);
2030 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002031
2032 case ISD::TRUNCATE:
2033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2034 case Legal:
2035 Result = LegalizeOp(Node->getOperand(0));
2036 assert(Result.getValueType() >= NVT &&
2037 "This truncation doesn't make sense!");
2038 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2039 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2040 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002041 case Promote:
2042 // The truncation is not required, because we don't guarantee anything
2043 // about high bits anyway.
2044 Result = PromoteOp(Node->getOperand(0));
2045 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002046 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002047 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2048 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002049 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002050 }
2051 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002052 case ISD::SIGN_EXTEND:
2053 case ISD::ZERO_EXTEND:
2054 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2055 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2056 case Legal:
2057 // Input is legal? Just do extend all the way to the larger type.
2058 Result = LegalizeOp(Node->getOperand(0));
2059 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2060 break;
2061 case Promote:
2062 // Promote the reg if it's smaller.
2063 Result = PromoteOp(Node->getOperand(0));
2064 // The high bits are not guaranteed to be anything. Insert an extend.
2065 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002066 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002067 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002068 else
Chris Lattner23993562005-04-13 02:38:47 +00002069 Result = DAG.getZeroExtendInReg(Result,
2070 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002071 break;
2072 }
2073 break;
2074
2075 case ISD::FP_EXTEND:
2076 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2077 case ISD::FP_ROUND:
2078 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2079 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2080 case Promote: assert(0 && "Unreachable with 2 FP types!");
2081 case Legal:
2082 // Input is legal? Do an FP_ROUND_INREG.
2083 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002084 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2085 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002086 break;
2087 }
2088 break;
2089
2090 case ISD::SINT_TO_FP:
2091 case ISD::UINT_TO_FP:
2092 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2093 case Legal:
2094 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002095 // No extra round required here.
2096 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002097 break;
2098
2099 case Promote:
2100 Result = PromoteOp(Node->getOperand(0));
2101 if (Node->getOpcode() == ISD::SINT_TO_FP)
2102 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002103 Result,
2104 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002105 else
Chris Lattner23993562005-04-13 02:38:47 +00002106 Result = DAG.getZeroExtendInReg(Result,
2107 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002108 // No extra round required here.
2109 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002110 break;
2111 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002112 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2113 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002114 // Round if we cannot tolerate excess precision.
2115 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002116 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2117 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002118 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002119 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002120 break;
2121
2122 case ISD::FP_TO_SINT:
2123 case ISD::FP_TO_UINT:
2124 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2125 case Legal:
2126 Tmp1 = LegalizeOp(Node->getOperand(0));
2127 break;
2128 case Promote:
2129 // The input result is prerounded, so we don't have to do anything
2130 // special.
2131 Tmp1 = PromoteOp(Node->getOperand(0));
2132 break;
2133 case Expand:
2134 assert(0 && "not implemented");
2135 }
Nate Begemand2558e32005-08-14 01:20:53 +00002136 // If we're promoting a UINT to a larger size, check to see if the new node
2137 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2138 // we can use that instead. This allows us to generate better code for
2139 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2140 // legal, such as PowerPC.
2141 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002142 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
2143 TLI.isOperationLegal(ISD::FP_TO_SINT, NVT)) {
Nate Begemand2558e32005-08-14 01:20:53 +00002144 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2145 } else {
2146 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2147 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002148 break;
2149
Chris Lattner2c8086f2005-04-02 05:00:07 +00002150 case ISD::FABS:
2151 case ISD::FNEG:
2152 Tmp1 = PromoteOp(Node->getOperand(0));
2153 assert(Tmp1.getValueType() == NVT);
2154 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2155 // NOTE: we do not have to do any extra rounding here for
2156 // NoExcessFPPrecision, because we know the input will have the appropriate
2157 // precision, and these operations don't modify precision at all.
2158 break;
2159
Chris Lattnerda6ba872005-04-28 21:44:33 +00002160 case ISD::FSQRT:
2161 case ISD::FSIN:
2162 case ISD::FCOS:
2163 Tmp1 = PromoteOp(Node->getOperand(0));
2164 assert(Tmp1.getValueType() == NVT);
2165 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2166 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002167 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2168 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002169 break;
2170
Chris Lattner03c85462005-01-15 05:21:40 +00002171 case ISD::AND:
2172 case ISD::OR:
2173 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002174 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002175 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002176 case ISD::MUL:
2177 // The input may have strange things in the top bits of the registers, but
2178 // these operations don't care. They may have wierd bits going out, but
2179 // that too is okay if they are integer operations.
2180 Tmp1 = PromoteOp(Node->getOperand(0));
2181 Tmp2 = PromoteOp(Node->getOperand(1));
2182 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2183 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2184
2185 // However, if this is a floating point operation, they will give excess
2186 // precision that we may not be able to tolerate. If we DO allow excess
2187 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002188 // FIXME: Why would we need to round FP ops more than integer ones?
2189 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00002190 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002191 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2192 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002193 break;
2194
Chris Lattner8b6fa222005-01-15 22:16:26 +00002195 case ISD::SDIV:
2196 case ISD::SREM:
2197 // These operators require that their input be sign extended.
2198 Tmp1 = PromoteOp(Node->getOperand(0));
2199 Tmp2 = PromoteOp(Node->getOperand(1));
2200 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002201 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2202 DAG.getValueType(VT));
2203 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2204 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002205 }
2206 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2207
2208 // Perform FP_ROUND: this is probably overly pessimistic.
2209 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
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 case ISD::UDIV:
2215 case ISD::UREM:
2216 // These operators require that their input be zero extended.
2217 Tmp1 = PromoteOp(Node->getOperand(0));
2218 Tmp2 = PromoteOp(Node->getOperand(1));
2219 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002220 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2221 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002222 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2223 break;
2224
2225 case ISD::SHL:
2226 Tmp1 = PromoteOp(Node->getOperand(0));
2227 Tmp2 = LegalizeOp(Node->getOperand(1));
2228 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2229 break;
2230 case ISD::SRA:
2231 // The input value must be properly sign extended.
2232 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002233 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2234 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002235 Tmp2 = LegalizeOp(Node->getOperand(1));
2236 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2237 break;
2238 case ISD::SRL:
2239 // The input value must be properly zero extended.
2240 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002241 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002242 Tmp2 = LegalizeOp(Node->getOperand(1));
2243 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2244 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002245 case ISD::LOAD:
2246 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2247 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002248 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002249 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002250 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2251 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002252 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002253 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2254 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002255
2256 // Remember that we legalized the chain.
2257 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2258 break;
2259 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002260 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2261 case Expand: assert(0 && "It's impossible to expand bools");
2262 case Legal:
2263 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2264 break;
2265 case Promote:
2266 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2267 break;
2268 }
Chris Lattner03c85462005-01-15 05:21:40 +00002269 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2270 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2271 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2272 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002273 case ISD::SELECT_CC:
2274 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2275 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2276 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2277 Node->getOperand(1), Tmp2, Tmp3,
2278 Node->getOperand(4));
2279 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002280 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002281 case ISD::CALL: {
2282 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2283 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2284
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002285 std::vector<SDOperand> Ops;
2286 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2287 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2288
Chris Lattner8ac532c2005-01-16 19:46:48 +00002289 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2290 "Can only promote single result calls");
2291 std::vector<MVT::ValueType> RetTyVTs;
2292 RetTyVTs.reserve(2);
2293 RetTyVTs.push_back(NVT);
2294 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002295 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2296 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002297 Result = SDOperand(NC, 0);
2298
2299 // Insert the new chain mapping.
2300 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2301 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002302 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002303 case ISD::CTPOP:
2304 case ISD::CTTZ:
2305 case ISD::CTLZ:
2306 Tmp1 = Node->getOperand(0);
2307 //Zero extend the argument
2308 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2309 // Perform the larger operation, then subtract if needed.
2310 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2311 switch(Node->getOpcode())
2312 {
2313 case ISD::CTPOP:
2314 Result = Tmp1;
2315 break;
2316 case ISD::CTTZ:
2317 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002318 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002319 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002320 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002321 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2322 break;
2323 case ISD::CTLZ:
2324 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002325 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2326 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002327 getSizeInBits(VT), NVT));
2328 break;
2329 }
2330 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002331 }
2332
2333 assert(Result.Val && "Didn't set a result!");
2334 AddPromotedOperand(Op, Result);
2335 return Result;
2336}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002337
Chris Lattner84f67882005-01-20 18:52:28 +00002338/// ExpandAddSub - Find a clever way to expand this add operation into
2339/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002340void SelectionDAGLegalize::
2341ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2342 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002343 // Expand the subcomponents.
2344 SDOperand LHSL, LHSH, RHSL, RHSH;
2345 ExpandOp(LHS, LHSL, LHSH);
2346 ExpandOp(RHS, RHSL, RHSH);
2347
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002348 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002349 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2350 if (LHSL.getValueType() == MVT::i32) {
2351 SDOperand LowEl;
2352 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2353 if (C->getValue() == 0)
2354 LowEl = RHSL;
2355 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2356 if (C->getValue() == 0)
2357 LowEl = LHSL;
2358 if (LowEl.Val) {
2359 // Turn this into an add/sub of the high part only.
2360 SDOperand HiEl =
2361 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2362 LowEl.getValueType(), LHSH, RHSH);
2363 Lo = LowEl;
2364 Hi = HiEl;
2365 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002366 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002367 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002368
Chris Lattner84f67882005-01-20 18:52:28 +00002369 std::vector<SDOperand> Ops;
2370 Ops.push_back(LHSL);
2371 Ops.push_back(LHSH);
2372 Ops.push_back(RHSL);
2373 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002374
2375 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2376 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002377 Hi = Lo.getValue(1);
2378}
2379
Chris Lattner5b359c62005-04-02 04:00:59 +00002380void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2381 SDOperand Op, SDOperand Amt,
2382 SDOperand &Lo, SDOperand &Hi) {
2383 // Expand the subcomponents.
2384 SDOperand LHSL, LHSH;
2385 ExpandOp(Op, LHSL, LHSH);
2386
2387 std::vector<SDOperand> Ops;
2388 Ops.push_back(LHSL);
2389 Ops.push_back(LHSH);
2390 Ops.push_back(Amt);
Chris Lattnere89083a2005-05-14 07:25:05 +00002391 std::vector<MVT::ValueType> VTs;
2392 VTs.push_back(LHSL.getValueType());
2393 VTs.push_back(LHSH.getValueType());
2394 VTs.push_back(Amt.getValueType());
2395 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002396 Hi = Lo.getValue(1);
2397}
2398
2399
Chris Lattnere34b3962005-01-19 04:19:40 +00002400/// ExpandShift - Try to find a clever way to expand this shift operation out to
2401/// smaller elements. If we can't find a way that is more efficient than a
2402/// libcall on this target, return false. Otherwise, return true with the
2403/// low-parts expanded into Lo and Hi.
2404bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2405 SDOperand &Lo, SDOperand &Hi) {
2406 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2407 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002408
Chris Lattnere34b3962005-01-19 04:19:40 +00002409 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002410 SDOperand ShAmt = LegalizeOp(Amt);
2411 MVT::ValueType ShTy = ShAmt.getValueType();
2412 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2413 unsigned NVTBits = MVT::getSizeInBits(NVT);
2414
2415 // Handle the case when Amt is an immediate. Other cases are currently broken
2416 // and are disabled.
2417 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2418 unsigned Cst = CN->getValue();
2419 // Expand the incoming operand to be shifted, so that we have its parts
2420 SDOperand InL, InH;
2421 ExpandOp(Op, InL, InH);
2422 switch(Opc) {
2423 case ISD::SHL:
2424 if (Cst > VTBits) {
2425 Lo = DAG.getConstant(0, NVT);
2426 Hi = DAG.getConstant(0, NVT);
2427 } else if (Cst > NVTBits) {
2428 Lo = DAG.getConstant(0, NVT);
2429 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002430 } else if (Cst == NVTBits) {
2431 Lo = DAG.getConstant(0, NVT);
2432 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002433 } else {
2434 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2435 Hi = DAG.getNode(ISD::OR, NVT,
2436 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2437 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2438 }
2439 return true;
2440 case ISD::SRL:
2441 if (Cst > VTBits) {
2442 Lo = DAG.getConstant(0, NVT);
2443 Hi = DAG.getConstant(0, NVT);
2444 } else if (Cst > NVTBits) {
2445 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2446 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002447 } else if (Cst == NVTBits) {
2448 Lo = InH;
2449 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002450 } else {
2451 Lo = DAG.getNode(ISD::OR, NVT,
2452 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2453 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2454 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2455 }
2456 return true;
2457 case ISD::SRA:
2458 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002459 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002460 DAG.getConstant(NVTBits-1, ShTy));
2461 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002462 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002463 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002464 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002465 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002466 } else if (Cst == NVTBits) {
2467 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002468 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002469 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002470 } else {
2471 Lo = DAG.getNode(ISD::OR, NVT,
2472 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2473 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2474 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2475 }
2476 return true;
2477 }
2478 }
2479 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2480 // so disable it for now. Currently targets are handling this via SHL_PARTS
2481 // and friends.
2482 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002483
2484 // If we have an efficient select operation (or if the selects will all fold
2485 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002486 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002487 return false;
2488
2489 SDOperand InL, InH;
2490 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002491 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2492 DAG.getConstant(NVTBits, ShTy), ShAmt);
2493
Chris Lattnere5544f82005-01-20 20:29:23 +00002494 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002495 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2496 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002497
Chris Lattnere34b3962005-01-19 04:19:40 +00002498 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2499 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2500 DAG.getConstant(NVTBits-1, ShTy));
2501 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2502 DAG.getConstant(NVTBits-1, ShTy));
2503 }
2504
2505 if (Opc == ISD::SHL) {
2506 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2507 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2508 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002509 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002510
Chris Lattnere34b3962005-01-19 04:19:40 +00002511 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2512 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2513 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002514 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002515 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2516 DAG.getConstant(32, ShTy),
2517 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002518 DAG.getConstant(0, NVT),
2519 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002520 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002521 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002522 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002523 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002524
2525 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002526 if (Opc == ISD::SRA)
2527 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2528 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002529 else
2530 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002531 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002532 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002533 }
2534 return true;
2535}
Chris Lattner77e77a62005-01-21 06:05:23 +00002536
Chris Lattner9530ddc2005-05-13 05:09:11 +00002537/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2538/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002539/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002540static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002541 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002542
Chris Lattner16cd04d2005-05-12 23:24:06 +00002543 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002544 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002545 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002546 Found = Node;
2547 return;
2548 }
2549
2550 // Otherwise, scan the operands of Node to see if any of them is a call.
2551 assert(Node->getNumOperands() != 0 &&
2552 "All leaves should have depth equal to the entry node!");
2553 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002554 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002555
2556 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002557 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002558 Found);
2559}
2560
2561
Chris Lattner9530ddc2005-05-13 05:09:11 +00002562/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2563/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002564/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002565static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2566 std::set<SDNode*> &Visited) {
2567 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2568 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002569
Chris Lattner16cd04d2005-05-12 23:24:06 +00002570 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002571 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002572 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002573 Found = Node;
2574 return;
2575 }
2576
2577 // Otherwise, scan the operands of Node to see if any of them is a call.
2578 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2579 if (UI == E) return;
2580 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002581 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002582
2583 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002584 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002585}
2586
Chris Lattner9530ddc2005-05-13 05:09:11 +00002587/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002588/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002589static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002590 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002591 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002592 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002593 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002594
2595 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002596 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002597
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002598 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002599 if (TheChain.getValueType() != MVT::Other)
2600 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002601 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002602
2603 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002604 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002605
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002606 // Make sure to only follow users of our token chain.
2607 SDNode *User = *UI;
2608 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2609 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002610 if (SDNode *Result = FindCallSeqEnd(User))
2611 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002612 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002613 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002614}
2615
Chris Lattner9530ddc2005-05-13 05:09:11 +00002616/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002617/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002618static SDNode *FindCallSeqStart(SDNode *Node) {
2619 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002620 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002621
2622 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2623 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002624 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002625}
2626
2627
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002628/// FindInputOutputChains - If we are replacing an operation with a call we need
2629/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002630/// properly serialize the calls in the block. The returned operand is the
2631/// input chain value for the new call (e.g. the entry node or the previous
2632/// call), and OutChain is set to be the chain node to update to point to the
2633/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002634static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2635 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002636 SDNode *LatestCallSeqStart = Entry.Val;
2637 SDNode *LatestCallSeqEnd = 0;
2638 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2639 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002640
Chris Lattner16cd04d2005-05-12 23:24:06 +00002641 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002642 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002643 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2644 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002645 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2646 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002647 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002648 LatestCallSeqEnd = Entry.Val;
2649 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002650
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002651 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002652 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002653 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002654 std::set<SDNode*> Visited;
2655 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002656
Chris Lattner9530ddc2005-05-13 05:09:11 +00002657 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002658 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002659 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002660
Chris Lattner9530ddc2005-05-13 05:09:11 +00002661 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002662}
2663
Jeff Cohen00b168892005-07-27 06:12:32 +00002664/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002665void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2666 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002667 // Nothing to splice it into?
2668 if (OutChain == 0) return;
2669
2670 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2671 //OutChain->dump();
2672
2673 // Form a token factor node merging the old inval and the new inval.
2674 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2675 OutChain->getOperand(0));
2676 // Change the node to refer to the new token.
2677 OutChain->setAdjCallChain(InToken);
2678}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002679
2680
Chris Lattner77e77a62005-01-21 06:05:23 +00002681// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2682// does not fit into a register, return the lo part and set the hi part to the
2683// by-reg argument. If it does fit into a single register, return the result
2684// and leave the Hi part unset.
2685SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2686 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002687 SDNode *OutChain;
2688 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2689 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002690 if (InChain.Val == 0)
2691 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002692
Chris Lattner77e77a62005-01-21 06:05:23 +00002693 TargetLowering::ArgListTy Args;
2694 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2695 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2696 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2697 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2698 }
2699 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002700
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002701 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002702 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002703 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002704 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2705 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002706 SpliceCallInto(CallInfo.second, OutChain);
2707
2708 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002709
2710 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002711 default: assert(0 && "Unknown thing");
2712 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002713 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002714 case Promote:
2715 assert(0 && "Cannot promote this yet!");
2716 case Expand:
2717 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002718 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002719 return Lo;
2720 }
2721}
2722
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002723
Chris Lattner77e77a62005-01-21 06:05:23 +00002724/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2725/// destination type is legal.
2726SDOperand SelectionDAGLegalize::
2727ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002728 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002729 assert(getTypeAction(Source.getValueType()) == Expand &&
2730 "This is not an expansion!");
2731 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2732
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002733 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002734 assert(Source.getValueType() == MVT::i64 &&
2735 "This only works for 64-bit -> FP");
2736 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2737 // incoming integer is set. To handle this, we dynamically test to see if
2738 // it is set, and, if so, add a fudge factor.
2739 SDOperand Lo, Hi;
2740 ExpandOp(Source, Lo, Hi);
2741
Chris Lattner66de05b2005-05-13 04:45:13 +00002742 // If this is unsigned, and not supported, first perform the conversion to
2743 // signed, then adjust the result if the sign bit is set.
2744 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2745 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2746
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002747 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2748 DAG.getConstant(0, Hi.getValueType()),
2749 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002750 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2751 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2752 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002753 uint64_t FF = 0x5f800000ULL;
2754 if (TLI.isLittleEndian()) FF <<= 32;
2755 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002756
Chris Lattner5839bf22005-08-26 17:15:30 +00002757 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002758 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2759 SDOperand FudgeInReg;
2760 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002761 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2762 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002763 else {
2764 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002765 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2766 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002767 }
2768 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002769 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002770
Chris Lattnera88a2602005-05-14 05:33:54 +00002771 // Check to see if the target has a custom way to lower this. If so, use it.
2772 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2773 default: assert(0 && "This action not implemented for this operation!");
2774 case TargetLowering::Legal:
2775 case TargetLowering::Expand:
2776 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002777 case TargetLowering::Custom: {
2778 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2779 Source), DAG);
2780 if (NV.Val)
2781 return LegalizeOp(NV);
2782 break; // The target decided this was legal after all
2783 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002784 }
2785
Chris Lattner13689e22005-05-12 07:00:44 +00002786 // Expand the source, then glue it back together for the call. We must expand
2787 // the source in case it is shared (this pass of legalize must traverse it).
2788 SDOperand SrcLo, SrcHi;
2789 ExpandOp(Source, SrcLo, SrcHi);
2790 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2791
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002792 SDNode *OutChain = 0;
2793 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2794 DAG.getEntryNode());
2795 const char *FnName = 0;
2796 if (DestTy == MVT::f32)
2797 FnName = "__floatdisf";
2798 else {
2799 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2800 FnName = "__floatdidf";
2801 }
2802
Chris Lattner77e77a62005-01-21 06:05:23 +00002803 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2804
2805 TargetLowering::ArgListTy Args;
2806 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002807
Chris Lattner77e77a62005-01-21 06:05:23 +00002808 Args.push_back(std::make_pair(Source, ArgTy));
2809
2810 // We don't care about token chains for libcalls. We just use the entry
2811 // node as our input and ignore the output chain. This allows us to place
2812 // calls wherever we need them to satisfy data dependences.
2813 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002814
2815 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002816 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2817 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002818
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002819 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002820 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002821}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002822
Chris Lattnere34b3962005-01-19 04:19:40 +00002823
2824
Chris Lattner3e928bb2005-01-07 07:47:09 +00002825/// ExpandOp - Expand the specified SDOperand into its two component pieces
2826/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2827/// LegalizeNodes map is filled in for any results that are not expanded, the
2828/// ExpandedNodes map is filled in for any results that are expanded, and the
2829/// Lo/Hi values are returned.
2830void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2831 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002832 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002833 SDNode *Node = Op.Val;
2834 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2835 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2836 assert(MVT::isInteger(NVT) && NVT < VT &&
2837 "Cannot expand to FP value or to larger int value!");
2838
2839 // If there is more than one use of this, see if we already expanded it.
2840 // There is no use remembering values that only have a single use, as the map
2841 // entries will never be reused.
2842 if (!Node->hasOneUse()) {
2843 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2844 = ExpandedNodes.find(Op);
2845 if (I != ExpandedNodes.end()) {
2846 Lo = I->second.first;
2847 Hi = I->second.second;
2848 return;
2849 }
Chris Lattner45982da2005-05-12 16:53:42 +00002850 } else {
2851 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002852 }
2853
Chris Lattner4e6c7462005-01-08 19:27:05 +00002854 // Expanding to multiple registers needs to perform an optimization step, and
2855 // is not careful to avoid operations the target does not support. Make sure
2856 // that all generated operations are legalized in the next iteration.
2857 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002858
Chris Lattner3e928bb2005-01-07 07:47:09 +00002859 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002860 case ISD::CopyFromReg:
2861 assert(0 && "CopyFromReg must be legal!");
2862 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002863 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2864 assert(0 && "Do not know how to expand this operator!");
2865 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002866 case ISD::UNDEF:
2867 Lo = DAG.getNode(ISD::UNDEF, NVT);
2868 Hi = DAG.getNode(ISD::UNDEF, NVT);
2869 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002870 case ISD::Constant: {
2871 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2872 Lo = DAG.getConstant(Cst, NVT);
2873 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2874 break;
2875 }
2876
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002877 case ISD::BUILD_PAIR:
2878 // Legalize both operands. FIXME: in the future we should handle the case
2879 // where the two elements are not legal.
2880 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2881 Lo = LegalizeOp(Node->getOperand(0));
2882 Hi = LegalizeOp(Node->getOperand(1));
2883 break;
2884
Chris Lattneredb1add2005-05-11 04:51:16 +00002885 case ISD::CTPOP:
2886 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002887 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2888 DAG.getNode(ISD::CTPOP, NVT, Lo),
2889 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002890 Hi = DAG.getConstant(0, NVT);
2891 break;
2892
Chris Lattner39a8f332005-05-12 19:05:01 +00002893 case ISD::CTLZ: {
2894 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002895 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002896 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2897 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002898 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2899 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002900 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2901 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2902
2903 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2904 Hi = DAG.getConstant(0, NVT);
2905 break;
2906 }
2907
2908 case ISD::CTTZ: {
2909 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002910 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002911 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2912 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002913 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2914 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002915 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2916 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2917
2918 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2919 Hi = DAG.getConstant(0, NVT);
2920 break;
2921 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002922
Chris Lattner3e928bb2005-01-07 07:47:09 +00002923 case ISD::LOAD: {
2924 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2925 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002926 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002927
2928 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002929 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002930 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2931 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00002932 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002933 //are from the same instruction?
2934 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002935
2936 // Build a factor node to remember that this load is independent of the
2937 // other one.
2938 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2939 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002940
Chris Lattner3e928bb2005-01-07 07:47:09 +00002941 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002942 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002943 if (!TLI.isLittleEndian())
2944 std::swap(Lo, Hi);
2945 break;
2946 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002947 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002948 case ISD::CALL: {
2949 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2950 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2951
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002952 bool Changed = false;
2953 std::vector<SDOperand> Ops;
2954 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2955 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2956 Changed |= Ops.back() != Node->getOperand(i);
2957 }
2958
Chris Lattner3e928bb2005-01-07 07:47:09 +00002959 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2960 "Can only expand a call once so far, not i64 -> i16!");
2961
2962 std::vector<MVT::ValueType> RetTyVTs;
2963 RetTyVTs.reserve(3);
2964 RetTyVTs.push_back(NVT);
2965 RetTyVTs.push_back(NVT);
2966 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002967 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2968 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002969 Lo = SDOperand(NC, 0);
2970 Hi = SDOperand(NC, 1);
2971
2972 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002973 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002974 break;
2975 }
2976 case ISD::AND:
2977 case ISD::OR:
2978 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2979 SDOperand LL, LH, RL, RH;
2980 ExpandOp(Node->getOperand(0), LL, LH);
2981 ExpandOp(Node->getOperand(1), RL, RH);
2982 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2983 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2984 break;
2985 }
2986 case ISD::SELECT: {
2987 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002988
2989 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2990 case Expand: assert(0 && "It's impossible to expand bools");
2991 case Legal:
2992 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2993 break;
2994 case Promote:
2995 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2996 break;
2997 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002998 ExpandOp(Node->getOperand(1), LL, LH);
2999 ExpandOp(Node->getOperand(2), RL, RH);
3000 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3001 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3002 break;
3003 }
Nate Begeman9373a812005-08-10 20:51:12 +00003004 case ISD::SELECT_CC: {
3005 SDOperand TL, TH, FL, FH;
3006 ExpandOp(Node->getOperand(2), TL, TH);
3007 ExpandOp(Node->getOperand(3), FL, FH);
3008 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3009 Node->getOperand(1), TL, FL, Node->getOperand(4));
3010 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3011 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003012 Lo = LegalizeOp(Lo);
3013 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003014 break;
3015 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003016 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003017 SDOperand In;
3018 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3019 case Expand: assert(0 && "expand-expand not implemented yet!");
3020 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3021 case Promote:
3022 In = PromoteOp(Node->getOperand(0));
3023 // Emit the appropriate sign_extend_inreg to get the value we want.
3024 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003025 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003026 break;
3027 }
3028
Chris Lattner3e928bb2005-01-07 07:47:09 +00003029 // The low part is just a sign extension of the input (which degenerates to
3030 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003031 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003032
Chris Lattner3e928bb2005-01-07 07:47:09 +00003033 // The high part is obtained by SRA'ing all but one of the bits of the lo
3034 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003035 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003036 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3037 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003038 break;
3039 }
Chris Lattner06098e02005-04-03 23:41:52 +00003040 case ISD::ZERO_EXTEND: {
3041 SDOperand In;
3042 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3043 case Expand: assert(0 && "expand-expand not implemented yet!");
3044 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3045 case Promote:
3046 In = PromoteOp(Node->getOperand(0));
3047 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003048 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003049 break;
3050 }
3051
Chris Lattner3e928bb2005-01-07 07:47:09 +00003052 // The low part is just a zero extension of the input (which degenerates to
3053 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003054 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003055
Chris Lattner3e928bb2005-01-07 07:47:09 +00003056 // The high part is just a zero.
3057 Hi = DAG.getConstant(0, NVT);
3058 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003059 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003060 // These operators cannot be expanded directly, emit them as calls to
3061 // library functions.
3062 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003063 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003064 SDOperand Op;
3065 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3066 case Expand: assert(0 && "cannot expand FP!");
3067 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3068 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3069 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003070
Chris Lattnerf20d1832005-07-30 01:40:57 +00003071 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3072
Chris Lattner80a3e942005-07-29 00:33:32 +00003073 // Now that the custom expander is done, expand the result, which is still
3074 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003075 if (Op.Val) {
3076 ExpandOp(Op, Lo, Hi);
3077 break;
3078 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003079 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003080
Chris Lattner4e6c7462005-01-08 19:27:05 +00003081 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003082 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003083 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003084 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003085 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003086
Chris Lattner4e6c7462005-01-08 19:27:05 +00003087 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003088 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3089 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3090 LegalizeOp(Node->getOperand(0)));
3091 // Now that the custom expander is done, expand the result, which is still
3092 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003093 Op = TLI.LowerOperation(Op, DAG);
3094 if (Op.Val) {
3095 ExpandOp(Op, Lo, Hi);
3096 break;
3097 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003098 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003099
Chris Lattner4e6c7462005-01-08 19:27:05 +00003100 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003101 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003102 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003103 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003104 break;
3105
Chris Lattnere34b3962005-01-19 04:19:40 +00003106 case ISD::SHL:
3107 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003108 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003109 break;
Chris Lattner47599822005-04-02 03:38:53 +00003110
3111 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003112 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003113 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3114 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003115 break;
3116 }
3117
Chris Lattnere34b3962005-01-19 04:19:40 +00003118 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003119 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003120 break;
3121
3122 case ISD::SRA:
3123 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003124 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003125 break;
Chris Lattner47599822005-04-02 03:38:53 +00003126
3127 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003128 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003129 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3130 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003131 break;
3132 }
3133
Chris Lattnere34b3962005-01-19 04:19:40 +00003134 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003135 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003136 break;
3137 case ISD::SRL:
3138 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003139 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003140 break;
Chris Lattner47599822005-04-02 03:38:53 +00003141
3142 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003143 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003144 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3145 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003146 break;
3147 }
3148
Chris Lattnere34b3962005-01-19 04:19:40 +00003149 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003150 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003151 break;
3152
Misha Brukmanedf128a2005-04-21 22:36:52 +00003153 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003154 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3155 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003156 break;
3157 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003158 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3159 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003160 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003161 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003162 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003163 SDOperand LL, LH, RL, RH;
3164 ExpandOp(Node->getOperand(0), LL, LH);
3165 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003166 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3167 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3168 // extended the sign bit of the low half through the upper half, and if so
3169 // emit a MULHS instead of the alternate sequence that is valid for any
3170 // i64 x i64 multiply.
3171 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3172 // is RH an extension of the sign bit of RL?
3173 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3174 RH.getOperand(1).getOpcode() == ISD::Constant &&
3175 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3176 // is LH an extension of the sign bit of LL?
3177 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3178 LH.getOperand(1).getOpcode() == ISD::Constant &&
3179 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3180 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3181 } else {
3182 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3183 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3184 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3185 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3186 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3187 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003188 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3189 } else {
3190 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3191 }
3192 break;
3193 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003194 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3195 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3196 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3197 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003198 }
3199
3200 // Remember in a map if the values will be reused later.
3201 if (!Node->hasOneUse()) {
3202 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3203 std::make_pair(Lo, Hi))).second;
3204 assert(isNew && "Value already expanded?!?");
3205 }
3206}
3207
3208
3209// SelectionDAG::Legalize - This is the entry point for the file.
3210//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003211void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003212 /// run - This is the main entry point to this class.
3213 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003214 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003215}
3216