blob: 56f93adf9fb37e593e93b3a18877dd4152aea898 [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"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.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
207 SDOperand Bias = DAG.getConstantFP(isSigned ? 0x1.000008p52 : 0x1.000000p52,
208 MVT::f64);
209 // subtract the bias
210 SDOperand Sub = DAG.getNode(ISD::SUB, MVT::f64, Load, Bias);
211 // final result
212 SDOperand Result;
213 // handle final rounding
214 if (DestVT == MVT::f64) {
215 // do nothing
216 Result = Sub;
217 } else {
218 // if f32 then cast to f32
219 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
220 }
221 NeedsAnotherIteration = true;
222 return Result;
223 }
224 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000225 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000226
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000227 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
228 DAG.getConstant(0, Op0.getValueType()),
229 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000230 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
231 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
232 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000233
Jim Laskey6269ed12005-08-17 00:39:29 +0000234 // If the sign bit of the integer is set, the large number will be treated
235 // as a negative number. To counteract this, the dynamic code adds an
236 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000237 uint64_t FF;
238 switch (Op0.getValueType()) {
239 default: assert(0 && "Unsupported integer type!");
240 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
241 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
242 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
243 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
244 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000245 if (TLI.isLittleEndian()) FF <<= 32;
246 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Chris Lattnercad063f2005-07-16 00:19:57 +0000248 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
249 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
250 TLI.getPointerTy());
251 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:
289 if (!TLI.hasNativeSupportFor(NewInTy))
290 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:
303 if (!TLI.hasNativeSupportFor(NewInTy))
304 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:
347 if (!TLI.hasNativeSupportFor(NewOutTy))
348 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:
360 if (!TLI.hasNativeSupportFor(NewOutTy))
361 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 Lattnere3304a32005-01-08 20:35:13 +0000396 assert(getTypeAction(Op.getValueType()) == Legal &&
397 "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:
459 case ISD::FrameIndex:
460 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000461 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000462 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000463 assert(getTypeAction(Node->getValueType(0)) == Legal &&
464 "This must be legal!");
465 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000466 case ISD::CopyFromReg:
467 Tmp1 = LegalizeOp(Node->getOperand(0));
468 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000469 Result = DAG.getCopyFromReg(Tmp1,
470 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
471 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000472 else
473 Result = Op.getValue(0);
474
475 // Since CopyFromReg produces two values, make sure to remember that we
476 // legalized both of them.
477 AddLegalizedOperand(Op.getValue(0), Result);
478 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
479 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000480 case ISD::ImplicitDef:
481 Tmp1 = LegalizeOp(Node->getOperand(0));
482 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000483 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
484 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000485 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000486 case ISD::UNDEF: {
487 MVT::ValueType VT = Op.getValueType();
488 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000489 default: assert(0 && "This action is not supported yet!");
490 case TargetLowering::Expand:
491 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000492 if (MVT::isInteger(VT))
493 Result = DAG.getConstant(0, VT);
494 else if (MVT::isFloatingPoint(VT))
495 Result = DAG.getConstantFP(0, VT);
496 else
497 assert(0 && "Unknown value type!");
498 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000499 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000500 break;
501 }
502 break;
503 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000504 case ISD::Constant:
505 // We know we don't need to expand constants here, constants only have one
506 // value and we check that it is fine above.
507
508 // FIXME: Maybe we should handle things like targets that don't support full
509 // 32-bit immediates?
510 break;
511 case ISD::ConstantFP: {
512 // Spill FP immediates to the constant pool if the target cannot directly
513 // codegen them. Targets often have some immediate values that can be
514 // efficiently generated into an FP register without a load. We explicitly
515 // leave these constants as ConstantFP nodes for the target to deal with.
516
517 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
518
519 // Check to see if this FP immediate is already legal.
520 bool isLegal = false;
521 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
522 E = TLI.legal_fpimm_end(); I != E; ++I)
523 if (CFP->isExactlyValue(*I)) {
524 isLegal = true;
525 break;
526 }
527
528 if (!isLegal) {
529 // Otherwise we need to spill the constant to memory.
530 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
531
532 bool Extend = false;
533
534 // If a FP immediate is precise when represented as a float, we put it
535 // into the constant pool as a float, even if it's is statically typed
536 // as a double.
537 MVT::ValueType VT = CFP->getValueType(0);
538 bool isDouble = VT == MVT::f64;
539 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
540 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000541 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
542 // Only do this if the target has a native EXTLOAD instruction from
543 // f32.
544 TLI.getOperationAction(ISD::EXTLOAD,
545 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000546 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
547 VT = MVT::f32;
548 Extend = true;
549 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000550
Chris Lattner3e928bb2005-01-07 07:47:09 +0000551 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
552 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000553 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000554 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
555 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000556 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000557 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
558 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000559 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000560 }
561 break;
562 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000563 case ISD::TokenFactor: {
564 std::vector<SDOperand> Ops;
565 bool Changed = false;
566 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000567 SDOperand Op = Node->getOperand(i);
568 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000569 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000570 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
571 Changed = true;
572 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
573 Ops.push_back(LegalizeOp(Op.getOperand(j)));
574 } else {
575 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
576 Changed |= Ops[i] != Op;
577 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000578 }
579 if (Changed)
580 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
581 break;
582 }
583
Chris Lattner16cd04d2005-05-12 23:24:06 +0000584 case ISD::CALLSEQ_START:
585 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000587 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000588 Tmp2 = Node->getOperand(0);
589 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000590 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000591
592 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
593 // this will cause the maps used to memoize results to get confused.
594 // Create and add a dummy use, just to increase its use count. This will
595 // be removed at the end of legalize when dead nodes are removed.
596 if (Tmp2.Val->hasOneUse())
597 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
598 DAG.getConstant(0, MVT::i32));
599 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000600 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000601 // nodes are treated specially and are mutated in place. This makes the dag
602 // legalization process more efficient and also makes libcall insertion
603 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000604 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000605 case ISD::DYNAMIC_STACKALLOC:
606 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
607 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
608 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
609 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000610 Tmp3 != Node->getOperand(2)) {
611 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
612 std::vector<SDOperand> Ops;
613 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
614 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
615 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000616 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000617
618 // Since this op produces two values, make sure to remember that we
619 // legalized both of them.
620 AddLegalizedOperand(SDOperand(Node, 0), Result);
621 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
622 return Result.getValue(Op.ResNo);
623
Chris Lattnerd71c0412005-05-13 18:43:43 +0000624 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000625 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000626 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
627 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000628
629 bool Changed = false;
630 std::vector<SDOperand> Ops;
631 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
632 Ops.push_back(LegalizeOp(Node->getOperand(i)));
633 Changed |= Ops.back() != Node->getOperand(i);
634 }
635
636 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000637 std::vector<MVT::ValueType> RetTyVTs;
638 RetTyVTs.reserve(Node->getNumValues());
639 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000640 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000641 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
642 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000643 } else {
644 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000645 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000646 // Since calls produce multiple values, make sure to remember that we
647 // legalized all of them.
648 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
649 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
650 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000651 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000652 case ISD::BR:
653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
654 if (Tmp1 != Node->getOperand(0))
655 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
656 break;
657
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000658 case ISD::BRCOND:
659 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000660
Chris Lattner47e92232005-01-18 19:27:06 +0000661 switch (getTypeAction(Node->getOperand(1).getValueType())) {
662 case Expand: assert(0 && "It's impossible to expand bools");
663 case Legal:
664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
665 break;
666 case Promote:
667 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
668 break;
669 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000670
671 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
672 default: assert(0 && "This action is not supported yet!");
673 case TargetLowering::Expand:
674 // Expand brcond's setcc into its constituent parts and create a BR_CC
675 // Node.
676 if (Tmp2.getOpcode() == ISD::SETCC) {
677 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
678 Tmp2.getOperand(0), Tmp2.getOperand(1),
679 Node->getOperand(2));
680 } else {
681 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
682 DAG.getCondCode(ISD::SETNE), Tmp2,
683 DAG.getConstant(0, Tmp2.getValueType()),
684 Node->getOperand(2));
685 }
686 break;
687 case TargetLowering::Legal:
688 // Basic block destination (Op#2) is always legal.
689 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
690 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
691 Node->getOperand(2));
692 break;
693 }
694 break;
695 case ISD::BR_CC:
696 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
697
698 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
699 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
700 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
701 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
702 Tmp3 != Node->getOperand(3)) {
703 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
704 Tmp2, Tmp3, Node->getOperand(4));
705 }
706 break;
707 } else {
708 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
709 Node->getOperand(2), // LHS
710 Node->getOperand(3), // RHS
711 Node->getOperand(1)));
712 // If we get a SETCC back from legalizing the SETCC node we just
713 // created, then use its LHS, RHS, and CC directly in creating a new
714 // node. Otherwise, select between the true and false value based on
715 // comparing the result of the legalized with zero.
716 if (Tmp2.getOpcode() == ISD::SETCC) {
717 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
718 Tmp2.getOperand(0), Tmp2.getOperand(1),
719 Node->getOperand(4));
720 } else {
721 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
722 DAG.getCondCode(ISD::SETNE),
723 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
724 Node->getOperand(4));
725 }
726 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000727 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000728 case ISD::BRCONDTWOWAY:
729 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
730 switch (getTypeAction(Node->getOperand(1).getValueType())) {
731 case Expand: assert(0 && "It's impossible to expand bools");
732 case Legal:
733 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
734 break;
735 case Promote:
736 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
737 break;
738 }
739 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
740 // pair.
741 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
742 case TargetLowering::Promote:
743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Legal:
745 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
746 std::vector<SDOperand> Ops;
747 Ops.push_back(Tmp1);
748 Ops.push_back(Tmp2);
749 Ops.push_back(Node->getOperand(2));
750 Ops.push_back(Node->getOperand(3));
751 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
752 }
753 break;
754 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000755 // If BRTWOWAY_CC is legal for this target, then simply expand this node
756 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
757 // BRCOND/BR pair.
758 if (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other) ==
759 TargetLowering::Legal) {
760 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.
779 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
780 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 Lattnerd5d0f9b2005-08-16 21:55:35 +0000910 assert(getTypeAction(Node->getOperand(2).getValueType()) == Legal &&
911 "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) {
975 union {
976 unsigned I;
977 float F;
978 } V;
979 V.F = CFP->getValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000980 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000981 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000982 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000983 } else {
984 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
985 union {
986 uint64_t I;
987 double F;
988 } V;
989 V.F = CFP->getValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000990 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000991 DAG.getConstant(V.I, MVT::i64), Tmp2,
992 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000993 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000994 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000995 }
996
Chris Lattner3e928bb2005-01-07 07:47:09 +0000997 switch (getTypeAction(Node->getOperand(1).getValueType())) {
998 case Legal: {
999 SDOperand Val = LegalizeOp(Node->getOperand(1));
1000 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1001 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001002 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1003 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001004 break;
1005 }
1006 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001007 // Truncate the value and store the result.
1008 Tmp3 = PromoteOp(Node->getOperand(1));
1009 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001010 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001011 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001012 break;
1013
Chris Lattner3e928bb2005-01-07 07:47:09 +00001014 case Expand:
1015 SDOperand Lo, Hi;
1016 ExpandOp(Node->getOperand(1), Lo, Hi);
1017
1018 if (!TLI.isLittleEndian())
1019 std::swap(Lo, Hi);
1020
Chris Lattneredb1add2005-05-11 04:51:16 +00001021 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1022 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001023 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001024 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1025 getIntPtrConstant(IncrementSize));
1026 assert(isTypeLegal(Tmp2.getValueType()) &&
1027 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001028 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001029 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1030 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001031 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1032 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001033 }
1034 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001035 case ISD::PCMARKER:
1036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001037 if (Tmp1 != Node->getOperand(0))
1038 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001039 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001040 case ISD::TRUNCSTORE:
1041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1042 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1043
1044 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1045 case Legal:
1046 Tmp2 = LegalizeOp(Node->getOperand(1));
1047 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1048 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +00001049 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001050 Node->getOperand(3), Node->getOperand(4));
Chris Lattner0f69b292005-01-15 06:18:18 +00001051 break;
1052 case Promote:
1053 case Expand:
1054 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1055 }
1056 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001057 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001058 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1059 case Expand: assert(0 && "It's impossible to expand bools");
1060 case Legal:
1061 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1062 break;
1063 case Promote:
1064 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1065 break;
1066 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001067 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001068 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001069
1070 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
1071 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001072 case TargetLowering::Expand:
1073 if (Tmp1.getOpcode() == ISD::SETCC) {
1074 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1075 Tmp2, Tmp3,
1076 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1077 } else {
1078 Result = DAG.getSelectCC(Tmp1,
1079 DAG.getConstant(0, Tmp1.getValueType()),
1080 Tmp2, Tmp3, ISD::SETNE);
1081 }
1082 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001083 case TargetLowering::Legal:
1084 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1085 Tmp3 != Node->getOperand(2))
1086 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1087 Tmp1, Tmp2, Tmp3);
1088 break;
1089 case TargetLowering::Promote: {
1090 MVT::ValueType NVT =
1091 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1092 unsigned ExtOp, TruncOp;
1093 if (MVT::isInteger(Tmp2.getValueType())) {
1094 ExtOp = ISD::ZERO_EXTEND;
1095 TruncOp = ISD::TRUNCATE;
1096 } else {
1097 ExtOp = ISD::FP_EXTEND;
1098 TruncOp = ISD::FP_ROUND;
1099 }
1100 // Promote each of the values to the new type.
1101 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1102 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1103 // Perform the larger operation, then round down.
1104 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1105 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1106 break;
1107 }
1108 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001109 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001110 case ISD::SELECT_CC:
1111 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1112 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1113
1114 if (getTypeAction(Node->getOperand(0).getValueType()) == Legal) {
1115 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1116 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1117 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1118 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1119 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1120 Tmp3, Tmp4, Node->getOperand(4));
1121 }
1122 break;
1123 } else {
1124 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1125 Node->getOperand(0), // LHS
1126 Node->getOperand(1), // RHS
1127 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001128 // If we get a SETCC back from legalizing the SETCC node we just
1129 // created, then use its LHS, RHS, and CC directly in creating a new
1130 // node. Otherwise, select between the true and false value based on
1131 // comparing the result of the legalized with zero.
1132 if (Tmp1.getOpcode() == ISD::SETCC) {
1133 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1134 Tmp1.getOperand(0), Tmp1.getOperand(1),
1135 Tmp3, Tmp4, Tmp1.getOperand(2));
1136 } else {
1137 Result = DAG.getSelectCC(Tmp1,
1138 DAG.getConstant(0, Tmp1.getValueType()),
1139 Tmp3, Tmp4, ISD::SETNE);
1140 }
Nate Begeman9373a812005-08-10 20:51:12 +00001141 }
1142 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001143 case ISD::SETCC:
1144 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1145 case Legal:
1146 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1147 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1148 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001149 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1150 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001151 break;
1152 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001153 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1154 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1155
1156 // If this is an FP compare, the operands have already been extended.
1157 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1158 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001159 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001160
1161 // Otherwise, we have to insert explicit sign or zero extends. Note
1162 // that we could insert sign extends for ALL conditions, but zero extend
1163 // is cheaper on many machines (an AND instead of two shifts), so prefer
1164 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001165 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001166 default: assert(0 && "Unknown integer comparison!");
1167 case ISD::SETEQ:
1168 case ISD::SETNE:
1169 case ISD::SETUGE:
1170 case ISD::SETUGT:
1171 case ISD::SETULE:
1172 case ISD::SETULT:
1173 // ALL of these operations will work if we either sign or zero extend
1174 // the operands (including the unsigned comparisons!). Zero extend is
1175 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001176 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1177 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001178 break;
1179 case ISD::SETGE:
1180 case ISD::SETGT:
1181 case ISD::SETLT:
1182 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001183 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1184 DAG.getValueType(VT));
1185 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1186 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001187 break;
1188 }
1189
1190 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001191 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1192 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001193 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001194 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001195 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1196 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1197 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001198 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001199 case ISD::SETEQ:
1200 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001201 if (RHSLo == RHSHi)
1202 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1203 if (RHSCST->isAllOnesValue()) {
1204 // Comparison to -1.
1205 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001206 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1207 RHSLo, Node->getOperand(2));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001208 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001209 }
1210
Chris Lattner3e928bb2005-01-07 07:47:09 +00001211 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1212 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1213 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001214 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1215 DAG.getConstant(0, Tmp1.getValueType()),
1216 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001217 break;
1218 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001219 // If this is a comparison of the sign bit, just look at the top part.
1220 // X > -1, x < 0
1221 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001222 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001223 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001224 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001225 (CST->isAllOnesValue()))) // X > -1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001226 return DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1227 Node->getOperand(2));
Chris Lattner5b95ed62005-04-12 02:19:10 +00001228
Chris Lattner3e928bb2005-01-07 07:47:09 +00001229 // FIXME: This generated code sucks.
1230 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001231 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001232 default: assert(0 && "Unknown integer setcc!");
1233 case ISD::SETLT:
1234 case ISD::SETULT: LowCC = ISD::SETULT; break;
1235 case ISD::SETGT:
1236 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1237 case ISD::SETLE:
1238 case ISD::SETULE: LowCC = ISD::SETULE; break;
1239 case ISD::SETGE:
1240 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1241 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001242
Chris Lattner3e928bb2005-01-07 07:47:09 +00001243 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1244 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1245 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1246
1247 // NOTE: on targets without efficient SELECT of bools, we can always use
1248 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001249 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1250 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1251 Node->getOperand(2));
1252 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001253 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1254 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001255 break;
1256 }
1257 }
1258 break;
1259
Chris Lattnere1bd8222005-01-11 05:57:22 +00001260 case ISD::MEMSET:
1261 case ISD::MEMCPY:
1262 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001263 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001264 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1265
1266 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1267 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1268 case Expand: assert(0 && "Cannot expand a byte!");
1269 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001270 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001271 break;
1272 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001273 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001274 break;
1275 }
1276 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001277 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001278 }
Chris Lattner272455b2005-02-02 03:44:41 +00001279
1280 SDOperand Tmp4;
1281 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001282 case Expand: {
1283 // Length is too big, just take the lo-part of the length.
1284 SDOperand HiPart;
1285 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1286 break;
1287 }
Chris Lattnere5605212005-01-28 22:29:18 +00001288 case Legal:
1289 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001290 break;
1291 case Promote:
1292 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001293 break;
1294 }
1295
1296 SDOperand Tmp5;
1297 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1298 case Expand: assert(0 && "Cannot expand this yet!");
1299 case Legal:
1300 Tmp5 = LegalizeOp(Node->getOperand(4));
1301 break;
1302 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001303 Tmp5 = PromoteOp(Node->getOperand(4));
1304 break;
1305 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001306
1307 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1308 default: assert(0 && "This action not implemented for this operation!");
1309 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001310 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1311 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1312 Tmp5 != Node->getOperand(4)) {
1313 std::vector<SDOperand> Ops;
1314 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1315 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1316 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1317 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001318 break;
1319 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001320 // Otherwise, the target does not support this operation. Lower the
1321 // operation to an explicit libcall as appropriate.
1322 MVT::ValueType IntPtr = TLI.getPointerTy();
1323 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1324 std::vector<std::pair<SDOperand, const Type*> > Args;
1325
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001326 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001327 if (Node->getOpcode() == ISD::MEMSET) {
1328 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1329 // Extend the ubyte argument to be an int value for the call.
1330 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1331 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1332 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1333
1334 FnName = "memset";
1335 } else if (Node->getOpcode() == ISD::MEMCPY ||
1336 Node->getOpcode() == ISD::MEMMOVE) {
1337 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1338 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1339 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1340 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1341 } else {
1342 assert(0 && "Unknown op!");
1343 }
Chris Lattner45982da2005-05-12 16:53:42 +00001344
Chris Lattnere1bd8222005-01-11 05:57:22 +00001345 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001346 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001347 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001348 Result = CallResult.second;
1349 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001350 break;
1351 }
1352 case TargetLowering::Custom:
1353 std::vector<SDOperand> Ops;
1354 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1355 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1356 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner50381b62005-05-14 05:50:48 +00001357 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001358 Result = LegalizeOp(Result);
1359 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001360 }
1361 break;
1362 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001363
1364 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001365 Tmp1 = LegalizeOp(Node->getOperand(0));
1366 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001367
Chris Lattner3e011362005-05-14 07:45:46 +00001368 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1369 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1370 std::vector<SDOperand> Ops;
1371 Ops.push_back(Tmp1);
1372 Ops.push_back(Tmp2);
1373 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1374 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001375 Result = SDOperand(Node, 0);
1376 // Since these produce two values, make sure to remember that we legalized
1377 // both of them.
1378 AddLegalizedOperand(SDOperand(Node, 0), Result);
1379 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1380 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001381 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001382 Tmp1 = LegalizeOp(Node->getOperand(0));
1383 Tmp2 = LegalizeOp(Node->getOperand(1));
1384 Tmp3 = LegalizeOp(Node->getOperand(2));
1385 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1386 Tmp3 != Node->getOperand(2))
1387 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1388 break;
1389
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001390 case ISD::READIO:
1391 Tmp1 = LegalizeOp(Node->getOperand(0));
1392 Tmp2 = LegalizeOp(Node->getOperand(1));
1393
1394 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1395 case TargetLowering::Custom:
1396 default: assert(0 && "This action not implemented for this operation!");
1397 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001398 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1399 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1400 std::vector<SDOperand> Ops;
1401 Ops.push_back(Tmp1);
1402 Ops.push_back(Tmp2);
1403 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1404 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001405 Result = SDOperand(Node, 0);
1406 break;
1407 case TargetLowering::Expand:
1408 // Replace this with a load from memory.
1409 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1410 Node->getOperand(1), DAG.getSrcValue(NULL));
1411 Result = LegalizeOp(Result);
1412 break;
1413 }
1414
1415 // Since these produce two values, make sure to remember that we legalized
1416 // both of them.
1417 AddLegalizedOperand(SDOperand(Node, 0), Result);
1418 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1419 return Result.getValue(Op.ResNo);
1420
1421 case ISD::WRITEIO:
1422 Tmp1 = LegalizeOp(Node->getOperand(0));
1423 Tmp2 = LegalizeOp(Node->getOperand(1));
1424 Tmp3 = LegalizeOp(Node->getOperand(2));
1425
1426 switch (TLI.getOperationAction(Node->getOpcode(),
1427 Node->getOperand(1).getValueType())) {
1428 case TargetLowering::Custom:
1429 default: assert(0 && "This action not implemented for this operation!");
1430 case TargetLowering::Legal:
1431 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1432 Tmp3 != Node->getOperand(2))
1433 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1434 break;
1435 case TargetLowering::Expand:
1436 // Replace this with a store to memory.
1437 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1438 Node->getOperand(1), Node->getOperand(2),
1439 DAG.getSrcValue(NULL));
1440 Result = LegalizeOp(Result);
1441 break;
1442 }
1443 break;
1444
Chris Lattner84f67882005-01-20 18:52:28 +00001445 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001446 case ISD::SUB_PARTS:
1447 case ISD::SHL_PARTS:
1448 case ISD::SRA_PARTS:
1449 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001450 std::vector<SDOperand> Ops;
1451 bool Changed = false;
1452 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1453 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1454 Changed |= Ops.back() != Node->getOperand(i);
1455 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001456 if (Changed) {
1457 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1458 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1459 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001460
1461 // Since these produce multiple values, make sure to remember that we
1462 // legalized all of them.
1463 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1464 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1465 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001466 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001467
1468 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001469 case ISD::ADD:
1470 case ISD::SUB:
1471 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001472 case ISD::MULHS:
1473 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001474 case ISD::UDIV:
1475 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001476 case ISD::AND:
1477 case ISD::OR:
1478 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001479 case ISD::SHL:
1480 case ISD::SRL:
1481 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001482 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001483 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1484 case Expand: assert(0 && "Not possible");
1485 case Legal:
1486 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1487 break;
1488 case Promote:
1489 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1490 break;
1491 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001492 if (Tmp1 != Node->getOperand(0) ||
1493 Tmp2 != Node->getOperand(1))
1494 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1495 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001496
Nate Begemanc105e192005-04-06 00:23:54 +00001497 case ISD::UREM:
1498 case ISD::SREM:
1499 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1500 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1501 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1502 case TargetLowering::Legal:
1503 if (Tmp1 != Node->getOperand(0) ||
1504 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001505 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001506 Tmp2);
1507 break;
1508 case TargetLowering::Promote:
1509 case TargetLowering::Custom:
1510 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001511 case TargetLowering::Expand:
1512 if (MVT::isInteger(Node->getValueType(0))) {
1513 MVT::ValueType VT = Node->getValueType(0);
1514 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1515 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1516 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1517 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1518 } else {
1519 // Floating point mod -> fmod libcall.
1520 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1521 SDOperand Dummy;
1522 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001523 }
1524 break;
1525 }
1526 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001527
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001528 case ISD::CTPOP:
1529 case ISD::CTTZ:
1530 case ISD::CTLZ:
1531 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1532 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1533 case TargetLowering::Legal:
1534 if (Tmp1 != Node->getOperand(0))
1535 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1536 break;
1537 case TargetLowering::Promote: {
1538 MVT::ValueType OVT = Tmp1.getValueType();
1539 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001540
1541 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001542 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1543 // Perform the larger operation, then subtract if needed.
1544 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1545 switch(Node->getOpcode())
1546 {
1547 case ISD::CTPOP:
1548 Result = Tmp1;
1549 break;
1550 case ISD::CTTZ:
1551 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001552 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1553 DAG.getConstant(getSizeInBits(NVT), NVT),
1554 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001555 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001556 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1557 break;
1558 case ISD::CTLZ:
1559 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001560 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1561 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001562 getSizeInBits(OVT), NVT));
1563 break;
1564 }
1565 break;
1566 }
1567 case TargetLowering::Custom:
1568 assert(0 && "Cannot custom handle this yet!");
1569 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001570 switch(Node->getOpcode())
1571 {
1572 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001573 static const uint64_t mask[6] = {
1574 0x5555555555555555ULL, 0x3333333333333333ULL,
1575 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1576 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1577 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001578 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001579 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1580 unsigned len = getSizeInBits(VT);
1581 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001582 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001583 Tmp2 = DAG.getConstant(mask[i], VT);
1584 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001585 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001586 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1587 DAG.getNode(ISD::AND, VT,
1588 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1589 Tmp2));
1590 }
1591 Result = Tmp1;
1592 break;
1593 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001594 case ISD::CTLZ: {
1595 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001596 x = x | (x >> 1);
1597 x = x | (x >> 2);
1598 ...
1599 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001600 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001601 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001602
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001603 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1604 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001605 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1606 unsigned len = getSizeInBits(VT);
1607 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1608 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001609 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001610 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1611 }
1612 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001613 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001614 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001615 }
1616 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001617 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001618 // unless the target has ctlz but not ctpop, in which case we use:
1619 // { return 32 - nlz(~x & (x-1)); }
1620 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001621 MVT::ValueType VT = Tmp1.getValueType();
1622 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001623 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001624 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1625 DAG.getNode(ISD::SUB, VT, Tmp1,
1626 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001627 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1628 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1629 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001630 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001631 DAG.getConstant(getSizeInBits(VT), VT),
1632 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1633 } else {
1634 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1635 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001636 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001637 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001638 default:
1639 assert(0 && "Cannot expand this yet!");
1640 break;
1641 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001642 break;
1643 }
1644 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001645
Chris Lattner2c8086f2005-04-02 05:00:07 +00001646 // Unary operators
1647 case ISD::FABS:
1648 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001649 case ISD::FSQRT:
1650 case ISD::FSIN:
1651 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001652 Tmp1 = LegalizeOp(Node->getOperand(0));
1653 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1654 case TargetLowering::Legal:
1655 if (Tmp1 != Node->getOperand(0))
1656 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1657 break;
1658 case TargetLowering::Promote:
1659 case TargetLowering::Custom:
1660 assert(0 && "Cannot promote/custom handle this yet!");
1661 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001662 switch(Node->getOpcode()) {
1663 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001664 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1665 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1666 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1667 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001668 break;
1669 }
1670 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001671 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1672 MVT::ValueType VT = Node->getValueType(0);
1673 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001674 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001675 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1676 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1677 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001678 break;
1679 }
1680 case ISD::FSQRT:
1681 case ISD::FSIN:
1682 case ISD::FCOS: {
1683 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001684 const char *FnName = 0;
1685 switch(Node->getOpcode()) {
1686 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1687 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1688 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1689 default: assert(0 && "Unreachable!");
1690 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001691 SDOperand Dummy;
1692 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001693 break;
1694 }
1695 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001696 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001697 }
1698 break;
1699 }
1700 break;
1701
1702 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001703 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001704 case ISD::UINT_TO_FP: {
1705 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001706 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1707 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001708 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001709 Node->getOperand(0).getValueType())) {
1710 default: assert(0 && "Unknown operation action!");
1711 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001712 Result = ExpandLegalINT_TO_FP(isSigned,
1713 LegalizeOp(Node->getOperand(0)),
1714 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001715 AddLegalizedOperand(Op, Result);
1716 return Result;
1717 case TargetLowering::Promote:
1718 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1719 Node->getValueType(0),
1720 isSigned);
1721 AddLegalizedOperand(Op, Result);
1722 return Result;
1723 case TargetLowering::Legal:
1724 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001725 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001726
Chris Lattner3e928bb2005-01-07 07:47:09 +00001727 Tmp1 = LegalizeOp(Node->getOperand(0));
1728 if (Tmp1 != Node->getOperand(0))
1729 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1730 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001731 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001732 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1733 Node->getValueType(0), Node->getOperand(0));
1734 break;
1735 case Promote:
1736 if (isSigned) {
1737 Result = PromoteOp(Node->getOperand(0));
1738 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1739 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1740 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1741 } else {
1742 Result = PromoteOp(Node->getOperand(0));
1743 Result = DAG.getZeroExtendInReg(Result,
1744 Node->getOperand(0).getValueType());
1745 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001746 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001747 break;
1748 }
1749 break;
1750 }
1751 case ISD::TRUNCATE:
1752 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1753 case Legal:
1754 Tmp1 = LegalizeOp(Node->getOperand(0));
1755 if (Tmp1 != Node->getOperand(0))
1756 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1757 break;
1758 case Expand:
1759 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1760
1761 // Since the result is legal, we should just be able to truncate the low
1762 // part of the source.
1763 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1764 break;
1765 case Promote:
1766 Result = PromoteOp(Node->getOperand(0));
1767 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1768 break;
1769 }
1770 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001771
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001772 case ISD::FP_TO_SINT:
1773 case ISD::FP_TO_UINT:
1774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1775 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001776 Tmp1 = LegalizeOp(Node->getOperand(0));
1777
Chris Lattner1618beb2005-07-29 00:11:56 +00001778 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1779 default: assert(0 && "Unknown operation action!");
1780 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001781 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1782 SDOperand True, False;
1783 MVT::ValueType VT = Node->getOperand(0).getValueType();
1784 MVT::ValueType NVT = Node->getValueType(0);
1785 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1786 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1787 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1788 Node->getOperand(0), Tmp2, ISD::SETLT);
1789 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1790 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1791 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1792 Tmp2));
1793 False = DAG.getNode(ISD::XOR, NVT, False,
1794 DAG.getConstant(1ULL << ShiftAmt, NVT));
1795 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001796 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001797 } else {
1798 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1799 }
1800 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001801 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001802 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001803 Node->getOpcode() == ISD::FP_TO_SINT);
1804 AddLegalizedOperand(Op, Result);
1805 return Result;
1806 case TargetLowering::Legal:
1807 break;
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001808 case TargetLowering::Custom:
1809 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1810 Result = TLI.LowerOperation(Result, DAG);
1811 AddLegalizedOperand(Op, Result);
1812 NeedsAnotherIteration = true;
1813 return Result;
Chris Lattner1618beb2005-07-29 00:11:56 +00001814 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001815
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001816 if (Tmp1 != Node->getOperand(0))
1817 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1818 break;
1819 case Expand:
1820 assert(0 && "Shouldn't need to expand other operators here!");
1821 case Promote:
1822 Result = PromoteOp(Node->getOperand(0));
1823 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1824 break;
1825 }
1826 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001827
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001828 case ISD::ZERO_EXTEND:
1829 case ISD::SIGN_EXTEND:
1830 case ISD::FP_EXTEND:
1831 case ISD::FP_ROUND:
1832 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1833 case Legal:
1834 Tmp1 = LegalizeOp(Node->getOperand(0));
1835 if (Tmp1 != Node->getOperand(0))
1836 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1837 break;
1838 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001839 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001840
Chris Lattner03c85462005-01-15 05:21:40 +00001841 case Promote:
1842 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001843 case ISD::ZERO_EXTEND:
1844 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001845 // NOTE: Any extend would work here...
1846 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001847 Result = DAG.getZeroExtendInReg(Result,
1848 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001849 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001850 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001851 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001852 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001853 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001854 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001855 Result,
1856 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001857 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001858 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001859 Result = PromoteOp(Node->getOperand(0));
1860 if (Result.getValueType() != Op.getValueType())
1861 // Dynamically dead while we have only 2 FP types.
1862 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1863 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001864 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001865 Result = PromoteOp(Node->getOperand(0));
1866 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1867 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001868 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001869 }
1870 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001871 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001872 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001873 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001874 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001875
1876 // If this operation is not supported, convert it to a shl/shr or load/store
1877 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001878 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1879 default: assert(0 && "This action not supported for this op yet!");
1880 case TargetLowering::Legal:
1881 if (Tmp1 != Node->getOperand(0))
1882 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001883 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001884 break;
1885 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001886 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001887 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001888 // NOTE: we could fall back on load/store here too for targets without
1889 // SAR. However, it is doubtful that any exist.
1890 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1891 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001892 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001893 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1894 Node->getOperand(0), ShiftCst);
1895 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1896 Result, ShiftCst);
1897 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1898 // The only way we can lower this is to turn it into a STORETRUNC,
1899 // EXTLOAD pair, targetting a temporary location (a stack slot).
1900
1901 // NOTE: there is a choice here between constantly creating new stack
1902 // slots and always reusing the same one. We currently always create
1903 // new ones, as reuse may inhibit scheduling.
1904 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1905 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1906 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1907 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001908 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001909 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1910 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1911 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001912 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001913 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00001914 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1915 Result, StackSlot, DAG.getSrcValue(NULL),
1916 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001917 } else {
1918 assert(0 && "Unknown op");
1919 }
1920 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001921 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001922 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001923 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001924 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001925 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001926
Chris Lattner45982da2005-05-12 16:53:42 +00001927 // Note that LegalizeOp may be reentered even from single-use nodes, which
1928 // means that we always must cache transformed nodes.
1929 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 return Result;
1931}
1932
Chris Lattner8b6fa222005-01-15 22:16:26 +00001933/// PromoteOp - Given an operation that produces a value in an invalid type,
1934/// promote it to compute the value into a larger type. The produced value will
1935/// have the correct bits for the low portion of the register, but no guarantee
1936/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001937SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1938 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001939 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001940 assert(getTypeAction(VT) == Promote &&
1941 "Caller should expand or legalize operands that are not promotable!");
1942 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1943 "Cannot promote to smaller type!");
1944
Chris Lattner03c85462005-01-15 05:21:40 +00001945 SDOperand Tmp1, Tmp2, Tmp3;
1946
1947 SDOperand Result;
1948 SDNode *Node = Op.Val;
1949
Chris Lattner45982da2005-05-12 16:53:42 +00001950 if (!Node->hasOneUse()) {
1951 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1952 if (I != PromotedNodes.end()) return I->second;
1953 } else {
1954 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1955 }
1956
Chris Lattner0f69b292005-01-15 06:18:18 +00001957 // Promotion needs an optimization step to clean up after it, and is not
1958 // careful to avoid operations the target does not support. Make sure that
1959 // all generated operations are legalized in the next iteration.
1960 NeedsAnotherIteration = true;
1961
Chris Lattner03c85462005-01-15 05:21:40 +00001962 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001963 case ISD::CopyFromReg:
1964 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00001965 default:
1966 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1967 assert(0 && "Do not know how to promote this operator!");
1968 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001969 case ISD::UNDEF:
1970 Result = DAG.getNode(ISD::UNDEF, NVT);
1971 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001972 case ISD::Constant:
1973 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1974 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1975 break;
1976 case ISD::ConstantFP:
1977 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1978 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1979 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001980
Chris Lattner82fbfb62005-01-18 02:59:52 +00001981 case ISD::SETCC:
1982 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1983 "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001984 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
1985 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00001986 Result = LegalizeOp(Result);
1987 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001988
1989 case ISD::TRUNCATE:
1990 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1991 case Legal:
1992 Result = LegalizeOp(Node->getOperand(0));
1993 assert(Result.getValueType() >= NVT &&
1994 "This truncation doesn't make sense!");
1995 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1996 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1997 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001998 case Promote:
1999 // The truncation is not required, because we don't guarantee anything
2000 // about high bits anyway.
2001 Result = PromoteOp(Node->getOperand(0));
2002 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002003 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002004 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2005 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002006 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002007 }
2008 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002009 case ISD::SIGN_EXTEND:
2010 case ISD::ZERO_EXTEND:
2011 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2012 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2013 case Legal:
2014 // Input is legal? Just do extend all the way to the larger type.
2015 Result = LegalizeOp(Node->getOperand(0));
2016 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2017 break;
2018 case Promote:
2019 // Promote the reg if it's smaller.
2020 Result = PromoteOp(Node->getOperand(0));
2021 // The high bits are not guaranteed to be anything. Insert an extend.
2022 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002023 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002024 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002025 else
Chris Lattner23993562005-04-13 02:38:47 +00002026 Result = DAG.getZeroExtendInReg(Result,
2027 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002028 break;
2029 }
2030 break;
2031
2032 case ISD::FP_EXTEND:
2033 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2034 case ISD::FP_ROUND:
2035 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2036 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2037 case Promote: assert(0 && "Unreachable with 2 FP types!");
2038 case Legal:
2039 // Input is legal? Do an FP_ROUND_INREG.
2040 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002041 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2042 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002043 break;
2044 }
2045 break;
2046
2047 case ISD::SINT_TO_FP:
2048 case ISD::UINT_TO_FP:
2049 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2050 case Legal:
2051 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002052 // No extra round required here.
2053 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002054 break;
2055
2056 case Promote:
2057 Result = PromoteOp(Node->getOperand(0));
2058 if (Node->getOpcode() == ISD::SINT_TO_FP)
2059 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002060 Result,
2061 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002062 else
Chris Lattner23993562005-04-13 02:38:47 +00002063 Result = DAG.getZeroExtendInReg(Result,
2064 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002065 // No extra round required here.
2066 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002067 break;
2068 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002069 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2070 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002071 // Round if we cannot tolerate excess precision.
2072 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002073 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2074 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002075 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002076 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002077 break;
2078
2079 case ISD::FP_TO_SINT:
2080 case ISD::FP_TO_UINT:
2081 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2082 case Legal:
2083 Tmp1 = LegalizeOp(Node->getOperand(0));
2084 break;
2085 case Promote:
2086 // The input result is prerounded, so we don't have to do anything
2087 // special.
2088 Tmp1 = PromoteOp(Node->getOperand(0));
2089 break;
2090 case Expand:
2091 assert(0 && "not implemented");
2092 }
Nate Begemand2558e32005-08-14 01:20:53 +00002093 // If we're promoting a UINT to a larger size, check to see if the new node
2094 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2095 // we can use that instead. This allows us to generate better code for
2096 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2097 // legal, such as PowerPC.
2098 if (Node->getOpcode() == ISD::FP_TO_UINT &&
2099 TargetLowering::Legal != TLI.getOperationAction(ISD::FP_TO_UINT, NVT) &&
2100 TargetLowering::Legal == TLI.getOperationAction(ISD::FP_TO_SINT, NVT)) {
2101 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2102 } else {
2103 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2104 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002105 break;
2106
Chris Lattner2c8086f2005-04-02 05:00:07 +00002107 case ISD::FABS:
2108 case ISD::FNEG:
2109 Tmp1 = PromoteOp(Node->getOperand(0));
2110 assert(Tmp1.getValueType() == NVT);
2111 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2112 // NOTE: we do not have to do any extra rounding here for
2113 // NoExcessFPPrecision, because we know the input will have the appropriate
2114 // precision, and these operations don't modify precision at all.
2115 break;
2116
Chris Lattnerda6ba872005-04-28 21:44:33 +00002117 case ISD::FSQRT:
2118 case ISD::FSIN:
2119 case ISD::FCOS:
2120 Tmp1 = PromoteOp(Node->getOperand(0));
2121 assert(Tmp1.getValueType() == NVT);
2122 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2123 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002124 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2125 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002126 break;
2127
Chris Lattner03c85462005-01-15 05:21:40 +00002128 case ISD::AND:
2129 case ISD::OR:
2130 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002131 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002132 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002133 case ISD::MUL:
2134 // The input may have strange things in the top bits of the registers, but
2135 // these operations don't care. They may have wierd bits going out, but
2136 // that too is okay if they are integer operations.
2137 Tmp1 = PromoteOp(Node->getOperand(0));
2138 Tmp2 = PromoteOp(Node->getOperand(1));
2139 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2140 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2141
2142 // However, if this is a floating point operation, they will give excess
2143 // precision that we may not be able to tolerate. If we DO allow excess
2144 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002145 // FIXME: Why would we need to round FP ops more than integer ones?
2146 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00002147 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002148 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2149 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002150 break;
2151
Chris Lattner8b6fa222005-01-15 22:16:26 +00002152 case ISD::SDIV:
2153 case ISD::SREM:
2154 // These operators require that their input be sign extended.
2155 Tmp1 = PromoteOp(Node->getOperand(0));
2156 Tmp2 = PromoteOp(Node->getOperand(1));
2157 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002158 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2159 DAG.getValueType(VT));
2160 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2161 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002162 }
2163 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2164
2165 // Perform FP_ROUND: this is probably overly pessimistic.
2166 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002167 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2168 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002169 break;
2170
2171 case ISD::UDIV:
2172 case ISD::UREM:
2173 // These operators require that their input be zero extended.
2174 Tmp1 = PromoteOp(Node->getOperand(0));
2175 Tmp2 = PromoteOp(Node->getOperand(1));
2176 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002177 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2178 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002179 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2180 break;
2181
2182 case ISD::SHL:
2183 Tmp1 = PromoteOp(Node->getOperand(0));
2184 Tmp2 = LegalizeOp(Node->getOperand(1));
2185 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2186 break;
2187 case ISD::SRA:
2188 // The input value must be properly sign extended.
2189 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002190 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2191 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002192 Tmp2 = LegalizeOp(Node->getOperand(1));
2193 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2194 break;
2195 case ISD::SRL:
2196 // The input value must be properly zero extended.
2197 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002198 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002199 Tmp2 = LegalizeOp(Node->getOperand(1));
2200 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2201 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002202 case ISD::LOAD:
2203 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2204 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002205 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002206 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002207 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2208 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002209 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002210 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2211 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002212
2213 // Remember that we legalized the chain.
2214 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2215 break;
2216 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002217 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2218 case Expand: assert(0 && "It's impossible to expand bools");
2219 case Legal:
2220 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2221 break;
2222 case Promote:
2223 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2224 break;
2225 }
Chris Lattner03c85462005-01-15 05:21:40 +00002226 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2227 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2228 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2229 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002230 case ISD::SELECT_CC:
2231 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2232 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2233 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2234 Node->getOperand(1), Tmp2, Tmp3,
2235 Node->getOperand(4));
2236 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002237 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002238 case ISD::CALL: {
2239 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2240 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2241
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002242 std::vector<SDOperand> Ops;
2243 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2244 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2245
Chris Lattner8ac532c2005-01-16 19:46:48 +00002246 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2247 "Can only promote single result calls");
2248 std::vector<MVT::ValueType> RetTyVTs;
2249 RetTyVTs.reserve(2);
2250 RetTyVTs.push_back(NVT);
2251 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002252 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2253 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002254 Result = SDOperand(NC, 0);
2255
2256 // Insert the new chain mapping.
2257 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2258 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002259 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002260 case ISD::CTPOP:
2261 case ISD::CTTZ:
2262 case ISD::CTLZ:
2263 Tmp1 = Node->getOperand(0);
2264 //Zero extend the argument
2265 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2266 // Perform the larger operation, then subtract if needed.
2267 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2268 switch(Node->getOpcode())
2269 {
2270 case ISD::CTPOP:
2271 Result = Tmp1;
2272 break;
2273 case ISD::CTTZ:
2274 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002275 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002276 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002277 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002278 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2279 break;
2280 case ISD::CTLZ:
2281 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002282 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2283 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002284 getSizeInBits(VT), NVT));
2285 break;
2286 }
2287 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002288 }
2289
2290 assert(Result.Val && "Didn't set a result!");
2291 AddPromotedOperand(Op, Result);
2292 return Result;
2293}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002294
Chris Lattner84f67882005-01-20 18:52:28 +00002295/// ExpandAddSub - Find a clever way to expand this add operation into
2296/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002297void SelectionDAGLegalize::
2298ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2299 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002300 // Expand the subcomponents.
2301 SDOperand LHSL, LHSH, RHSL, RHSH;
2302 ExpandOp(LHS, LHSL, LHSH);
2303 ExpandOp(RHS, RHSL, RHSH);
2304
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002305 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002306 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2307 if (LHSL.getValueType() == MVT::i32) {
2308 SDOperand LowEl;
2309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2310 if (C->getValue() == 0)
2311 LowEl = RHSL;
2312 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2313 if (C->getValue() == 0)
2314 LowEl = LHSL;
2315 if (LowEl.Val) {
2316 // Turn this into an add/sub of the high part only.
2317 SDOperand HiEl =
2318 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2319 LowEl.getValueType(), LHSH, RHSH);
2320 Lo = LowEl;
2321 Hi = HiEl;
2322 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002323 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002324 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002325
Chris Lattner84f67882005-01-20 18:52:28 +00002326 std::vector<SDOperand> Ops;
2327 Ops.push_back(LHSL);
2328 Ops.push_back(LHSH);
2329 Ops.push_back(RHSL);
2330 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002331
2332 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2333 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002334 Hi = Lo.getValue(1);
2335}
2336
Chris Lattner5b359c62005-04-02 04:00:59 +00002337void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2338 SDOperand Op, SDOperand Amt,
2339 SDOperand &Lo, SDOperand &Hi) {
2340 // Expand the subcomponents.
2341 SDOperand LHSL, LHSH;
2342 ExpandOp(Op, LHSL, LHSH);
2343
2344 std::vector<SDOperand> Ops;
2345 Ops.push_back(LHSL);
2346 Ops.push_back(LHSH);
2347 Ops.push_back(Amt);
Chris Lattnere89083a2005-05-14 07:25:05 +00002348 std::vector<MVT::ValueType> VTs;
2349 VTs.push_back(LHSL.getValueType());
2350 VTs.push_back(LHSH.getValueType());
2351 VTs.push_back(Amt.getValueType());
2352 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002353 Hi = Lo.getValue(1);
2354}
2355
2356
Chris Lattnere34b3962005-01-19 04:19:40 +00002357/// ExpandShift - Try to find a clever way to expand this shift operation out to
2358/// smaller elements. If we can't find a way that is more efficient than a
2359/// libcall on this target, return false. Otherwise, return true with the
2360/// low-parts expanded into Lo and Hi.
2361bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2362 SDOperand &Lo, SDOperand &Hi) {
2363 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2364 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002365
Chris Lattnere34b3962005-01-19 04:19:40 +00002366 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002367 SDOperand ShAmt = LegalizeOp(Amt);
2368 MVT::ValueType ShTy = ShAmt.getValueType();
2369 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2370 unsigned NVTBits = MVT::getSizeInBits(NVT);
2371
2372 // Handle the case when Amt is an immediate. Other cases are currently broken
2373 // and are disabled.
2374 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2375 unsigned Cst = CN->getValue();
2376 // Expand the incoming operand to be shifted, so that we have its parts
2377 SDOperand InL, InH;
2378 ExpandOp(Op, InL, InH);
2379 switch(Opc) {
2380 case ISD::SHL:
2381 if (Cst > VTBits) {
2382 Lo = DAG.getConstant(0, NVT);
2383 Hi = DAG.getConstant(0, NVT);
2384 } else if (Cst > NVTBits) {
2385 Lo = DAG.getConstant(0, NVT);
2386 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002387 } else if (Cst == NVTBits) {
2388 Lo = DAG.getConstant(0, NVT);
2389 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002390 } else {
2391 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2392 Hi = DAG.getNode(ISD::OR, NVT,
2393 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2394 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2395 }
2396 return true;
2397 case ISD::SRL:
2398 if (Cst > VTBits) {
2399 Lo = DAG.getConstant(0, NVT);
2400 Hi = DAG.getConstant(0, NVT);
2401 } else if (Cst > NVTBits) {
2402 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2403 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002404 } else if (Cst == NVTBits) {
2405 Lo = InH;
2406 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002407 } else {
2408 Lo = DAG.getNode(ISD::OR, NVT,
2409 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2410 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2411 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2412 }
2413 return true;
2414 case ISD::SRA:
2415 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002416 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002417 DAG.getConstant(NVTBits-1, ShTy));
2418 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002419 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002420 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002421 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002422 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002423 } else if (Cst == NVTBits) {
2424 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002425 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002426 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002427 } else {
2428 Lo = DAG.getNode(ISD::OR, NVT,
2429 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2430 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2431 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2432 }
2433 return true;
2434 }
2435 }
2436 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2437 // so disable it for now. Currently targets are handling this via SHL_PARTS
2438 // and friends.
2439 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002440
2441 // If we have an efficient select operation (or if the selects will all fold
2442 // away), lower to some complex code, otherwise just emit the libcall.
2443 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2444 !isa<ConstantSDNode>(Amt))
2445 return false;
2446
2447 SDOperand InL, InH;
2448 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002449 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2450 DAG.getConstant(NVTBits, ShTy), ShAmt);
2451
Chris Lattnere5544f82005-01-20 20:29:23 +00002452 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002453 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2454 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002455
Chris Lattnere34b3962005-01-19 04:19:40 +00002456 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2457 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2458 DAG.getConstant(NVTBits-1, ShTy));
2459 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2460 DAG.getConstant(NVTBits-1, ShTy));
2461 }
2462
2463 if (Opc == ISD::SHL) {
2464 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2465 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2466 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002467 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002468
Chris Lattnere34b3962005-01-19 04:19:40 +00002469 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2470 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2471 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002472 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002473 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2474 DAG.getConstant(32, ShTy),
2475 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002476 DAG.getConstant(0, NVT),
2477 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002478 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002479 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002480 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002481 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002482
2483 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002484 if (Opc == ISD::SRA)
2485 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2486 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002487 else
2488 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002489 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002490 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002491 }
2492 return true;
2493}
Chris Lattner77e77a62005-01-21 06:05:23 +00002494
Chris Lattner9530ddc2005-05-13 05:09:11 +00002495/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2496/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002497/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002498static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002499 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002500
Chris Lattner16cd04d2005-05-12 23:24:06 +00002501 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002502 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002503 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002504 Found = Node;
2505 return;
2506 }
2507
2508 // Otherwise, scan the operands of Node to see if any of them is a call.
2509 assert(Node->getNumOperands() != 0 &&
2510 "All leaves should have depth equal to the entry node!");
2511 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002512 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002513
2514 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002515 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002516 Found);
2517}
2518
2519
Chris Lattner9530ddc2005-05-13 05:09:11 +00002520/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2521/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002522/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002523static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2524 std::set<SDNode*> &Visited) {
2525 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2526 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002527
Chris Lattner16cd04d2005-05-12 23:24:06 +00002528 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002529 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002530 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002531 Found = Node;
2532 return;
2533 }
2534
2535 // Otherwise, scan the operands of Node to see if any of them is a call.
2536 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2537 if (UI == E) return;
2538 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002539 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002540
2541 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002542 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002543}
2544
Chris Lattner9530ddc2005-05-13 05:09:11 +00002545/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002546/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002547static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002548 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002549 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002550 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002551 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002552
2553 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002554 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002555
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002556 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002557 if (TheChain.getValueType() != MVT::Other)
2558 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002559 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002560
2561 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002562 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002563
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002564 // Make sure to only follow users of our token chain.
2565 SDNode *User = *UI;
2566 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2567 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002568 if (SDNode *Result = FindCallSeqEnd(User))
2569 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002570 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002571 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002572}
2573
Chris Lattner9530ddc2005-05-13 05:09:11 +00002574/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002575/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002576static SDNode *FindCallSeqStart(SDNode *Node) {
2577 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002578 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002579
2580 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2581 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002582 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002583}
2584
2585
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002586/// FindInputOutputChains - If we are replacing an operation with a call we need
2587/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002588/// properly serialize the calls in the block. The returned operand is the
2589/// input chain value for the new call (e.g. the entry node or the previous
2590/// call), and OutChain is set to be the chain node to update to point to the
2591/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002592static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2593 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002594 SDNode *LatestCallSeqStart = Entry.Val;
2595 SDNode *LatestCallSeqEnd = 0;
2596 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2597 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002598
Chris Lattner16cd04d2005-05-12 23:24:06 +00002599 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002600 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002601 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2602 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002603 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2604 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002605 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002606 LatestCallSeqEnd = Entry.Val;
2607 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002608
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002609 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002610 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002611 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002612 std::set<SDNode*> Visited;
2613 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002614
Chris Lattner9530ddc2005-05-13 05:09:11 +00002615 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002616 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002617 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002618
Chris Lattner9530ddc2005-05-13 05:09:11 +00002619 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002620}
2621
Jeff Cohen00b168892005-07-27 06:12:32 +00002622/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002623void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2624 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002625 // Nothing to splice it into?
2626 if (OutChain == 0) return;
2627
2628 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2629 //OutChain->dump();
2630
2631 // Form a token factor node merging the old inval and the new inval.
2632 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2633 OutChain->getOperand(0));
2634 // Change the node to refer to the new token.
2635 OutChain->setAdjCallChain(InToken);
2636}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002637
2638
Chris Lattner77e77a62005-01-21 06:05:23 +00002639// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2640// does not fit into a register, return the lo part and set the hi part to the
2641// by-reg argument. If it does fit into a single register, return the result
2642// and leave the Hi part unset.
2643SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2644 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002645 SDNode *OutChain;
2646 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2647 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002648 if (InChain.Val == 0)
2649 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002650
Chris Lattner77e77a62005-01-21 06:05:23 +00002651 TargetLowering::ArgListTy Args;
2652 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2653 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2654 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2655 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2656 }
2657 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002658
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002659 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002660 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002661 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002662 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2663 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002664 SpliceCallInto(CallInfo.second, OutChain);
2665
2666 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002667
2668 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002669 default: assert(0 && "Unknown thing");
2670 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002671 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002672 case Promote:
2673 assert(0 && "Cannot promote this yet!");
2674 case Expand:
2675 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002676 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002677 return Lo;
2678 }
2679}
2680
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002681
Chris Lattner77e77a62005-01-21 06:05:23 +00002682/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2683/// destination type is legal.
2684SDOperand SelectionDAGLegalize::
2685ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2686 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2687 assert(getTypeAction(Source.getValueType()) == Expand &&
2688 "This is not an expansion!");
2689 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2690
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002691 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002692 assert(Source.getValueType() == MVT::i64 &&
2693 "This only works for 64-bit -> FP");
2694 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2695 // incoming integer is set. To handle this, we dynamically test to see if
2696 // it is set, and, if so, add a fudge factor.
2697 SDOperand Lo, Hi;
2698 ExpandOp(Source, Lo, Hi);
2699
Chris Lattner66de05b2005-05-13 04:45:13 +00002700 // If this is unsigned, and not supported, first perform the conversion to
2701 // signed, then adjust the result if the sign bit is set.
2702 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2703 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2704
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002705 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2706 DAG.getConstant(0, Hi.getValueType()),
2707 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002708 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2709 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2710 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002711 uint64_t FF = 0x5f800000ULL;
2712 if (TLI.isLittleEndian()) FF <<= 32;
2713 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002714
2715 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2716 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2717 TLI.getPointerTy());
2718 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2719 SDOperand FudgeInReg;
2720 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002721 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2722 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002723 else {
2724 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002725 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2726 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002727 }
2728 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002729 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002730
Chris Lattnera88a2602005-05-14 05:33:54 +00002731 // Check to see if the target has a custom way to lower this. If so, use it.
2732 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2733 default: assert(0 && "This action not implemented for this operation!");
2734 case TargetLowering::Legal:
2735 case TargetLowering::Expand:
2736 break; // This case is handled below.
2737 case TargetLowering::Custom:
2738 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner50381b62005-05-14 05:50:48 +00002739 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnera88a2602005-05-14 05:33:54 +00002740 }
2741
Chris Lattner13689e22005-05-12 07:00:44 +00002742 // Expand the source, then glue it back together for the call. We must expand
2743 // the source in case it is shared (this pass of legalize must traverse it).
2744 SDOperand SrcLo, SrcHi;
2745 ExpandOp(Source, SrcLo, SrcHi);
2746 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2747
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002748 SDNode *OutChain = 0;
2749 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2750 DAG.getEntryNode());
2751 const char *FnName = 0;
2752 if (DestTy == MVT::f32)
2753 FnName = "__floatdisf";
2754 else {
2755 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2756 FnName = "__floatdidf";
2757 }
2758
Chris Lattner77e77a62005-01-21 06:05:23 +00002759 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2760
2761 TargetLowering::ArgListTy Args;
2762 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002763
Chris Lattner77e77a62005-01-21 06:05:23 +00002764 Args.push_back(std::make_pair(Source, ArgTy));
2765
2766 // We don't care about token chains for libcalls. We just use the entry
2767 // node as our input and ignore the output chain. This allows us to place
2768 // calls wherever we need them to satisfy data dependences.
2769 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002770
2771 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002772 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2773 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002774
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002775 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002776 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002777}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002778
Chris Lattnere34b3962005-01-19 04:19:40 +00002779
2780
Chris Lattner3e928bb2005-01-07 07:47:09 +00002781/// ExpandOp - Expand the specified SDOperand into its two component pieces
2782/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2783/// LegalizeNodes map is filled in for any results that are not expanded, the
2784/// ExpandedNodes map is filled in for any results that are expanded, and the
2785/// Lo/Hi values are returned.
2786void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2787 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002788 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002789 SDNode *Node = Op.Val;
2790 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2791 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2792 assert(MVT::isInteger(NVT) && NVT < VT &&
2793 "Cannot expand to FP value or to larger int value!");
2794
2795 // If there is more than one use of this, see if we already expanded it.
2796 // There is no use remembering values that only have a single use, as the map
2797 // entries will never be reused.
2798 if (!Node->hasOneUse()) {
2799 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2800 = ExpandedNodes.find(Op);
2801 if (I != ExpandedNodes.end()) {
2802 Lo = I->second.first;
2803 Hi = I->second.second;
2804 return;
2805 }
Chris Lattner45982da2005-05-12 16:53:42 +00002806 } else {
2807 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002808 }
2809
Chris Lattner4e6c7462005-01-08 19:27:05 +00002810 // Expanding to multiple registers needs to perform an optimization step, and
2811 // is not careful to avoid operations the target does not support. Make sure
2812 // that all generated operations are legalized in the next iteration.
2813 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002814
Chris Lattner3e928bb2005-01-07 07:47:09 +00002815 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002816 case ISD::CopyFromReg:
2817 assert(0 && "CopyFromReg must be legal!");
2818 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002819 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2820 assert(0 && "Do not know how to expand this operator!");
2821 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002822 case ISD::UNDEF:
2823 Lo = DAG.getNode(ISD::UNDEF, NVT);
2824 Hi = DAG.getNode(ISD::UNDEF, NVT);
2825 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002826 case ISD::Constant: {
2827 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2828 Lo = DAG.getConstant(Cst, NVT);
2829 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2830 break;
2831 }
2832
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002833 case ISD::BUILD_PAIR:
2834 // Legalize both operands. FIXME: in the future we should handle the case
2835 // where the two elements are not legal.
2836 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2837 Lo = LegalizeOp(Node->getOperand(0));
2838 Hi = LegalizeOp(Node->getOperand(1));
2839 break;
2840
Chris Lattneredb1add2005-05-11 04:51:16 +00002841 case ISD::CTPOP:
2842 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002843 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2844 DAG.getNode(ISD::CTPOP, NVT, Lo),
2845 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002846 Hi = DAG.getConstant(0, NVT);
2847 break;
2848
Chris Lattner39a8f332005-05-12 19:05:01 +00002849 case ISD::CTLZ: {
2850 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002851 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002852 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2853 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002854 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2855 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002856 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2857 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2858
2859 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2860 Hi = DAG.getConstant(0, NVT);
2861 break;
2862 }
2863
2864 case ISD::CTTZ: {
2865 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002866 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002867 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2868 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002869 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2870 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002871 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2872 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2873
2874 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2875 Hi = DAG.getConstant(0, NVT);
2876 break;
2877 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002878
Chris Lattner3e928bb2005-01-07 07:47:09 +00002879 case ISD::LOAD: {
2880 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2881 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002882 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002883
2884 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002885 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002886 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2887 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00002888 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002889 //are from the same instruction?
2890 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002891
2892 // Build a factor node to remember that this load is independent of the
2893 // other one.
2894 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2895 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002896
Chris Lattner3e928bb2005-01-07 07:47:09 +00002897 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002898 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002899 if (!TLI.isLittleEndian())
2900 std::swap(Lo, Hi);
2901 break;
2902 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002903 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002904 case ISD::CALL: {
2905 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2906 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2907
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002908 bool Changed = false;
2909 std::vector<SDOperand> Ops;
2910 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2911 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2912 Changed |= Ops.back() != Node->getOperand(i);
2913 }
2914
Chris Lattner3e928bb2005-01-07 07:47:09 +00002915 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2916 "Can only expand a call once so far, not i64 -> i16!");
2917
2918 std::vector<MVT::ValueType> RetTyVTs;
2919 RetTyVTs.reserve(3);
2920 RetTyVTs.push_back(NVT);
2921 RetTyVTs.push_back(NVT);
2922 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002923 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2924 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002925 Lo = SDOperand(NC, 0);
2926 Hi = SDOperand(NC, 1);
2927
2928 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002929 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002930 break;
2931 }
2932 case ISD::AND:
2933 case ISD::OR:
2934 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2935 SDOperand LL, LH, RL, RH;
2936 ExpandOp(Node->getOperand(0), LL, LH);
2937 ExpandOp(Node->getOperand(1), RL, RH);
2938 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2939 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2940 break;
2941 }
2942 case ISD::SELECT: {
2943 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002944
2945 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2946 case Expand: assert(0 && "It's impossible to expand bools");
2947 case Legal:
2948 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2949 break;
2950 case Promote:
2951 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2952 break;
2953 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002954 ExpandOp(Node->getOperand(1), LL, LH);
2955 ExpandOp(Node->getOperand(2), RL, RH);
2956 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2957 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2958 break;
2959 }
Nate Begeman9373a812005-08-10 20:51:12 +00002960 case ISD::SELECT_CC: {
2961 SDOperand TL, TH, FL, FH;
2962 ExpandOp(Node->getOperand(2), TL, TH);
2963 ExpandOp(Node->getOperand(3), FL, FH);
2964 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2965 Node->getOperand(1), TL, FL, Node->getOperand(4));
2966 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2967 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00002968 Lo = LegalizeOp(Lo);
2969 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00002970 break;
2971 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002972 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002973 SDOperand In;
2974 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2975 case Expand: assert(0 && "expand-expand not implemented yet!");
2976 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2977 case Promote:
2978 In = PromoteOp(Node->getOperand(0));
2979 // Emit the appropriate sign_extend_inreg to get the value we want.
2980 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00002981 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00002982 break;
2983 }
2984
Chris Lattner3e928bb2005-01-07 07:47:09 +00002985 // The low part is just a sign extension of the input (which degenerates to
2986 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002987 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002988
Chris Lattner3e928bb2005-01-07 07:47:09 +00002989 // The high part is obtained by SRA'ing all but one of the bits of the lo
2990 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002991 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002992 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2993 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002994 break;
2995 }
Chris Lattner06098e02005-04-03 23:41:52 +00002996 case ISD::ZERO_EXTEND: {
2997 SDOperand In;
2998 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2999 case Expand: assert(0 && "expand-expand not implemented yet!");
3000 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3001 case Promote:
3002 In = PromoteOp(Node->getOperand(0));
3003 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003004 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003005 break;
3006 }
3007
Chris Lattner3e928bb2005-01-07 07:47:09 +00003008 // The low part is just a zero extension of the input (which degenerates to
3009 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003010 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003011
Chris Lattner3e928bb2005-01-07 07:47:09 +00003012 // The high part is just a zero.
3013 Hi = DAG.getConstant(0, NVT);
3014 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003015 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00003016 // These operators cannot be expanded directly, emit them as calls to
3017 // library functions.
3018 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003019 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003020 SDOperand Op;
3021 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3022 case Expand: assert(0 && "cannot expand FP!");
3023 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3024 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3025 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003026
Chris Lattnerf20d1832005-07-30 01:40:57 +00003027 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3028
Chris Lattner80a3e942005-07-29 00:33:32 +00003029 // Now that the custom expander is done, expand the result, which is still
3030 // VT.
Chris Lattnerf20d1832005-07-30 01:40:57 +00003031 ExpandOp(Op, Lo, Hi);
Chris Lattner80a3e942005-07-29 00:33:32 +00003032 break;
3033 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003034
Chris Lattner4e6c7462005-01-08 19:27:05 +00003035 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003036 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003037 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003038 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003039 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003040
Chris Lattner4e6c7462005-01-08 19:27:05 +00003041 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003042 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3043 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3044 LegalizeOp(Node->getOperand(0)));
3045 // Now that the custom expander is done, expand the result, which is still
3046 // VT.
3047 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
3048 break;
3049 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003050
Chris Lattner4e6c7462005-01-08 19:27:05 +00003051 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003052 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003053 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003054 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003055 break;
3056
Chris Lattnere34b3962005-01-19 04:19:40 +00003057 case ISD::SHL:
3058 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003059 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003060 break;
Chris Lattner47599822005-04-02 03:38:53 +00003061
3062 // If this target supports SHL_PARTS, use it.
3063 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003064 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3065 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003066 break;
3067 }
3068
Chris Lattnere34b3962005-01-19 04:19:40 +00003069 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003070 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003071 break;
3072
3073 case ISD::SRA:
3074 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003075 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003076 break;
Chris Lattner47599822005-04-02 03:38:53 +00003077
3078 // If this target supports SRA_PARTS, use it.
3079 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003080 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3081 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003082 break;
3083 }
3084
Chris Lattnere34b3962005-01-19 04:19:40 +00003085 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003086 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003087 break;
3088 case ISD::SRL:
3089 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003090 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003091 break;
Chris Lattner47599822005-04-02 03:38:53 +00003092
3093 // If this target supports SRL_PARTS, use it.
3094 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003095 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3096 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003097 break;
3098 }
3099
Chris Lattnere34b3962005-01-19 04:19:40 +00003100 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003101 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003102 break;
3103
Misha Brukmanedf128a2005-04-21 22:36:52 +00003104 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003105 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3106 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003107 break;
3108 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003109 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3110 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003111 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003112 case ISD::MUL: {
3113 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
3114 SDOperand LL, LH, RL, RH;
3115 ExpandOp(Node->getOperand(0), LL, LH);
3116 ExpandOp(Node->getOperand(1), RL, RH);
3117 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3118 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3119 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3120 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3121 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3122 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3123 } else {
3124 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3125 }
3126 break;
3127 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003128 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3129 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3130 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3131 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003132 }
3133
3134 // Remember in a map if the values will be reused later.
3135 if (!Node->hasOneUse()) {
3136 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3137 std::make_pair(Lo, Hi))).second;
3138 assert(isNew && "Value already expanded?!?");
3139 }
3140}
3141
3142
3143// SelectionDAG::Legalize - This is the entry point for the file.
3144//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003145void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003146 /// run - This is the main entry point to this class.
3147 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003148 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003149}
3150